Skip to content

Commit

Permalink
Merge pull request #1343 from Poeticrow/feat/add-delete-timezone-endp…
Browse files Browse the repository at this point in the history
…oint

feat: add endpoint to  delete timezones
  • Loading branch information
theblvckdev authored Mar 1, 2025
2 parents 2ca80c4 + 6dcf607 commit 05b3be2
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 2 deletions.
43 changes: 42 additions & 1 deletion src/modules/timezones/tests/timezones.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { getRepositoryToken } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Timezone } from '../entities/timezone.entity';
import { CreateTimezoneDto } from '../dto/create-timezone.dto';
import { HttpException, HttpStatus } from '@nestjs/common';
import { HttpException, HttpStatus, NotFoundException } from '@nestjs/common';

const mockTimezoneRepository = {
findOne: jest.fn(),
Expand Down Expand Up @@ -175,3 +175,44 @@ describe('TimezonesController', () => {
});
});
});

describe('TimezonesController - deleteTimezone', () => {
let controller: TimezonesController;
let service: TimezonesService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [TimezonesController],
providers: [
{
provide: TimezonesService,
useValue: {
deleteTimezone: jest.fn(),
},
},
],
}).compile();

controller = module.get<TimezonesController>(TimezonesController);
service = module.get<TimezonesService>(TimezonesService);
});

it('should successfully delete a timezone', async () => {
jest.spyOn(service, 'deleteTimezone').mockResolvedValue({
status_code: 200,
message: 'Timezone deleted successfully',
});

const response = await controller.deleteTimezone('123');
expect(response).toEqual({
status_code: 200,
message: 'Timezone deleted successfully',
});
});

it('should return NotFoundException if timezone does not exist', async () => {
jest.spyOn(service, 'deleteTimezone').mockRejectedValue(new NotFoundException());

await expect(controller.deleteTimezone('invalid-id')).rejects.toThrow(NotFoundException);
});
});
10 changes: 9 additions & 1 deletion src/modules/timezones/timezones.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Controller, Post, Body, Get, Res, HttpStatus, Patch, Param } from '@nestjs/common';
import { Controller, Post, Body, Get, Res, HttpStatus, Patch, Param, Delete } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiBody, ApiBearerAuth } from '@nestjs/swagger';
import { CreateTimezoneDto } from './dto/create-timezone.dto';
import { UpdateTimezoneDto } from './dto/update-timezone.dto';
Expand Down Expand Up @@ -38,4 +38,12 @@ export class TimezonesController {
async updateTimezone(@Param('id') id: string, @Body() updateTimezoneDto: UpdateTimezoneDto) {
return this.timezonesService.updateTimezone(id, updateTimezoneDto);
}
@Delete(':id')
@ApiBearerAuth()
@ApiOperation({ summary: 'Delete a timezone' })
@ApiResponse({ status: 200, description: 'Timezone successfully deleted.' })
@ApiResponse({ status: 404, description: 'Timezone not found.' })
async deleteTimezone(@Param('id') id: string) {
return this.timezonesService.deleteTimezone(id);
}
}
49 changes: 49 additions & 0 deletions src/modules/timezones/timezones.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { TimezonesService } from './timezones.service';
import { getRepositoryToken } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Timezone } from './entities/timezone.entity';
import { NotFoundException } from '@nestjs/common';

const mockTimezoneRepository = {
findOne: jest.fn(),
Expand Down Expand Up @@ -34,3 +35,51 @@ describe('TimezonesService', () => {
expect(service).toBeDefined();
});
});

describe('TimezonesService - deleteTimezone', () => {
let service: TimezonesService;
let timezoneRepository: Repository<Timezone>;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
TimezonesService,
{
provide: getRepositoryToken(Timezone),
useClass: Repository,
},
],
}).compile();

service = module.get<TimezonesService>(TimezonesService);
timezoneRepository = module.get<Repository<Timezone>>(getRepositoryToken(Timezone));
});

it('should successfully delete a timezone', async () => {
const mockId = '123';

const mockTimezone: Timezone = {
id: '123',
timezone: 'UTC',
gmtOffset: '0',
description: 'Coordinated Universal Time',
created_at: new Date(),
updated_at: new Date(),
};
jest.spyOn(timezoneRepository, 'findOne').mockResolvedValue(mockTimezone);
jest.spyOn(timezoneRepository, 'delete').mockResolvedValue({ affected: 1 } as any);

const result = await service.deleteTimezone(mockId);

expect(result).toEqual({
status_code: 200,
message: 'Timezone deleted successfully',
});
});

it('should throw NotFoundException if timezone is not found', async () => {
jest.spyOn(timezoneRepository, 'findOne').mockResolvedValue(null);

await expect(service.deleteTimezone('invalid-id')).rejects.toThrow(NotFoundException);
});
});
31 changes: 31 additions & 0 deletions src/modules/timezones/timezones.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
TIMEZONE_CREATED_SUCCESSFULLY,
SUCCESS,
FETCH_TIMEZONE_FAILURE,
TIMEZONE_DELETED_SUCCESSFULLY,
} from '@shared/constants/SystemMessages';

@Injectable()
Expand Down Expand Up @@ -61,6 +62,7 @@ export class TimezonesService {
try {
const timezones = await this.timezoneRepository.find();
const formattedTimezones = timezones.map(tz => ({
id: tz.id,
timezone: tz.timezone,
gmtOffset: tz.gmtOffset,
description: tz.description,
Expand Down Expand Up @@ -106,4 +108,33 @@ export class TimezonesService {
});
}
}

async deleteTimezone(id: string): Promise<any> {
try {
const timezone = await this.timezoneRepository.findOne({ where: { id } });
if (!timezone) {
throw new NotFoundException({
status_code: HttpStatus.NOT_FOUND,
message: 'Timezone not found',
});
}

await this.timezoneRepository.delete(id);

return {
status_code: HttpStatus.OK,
message: TIMEZONE_DELETED_SUCCESSFULLY,
};
} catch (error) {
Logger.error('TimezonesServiceError ~ deleteTimezone ~', error);
if (error instanceof NotFoundException) {
throw error;
}

throw new InternalServerErrorException({
message: ERROR_OCCURED,
status_code: HttpStatus.INTERNAL_SERVER_ERROR,
});
}
}
}
1 change: 1 addition & 0 deletions src/shared/constants/SystemMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const WORK_IN_PROGRESS = 'Work in progress';
export const FETCH_LANGUAGE_FAILURE = 'Failed to fetch language';
export const UNAUTHORISED_TOKEN = 'Invalid token or email';
export const TIMEZONE_CREATED_SUCCESSFULLY = 'Timezone created Successfully';
export const TIMEZONE_DELETED_SUCCESSFULLY = 'Timezone deleted successfully';
export const FETCH_TIMEZONE_FAILURE = 'Error Occured while creating Timezone, kindly try again';
export const SUCCESS = 'Timezone fetched successfully';
export const TIMEZONE_ALREADY_EXISTS = 'Timezone already exists';
Expand Down

0 comments on commit 05b3be2

Please sign in to comment.