Skip to content

Commit ed1fc10

Browse files
authored
Get points (#55)
* Get points * moreit
1 parent d758ca8 commit ed1fc10

File tree

6 files changed

+465
-14930
lines changed

6 files changed

+465
-14930
lines changed

functions/index.js

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const express = require('express');
1616
const user = require('./utilities/user');
1717
const { countries } = require('./utilities/countries')
1818
const isProfileComplete = require('./utilities/profileComplete');
19+
const { getCleanAnswers, getPgPoints } = require('./utilities/pgPoints')
1920

2021
const PAGES = require('./build/pages.json');
2122
const LEVELS = ['year6', 'year7', 'year8', 'year9', 'year10', 'year11'];
@@ -128,27 +129,11 @@ app.get('/api/scores', async (req, res) => {
128129
return res.status(200).send({ error: true });
129130
}
130131

131-
const clean = Object.keys(answers).reduce((acc, key) => {
132-
const { submitted, score, time, sameWeek} = answers[key];
133-
134-
const year = 'year' + key.split('-')[0];
135-
136-
const afterMigration = time && time > 1661382000000 ? true : false;
137-
138-
if(!submitted) return acc;
139-
if(!LEVELS.includes(year)) return acc;
140-
141-
acc[year] = acc[year] || {};
142-
143-
acc[year][key] = {
144-
score, time, sameWeek, afterMigration
145-
}
146-
147-
return acc;
148-
}, {})
132+
const clean = getCleanAnswers(answers)
149133

150134
res.status(200).send({
151135
profileComplete: res.locals.profileComplete,
136+
computedFullPGPoints: getPgPoints(res?.locals?.user),
152137
level,
153138
awardAdjustments,
154139
answers: clean,

functions/utilities/pgPoints.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
const LEVELS = ['year6', 'year7', 'year8', 'year9', 'year10', 'year11'];
2+
3+
const getCleanAnswers = answers => Object.keys(answers).reduce((acc, key) => {
4+
const { submitted, score, time, sameWeek } = answers[key];
5+
const year = 'year' + key.split('-')[0];
6+
const afterMigration = time && time > 1661382000000 ? true : false;
7+
8+
if (!submitted) return acc;
9+
if (!LEVELS.includes(year)) return acc;
10+
11+
acc[year] = acc[year] || {};
12+
acc[year][key] = {
13+
score, time, sameWeek, afterMigration
14+
}
15+
16+
return acc;
17+
}, {});
18+
19+
const getBonus = (numberCompleted) => {
20+
let count = 0;
21+
for (let x = 0; x < numberCompleted; x++) {
22+
count += x + 1;
23+
}
24+
return count;
25+
};
26+
27+
const isValidScore = (score) => {
28+
if (typeof score !== 'number') return false;
29+
if (score > 0 && score <= 100) return true;
30+
return false;
31+
};
32+
33+
const getNumberAfterMigration = (answerGroup) =>
34+
Object.entries(answerGroup).reduce((acc, [, value]) => {
35+
if (value.afterMigration) return acc + 1;
36+
37+
return acc;
38+
}, 0);
39+
40+
const getPoints = (answerGroup) =>
41+
Object.entries(answerGroup).reduce((acc, [, value]) => {
42+
if (value.score && isValidScore(value.score)) {
43+
if (!value.afterMigration) return acc;
44+
45+
acc += value.score;
46+
47+
if (value.sameWeek) {
48+
acc += 20 * (value.score / 100);
49+
}
50+
}
51+
return acc;
52+
}, 0);
53+
54+
const calculatePgYearPoints = (answerGroup, adjustment) => {
55+
const bonus = getBonus(Object.keys(answerGroup).length);
56+
57+
// Remove bonus of PGs done premigration
58+
const negativeBonus = getBonus(
59+
Object.keys(answerGroup).length - getNumberAfterMigration(answerGroup)
60+
);
61+
62+
const points = getPoints(answerGroup);
63+
64+
return Math.ceil(bonus + points - negativeBonus) + (adjustment || 0);
65+
};
66+
67+
const getPgPoints = (user) => {
68+
const awardAdjustments = user?.awardAdjustments || {};
69+
const cleanAnswers = getCleanAnswers(user?.answers || {})
70+
71+
return LEVELS.reduce((acc, level) => {
72+
acc[level] = calculatePgYearPoints(cleanAnswers[level] || {}, awardAdjustments[level])
73+
return acc;
74+
}, {})
75+
}
76+
77+
module.exports = {
78+
LEVELS,
79+
getPgPoints,
80+
getCleanAnswers
81+
}

0 commit comments

Comments
 (0)