Chore: Update User model (#17171)

* fix(logs): Remove console.log's

* chore(challengeMap): challengeMap -> completedChallenges

* chore(userModel): Update user model

* feat(userIDs): Add user ident fields

* chore(github): Remove more refs to github data
This commit is contained in:
Stuart Taylor
2018-05-15 14:56:26 +01:00
committed by mrugesh mohapatra
parent 156ea1af76
commit f916204ba5
23 changed files with 233 additions and 742 deletions

View File

@@ -40,8 +40,14 @@ const renderCertifedEmail = loopback.template(path.join(
'certified.ejs'
));
function isCertified(ids, challengeMap = {}) {
return _.every(ids, ({ id }) => _.has(challengeMap, id));
function isCertified(ids, completedChallenges = []) {
return _.every(
ids,
({ id }) => _.find(
completedChallenges,
({ id: completedId }) => completedId === id
)
);
}
const certIds = {
@@ -73,17 +79,17 @@ const certViews = {
};
const certText = {
[certTypes.frontEnd]: 'Legacy Front End certified',
[certTypes.backEnd]: 'Legacy Back End Certified',
[certTypes.dataVis]: 'Legacy Data Visualization Certified',
[certTypes.fullStack]: 'Legacy Full Stack Certified',
[certTypes.respWebDesign]: 'Responsive Web Design Certified',
[certTypes.frontEndLibs]: 'Front End Libraries Certified',
[certTypes.frontEnd]: 'Legacy Front End',
[certTypes.backEnd]: 'Legacy Back End',
[certTypes.dataVis]: 'Legacy Data Visualization',
[certTypes.fullStack]: 'Legacy Full Stack',
[certTypes.respWebDesign]: 'Responsive Web Design',
[certTypes.frontEndLibs]: 'Front End Libraries',
[certTypes.jsAlgoDataStruct]:
'JavaScript Algorithms and Data Structures Certified',
[certTypes.dataVis2018]: 'Data Visualization Certified',
[certTypes.apisMicroservices]: 'APIs and Microservices Certified',
[certTypes.infosecQa]: 'Information Security and Quality Assurance Certified'
'JavaScript Algorithms and Data Structures',
[certTypes.dataVis2018]: 'Data Visualization',
[certTypes.apisMicroservices]: 'APIs and Microservices',
[certTypes.infosecQa]: 'Information Security and Quality Assurance'
};
function getIdsForCert$(id, Challenge) {
@@ -101,19 +107,6 @@ function getIdsForCert$(id, Challenge) {
.shareReplay();
}
// sendCertifiedEmail(
// {
// email: String,
// username: String,
// isRespWebDesignCert: Boolean,
// isFrontEndLibsCert: Boolean,
// isJsAlgoDataStructCert: Boolean,
// isDataVisCert: Boolean,
// isApisMicroservicesCert: Boolean,
// isInfosecQaCert: Boolean
// },
// send$: Observable
// ) => Observable
function sendCertifiedEmail(
{
email,
@@ -230,46 +223,49 @@ export default function certificate(app) {
log(superBlock);
let certType = superBlockCertTypeMap[superBlock];
log(certType);
return user.getChallengeMap$()
return user.getCompletedChallenges$()
.flatMap(() => certTypeIds[certType])
.flatMap(challenge => {
const {
id,
tests,
name,
challengeType
} = challenge;
const certName = certText[certType];
if (user[certType]) {
return Observable.just(alreadyClaimedMessage(name));
return Observable.just(alreadyClaimedMessage(certName));
}
if (!user[certType] && !isCertified(tests, user.challengeMap)) {
return Observable.just(notCertifiedMessage(name));
if (!user[certType] && !isCertified(tests, user.completedChallenges)) {
return Observable.just(notCertifiedMessage(certName));
}
if (!user.name) {
return Observable.just(noNameMessage);
}
const updateData = {
$set: {
[`challengeMap.${id}`]: {
$push: {
completedChallenges: {
id,
name,
completedDate: new Date(),
challengeType
},
}
},
$set: {
[certType]: true
}
};
// set here so sendCertifiedEmail works properly
// not used otherwise
user[certType] = true;
user.challengeMap[id] = { completedDate: new Date() };
user.completedChallenges[
user.completedChallenges.length - 1
] = { id, completedDate: new Date() };
return Observable.combineLatest(
// update user data
user.update$(updateData),
// If user has committed to nonprofit,
// this will complete their pledge
completeCommitment$(user),
// sends notification email is user has all three certs
// sends notification email is user has all 6 certs
// if not it noop
sendCertifiedEmail(user, Email.send$),
({ count }, pledgeOrMessage) => ({ count, pledgeOrMessage })
@@ -280,7 +276,7 @@ export default function certificate(app) {
log(pledgeOrMessage);
}
log(`${count} documents updated`);
return successMessage(user.username, name);
return successMessage(user.username, certName);
}
);
})
@@ -326,12 +322,12 @@ export default function certificate(app) {
isHonest: true,
username: true,
name: true,
challengeMap: true
completedChallenges: true
}
)
.subscribe(
user => {
const profile = `/${user.username}`;
const profile = `/portfolio/${user.username}`;
if (!user) {
req.flash(
'danger',
@@ -352,7 +348,7 @@ export default function certificate(app) {
}
if (user.isCheater) {
return res.redirect(`/${user.username}`);
return res.redirect(profile);
}
if (user.isLocked) {
@@ -378,8 +374,10 @@ export default function certificate(app) {
}
if (user[certType]) {
const { challengeMap = {} } = user;
const { completedDate = new Date() } = challengeMap[certId] || {};
const { completedChallenges = {} } = user;
const { completedDate = new Date() } = _.find(
completedChallenges, ({ id }) => certId === id
) || {};
return res.render(
certViews[certType],
@@ -392,7 +390,7 @@ export default function certificate(app) {
}
req.flash(
'danger',
`Looks like user ${username} is not ${certText[certType]}`
`Looks like user ${username} is not ${certText[certType]} certified`
);
return res.redirect(profile);
},