Files
freeCodeCamp/server/boot/certificate.js

150 lines
3.4 KiB
JavaScript
Raw Normal View History

2015-10-02 11:47:36 -07:00
import _ from 'lodash';
import dedent from 'dedent';
import { Observable } from 'rx';
2016-02-09 20:54:49 -08:00
import debug from 'debug';
2015-10-02 11:47:36 -07:00
import {
ifNoUser401,
ifNoUserSend
} from '../utils/middleware';
2016-02-09 20:54:49 -08:00
import { observeQuery } from '../utils/rx';
2015-10-02 11:47:36 -07:00
import {
2015-12-09 14:34:33 -08:00
frontEndChallengeId,
2016-01-11 15:58:37 -08:00
dataVisChallengeId,
2015-12-09 14:34:33 -08:00
backEndChallengeId
} from '../utils/constantStrings.json';
import {
completeCommitment$
} from '../utils/commit';
2016-01-11 15:58:37 -08:00
import certTypes from '../utils/certTypes.json';
2016-02-09 20:54:49 -08:00
const log = debug('freecc:certification');
2015-10-02 11:47:36 -07:00
const sendMessageToNonUser = ifNoUserSend(
'must be logged in to complete.'
);
2016-02-09 20:54:49 -08:00
function isCertified(ids, challengeMap = {}) {
return _.every(ids, ({ id }) => challengeMap[id]);
2015-10-02 11:47:36 -07:00
}
2016-01-11 15:58:37 -08:00
function getIdsForCert$(id, Challenge) {
return observeQuery(
2015-10-02 11:47:36 -07:00
Challenge,
'findById',
2016-01-11 15:58:37 -08:00
id,
2015-10-02 11:47:36 -07:00
{
id: true,
tests: true,
name: true,
challengeType: true
2015-10-02 11:47:36 -07:00
}
)
.shareReplay();
2016-01-11 15:58:37 -08:00
}
2015-10-02 11:47:36 -07:00
2016-01-11 15:58:37 -08:00
export default function certificate(app) {
const router = app.loopback.Router();
const { Challenge } = app.models;
const certTypeIds = {
[certTypes.frontEnd]: getIdsForCert$(frontEndChallengeId, Challenge),
2016-01-11 16:23:24 -08:00
[certTypes.dataVis]: getIdsForCert$(dataVisChallengeId, Challenge),
[certTypes.backEnd]: getIdsForCert$(backEndChallengeId, Challenge)
2016-01-11 15:58:37 -08:00
};
2015-10-02 11:47:36 -07:00
router.post(
'/certificate/verify/front-end',
ifNoUser401,
2016-01-11 15:58:37 -08:00
verifyCert.bind(null, certTypes.frontEnd)
2015-10-02 11:47:36 -07:00
);
router.post(
2015-12-09 14:34:33 -08:00
'/certificate/verify/back-end',
2015-10-02 11:47:36 -07:00
ifNoUser401,
2016-01-11 15:58:37 -08:00
verifyCert.bind(null, certTypes.backEnd)
);
router.post(
'/certificate/verify/data-visualization',
ifNoUser401,
verifyCert.bind(null, certTypes.dataVis)
2015-10-02 11:47:36 -07:00
);
router.post(
'/certificate/honest',
sendMessageToNonUser,
postHonest
);
app.use(router);
2016-01-11 15:58:37 -08:00
function verifyCert(certType, req, res, next) {
2016-02-09 20:54:49 -08:00
const { user } = req;
2016-02-10 10:05:51 -08:00
return certTypeIds[certType]
.flatMap(challenge => {
const {
id,
tests,
name,
challengeType
} = challenge;
2015-10-02 11:47:36 -07:00
if (
2016-01-11 15:58:37 -08:00
!user[certType] &&
2016-02-09 20:54:49 -08:00
isCertified(tests, user.challengeMap)
2015-10-02 11:47:36 -07:00
) {
2016-02-09 20:54:49 -08:00
const updateData = {
$set: {
[`challengeMap.${id}`]: {
id,
name,
completedDate: new Date(),
challengeType
},
[certType]: true
}
};
2016-02-10 10:05:51 -08:00
return req.user.update$(updateData)
// If user has commited to nonprofit,
// this will complete his pledge
.flatMap(
2016-02-09 20:54:49 -08:00
() => completeCommitment$(user),
({ count }, pledgeOrMessage) => {
if (typeof pledgeOrMessage === 'string') {
2016-02-09 20:54:49 -08:00
log(pledgeOrMessage);
}
2016-02-09 20:54:49 -08:00
log(`${count} documents updated`);
return true;
}
);
2015-10-02 11:47:36 -07:00
}
2016-02-09 20:54:49 -08:00
return Observable.just(false);
2015-10-02 11:47:36 -07:00
})
.subscribe(
2016-02-09 20:54:49 -08:00
(didCertify) => {
if (didCertify) {
2015-10-02 11:47:36 -07:00
return res.status(200).send(true);
}
return res.status(200).send(
dedent`
Looks like you have not completed the neccessary steps.
Please return to the challenge map.
2015-10-02 11:47:36 -07:00
`
);
},
next
);
}
function postHonest(req, res, next) {
2016-02-09 20:54:49 -08:00
return req.user.update$({ $set: { isHonest: true } }).subscribe(
() => res.status(200).send(true),
next
);
2015-10-02 11:47:36 -07:00
}
}