Skip to content

Commit

Permalink
Merge pull request #2389 from sophiemoustard/COM-3715
Browse files Browse the repository at this point in the history
Com 3715
  • Loading branch information
ulysseferreira committed May 27, 2024
2 parents cec9632 + bee7fd8 commit 93e3311
Show file tree
Hide file tree
Showing 9 changed files with 984 additions and 515 deletions.
4 changes: 2 additions & 2 deletions src/controllers/questionnaireController.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ const getUserQuestionnaires = async (req) => {

const getFollowUp = async (req) => {
try {
const followUp = await QuestionnaireHelper.getFollowUp(req.params._id, req.query.course, req.auth.credentials);
const followUp = await QuestionnaireHelper.getFollowUp(req.params._id, req.query, req.auth.credentials);

return { message: translate[language].questionnaireFound, data: { followUp } };
return { message: translate[language].questionnaireFound, data: { ...followUp } };
} catch (e) {
req.log('error', e);
return Boom.isBoom(e) ? e : Boom.badImplementation(e);
Expand Down
3 changes: 2 additions & 1 deletion src/helpers/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,10 +434,11 @@ module.exports = {
{ label: 'Question\t&\tRéponse', value: this.QUESTION_ANSWER, type: this.QUESTIONNAIRE },
];
},
// QUESTIONNAIRE_TYPES
// QUESTIONNAIRE
EXPECTATIONS: 'expectations',
END_OF_COURSE: 'end_of_course',
SELF_POSITIONNING: 'self_positionning',
REVIEW: 'review',
// QUESTIONNAIRE_HISTORY
START_COURSE: 'start_course',
END_COURSE: 'end_course',
Expand Down
59 changes: 41 additions & 18 deletions src/helpers/questionnaires.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const get = require('lodash/get');
const pick = require('lodash/pick');
const QRCode = require('qrcode');
const Questionnaire = require('../models/Questionnaire');
const Course = require('../models/Course');
Expand All @@ -17,6 +18,7 @@ const {
BEFORE_MIDDLE_COURSE_END_DATE,
BETWEEN_MID_AND_END_COURSE,
ENDED,
REVIEW,
} = require('./constants');
const DatesUtilsHelper = require('./dates/utils');
const { CompaniDate } = require('./dates/companiDates');
Expand Down Expand Up @@ -172,26 +174,20 @@ const formatQuestionnaireAnswersWithCourse = async (courseId, questionnaireAnswe
};
};

exports.getFollowUp = async (id, courseId, credentials) => {
const isVendorUser = !!get(credentials, 'role.vendor');
const questionnaire = await Questionnaire.findOne({ _id: id })
.select('type name')
.populate({
path: 'histories',
match: courseId ? { course: courseId } : null,
options: { isVendorUser },
select: '-__v -createdAt -updatedAt',
populate: [
{ path: 'questionnaireAnswersList.card', select: '-__v -createdAt -updatedAt' },
{
path: 'course',
select: 'trainer subProgram',
populate: { path: 'subProgram', select: 'program', populate: { path: 'program', select: '_id' } },
},
],
})
const getFollowUpForReview = async (questionnaire, courseId) => {
const followUp = questionnaire.histories.map(h => pick(h, ['user', 'questionnaireAnswersList', 'timeline']));

const course = await Course.findOne({ _id: courseId })
.select('subProgram companies misc type holding')
.populate({ path: 'subProgram', select: 'program', populate: [{ path: 'program', select: 'name' }] })
.populate({ path: 'companies', select: 'name' })
.populate({ path: 'holding', select: 'name' })
.lean();

return { followUp, course };
};

const getFollowUpForList = async (questionnaire, courseId) => {
const followUp = {};
for (const history of questionnaire.histories) {
for (const answer of history.questionnaireAnswersList) {
Expand All @@ -212,6 +208,33 @@ exports.getFollowUp = async (id, courseId, credentials) => {
return courseId ? formatQuestionnaireAnswersWithCourse(courseId, questionnaireAnswers) : questionnaireAnswers;
};

exports.getFollowUp = async (questionnaireId, query, credentials) => {
const isVendorUser = !!get(credentials, 'role.vendor');
const { course, action } = query;

const questionnaire = await Questionnaire.findOne({ _id: questionnaireId })
.select('type name')
.populate({
path: 'histories',
match: course ? { course } : null,
options: { isVendorUser },
select: '-__v -createdAt -updatedAt',
populate: [
{ path: 'questionnaireAnswersList.card', select: '-__v -createdAt -updatedAt' },
{
path: 'course',
select: 'trainer subProgram',
populate: { path: 'subProgram', select: 'program', populate: { path: 'program', select: '_id' } },
},
],
})
.lean();

return action === REVIEW
? getFollowUpForReview(questionnaire, course)
: getFollowUpForList(questionnaire, course);
};

exports.generateQRCode = async (courseId) => {
const qrCode = await QRCode
.toDataURL(`${process.env.WEBSITE_HOSTNAME}/ni/questionnaires?courseId=${courseId}`, { margin: 0 });
Expand Down
36 changes: 28 additions & 8 deletions src/routes/preHandlers/questionnaires.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ const {
PUBLISHED,
TRAINER,
BLENDED,
REVIEW,
SELF_POSITIONNING,
} = require('../../helpers/constants');
const translate = require('../../helpers/translate');
const { areObjectIdsEquals } = require('../../helpers/utils');
const Questionnaire = require('../../models/Questionnaire');
const Card = require('../../models/Card');
const Course = require('../../models/Course');
Expand Down Expand Up @@ -89,17 +92,34 @@ exports.authorizeCardDeletion = async (req) => {

exports.authorizeGetFollowUp = async (req) => {
const credentials = get(req, 'auth.credentials');
if (req.query.course) {
const countQuery = get(credentials, 'role.vendor.name') === TRAINER
? { _id: req.query.course, format: BLENDED, trainer: credentials._id }
: { _id: req.query.course, format: BLENDED };
const course = await Course.countDocuments(countQuery);
if (!course) throw Boom.notFound();
} else if (get(credentials, 'role.vendor.name') === TRAINER) throw Boom.forbidden();

const questionnaire = await Questionnaire.countDocuments({ _id: req.params._id });
const questionnaire = await Questionnaire.findOne({ _id: req.params._id }, { type: 1, program: 1 }).lean();
if (!questionnaire) throw Boom.notFound();

if (req.query.course) {
if (req.query.action === REVIEW) {
if (questionnaire.type !== SELF_POSITIONNING) throw Boom.notFound();

const course = await Course
.findOne({ _id: req.query.course, format: BLENDED }, { trainer: 1, subProgram: 1 })
.populate({ path: 'subProgram', select: 'program', populate: { path: 'program', select: '_id' } })
.lean();
if (!course) throw Boom.notFound();

if (!areObjectIdsEquals(questionnaire.program, course.subProgram.program._id)) throw Boom.notFound();

const loggedUserIsCourseTrainer = areObjectIdsEquals(course.trainer, credentials._id);
if (!loggedUserIsCourseTrainer) throw Boom.forbidden();
} else {
const countQuery = get(credentials, 'role.vendor.name') === TRAINER
? { _id: req.query.course, format: BLENDED, trainer: credentials._id }
: { _id: req.query.course, format: BLENDED };

const course = await Course.countDocuments(countQuery);
if (!course) throw Boom.notFound();
}
} else if (get(credentials, 'role.vendor.name') === TRAINER) throw Boom.forbidden();

return null;
};

Expand Down
9 changes: 8 additions & 1 deletion src/routes/questionnaires.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ const {
SURVEY,
QUESTION_ANSWER,
SELF_POSITIONNING,
REVIEW,
LIST,
} = require('../helpers/constants');

const QUESTIONNAIRE_CARD_TEMPLATES = [
Expand Down Expand Up @@ -96,7 +98,12 @@ exports.plugin = {
options: {
validate: {
params: Joi.object({ _id: Joi.objectId().required() }),
query: Joi.object({ course: Joi.objectId() }),
query: Joi.object({
course: Joi.objectId(),
action: Joi.string()
.when('course', { is: Joi.exist(), then: Joi.valid(LIST, REVIEW), otherwise: Joi.valid(LIST) })
.default(LIST),
}),
},
auth: { scope: ['questionnaires:read'] },
pre: [{ method: authorizeGetFollowUp }],
Expand Down

0 comments on commit 93e3311

Please sign in to comment.