fix(updated): Update completedChallenges and progressTimestamps without duplicates (#17226)

Closes #17200
This commit is contained in:
Stuart Taylor
2018-05-24 14:59:46 +01:00
committed by mrugesh mohapatra
parent 58a5d0d181
commit 3e52c666da
2 changed files with 34 additions and 19 deletions

View File

@ -46,24 +46,20 @@ function destroyAll(id, Model) {
function buildCompletedChallengesUpdate(completedChallenges, project) { function buildCompletedChallengesUpdate(completedChallenges, project) {
const key = Object.keys(project)[0]; const key = Object.keys(project)[0];
const solutions = project[key]; const solutions = project[key];
const solutionKeys = Object.keys(solutions);
const currentCompletedChallenges = [ ...completedChallenges ]; const currentCompletedChallenges = [ ...completedChallenges ];
const currentCompletedProjects = currentCompletedChallenges const currentCompletedProjects = currentCompletedChallenges
.filter(({id}) => Object.keys(solutions).includes(id)); .filter(({id}) => solutionKeys.includes(id));
const now = Date.now(); const now = Date.now();
const update = Object.keys(solutions).reduce((update, currentId) => { const update = solutionKeys.reduce((update, currentId) => {
const indexOfCurrentId = _.findIndex( const indexOfCurrentId = _.findIndex(
currentCompletedProjects, update,
({id}) => id === currentId ({id}) => id === currentId
); );
const isCurrentlyCompleted = indexOfCurrentId !== -1; const isCurrentlyCompleted = indexOfCurrentId !== -1;
if ( if (isCurrentlyCompleted) {
isCurrentlyCompleted &&
currentCompletedProjects[
indexOfCurrentId
].solution !== solutions[currentId]
) {
update[indexOfCurrentId] = { update[indexOfCurrentId] = {
...update[indexOfCurrentId], ..._.find(update, ({id}) => id === currentId).__data,
solution: solutions[currentId] solution: solutions[currentId]
}; };
} }
@ -87,7 +83,11 @@ function buildCompletedChallengesUpdate(completedChallenges, project) {
], ],
'id' 'id'
); );
return updatedExisting; return {
updated: updatedExisting,
isNewCompletionCount:
updatedExisting.length - completedChallenges.length
};
} }
function isTheSame(val1, val2) { function isTheSame(val1, val2) {
@ -735,13 +735,26 @@ module.exports = function(User) {
}; };
User.prototype.updateMyProjects = function updateMyProjects(project) { User.prototype.updateMyProjects = function updateMyProjects(project) {
const updateData = {}; const updateData = { $set: {} };
return this.getCompletedChallenges$() return this.getCompletedChallenges$()
.flatMap(completedChallenges => { .flatMap(() => {
updateData.completedChallenges = buildCompletedChallengesUpdate( const {
completedChallenges, updated,
isNewCompletionCount
} = buildCompletedChallengesUpdate(
this.completedChallenges,
project 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); return this.update$(updateData);
}) })
.do(() => Object.assign(this, updateData)) .do(() => Object.assign(this, updateData))

View File

@ -20,7 +20,7 @@ function buildUserUpdate(
timezone timezone
) { ) {
let finalChallenge; let finalChallenge;
const updateData = { $push: {} }; const updateData = {};
const { timezone: userTimezone, completedChallenges = [] } = user; const { timezone: userTimezone, completedChallenges = [] } = user;
const oldChallenge = _.find( const oldChallenge = _.find(
@ -44,9 +44,11 @@ function buildUserUpdate(
}; };
} }
updateData.$push = { updateData.$set = {
...updateData.$push, completedChallenges: _.uniqBy(
completedChallenges: finalChallenge [finalChallenge, ...completedChallenges],
'id'
)
}; };
if ( if (