Skip to content

Commit f77737a

Browse files
Merge pull request #37 from call-0f-code/interview-fixes
Interview fixes
2 parents b7a6e6b + 5ac1593 commit f77737a

2 files changed

Lines changed: 56 additions & 14 deletions

File tree

src/controllers/interview.controller.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,17 +103,27 @@ export const updateInterviewById = async (req: Request, res: Response) => {
103103

104104
export const deleteInterviewById = async (req: Request, res: Response) => {
105105
const interviewId = parseInt(req.params.id);
106+
const { memberId } = req.body;
106107

107108
if (!interviewId) {
108109
throw new ApiError("Invalid interview ID", 400);
109110
}
110111

112+
if (!memberId) {
113+
throw new ApiError("Member ID is required for verification", 400);
114+
}
115+
111116
const existingInterview = await interviewService.getInterviewById(interviewId);
112117

113118
if (!existingInterview) {
114119
throw new ApiError("Interview experience not found", 404);
115120
}
116121

122+
123+
if (existingInterview.memberId !== memberId) {
124+
throw new ApiError("You are not authorized to delete this interview", 403);
125+
}
126+
117127
await interviewService.deleteInterviewById(interviewId);
118128

119129
res.status(200).json({
@@ -122,4 +132,3 @@ export const deleteInterviewById = async (req: Request, res: Response) => {
122132
});
123133
};
124134

125-

tests/Interview.test.ts

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -309,11 +309,14 @@ describe('updateInterviewById', () => {
309309

310310

311311
describe('deleteInterviewById', () => {
312+
afterEach(() => {
313+
jest.restoreAllMocks();
314+
});
315+
312316
it('should return 200 and success message', async () => {
313317
const req: any = {
314-
params: {
315-
id: '1',
316-
},
318+
params: { id: '1' },
319+
body: { memberId: 'user_123' },
317320
};
318321

319322
const res: any = {
@@ -331,13 +334,8 @@ describe('deleteInterviewById', () => {
331334
memberId: 'user_123',
332335
};
333336

334-
jest
335-
.spyOn(interviewService, 'getInterviewById')
336-
.mockResolvedValue(mockInterview);
337-
338-
jest
339-
.spyOn(interviewService, 'deleteInterviewById')
340-
.mockResolvedValue();
337+
jest.spyOn(interviewService, 'getInterviewById').mockResolvedValue(mockInterview);
338+
jest.spyOn(interviewService, 'deleteInterviewById').mockResolvedValue();
341339

342340
await deleteInterviewById(req, res);
343341

@@ -351,22 +349,57 @@ describe('deleteInterviewById', () => {
351349
it('should throw 400 for invalid interview ID', async () => {
352350
const req: any = {
353351
params: { id: 'invalid' },
352+
body: { memberId: 'user_123' },
354353
};
355354

356355
const res: any = {};
356+
357+
await expect(deleteInterviewById(req, res)).rejects.toThrow(ApiError);
358+
});
359+
360+
it('should throw 400 if memberId is missing', async () => {
361+
const req: any = {
362+
params: { id: '1' },
363+
body: {},
364+
};
365+
366+
const res: any = {};
367+
357368
await expect(deleteInterviewById(req, res)).rejects.toThrow(ApiError);
358369
});
359370

360371
it('should throw 404 if interview not found', async () => {
361372
const req: any = {
362373
params: { id: '999' },
374+
body: { memberId: 'user_123' },
363375
};
364376

365377
const res: any = {};
366378

367-
jest
368-
.spyOn(interviewService, 'getInterviewById')
369-
.mockResolvedValue(null);
379+
jest.spyOn(interviewService, 'getInterviewById').mockResolvedValue(null);
380+
381+
await expect(deleteInterviewById(req, res)).rejects.toThrow(ApiError);
382+
});
383+
384+
it('should throw 403 if memberId does not match', async () => {
385+
const req: any = {
386+
params: { id: '1' },
387+
body: { memberId: 'wrong_user' },
388+
};
389+
390+
const res: any = {};
391+
392+
const mockInterview = {
393+
id: 1,
394+
company: 'Google',
395+
role: 'SDE',
396+
verdict: Verdict.Selected,
397+
content: 'Nice',
398+
isAnonymous: false,
399+
memberId: 'user_123',
400+
};
401+
402+
jest.spyOn(interviewService, 'getInterviewById').mockResolvedValue(mockInterview);
370403

371404
await expect(deleteInterviewById(req, res)).rejects.toThrow(ApiError);
372405
});

0 commit comments

Comments
 (0)