Merge pull request #3557 from FreeCodeCamp/feature/make-completed-challenges-unique

feature make completedChallenges uniq
This commit is contained in:
Quincy Larson
2015-10-01 22:28:57 -07:00
2 changed files with 39 additions and 37 deletions

View File

@ -88,39 +88,6 @@
"google": {
"type": "string"
},
"completedBonfires": {
"type": [
{
"id": "string",
"name": "string",
"completedWith": "string",
"completedDate": "string",
"solution": "string"
}
],
"default": []
},
"uncompletedCoursewares": {
"type": "array",
"default": []
},
"completedCoursewares": {
"type": [
{
"completedDate": {
"type": "string",
"defaultFn": "now"
},
"id": "string",
"name": "string",
"completedWith": "string",
"solution": "string",
"githubLink": "string",
"verified": "boolean"
}
],
"default": []
},
"currentStreak": {
"type": "number",
"default": 0
@ -140,10 +107,15 @@
"currentChallenge": {
"type": {}
},
"isUniqMigrated": {
"type": "boolean",
"default": false
},
"completedChallenges": {
"type": [
{
"completedDate": "number",
"lastUpdated": "number",
"id": "string",
"name": "string",
"completedWith": "string",

View File

@ -34,22 +34,52 @@ const dasherize = utils.dasherize;
const unDasherize = utils.unDasherize;
const getMDNLinks = utils.getMDNLinks;
function makeChallengesUnique(challengeArr) {
// clone and reverse challenges
// then filter by unique id's
// then reverse again
return _.uniq(challengeArr.slice().reverse(), 'id').reverse();
}
function numberWithCommas(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
}
function updateUserProgress(user, challengeId, completedChallenge) {
const alreadyCompleted = user.completedChallenges.some(({ id }) => {
return id === challengeId;
let { completedChallenges } = user;
// migrate user challenges object to remove
if (!user.isUniqMigrated) {
user.isUniqMigrated = true;
completedChallenges = user.completedChallenges =
makeChallengesUnique(completedChallenges);
}
const indexOfChallenge = _.findIndex(completedChallenges, {
id: challengeId
});
const alreadyCompleted = indexOfChallenge !== -1;
if (!alreadyCompleted) {
user.progressTimestamps.push({
timestamp: Date.now(),
completedChallenge
completedChallenge: challengeId
});
user.completedChallenges.push(completedChallenge);
return user;
}
user.completedChallenges.push(completedChallenge);
const oldCompletedChallenge = completedChallenges[indexOfChallenge];
user.completedChallenges[indexOfChallenge] =
Object.assign(
{},
completedChallenge,
{
completedDate: oldCompletedChallenge.completedDate,
lastUpdated: completedChallenge.completedDate
}
);
return user;
}