-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[feat] 면접 옵션 선택 후 시작 API
- Loading branch information
Showing
9 changed files
with
140 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default as interviewController } from './interviewController'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { startInterviewDTO } from '../interface/DTO'; | ||
import { Request, Response, NextFunction } from 'express'; | ||
import { message, statusCode } from '../module/constant'; | ||
import { success } from '../module/constant/utils'; | ||
import { interviewService } from '../service'; | ||
|
||
const startInterview = async (req: Request, res: Response, next: NextFunction) => { | ||
const startInterviewDTO: startInterviewDTO = req.body; | ||
const refreshToken = req.body; | ||
|
||
try { | ||
const data = await interviewService.startInterview(startInterviewDTO, refreshToken); | ||
|
||
return res | ||
.status(statusCode.CREATED) | ||
.send(success(statusCode.CREATED, message.START_INTERVIEW_SUCCESS, data)); | ||
} catch (error) { | ||
next(error); | ||
} | ||
}; | ||
|
||
export default { | ||
startInterview, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export interface startInterviewDTO { | ||
subjectText: string; | ||
questionNum: number; | ||
onlyVoice: boolean; | ||
startDateTime: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
import { Router } from "express"; | ||
import interviewRouter from './interviewRouter'; | ||
|
||
const router: Router = Router(); | ||
|
||
router.use('/interview', interviewRouter); | ||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Router } from 'express'; | ||
import { body, header, param } from 'express-validator'; | ||
import { interviewController } from '../controller'; | ||
import errorValidator from '../middleware/error/errorValidator'; | ||
|
||
const router: Router = Router(); | ||
|
||
router.post( | ||
'/', | ||
[ | ||
body('refreshToken').notEmpty(), | ||
body('subjectText').notEmpty(), | ||
body('questionNum').notEmpty(), | ||
body('onlyVoice').notEmpty(), | ||
body('startDateTime').notEmpty(), | ||
], | ||
errorValidator, | ||
interviewController.startInterview, | ||
); | ||
|
||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default as interviewService } from './interviewService'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { startInterviewDTO } from '../interface/DTO'; | ||
import { PrismaClient } from '@prisma/client'; | ||
import errorGenerator from '../middleware/error/errorGenerator'; | ||
import { message, statusCode } from '../module/constant'; | ||
const prisma = new PrismaClient(); | ||
|
||
const startInterview = async (startInterviewDTO: startInterviewDTO, refreshToken: string) => { | ||
try { | ||
const findUserId = await prisma.User.find({ | ||
where: { | ||
refreshToken: refreshToken, | ||
}, | ||
select: { | ||
id: true, | ||
}, | ||
}); | ||
|
||
const findSubjectId = await prisma.Subject.find({ | ||
where: { | ||
subjectText: startInterviewDTO.subjectText, | ||
}, | ||
select: { | ||
id: true, | ||
}, | ||
}); | ||
|
||
const countInterviewToday = await prisma.Subject.findMany({ | ||
include: { | ||
_count: { | ||
select: {startDateTime: startInterviewDTO.startDateTime} | ||
} | ||
} | ||
}); | ||
|
||
const questionList = await prisma.Question.findMany({ | ||
where: { | ||
subjectId: findSubjectId.id | ||
}, | ||
select: { | ||
id: true, | ||
questionText: true, | ||
}, | ||
}); | ||
|
||
const selectedQuestions = []; | ||
const numQuestions = Math.min(questionList.length, startInterviewDTO.questionNum); | ||
const shuffledQuestions = questionList.sort(() => Math.random() - 0.5); | ||
for (let i = 0; i < numQuestions; i++) { | ||
selectedQuestions.push(shuffledQuestions[i]); | ||
}; | ||
|
||
const interview = await prisma.Interview.create({ | ||
data: { | ||
userId: findUserId.id, | ||
subjectId: findSubjectId.id, | ||
questionNum: startInterviewDTO.questionNum, | ||
subjectText: startInterviewDTO.subjectText, | ||
title: null, | ||
onlyVoice: startInterviewDTO.onlyVoice, | ||
startDateTime: startInterviewDTO.startDateTime, | ||
questionList: selectedQuestions.map(question => ({ | ||
id: question.id, | ||
questionText: question.questionText | ||
})), | ||
}, | ||
}); | ||
return interview; | ||
} catch (error) { | ||
throw error; | ||
} | ||
}; | ||
|
||
export default { | ||
startInterview, | ||
}; |