Skip to content

Commit

Permalink
Cyberleague: [premieroctet#114] Add action next_question
Browse files Browse the repository at this point in the history
  • Loading branch information
Bastien-Wappizy committed Sep 23, 2024
1 parent 29eb5d0 commit ca48f12
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions backend/web/server/plugins/cyberleague/actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const { addAction } = require('../../utils/studio/actions')
const Score = require("../../models/Score")
const lodash = require('lodash')
const { idEqual } = require('../../utils/database')
const { NotFoundError } = require('../../utils/errors')
const { createScore } = require('./score')

//value : _id of the answered question
const nextQuestion = async ({ value }, user) => {
const score = await Score.findOne({answers: value}).populate('answers')
const nextQuestionIndex = lodash.findIndex(score.answers, (a)=> idEqual(a._id, value)) + 1
if (nextQuestionIndex == score.answers.length) {
throw new NotFoundError(`Question ${value} is the last of the quiz`)
}
console.log('next Question', score.answers[nextQuestionIndex]);

return score.answers[nextQuestionIndex]
}
//TODO rename action to next_question
addAction('smartdiet_next_question', nextQuestion)

0 comments on commit ca48f12

Please sign in to comment.