forked from premieroctet/openchakra
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cyberleague: [premieroctet#114] Add action next_question
- Loading branch information
1 parent
29eb5d0
commit ca48f12
Showing
1 changed file
with
20 additions
and
0 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
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) |