Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update job listing feature #623

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/modules/jobs/jobs.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,14 @@ export class JobsController {
async delete(@Param('id') id: string) {
return this.jobService.delete(id);
}

@UseGuards(JobGuard)
@Patch('/:id')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for the forward slash

@ApiOperation({ summary: 'Update a job' })
@ApiResponse({ status: 200, description: 'Job updated successfully' })
@ApiResponse({ status: 403, description: 'You do not have permission to perform this action' })
@ApiResponse({ status: 404, description: 'Job not found' })
async update(@Param('id') id: string, @Body() updateJobDto: Partial<JobDto>) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add UUID validation pipe to ensure id is of type UUID string.

return this.jobService.update(id, updateJobDto);
}
Comment on lines +58 to +60
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
async update(@Param('id') id: string, @Body() updateJobDto: Partial<JobDto>) {
return this.jobService.update(id, updateJobDto);
}
async updateJobById(@Param('id') id: string, @Body() updateJobDto: Partial<JobDto>) {
return this.jobService.updateJobById(id, updateJobDto);
}

}
21 changes: 21 additions & 0 deletions src/modules/jobs/jobs.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,27 @@ export class JobsService {
data: job,
};
}

async update(id: string, updateJobDto: Partial<JobDto>) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
async update(id: string, updateJobDto: Partial<JobDto>) {
async updateJobById(id: string, updateJobDto: Partial<JobDto>) {

const job = await this.jobRepository.findOne({ where: { id } });
if (!job) {
throw new NotFoundException({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use provided custom http exception handler

status_code: 404,
status: 'Not found Exception',
message: 'Job not found',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to use constants where possible. This makes code cleaner and maintainable

});
}

Object.assign(job, updateJobDto);
const updatedJob = await this.jobRepository.save(job);

return {
data: updatedJob,
message: 'Job details updated successfully',
status_code: 200,
};
}

async delete(jobId: string) {
// Check if listing exists
const job = await this.jobRepository.findOne({
Expand Down
55 changes: 55 additions & 0 deletions src/modules/jobs/tests/jobs.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import UserResponseDTO from '../../user/dto/user-response.dto';
import { User } from '../../user/entities/user.entity';
import { getRepositoryToken } from '@nestjs/typeorm';
import { jobsMock } from './mocks/jobs.mock';
import { NotFoundException } from '@nestjs/common';

describe('JobsService', () => {
let service: JobsService;
Expand Down Expand Up @@ -96,6 +97,60 @@ describe('JobsService', () => {
});
});

describe('updateJob', () => {
it('should update the job successfully with partial data', async () => {
const jobId = 'job-1';
const updateJobDto: Partial<JobDto> = {
title: 'Updated Software Engineer II',
salary_range: '80k_to_110k',
};

const existingJob = { ...createJobDto, id: jobId, is_deleted: false, user: userDto };
const updatedJob = { ...existingJob, ...updateJobDto };

jest.spyOn(jobRepository, 'findOne').mockResolvedValue(existingJob as Job);
jest.spyOn(jobRepository, 'save').mockResolvedValue(updatedJob as Job);

const result = await service.update(jobId, updateJobDto);

expect(result.status_code).toEqual(200);
expect(result.message).toEqual('Job details updated successfully');
expect(result.data).toEqual(updatedJob);

expect(jobRepository.findOne).toHaveBeenCalledWith({ where: { id: jobId } });
expect(jobRepository.save).toHaveBeenCalledWith(expect.objectContaining(updateJobDto));
});

it('should throw NotFoundException when job is not found', async () => {
const jobId = 'non-existent-job';
const updateJobDto: Partial<JobDto> = { title: 'Updated Title' };

jest.spyOn(jobRepository, 'findOne').mockResolvedValue(null);

await expect(service.update(jobId, updateJobDto)).rejects.toThrow(NotFoundException);
await expect(service.update(jobId, updateJobDto)).rejects.toThrow('Job not found');
});

it('should only update the provided fields', async () => {
const jobId = 'job-1';
const updateJobDto: Partial<JobDto> = {
title: 'Updated Software Engineer II',
};

const existingJob = { ...createJobDto, id: jobId, is_deleted: false, user: userDto };
const updatedJob = { ...existingJob, ...updateJobDto };

jest.spyOn(jobRepository, 'findOne').mockResolvedValue(existingJob as Job);
jest.spyOn(jobRepository, 'save').mockResolvedValue(updatedJob as Job);

const result = await service.update(jobId, updateJobDto);

expect(result.data.title).toEqual(updateJobDto.title);
expect(result.data.salary_range).toEqual(createJobDto.salary_range);
expect(jobRepository.save).toHaveBeenCalledWith(expect.objectContaining(updateJobDto));
});
});

describe('deleteJob', () => {
it('should delete the job successfully', async () => {
const result = await service.delete('job-1');
Expand Down
Loading