Skip to content

Commit

Permalink
Merge pull request freeCodeCamp#3557 from FreeCodeCamp/feature/make-c…
Browse files Browse the repository at this point in the history
…ompleted-challenges-unique

feature make completedChallenges uniq
  • Loading branch information
QuincyLarson committed Oct 2, 2015
2 parents fefefed + 6c305db commit 7563b94
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 37 deletions.
38 changes: 5 additions & 33 deletions common/models/user.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -140,10 +107,15 @@
"currentChallenge": {
"type": {}
},
"isUniqMigrated": {
"type": "boolean",
"default": false
},
"completedChallenges": {
"type": [
{
"completedDate": "number",
"lastUpdated": "number",
"id": "string",
"name": "string",
"completedWith": "string",
Expand Down
38 changes: 34 additions & 4 deletions server/boot/challenge.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit 7563b94

Please sign in to comment.