-
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.
Merge pull request #2382 from sophiemoustard/COM-3677
Com 3677
- Loading branch information
Showing
9 changed files
with
576 additions
and
41 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 |
---|---|---|
@@ -1,11 +1,50 @@ | ||
const Boom = require('@hapi/boom'); | ||
const QuestionnaireHistory = require('../models/QuestionnaireHistory'); | ||
const { COURSE, TRAINEE } = require('./constants'); | ||
const Questionnaire = require('../models/Questionnaire'); | ||
const { | ||
COURSE, | ||
TRAINEE, | ||
SELF_POSITIONNING, | ||
START_COURSE, | ||
END_COURSE, | ||
BEFORE_MIDDLE_COURSE_END_DATE, | ||
ENDED, | ||
UNKNOWN, | ||
} = require('./constants'); | ||
const CourseHistoriesHelper = require('./courseHistories'); | ||
const QuestionnaireHelper = require('./questionnaires'); | ||
const translate = require('./translate'); | ||
|
||
const { language } = translate; | ||
|
||
exports.addQuestionnaireHistory = async (payload) => { | ||
const traineesCompanyAtCourseRegistrationList = await CourseHistoriesHelper.getCompanyAtCourseRegistrationList( | ||
{ key: COURSE, value: payload.course }, { key: TRAINEE, value: [payload.user] } | ||
); | ||
const { user: userId, questionnaire: questionnaireId, course: courseId } = payload; | ||
const traineesCompanyAtCourseRegistrationList = await CourseHistoriesHelper | ||
.getCompanyAtCourseRegistrationList({ key: COURSE, value: courseId }, { key: TRAINEE, value: [userId] }); | ||
|
||
const questionnaire = await Questionnaire.findOne({ _id: questionnaireId }, { type: 1 }).lean(); | ||
|
||
let timeline; | ||
if (questionnaire.type === SELF_POSITIONNING) { | ||
const { courseTimeline } = await QuestionnaireHelper.getCourseInfos(courseId); | ||
|
||
return QuestionnaireHistory.create({ ...payload, company: traineesCompanyAtCourseRegistrationList[0].company }); | ||
switch (courseTimeline) { | ||
case BEFORE_MIDDLE_COURSE_END_DATE: | ||
timeline = START_COURSE; | ||
break; | ||
case ENDED: | ||
timeline = END_COURSE; | ||
break; | ||
default: | ||
timeline = UNKNOWN; | ||
} | ||
} | ||
|
||
const questionnaireHistoryExists = await QuestionnaireHistory | ||
.countDocuments({ course: courseId, user: userId, questionnaire: questionnaireId, ...(timeline && { timeline }) }); | ||
if (questionnaireHistoryExists) throw Boom.conflict(translate[language].questionnaireHistoryConflict); | ||
|
||
return QuestionnaireHistory.create( | ||
{ ...payload, company: traineesCompanyAtCourseRegistrationList[0].company, ...(timeline && { timeline }) } | ||
); | ||
}; |
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
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
Oops, something went wrong.