Skip to content

Commit

Permalink
fix: validate and handle duplicate application
Browse files Browse the repository at this point in the history
  • Loading branch information
Ibrahim Fawaz Olamide authored and Ibrahim Fawaz Olamide committed Mar 1, 2025
1 parent 2ca80c4 commit 907dca4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/modules/jobs/jobs.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,22 @@ export class JobsService {

const { applicant_name, ...others } = jobApplicationDto;

const existingApplication = await this.jobApplicationRepository.findOne({
where: { job: { id: jobId }, applicant_name: jobApplicationDto.applicant_name },
relations: ['job'],
});

if (existingApplication) {
throw new CustomHttpException('Duplicate application', HttpStatus.BAD_REQUEST);
}

const resumeUrl = await this.s3Service.uploadFile(resume, 'resumes');

const createJobApplication = this.jobApplicationRepository.create({
...others,
applicant_name,
resume: resumeUrl,
...job,
job: job.data
});

await this.jobApplicationRepository.save(createJobApplication);
Expand Down
14 changes: 14 additions & 0 deletions src/modules/jobs/tests/jobs.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,20 @@ describe('JobsService', () => {
expect(service['jobApplicationRepository'].create).toHaveBeenCalled();
expect(service['jobApplicationRepository'].save).toHaveBeenCalled();
});

it('should throw error if duplicate application is found', async () => {
const resume = { buffer: Buffer.from('test file'), originalname: 'resume.pdf' } as Express.Multer.File;

jest.spyOn(service, 'getJob').mockResolvedValue(mockJob as any);
jest
.spyOn(service['jobApplicationRepository'], 'findOne')
.mockResolvedValue({ ...mockJobApplicationDto, resumeUrl: 'https://s3-bucket-url/resume.pdf' } as any);

await expect(service.applyForJob('jobId', mockJobApplicationDto, resume)).rejects.toThrow(
new CustomHttpException('Duplicate application', HttpStatus.CONFLICT)
);
});

});

describe('searchJobs', () => {
Expand Down

0 comments on commit 907dca4

Please sign in to comment.