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

ENDPONT_TO_GET_SINGLE_JOB_INSTANCE_WITH_TEST_CASES #473

Closed
wants to merge 4 commits into from
Closed
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
121 changes: 121 additions & 0 deletions src/controllers/jobController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ export class JobController {
* type: integer
* example: 500
*/

async createJob(req: Request, res: Response) {
try {
const job = await this.jobService.createJob(req.body, req.user?.id);
Expand All @@ -200,6 +201,125 @@ export class JobController {
}
}

/**
* @swagger
* /api/v1/jobs/{jobId}:
* get:
* summary: Get a job listing by id
* description: Get a job listing by id
* tags: [Jobs]
* parameters:
* - in: path
* name: jobId
* description: Job id
* required: true
* schema:
* type: string
* format: uuid
* responses:
* 200:
* description: Job listing found
* content:
* application/json:
* schema:
* type: object
* properties:
* status:
* type: string
* example: success
* status_code:
* type: integer
* example: 200
* message:
* type: string
* example: Job listing found
* data:
* type: object
* properties:
* id:
* type: string
* example: 123e4567-e89b-12d3-a456-426614174000
* title:
* type: string
* example: Software Engineer
* description:
* type: string
* example: This is a job description
* location:
* type: string
* example: Remote
* deadline:
* type: string
* format: date-time
* example: 2023-07-21T19:58:00.000Z
* salary_range:
* type: string
* example: 50k_to_70k
* job_type:
* type: string
* example: full-time
* job_mode:
* type: string
* example: remote
* company_name:
* type: string
* example: ABC Company
* 400:
* description: Invalid request
* content:
* application/json:
* schema:
* type: object
* properties:
* status:
* type: string
* example: error
* message:
* type: string
* example: Invalid request
* status_code:
* type: integer
* example: 400
* 500:
* description: Internal server error
* content:
* application/json:
* schema:
* type: object
* properties:
* status:
* type: string
* example: error
* message:
* type: string
* example: Internal server error
* status_code:
* type: integer
* example: 500
*/

async getJobById(req: Request, res: Response) {
try {
const { jobId } = req.params;
const job = await this.jobService.getJobById(jobId);
if (!job) {
return res.status(404).json({
status: "error",
status_code: 404,
message: "Job listing not found",
});
}
res.json({
status: "success",
status_code: 200,
message: "Job listing retrieved successfully",
data: job,
});
} catch (error) {
res.status(500).json({ message: error.message, status_code: 500 });
}
}

/**
* @swagger
* /api/v1/jobs:
Expand Down Expand Up @@ -274,6 +394,7 @@ export class JobController {
* type: integer
* example: 500
*/

async getAllJobs(req: Request, res: Response) {
try {
const jobs = await this.jobService.getAllJobs();
Expand Down
8 changes: 8 additions & 0 deletions src/routes/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ jobRoute.post(
jobController.createJob.bind(jobController),
);


// Add a route to get a job by id
jobRoute.get(
"/jobs/:jobId",
authMiddleware,
jobController.getJobById.bind(jobController),
)

jobRoute.get("/jobs", jobController.getAllJobs.bind(jobController));

export { jobRoute };
9 changes: 9 additions & 0 deletions src/services/jobservice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ export class JobService {
}
}

async getJobById(jobId: string): Promise<Job> {
try {
return await this.jobRepository.findOne({
where: { id: jobId },
});
} catch (error) {
throw new Error(error);
}

async getAllJobs(): Promise<Job[]> {
try {
return await this.jobRepository.find();
Expand Down
87 changes: 87 additions & 0 deletions src/test/job.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { Repository } from "typeorm";
import { JobService } from '../services/jobservice';
import { Job } from '../models/job';
import { getJobSchema } from '../schemas/job';
import { ServerError, ResourceNotFound } from "../middleware";

jest.mock("../utils", () => ({
getIsInvalidMessage: jest
.fn()
.mockImplementation((field: string) => `${field} is invalid`),
}));

jest.mock("../data-source", () => ({
getRepository: jest.fn().mockImplementation((entity) => ({
create: jest.fn().mockReturnValue({}),
save: jest.fn(),
findOne: jest.fn(),
createQueryBuilder: jest.fn(() => ({
where: jest.fn().mockReturnThis(),
andWhere: jest.fn().mockReturnThis(),
skip: jest.fn().mockReturnThis(),
take: jest.fn().mockReturnThis(),
getManyAndCount: jest.fn().mockReturnValue([[], 0]),
})),
})),
}));

jest.mock('../schemas/job', () => ({
getJobSchema: jest.fn()
}));

describe("JobService", () => {
let jobService: JobService;
let jobRepository: Repository<Job>;

beforeEach(() => {
jobService = new JobService();
jobRepository = jobService["jobRepository"];
});

describe("getJob", () => {
it("should retrieve a job successfully", async () => {
// Mock data
const mockJobId = "1";
const mockJob: Job = {
id: "1",
title: "Software Engineer",
description: "This is a job description",
location: "Remote",
deadline: new Date("2023-07-21T19:58:00.000Z"),
salary_range: "50k_to_70k",
job_type: "full-time",
job_mode: "remote",
company_name: "ABC Company",
is_deleted: false,
user_id: "1",
created_at: new Date("2023-07-21T19:58:00.000Z"),
updated_at: new Date("2023-07-21T19:58:00.000Z"),
};

jobRepository.findOne = jest.fn().mockResolvedValue(mockJob);
(getJobSchema as jest.Mock).mockReturnValue(mockJob);

const result = await jobService.getJob(mockJobId);

expect(jobRepository.findOne).toHaveBeenCalledWith({ where: { id: mockJobId } });
expect(getJobSchema).toHaveBeenCalledWith(mockJob);
expect(result).toEqual(mockJob);
});

it("should throw ResourceNotFound error when job is not found", async () => {
const mockJobId = "1";
jobRepository.findOne = jest.fn().mockResolvedValue(null);

await expect(jobService.getJob(mockJobId)).rejects.toThrow(ResourceNotFound);
expect(jobRepository.findOne).toHaveBeenCalledWith({ where: { id: mockJobId } });
});

it("should throw ServerError on database error", async () => {
const mockJobId = "1";
jobRepository.findOne = jest.fn().mockRejectedValue(new Error("Database error"));

await expect(jobService.getJob(mockJobId)).rejects.toThrow(ServerError);
expect(jobRepository.findOne).toHaveBeenCalledWith({ where: { id: mockJobId } });
});
});
});
Loading