Skip to content

Commit

Permalink
Cyberleague: [premieroctet#134] delete virtual is_drafted and create …
Browse files Browse the repository at this point in the history
…completed field instead
  • Loading branch information
Bastien-Wappizy committed Sep 19, 2024
1 parent 89d41e7 commit dde9d06
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
6 changes: 3 additions & 3 deletions backend/web/server/plugins/cyberleague/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,6 @@ declareVirtualField({model: 'expertiseSet', field: 'display_categories', require

//Score declarations
declareVirtualField({model: 'score', field: 'deviation', requires: 'answers.answer', instance: 'Number'})
declareVirtualField({model: 'score', field: 'is_drafted', requires: 'answers.answer', instance: 'Boolean'})
declareVirtualField({model: 'score', field: 'question_count', require: 'answers', instance: 'Number'})
declareEnumField( {model: 'score', field: 'level', enumValues: SCORE_LEVELS})
declareComputedField({model: 'score', field: 'questions_by_category', requires: 'answers.question.question_category._id', getterFn: getQuestionsByCategory})
Expand Down Expand Up @@ -478,11 +477,12 @@ const postPutData = async ({model, id, params, user}) => {
if (model == 'answer') {
const answer = await Answer.findById(id)
const score = await Score.findById(answer.score)
const completed = score.answers?.filter(a => !a.answer).length == 0

if (score.is_drafted) {
if (completed) {
const computedScores = await computeScores(score.answers)

await Score.findByIdAndUpdate(score._id, {$set: {...computedScores}})
await Score.findByIdAndUpdate(score._id, {$set: {...computedScores, completed}})
}
}
return {model, id, params, user}
Expand Down
9 changes: 5 additions & 4 deletions backend/web/server/plugins/cyberleague/schemas/ScoreSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ const ScoreSchema = new Schema({
}]
}
}]
},
completed: {
type: Boolean,
required: true,
default: false
}
}, {...schemaOptions})

Expand All @@ -79,10 +84,6 @@ ScoreSchema.virtual('deviation', DUMMY_REF).get(function() {
return this?.answers?.filter(a => a.answer==ANSWER_NO).length || 0
})

ScoreSchema.virtual('is_drafted', DUMMY_REF).get(function() {
return this?.answers?.filter(a => !a.answer).length == 0
})

ScoreSchema.virtual('question_count',DUMMY_REF).get(function() {
return this.answers?.length || 0
})
Expand Down

0 comments on commit dde9d06

Please sign in to comment.