Skip to content

Commit

Permalink
Cyberleague: [premieroctet#128] Adapt score post create to match new …
Browse files Browse the repository at this point in the history
…spec (lvl 3 includes lvl 2 includes lvl 1)
  • Loading branch information
Bastien-Wappizy committed Sep 13, 2024
1 parent d3702c3 commit 7ff1698
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 21 deletions.
19 changes: 15 additions & 4 deletions backend/web/server/plugins/cyberleague/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const {
setPostPutData,
idEqual,
} = require('../../utils/database')
const { ROLES, SECTOR, EXPERTISE_CATEGORIES, CONTENT_TYPE, JOBS, COMPANY_SIZE, ROLE_PARTNER, ROLE_ADMIN, ROLE_MEMBER, ESTIMATED_DURATION_UNITS, LOOKING_FOR_MISSION, CONTENT_VISIBILITY, EVENT_VISIBILITY, ANSWERS, QUESTION_CATEGORIES, SCORE_LEVELS, COIN_SOURCES } = require('./consts')
const { ROLES, SECTOR, EXPERTISE_CATEGORIES, CONTENT_TYPE, JOBS, COMPANY_SIZE, ROLE_PARTNER, ROLE_ADMIN, ROLE_MEMBER, ESTIMATED_DURATION_UNITS, LOOKING_FOR_MISSION, CONTENT_VISIBILITY, EVENT_VISIBILITY, ANSWERS, QUESTION_CATEGORIES, SCORE_LEVELS, COIN_SOURCES, SCORE_LEVEL_1, SCORE_LEVEL_3, SCORE_LEVEL_2 } = require('./consts')
const { PURCHASE_STATUS } = require('../../../utils/consts')
const Company = require('../../models/Company')
const { BadRequestError } = require('../../utils/errors')
Expand All @@ -24,7 +24,7 @@ const QuestionCategory = require('../../models/QuestionCategory')
const { isMineForMessage } = require('./message')
const { getConversationPartner } = require('./conversation')
const ExpertiseCategory = require('../../models/ExpertiseCategory')
const { computeScores, booleanLevelFieldName, getQuestionsByCategory } = require('./score')
const { computeScores, getQuestionsByCategory } = require('./score')
const Conversation = require('../../models/Conversation')
const Question = require('../../models/Question')
const Answer = require('../../models/Answer')
Expand Down Expand Up @@ -443,8 +443,19 @@ const postCreate = async ({ model, params, data, user }) => {
}

if (model == 'score') {
const booleanField = booleanLevelFieldName(data.level)
const questions = await Question.find({[booleanField]: true})
let questions = await Question.find()

switch (data.level) {
case SCORE_LEVEL_3:
questions = lodash.filter(questions, (q) => q.is_level_1 || q.is_level_2 || q.is_level_3)
break;
case SCORE_LEVEL_2:
questions = lodash.filter(questions, (q) => q.is_level_1 || q.is_level_2)
break;
case SCORE_LEVEL_1:
questions = lodash.filter(questions, (q) => q.is_level_1)
break;
}
const answers=await Promise.all(questions.map(async q => {
return Answer.create({score: data._id, question: q._id})
}))
Expand Down
17 changes: 0 additions & 17 deletions backend/web/server/plugins/cyberleague/score.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,22 +78,6 @@ const computeScores = async (answerArray) => {
return {global_rate, category_rates, bellwether_rates}
}

const booleanLevelFieldName = (scoreLevel) => {
switch (scoreLevel) {
case SCORE_LEVEL_1:
return `is_level_1`

case SCORE_LEVEL_2:
return `is_level_2`

case SCORE_LEVEL_3:
return `is_level_3`

default:
throw new Error(`Unknown score level`);
}
}

const getQuestionsByCategory = async (userId, params, data) => {
const groupedQuestions = lodash.groupBy(data.answers, (a) => a.question.question_category._id)
const res = []
Expand All @@ -105,6 +89,5 @@ const getQuestionsByCategory = async (userId, params, data) => {

module.exports = {
computeScores,
booleanLevelFieldName,
getQuestionsByCategory
}

0 comments on commit 7ff1698

Please sign in to comment.