-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev' of https://github.com/PreciousIfeaka/hng_boilerpla…
…te_nestjs into fix/product-update
- Loading branch information
Showing
11 changed files
with
146 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { registerAs } from '@nestjs/config'; | ||
|
||
export default registerAs('s3', () => ({ | ||
accessKey: process.env.AWS_ACCESS_KEY || '', | ||
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY || '', | ||
region: process.env.AWS_REGION || 'us-east-2', | ||
bucketName: process.env.AWS_S3_BUCKET_NAME || '', | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
gerge branch 'dev' into feat/s3-resume-upload | ||
# Please enter a commit message to explain why this merge is necessary, | ||
# especially if it merges an updated upstream into a topic branch. | ||
# | ||
# Lines starting with '#' will be ignored, and an empty message aborts | ||
# the commit. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,25 +13,42 @@ import { JobsService } from '../jobs.service'; | |
import { jobsMock } from './mocks/jobs.mock'; | ||
import { JobSearchDto } from '../dto/jobSearch.dto'; | ||
import { UpdateJobDto } from '../dto/update-job.dto'; | ||
import { S3Service } from '@modules/s3/s3.service'; | ||
import { isPassed } from '../utils/helpers'; | ||
|
||
jest.mock('../utils/helpers', () => ({ | ||
isPassed: jest.fn(() => false), | ||
})); | ||
describe('JobsService', () => { | ||
let service: JobsService; | ||
let jobRepository: Repository<Job>; | ||
let userRepository: Repository<User>; | ||
let userDto: UserResponseDTO; | ||
let createJobDto: JobDto; | ||
let s3Service: S3Service; | ||
|
||
const mockJob = { | ||
data: { | ||
is_deleted: false, | ||
deadline: new Date(new Date().getTime() + 1000 * 60 * 60 * 24).toISOString(), | ||
}, | ||
}; | ||
|
||
const mockFile: Express.Multer.File = { | ||
fieldname: 'resume', | ||
originalname: 'resume.pdf', | ||
encoding: '7bit', | ||
mimetype: 'application/pdf', | ||
buffer: Buffer.from('mock file content'), | ||
size: 1024, | ||
destination: '', | ||
filename: '', | ||
path: '', | ||
stream: null, | ||
}; | ||
const mockJobApplicationDto: JobApplicationDto = { | ||
applicant_name: 'John Doe', | ||
email: '[email protected]', | ||
resume: 'resume content', | ||
resume: mockFile, | ||
cover_letter: 'Cover letter text', | ||
}; | ||
|
||
|
@@ -72,6 +89,7 @@ describe('JobsService', () => { | |
save: jest.fn(), | ||
findOneBy: jest.fn(), | ||
update: jest.fn(), | ||
|
||
createQueryBuilder: jest.fn().mockReturnValue({ | ||
where: jest.fn().mockReturnThis(), | ||
andWhere: jest.fn().mockReturnThis(), | ||
|
@@ -104,12 +122,19 @@ describe('JobsService', () => { | |
update: jest.fn(), | ||
}, | ||
}, | ||
{ | ||
provide: S3Service, | ||
useValue: { | ||
uploadFile: jest.fn().mockResolvedValue('https://s3-bucket-url/resume.pdf'), | ||
}, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
service = module.get<JobsService>(JobsService); | ||
userRepository = module.get(getRepositoryToken(User)); | ||
jobRepository = module.get(getRepositoryToken(Job)); | ||
s3Service = module.get<S3Service>(S3Service); | ||
|
||
jest.spyOn(userRepository, 'findOne').mockResolvedValue(userDto as User); | ||
jest.spyOn(jobRepository, 'create').mockReturnValue({ ...createJobDto, user: userDto } as Job); | ||
|
@@ -161,7 +186,7 @@ describe('JobsService', () => { | |
data: { is_deleted: true, deadline: new Date().toISOString() }, | ||
} as any); | ||
|
||
await expect(service.applyForJob('jobId', mockJobApplicationDto)).rejects.toThrow( | ||
await expect(service.applyForJob('jobId', mockJobApplicationDto, {} as Express.Multer.File)).rejects.toThrow( | ||
new CustomHttpException('Job deleted', HttpStatus.NOT_FOUND) | ||
); | ||
}); | ||
|
@@ -170,30 +195,34 @@ describe('JobsService', () => { | |
jest.spyOn(service, 'getJob').mockResolvedValue({ | ||
data: { is_deleted: false, deadline: new Date(new Date().getTime() - 1000 * 60 * 60 * 24).toISOString() }, | ||
} as any); | ||
(isPassed as jest.Mock).mockReturnValue(true); | ||
|
||
await expect(service.applyForJob('jobId', mockJobApplicationDto)).rejects.toThrow( | ||
await expect( | ||
service.applyForJob('jobId', mockJobApplicationDto, {} as Express.Multer.File) | ||
).rejects.toMatchObject( | ||
new CustomHttpException('Job application deadline passed', HttpStatus.UNPROCESSABLE_ENTITY) | ||
); | ||
}); | ||
it('should successfully create a job application with resume uploaded to S3', async () => { | ||
const resume = { buffer: Buffer.from('test file'), originalname: 'resume.pdf' } as Express.Multer.File; | ||
|
||
it('should successfully create a job application', async () => { | ||
jest.spyOn(service, 'getJob').mockResolvedValue(mockJob as any); | ||
const createMock = jest.fn().mockReturnValue(mockJobApplicationDto); | ||
const saveMock = jest.fn().mockResolvedValue(mockJobApplicationResponse); | ||
jest.spyOn(s3Service, 'uploadFile').mockResolvedValue('https://s3-bucket-url/resume.pdf'); | ||
(isPassed as jest.Mock).mockReturnValue(false); | ||
|
||
jest.spyOn(service['jobApplicationRepository'], 'create').mockImplementation(createMock); | ||
jest.spyOn(service['jobApplicationRepository'], 'save').mockImplementation(saveMock); | ||
jest | ||
.spyOn(service['jobApplicationRepository'], 'create') | ||
.mockReturnValue({ id: '1', ...mockJobApplicationDto } as any); | ||
jest | ||
.spyOn(service['jobApplicationRepository'], 'save') | ||
.mockResolvedValue({ id: '1', ...mockJobApplicationDto } as any); | ||
|
||
const result = await service.applyForJob('jobId', mockJobApplicationDto); | ||
const result = await service.applyForJob('jobId', mockJobApplicationDto, resume); | ||
|
||
expect(result).toEqual(mockJobApplicationResponse); | ||
expect(createMock).toHaveBeenCalledWith({ | ||
...mockJobApplicationDto, | ||
applicant_name: 'John Doe', | ||
resume: `https://example.com/John_Doe.pdf`, | ||
...mockJob, | ||
}); | ||
expect(saveMock).toHaveBeenCalled(); | ||
expect(s3Service.uploadFile).toHaveBeenCalledWith(resume, 'resumes'); // ✅ Updated expectation | ||
expect(service['jobApplicationRepository'].create).toHaveBeenCalled(); | ||
expect(service['jobApplicationRepository'].save).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { S3 } from 'aws-sdk'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { CustomHttpException } from '@shared/helpers/custom-http-filter'; | ||
import { HttpStatus } from '@nestjs/common'; | ||
|
||
@Injectable() | ||
export class S3Service { | ||
private s3: S3; | ||
private bucketName: string; | ||
|
||
constructor(private readonly configService: ConfigService) { | ||
this.s3 = new S3({ | ||
accessKeyId: this.configService.get<string>('AWS_ACCESS_KEY'), | ||
secretAccessKey: this.configService.get<string>('AWS_SECRET_ACCESS_KEY'), | ||
region: this.configService.get<string>('AWS_REGION'), | ||
}); | ||
this.bucketName = this.configService.get<string>('AWS_S3_BUCKET_NAME'); | ||
} | ||
|
||
/** | ||
* Uploads a file to S3 | ||
* @param file - The file to upload | ||
* @param folder - The folder in S3 where the file should be stored (e.g., 'resumes', 'images') | ||
* @returns The file URL | ||
*/ | ||
async uploadFile(file: Express.Multer.File, folder: string): Promise<string> { | ||
if (!file) { | ||
throw new CustomHttpException('File is required', HttpStatus.BAD_REQUEST); | ||
} | ||
|
||
const fileExtension = file.originalname.split('.').pop(); | ||
const fileKey = `${folder}/${Date.now()}_${file.originalname}`; | ||
|
||
const uploadParams = { | ||
Bucket: this.bucketName, | ||
Key: fileKey, | ||
Body: file.buffer, | ||
ContentType: file.mimetype, | ||
}; | ||
|
||
try { | ||
const { Location } = await this.s3.upload(uploadParams).promise(); | ||
return Location; | ||
} catch (error) { | ||
throw new CustomHttpException('Failed to upload file', HttpStatus.INTERNAL_SERVER_ERROR); | ||
} | ||
} | ||
} |