Skip to content

Commit 0a91c6b

Browse files
Merge pull request #50 from call-0f-code/interview-pagination
Interview pagination
2 parents ea2ffee + b2165f6 commit 0a91c6b

3 files changed

Lines changed: 71 additions & 13 deletions

File tree

src/controllers/interview.controller.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,29 @@ import * as interviewService from "../services/interview.service";
33
import { ApiError } from "../utils/apiError";
44

55
export const getInterviews = async (req: Request, res: Response) => {
6-
const interviews = await interviewService.getInterviews();
6+
const page = parseInt(req.query.page as string) || 1;
7+
const limit = parseInt(req.query.limit as string) || 10;
78

8-
res.status(200).json({
9+
if(isNaN(page) || page<1){
10+
throw new ApiError("Page must be greater than or equal to 1",400);
11+
}
12+
if(isNaN(limit) || limit<1 || limit>100){
13+
throw new ApiError("Limit must be between 1 to 100",400)
14+
}
15+
16+
const { interviews, total } = await interviewService.getInterviews(page, limit);
17+
18+
return res.status(200).json({
919
success: true,
10-
data: interviews,
20+
data: interviews,
21+
page,
22+
limit,
23+
total,
24+
totalPages: Math.ceil(total / limit),
1125
});
1226
};
1327

28+
1429
export const getInterviewById = async (req: Request, res: Response) => {
1530
const interviewId = parseInt(req.params.id);
1631

src/services/interview.service.ts

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,39 @@
11
import { prisma } from "../db/client"
22

3-
export const getInterviews = async () => {
4-
return await prisma.interviewExperience.findMany({
5-
orderBy: {
6-
id: "desc",
7-
},
8-
});
3+
export const getInterviews = async (page: number = 1, limit: number = 10) => {
4+
const skip = (page - 1) * limit;
5+
6+
const [interviews, total] = await Promise.all([
7+
prisma.interviewExperience.findMany({
8+
skip,
9+
take: limit,
10+
include: {
11+
member: {
12+
select: {
13+
id: true,
14+
name: true,
15+
profilePhoto: true,
16+
},
17+
},
18+
},
19+
orderBy: {
20+
id: "desc",
21+
},
22+
}),
23+
24+
prisma.interviewExperience.count(),
25+
]);
26+
27+
const formattedInterviews = interviews.map(
28+
({ isAnonymous, member, memberId, ...rest }) => {
29+
return isAnonymous ? {...rest,isAnonymous} : { ...rest,isAnonymous, member };
30+
}
31+
);
32+
33+
return { interviews : formattedInterviews , total };
934
};
1035

36+
1137
export const getInterviewById = async (interviewId: number) => {
1238
return await prisma.interviewExperience.findUnique({
1339
where: {

tests/Interview.test.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ import { Verdict } from '../src/generated/prisma';
55

66
describe('getInterviews', () => {
77
it('should return 200 and all interviews', async () => {
8-
const req: any = {};
8+
const req: any = {
9+
query: {
10+
page: '1',
11+
limit: '10',
12+
},
13+
};
914
const res: any = {
1015
status: jest.fn().mockReturnThis(),
1116
json: jest.fn(),
@@ -19,25 +24,37 @@ describe('getInterviews', () => {
1924
verdict: Verdict.Selected,
2025
content: 'Great experience',
2126
isAnonymous: false,
22-
memberId: 'member123',
27+
member: {
28+
id: 'member123',
29+
name: 'John Doe',
30+
profilePhoto: null,
31+
},
2332
},
2433
];
2534

35+
const mockReturn = {
36+
interviews: mockInterviews,
37+
total: 1,
38+
};
39+
2640
jest
2741
.spyOn(interviewService, 'getInterviews')
28-
.mockResolvedValue(mockInterviews);
42+
.mockResolvedValue(mockReturn);
2943

3044
await getInterviews(req, res);
3145

3246
expect(res.status).toHaveBeenCalledWith(200);
3347
expect(res.json).toHaveBeenCalledWith({
3448
success: true,
3549
data: mockInterviews,
50+
page: 1,
51+
limit: 10,
52+
total: 1,
53+
totalPages: Math.ceil(1 / 10),
3654
});
3755
});
3856
});
3957

40-
4158
describe('getInterviewById', () => {
4259
it('should return 200 and the interview if found', async () => {
4360
const req: any = {

0 commit comments

Comments
 (0)