From 3e52c666da220d8637441cd9abcebe10ece80a95 Mon Sep 17 00:00:00 2001 From: Stuart Taylor Date: Thu, 24 May 2018 14:59:46 +0100 Subject: [PATCH] fix(updated): Update completedChallenges and progressTimestamps without duplicates (#17226) Closes #17200 --- common/models/user.js | 43 ++++++++++++++++++++++++++-------------- server/boot/challenge.js | 10 ++++++---- 2 files changed, 34 insertions(+), 19 deletions(-) diff --git a/common/models/user.js b/common/models/user.js index 2db2b228f1..56acb92ac5 100644 --- a/common/models/user.js +++ b/common/models/user.js @@ -46,24 +46,20 @@ function destroyAll(id, Model) { function buildCompletedChallengesUpdate(completedChallenges, project) { const key = Object.keys(project)[0]; const solutions = project[key]; + const solutionKeys = Object.keys(solutions); const currentCompletedChallenges = [ ...completedChallenges ]; const currentCompletedProjects = currentCompletedChallenges - .filter(({id}) => Object.keys(solutions).includes(id)); + .filter(({id}) => solutionKeys.includes(id)); const now = Date.now(); - const update = Object.keys(solutions).reduce((update, currentId) => { + const update = solutionKeys.reduce((update, currentId) => { const indexOfCurrentId = _.findIndex( - currentCompletedProjects, + update, ({id}) => id === currentId ); const isCurrentlyCompleted = indexOfCurrentId !== -1; - if ( - isCurrentlyCompleted && - currentCompletedProjects[ - indexOfCurrentId - ].solution !== solutions[currentId] - ) { + if (isCurrentlyCompleted) { update[indexOfCurrentId] = { - ...update[indexOfCurrentId], + ..._.find(update, ({id}) => id === currentId).__data, solution: solutions[currentId] }; } @@ -87,7 +83,11 @@ function buildCompletedChallengesUpdate(completedChallenges, project) { ], 'id' ); - return updatedExisting; + return { + updated: updatedExisting, + isNewCompletionCount: + updatedExisting.length - completedChallenges.length + }; } function isTheSame(val1, val2) { @@ -735,13 +735,26 @@ module.exports = function(User) { }; User.prototype.updateMyProjects = function updateMyProjects(project) { - const updateData = {}; + const updateData = { $set: {} }; return this.getCompletedChallenges$() - .flatMap(completedChallenges => { - updateData.completedChallenges = buildCompletedChallengesUpdate( - completedChallenges, + .flatMap(() => { + const { + updated, + isNewCompletionCount + } = buildCompletedChallengesUpdate( + this.completedChallenges, project ); + updateData.$set.completedChallenges = updated; + if (isNewCompletionCount) { + let points = []; + // give points a length of isNewCompletionCount + points[isNewCompletionCount - 1] = true; + updateData.$push = {}; + updateData.$push.progressTimestamps = { + $each: points.map(() => Date.now()) + }; + } return this.update$(updateData); }) .do(() => Object.assign(this, updateData)) diff --git a/server/boot/challenge.js b/server/boot/challenge.js index 9b84814380..db68524635 100644 --- a/server/boot/challenge.js +++ b/server/boot/challenge.js @@ -20,7 +20,7 @@ function buildUserUpdate( timezone ) { let finalChallenge; - const updateData = { $push: {} }; + const updateData = {}; const { timezone: userTimezone, completedChallenges = [] } = user; const oldChallenge = _.find( @@ -44,9 +44,11 @@ function buildUserUpdate( }; } - updateData.$push = { - ...updateData.$push, - completedChallenges: finalChallenge + updateData.$set = { + completedChallenges: _.uniqBy( + [finalChallenge, ...completedChallenges], + 'id' + ) }; if (