Skip to content

Commit

Permalink
Cyberleague:[premieroctet#114] Add isActionAllowed to manage nextQues…
Browse files Browse the repository at this point in the history
…tion action
  • Loading branch information
Bastien-Wappizy committed Sep 24, 2024
1 parent 03f5b5c commit 1b5e1e3
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions backend/web/server/plugins/cyberleague/actions.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const { addAction } = require('../../utils/studio/actions')
const { addAction, setAllowActionFn } = require('../../utils/studio/actions')
const Score = require("../../models/Score")
const lodash = require('lodash')
const { idEqual } = require('../../utils/database')
const { NotFoundError } = require('../../utils/errors')
const { NotFoundError, ForbiddenError } = require('../../utils/errors')
const { createScore } = require('./score')
const { SCORE_LEVEL_1 } = require('./consts')
const { SCORE_LEVEL_1, ANSWERS } = require('./consts')

//
const startSurvey = async (_, user) => {
Expand Down Expand Up @@ -33,4 +33,23 @@ const nextQuestion = async ({ value }, user) => {
return score.answers[answerIndex +1]
}
//TODO rename action to next_question
addAction('smartdiet_next_question', nextQuestion)
addAction('smartdiet_next_question', nextQuestion)

const isActionAllowed = async ({action, dataId, user, ...rest}) => {
if (action == 'smartdiet_next_question') {
const score = await Score.findOne({answers: dataId}).populate('answers')
const answerIndex = lodash.findIndex(score.answers, (a)=> idEqual(a._id, dataId))

//if answer without answer
if (!lodash.includes(ANSWERS,score.answers[answerIndex])) {
throw new ForbiddenError(`Il faut répondre à la question avant de pouvoir passer à la suivante`)
}

//si pas d'autres questions -> throw error
if (answerIndex + 1 == score.answers.length) {
throw new NotFoundError(`Il n'y a pas de question suivante`)
}
}
}

setAllowActionFn(isActionAllowed)

0 comments on commit 1b5e1e3

Please sign in to comment.