Skip to content

Commit

Permalink
Merge pull request #2395 from sophiemoustard/COM-3719
Browse files Browse the repository at this point in the history
Com 3719
  • Loading branch information
sophiemoustard committed Jun 19, 2024
2 parents bcaa7b3 + 6492456 commit 70f1925
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 14 deletions.
31 changes: 25 additions & 6 deletions src/helpers/questionnaireHistories.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const Boom = require('@hapi/boom');
const keyBy = require('lodash/keyBy');
const QuestionnaireHistory = require('../models/QuestionnaireHistory');
const Questionnaire = require('../models/Questionnaire');
const {
Expand Down Expand Up @@ -50,11 +51,29 @@ exports.addQuestionnaireHistory = async (payload) => {
};

exports.updateQuestionnaireHistory = async (questionnaireHistoryId, payload) => {
const { trainerComment } = payload;
const { trainerComment, trainerAnswers } = payload;

return QuestionnaireHistory
.updateOne(
{ _id: questionnaireHistoryId },
{ $set: { isValidated: true, ...(trainerComment && { trainerComment }) } }
);
let setFields = { isValidated: true, ...(trainerComment && { trainerComment }) };
if (trainerAnswers.some(a => a.answer)) {
const trainerAnswersByCard = keyBy(trainerAnswers, 'card');
const questionnaireHistory = await QuestionnaireHistory
.findOne({ _id: questionnaireHistoryId }, { questionnaireAnswersList: 1 })
.lean();

const questionnaireAnswersList = [];
for (const bddAnswer of questionnaireHistory.questionnaireAnswersList) {
const trainerAnswer = trainerAnswersByCard[bddAnswer.card];

if (!trainerAnswer.answer) {
questionnaireAnswersList.push(bddAnswer);
continue;
}

questionnaireAnswersList.push({ ...bddAnswer, trainerAnswerList: [trainerAnswer.answer] });
}

setFields = { ...setFields, questionnaireAnswersList };
}

return QuestionnaireHistory.updateOne({ _id: questionnaireHistoryId }, { $set: setFields });
};
1 change: 1 addition & 0 deletions src/models/QuestionnaireHistory.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const QuestionnaireHistorySchema = mongoose.Schema({
questionnaireAnswersList: [{
card: { type: mongoose.Schema.Types.ObjectId, ref: 'Card' },
answerList: { type: [String] },
trainerAnswerList: { type: [String], default: undefined },
}],
company: { type: mongoose.Schema.Types.ObjectId, ref: 'Company', required: true },
origin: { type: String, enum: ORIGIN_OPTIONS, required: true, immutable: true, default: MOBILE },
Expand Down
6 changes: 4 additions & 2 deletions src/routes/preHandlers/questionnaireHistories.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ exports.authorizeQuestionnaireHistoryUpdate = async (req) => {

const cardIds = trainerAnswers.map(answer => answer.card);
const questionnaire = await Questionnaire
.findOne({ _id: questionnaireHistory.questionnaire, cards: { $in: cardIds } })
.lean();
.countDocuments({ _id: questionnaireHistory.questionnaire, cards: { $in: cardIds } });
if (!questionnaire) throw Boom.notFound();

const answersHasGoodLength = trainerAnswers.length === questionnaireHistory.questionnaireAnswersList.length;
if (!answersHasGoodLength) throw Boom.badRequest();

const everyAnswerIsAuthorized = trainerAnswers.every(a => !a.answer || ['1', '2', '3', '4', '5'].includes(a.answer));
if (!everyAnswerIsAuthorized) throw Boom.badRequest();

return null;
};
2 changes: 1 addition & 1 deletion src/routes/questionnaireHistories.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ exports.plugin = {
validate: {
params: Joi.object({ _id: Joi.objectId().required() }),
payload: Joi.object({
trainerAnswers: Joi.array().items(Joi.object({ card: Joi.objectId() })),
trainerAnswers: Joi.array().items(Joi.object({ card: Joi.objectId(), answer: Joi.string() })),
trainerComment: Joi.string(),
}),
},
Expand Down
20 changes: 19 additions & 1 deletion tests/integration/questionnaireHistories.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,10 @@ describe('QUESTIONNAIRE HISTORIES ROUTES - PUT /questionnairehistories/{_id}', (
});

it('should update questionnaireHistory', async () => {
const payload = { trainerAnswers: [{ card: cardsList[1]._id }], trainerComment: 'Appréciation du formateur' };
const payload = {
trainerAnswers: [{ card: cardsList[1]._id, answer: '1' }],
trainerComment: 'Appréciation du formateur',
};
const response = await app.inject({
method: 'PUT',
url: `/questionnairehistories/${endSelfPositionningQuestionnaireHistoryId}`,
Expand Down Expand Up @@ -521,6 +524,21 @@ describe('QUESTIONNAIRE HISTORIES ROUTES - PUT /questionnairehistories/{_id}', (

expect(response.statusCode).toBe(400);
});

it('should return 400 if a trainerAnswer is not allowed', async () => {
const payload = {
trainerAnswers: [{ card: cardsList[1]._id, answer: 'mauvaiseReponse' }],
trainerComment: 'Appréciation du formateur',
};
const response = await app.inject({
method: 'PUT',
url: `/questionnairehistories/${endSelfPositionningQuestionnaireHistoryId}`,
payload,
headers: { Cookie: `alenvi_token=${authToken}` },
});

expect(response.statusCode).toBe(400);
});
});

describe('OTHER ROLES', () => {
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/seed/questionnairesSeed.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,8 @@ const questionnaireHistories = [
questionnaire: questionnairesList[3]._id,
user: traineeList[0]._id,
questionnaireAnswersList: [
{ card: cardsList[1]._id, answerList: ['test'] },
{ card: cardsList[3]._id, answerList: ['blabla2'] },
{ card: cardsList[1]._id, answerList: ['3'] },
{ card: cardsList[3]._id, answerList: ['4'] },
],
timeline: START_COURSE,
},
Expand Down
25 changes: 25 additions & 0 deletions tests/integration/seed/seedsVerification.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2083,6 +2083,31 @@ describe('SEEDS VERIFICATION', () => {

expect(everySelfPositionningHistoryHasTimeline).toBeTruthy();
});

it('should pass if every trainee\'s answer is authorized (SELF POSITIONNING QUESTIONNAIRE)', () => {
const everySelfPositionningHistoryHasAuthorizedTraineeAnswer = questionnaireHistoryList
.filter(qh => qh.questionnaire.type === SELF_POSITIONNING)
.every(qh => qh.questionnaireAnswersList.every(a => ['1', '2', '3', '4', '5'].includes(a.answerList[0])));

expect(everySelfPositionningHistoryHasAuthorizedTraineeAnswer).toBeTruthy();
});

it('should pass if every trainer\'s answer is authorized (SELF POSITIONNING QUESTIONNAIRE)', () => {
const everySelfPositionningHistoryHasAuthorizedTrainerAnswer = questionnaireHistoryList
.filter(qh => qh.questionnaire.type === SELF_POSITIONNING)
.every(qh => qh.questionnaireAnswersList
.every(a => !a.trainerAnswerList || ['1', '2', '3', '4', '5'].includes(a.trainerAnswerList[0])));

expect(everySelfPositionningHistoryHasAuthorizedTrainerAnswer).toBeTruthy();
});

it('should pass if validated histories are linked to end self-positioning questionnaire', () => {
const everySelfPositionningHistoryHasAuthorizedTrainerAnswer = questionnaireHistoryList
.filter(qh => qh.isValidated)
.every(qh => qh.questionnaire.type === SELF_POSITIONNING && qh.timeline === END_COURSE);

expect(everySelfPositionningHistoryHasAuthorizedTrainerAnswer).toBeTruthy();
});
});

describe('Collection SectorHistory', () => {
Expand Down
58 changes: 56 additions & 2 deletions tests/unit/helpers/questionnaireHistories.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,30 +306,84 @@ describe('addQuestionnaireHistory', () => {
describe('updateQuestionnaireHistory', () => {
const questionnaireHistoryId = new ObjectId();
let updateOne;
let findOne;

beforeEach(() => {
updateOne = sinon.stub(QuestionnaireHistory, 'updateOne');
findOne = sinon.stub(QuestionnaireHistory, 'findOne');
});

afterEach(() => {
updateOne.restore();
findOne.restore();
});

it('should update questionnaireHistory', async () => {
const payload = { trainerAnswers: [{ _id: new ObjectId() }], trainerComment: '' };
const payload = { trainerAnswers: [{ card: new ObjectId() }], trainerComment: '' };

await QuestionnaireHistoriesHelper.updateQuestionnaireHistory(questionnaireHistoryId, payload);

sinon.assert.notCalled(findOne);
sinon.assert.calledWithExactly(updateOne, { _id: questionnaireHistoryId }, { $set: { isValidated: true } });
});

it('should update questionnaireHistory with trainer comment', async () => {
const payload = { trainerAnswers: [{ _id: new ObjectId() }], trainerComment: 'Test avec un commentaire' };
const payload = { trainerAnswers: [{ card: new ObjectId() }], trainerComment: 'Test avec un commentaire' };

await QuestionnaireHistoriesHelper.updateQuestionnaireHistory(questionnaireHistoryId, payload);

sinon.assert.notCalled(findOne);
sinon.assert.calledWithExactly(
updateOne,
{ _id: questionnaireHistoryId },
{ $set: { isValidated: true, trainerComment: payload.trainerComment } }
);
});

it('should update questionnaireHistory with trainer answers', async () => {
const cardIds = [new ObjectId(), new ObjectId(), new ObjectId()];
const payload = {
trainerAnswers: [
{ card: cardIds[0], answer: '4' },
{ card: cardIds[1] },
{ card: cardIds[2], answer: '5' },
],
};
const questionnaireHistory = {
_id: questionnaireHistoryId,
questionnaire: new ObjectId(),
user: new ObjectId(),
timeline: END_COURSE,
questionnaireAnswersList: [
{ card: cardIds[0], answerList: ['3'] },
{ card: cardIds[1], answerList: ['4'] },
{ card: cardIds[2], answerList: ['4'] },
],
};
findOne.returns(SinonMongoose.stubChainedQueries(questionnaireHistory, ['lean']));

await QuestionnaireHistoriesHelper.updateQuestionnaireHistory(questionnaireHistoryId, payload);

SinonMongoose.calledOnceWithExactly(
findOne,
[
{ query: 'findOne', args: [{ _id: questionnaireHistoryId }, { questionnaireAnswersList: 1 }] },
{ query: 'lean' },
]
);
sinon.assert.calledWithExactly(
updateOne,
{ _id: questionnaireHistoryId },
{
$set: {
isValidated: true,
questionnaireAnswersList: [
{ card: cardIds[0], answerList: ['3'], trainerAnswerList: ['4'] },
{ card: cardIds[1], answerList: ['4'] },
{ card: cardIds[2], answerList: ['4'], trainerAnswerList: ['5'] },
],
},
}
);
});
});

0 comments on commit 70f1925

Please sign in to comment.