diff --git a/common/models/user.json b/common/models/user.json index 01a3b3a6bc15f0..bc5e5796049d1b 100644 --- a/common/models/user.json +++ b/common/models/user.json @@ -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", diff --git a/server/boot/challenge.js b/server/boot/challenge.js index 1102da7f626d77..b8b5ed60f4f74e 100644 --- a/server/boot/challenge.js +++ b/server/boot/challenge.js @@ -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; }