Skip to content

Commit

Permalink
Issues apis and test ✅
Browse files Browse the repository at this point in the history
  • Loading branch information
HeeManSu committed Jul 7, 2024
1 parent 5f7340e commit be4ecae
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 29 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules
# Keep environment variables out of version control
.env
dist
coverage
3 changes: 3 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module.exports = {
preset: "ts-jest",
testEnvironment: "node",
testMatch: ["**/__tests__/**/*.ts", "**/?(*.)+(spec|test).ts"],
collectCoverage: true,
coverageDirectory: "coverage",
coverageReporters: ["text", "lcov"],
};
24 changes: 11 additions & 13 deletions src/__tests__/issuesController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Request, Response, NextFunction } from "express";
import { createIssue, deleteIssue, getAllIssues, updateIssue } from "../controllers/issuesController";
import issueService from "../services/issueService";
import issueRepository from "../repositories/issueRepository";
import ErrorHandlerClass from "../utils/errorClass";

jest.mock('../services/issueService');
jest.mock('../repositories/issueRepository');
Expand Down Expand Up @@ -146,7 +147,7 @@ describe('Issue Controller', () => {
});
});
describe('deleteIssue', () => {
it.only('should delete an issue and return 200 status', async () => {
it('should delete an issue and return 200 status', async () => {
const req = {
params: { issue_id: '1' },
} as unknown as Request;
Expand All @@ -169,7 +170,8 @@ describe('Issue Controller', () => {
message: 'Issue deleted successfully',
});
});
it('should return 404 if the issue does not exist', async () => {

it('should call next with an error if issue deletion fails', async () => {
const req = {
params: { issue_id: '1' },
} as unknown as Request;
Expand All @@ -181,18 +183,15 @@ describe('Issue Controller', () => {

const next = jest.fn() as NextFunction;

(issueRepository.isIssuePresent as jest.Mock).mockResolvedValue(false);
(issueRepository.isIssuePresent as jest.Mock).mockResolvedValue(true);
(issueService.deleteIssue as jest.Mock).mockRejectedValue(new Error('Unable to delete issue'));

await deleteIssue(req, res, next);

expect(res.status).toHaveBeenCalledWith(404);
expect(res.json).toHaveBeenCalledWith({
success: false,
message: 'Issue not found',
});
expect(next).toHaveBeenCalledWith(expect.any(Error));
});

it('should call next with an error if issue deletion fails', async () => {
it('should log a message if the issue does not exist', async () => {
const req = {
params: { issue_id: '1' },
} as unknown as Request;
Expand All @@ -205,11 +204,10 @@ describe('Issue Controller', () => {
const next = jest.fn() as NextFunction;

(issueRepository.isIssuePresent as jest.Mock).mockResolvedValue(false);
(issueService.deleteIssue as jest.Mock).mockRejectedValue(new Error('Unable to delete issue'));

await deleteIssue(req, res, next);
await deleteIssue(req as Request, res as Response, next as NextFunction);

expect(next).toHaveBeenCalledWith(expect.any(Error));
expect(next).toHaveBeenCalledWith(new ErrorHandlerClass('Requested issue not found', 404));
});
});
})
});
22 changes: 9 additions & 13 deletions src/controllers/issuesController.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { Request, Response, NextFunction } from "express";
import { catchAsyncError } from "../middlewares/catchAsyncError";
import ErrorHandlerClass from "../utils/errorClass";
import issueService from "../services/issueService";
import prisma from "../config/primsa-client";
import issueRepository from "../repositories/issueRepository";

export const createIssue = catchAsyncError(async (req: Request, res: Response, next: NextFunction): Promise<void> => {
export const createIssue = async (req: Request, res: Response, next: NextFunction): Promise<void> => {

try {
const newIssue = await issueService.createIssue(req.body);
Expand All @@ -19,9 +17,9 @@ export const createIssue = catchAsyncError(async (req: Request, res: Response, n
} catch (error) {
next(new ErrorHandlerClass("Unable to create issue", 500));
}
});
};

export const getAllIssues = catchAsyncError(async (req: Request, res: Response, next: NextFunction): Promise<void> => {
export const getAllIssues = async (req: Request, res: Response, next: NextFunction): Promise<void> => {
try {
const allIssues = await issueService.getAllIssues();

Expand All @@ -33,9 +31,9 @@ export const getAllIssues = catchAsyncError(async (req: Request, res: Response,
} catch (error) {
next(new ErrorHandlerClass("Failed to fetch issues", 500))
}
});
};

export const updateIssue = catchAsyncError(async (req: Request, res: Response, next: NextFunction): Promise<void> => {
export const updateIssue = async (req: Request, res: Response, next: NextFunction): Promise<void> => {
try {
const { issue_id } = req.params;

Expand All @@ -51,17 +49,15 @@ export const updateIssue = catchAsyncError(async (req: Request, res: Response, n
next(new ErrorHandlerClass("unable to update the issue", 500))
}

});
};


export const deleteIssue = catchAsyncError(async (req: Request, res: Response, next: NextFunction): Promise<void> => {
export const deleteIssue = async (req: Request, res: Response, next: NextFunction): Promise<void> => {
try {
const { issue_id } = req.params;

const isExists = await issueRepository.isIssuePresent(Number(issue_id));

if (!isExists) {
throw new ErrorHandlerClass("Issue not found", 404)
next(new ErrorHandlerClass("Requested issue not found", 404));
}

await issueService.deleteIssue(Number(issue_id));
Expand All @@ -74,4 +70,4 @@ export const deleteIssue = catchAsyncError(async (req: Request, res: Response, n
} catch (error) {
next(new ErrorHandlerClass("Unable to delete the issue", 500));
}
});
};
7 changes: 6 additions & 1 deletion src/middlewares/catchAsyncError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import { Request, Response, NextFunction } from 'express';

export const catchAsyncError = (passedFunction: (req: Request, res: Response, next: NextFunction) => Promise<void>) => {
return (req: Request, res: Response, next: NextFunction) => {
Promise.resolve(passedFunction(req, res, next)).catch(next);
Promise.resolve(passedFunction(req, res, next)).catch((error) => {
res.status(500).json({
success: false,
message: 'An unexpected error occurred',
});
});
};
};
4 changes: 2 additions & 2 deletions src/repositories/issueRepository.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Customer, Issue, Organization, Organization_People, Prisma } from "@prisma/client";
import prisma from "../config/primsa-client";
import { IssueBodyData, IssueCreateData } from "../interfaces/issueInterface";
import { IssueCreateData } from "../interfaces/issueInterface";

class IssueRepository {
async findOrganization(): Promise<Organization | null> {
Expand Down Expand Up @@ -55,7 +55,7 @@ class IssueRepository {
}

async deleteIssue(issue_id: number): Promise<Issue> {
return prisma.issue.delete({
return await prisma.issue.delete({
where: { issue_id }
});
}
Expand Down

0 comments on commit be4ecae

Please sign in to comment.