Skip to content

Commit

Permalink
Cyberleague: [premieroctet#121] add updateMarketScore function
Browse files Browse the repository at this point in the history
  • Loading branch information
Bastien-Wappizy committed Sep 26, 2024
1 parent 41c53ca commit b99e63e
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion backend/web/server/plugins/cyberleague/score.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const { ANSWER_NOT_APPLICABLE, ANSWER_YES, SCORE_LEVEL_3, SCORE_LEVEL_2, SCORE_L
const Score = require("../../models/Score")
const Question = require("../../models/Question")
const Answer = require("../../models/Answer")
const User = require("../../models/User")

// questionArray: [{question, answer}]
const computeScores = async (answers) => {
Expand Down Expand Up @@ -131,9 +132,26 @@ const getCategoryRates = async (userId, params, data) => {
return res
}

const updateMarketScore = async ({_category_rates}) => {
const marketScore = await Score.findOne({_market: true})
//if no market score we create one
if (!marketScore) {
const admin = await User.findOne({role: ROLE_ADMIN})
return Score.create({creator: admin._id, _market: true, _category_rates: _category_rates})
}
//if there is a market score we update only if _category_rates is not null
if (!_category_rates) {
return Promise.resolve()
} else {
Score.findOneAndUpdate({_market: true}, {_category_rates: _category_rates})
}

}

module.exports = {
computeScoresIfRequired,
getQuestionsByCategory,
createScore,
getCategoryRates
getCategoryRates,
updateMarketScore
}

0 comments on commit b99e63e

Please sign in to comment.