refactor(api): clean up certification logic (#39196)

This commit is contained in:
Oliver Eyton-Williams
2020-07-15 17:58:26 +02:00
committed by GitHub
parent 8ebb4922d4
commit 31f9211016

View File

@ -137,7 +137,7 @@ function createCertTypeIds(allChallenges) {
}; };
} }
function isCertified(ids, completedChallenges = []) { function canClaim(ids, completedChallenges = []) {
return _.every(ids, ({ id }) => return _.every(ids, ({ id }) =>
_.find(completedChallenges, ({ id: completedId }) => completedId === id) _.find(completedChallenges, ({ id: completedId }) => completedId === id)
); );
@ -197,7 +197,6 @@ const completionHours = {
[certTypes.machineLearningPyV7]: 400 [certTypes.machineLearningPyV7]: 400
}; };
// returns an array with a single element, to be flatMap'd by createdVerifyCert
function getCertById(anId, allChallenges) { function getCertById(anId, allChallenges) {
return allChallenges return allChallenges
.filter(({ id }) => id === anId) .filter(({ id }) => id === anId)
@ -206,7 +205,7 @@ function getCertById(anId, allChallenges) {
tests, tests,
name, name,
challengeType challengeType
})); }))[0];
} }
const superBlocks = Object.keys(superBlockCertTypeMap); const superBlocks = Object.keys(superBlockCertTypeMap);
@ -308,19 +307,13 @@ function createVerifyCert(certTypeIds, app) {
log(superBlock); log(superBlock);
let certType = superBlockCertTypeMap[superBlock]; let certType = superBlockCertTypeMap[superBlock];
log(certType); log(certType);
return user return Observable.of(certTypeIds[certType])
.getCompletedChallenges$()
.flatMap(() => certTypeIds[certType])
.flatMap(challenge => { .flatMap(challenge => {
const certName = certText[certType]; const certName = certText[certType];
if (user[certType]) { if (user[certType]) {
return Observable.just(alreadyClaimedMessage(certName)); return Observable.just(alreadyClaimedMessage(certName));
} }
let updateData = {
[certType]: true
};
// certificate doesn't exist or // certificate doesn't exist or
// connection error // connection error
if (!challenge) { if (!challenge) {
@ -329,11 +322,12 @@ function createVerifyCert(certTypeIds, app) {
} }
const { id, tests, challengeType } = challenge; const { id, tests, challengeType } = challenge;
if (!user[certType] && !isCertified(tests, user.completedChallenges)) { if (!canClaim(tests, user.completedChallenges)) {
return Observable.just(notCertifiedMessage(certName)); return Observable.just(notCertifiedMessage(certName));
} }
updateData = {
...updateData, const updateData = {
[certType]: true,
completedChallenges: [ completedChallenges: [
...user.completedChallenges, ...user.completedChallenges,
{ {