Skip to content

Commit

Permalink
feat: update single user profile
Browse files Browse the repository at this point in the history
  • Loading branch information
Benson-Ogheneochuko committed Aug 8, 2024
2 parents b633586 + 47f2b88 commit fa43129
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 1 deletion.
76 changes: 75 additions & 1 deletion src/controllers/jobController.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Request, Response } from "express";
import { NextFunction, Request, Response } from "express";
import { JobService } from "../services/job.service";
import { HttpError } from "../middleware";
import AppDataSource from "../data-source";
Expand All @@ -23,6 +23,80 @@ export class JobController {
}
}

/**
* @swagger
* /api/v1/jobs/{jobId}:
* get:
* summary: Get job details by ID
* description: Retrieve the details of a job by its unique identifier.
* tags: [Jobs]
* parameters:
* - in: path
* name: jobId
* required: true
* schema:
* type: string
* description: The unique identifier of the job.
* responses:
* 200:
* description: Successfully retrieved job details.
* content:
* application/json:
* schema:
* type: object
* properties:
* id:
* type: string
* example: "1"
* title:
* type: string
* example: "Software Engineer"
* description:
* type: string
* example: "Job description here..."
* company_name:
* type: string
* example: "Company Name"
* location:
* type: string
* example: "Remote"
* salary:
* type: number
* example: 60000
* job_type:
* type: string
* example: Backend Devloper
* 404:
* description: Job not found.
* 400:
* description: Invalid job ID format.
* 500:
* description: Internal server error.
*/
public async getJobById(req: Request, res: Response, next: NextFunction) {
try {
const jobId = req.params.id;

const job = await this.jobService.getById(jobId);
if (!job) {
return res.status(404).json({
status_code: 404,
success: false,
message: "Job not found",
});
}

res.status(200).json({
status_code: 200,
success: true,
message: "The Job is retrieved successfully.",
data: job,
});
} catch (error) {
next(error);
}
}

async getAllJobs(req: Request, res: Response) {
try {
const billing = await this.jobService.getAllJobs(req);
Expand Down
2 changes: 2 additions & 0 deletions src/routes/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ jobRouter.delete(

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

jobRouter.get("/jobs/:id", jobController.getJobById.bind(jobController));

export { jobRouter };
12 changes: 12 additions & 0 deletions src/services/job.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ export class JobService {
return job;
}

public async getById(jobId: string): Promise<Job | null> {
try {
const job = await this.jobRepository.findOne({
where: { id: jobId },
});

return job;
} catch (error) {
throw new Error("Failed to fetch job details");
}
}

public async getAllJobs(req: Request): Promise<Job[] | null> {
try {
return await Job.find();
Expand Down
56 changes: 56 additions & 0 deletions src/test/jobService.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { JobService } from "../services/job.service";
import { Repository } from "typeorm";
import { Job } from "../models/job";
import AppDataSource from "../data-source";
import { BadRequest } from "../middleware";

jest.mock("../data-source");

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

beforeEach(() => {
jobRepository = {
findOne: jest.fn(),
} as any;

AppDataSource.getRepository = jest.fn().mockImplementation((model) => {
if (model === Job) {
return jobRepository;
}
throw new Error("Unknown model");
});

jobService = new JobService();
});

afterEach(() => {
jest.clearAllMocks();
});

describe("getJobById", () => {
it("should return job details for a valid ID", async () => {
const jobId = "1";
const jobDetails = {
id: jobId,
title: "Software Engineer",
description: "Job description here...",
user_id: "21",
location: "Remote",
salary: "60000",
job_type: "Developer",
company_name: "Company Name",
} as Job;

jobRepository.findOne.mockResolvedValue(jobDetails);

const result = await jobService.getById(jobId);

expect(jobRepository.findOne).toHaveBeenCalledWith({
where: { id: jobId },
});
expect(result).toEqual(jobDetails);
});
});
});

0 comments on commit fa43129

Please sign in to comment.