From 6cbdbc25807162f4adadd399c76caa8201c1c246 Mon Sep 17 00:00:00 2001 From: Joshua Riddle Date: Mon, 4 Sep 2017 03:41:55 -0700 Subject: [PATCH 1/2] feat(user): Track challenge submissions count BREAKING CHANGE: none Closes #14881 --- common/models/user.json | 1 + server/boot/challenge.js | 13 +++++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/common/models/user.json b/common/models/user.json index 5874419451..521b36f9bf 100644 --- a/common/models/user.json +++ b/common/models/user.json @@ -182,6 +182,7 @@ { "completedDate": "number", "lastUpdated": "number", + "numOfAttempts": "number", "id": "string", "name": "string", "completedWith": "string", diff --git a/server/boot/challenge.js b/server/boot/challenge.js index fb7da84af7..a4d15b3412 100644 --- a/server/boot/challenge.js +++ b/server/boot/challenge.js @@ -21,13 +21,15 @@ function buildUserUpdate( const oldChallenge = challengeMap[challengeId]; const alreadyCompleted = !!oldChallenge; - if (alreadyCompleted) { // add data from old challenge - finalChallenge = { + const attempts = oldChallenge.numOfAttempts; + const numOfAttempts = attempts ? attempts + 1 : 0; + finalChallenge = { ...completedChallenge, completedDate: oldChallenge.completedDate, - lastUpdated: completedChallenge.completedDate + lastUpdated: completedChallenge.completedDate, + numOfAttempts: numOfAttempts }; } else { updateData.$push = { @@ -36,7 +38,10 @@ function buildUserUpdate( completedChallenge: challengeId } }; - finalChallenge = completedChallenge; + finalChallenge = { + ...completedChallenge, + numOfAttempts: 1 + }; } updateData.$set = { From 56e3d3a9862d3b79759bae1384655cf1712dbabd Mon Sep 17 00:00:00 2001 From: Berkeley Martinez Date: Fri, 8 Sep 2017 00:16:02 -0700 Subject: [PATCH 2/2] refactor(server/challenge): Make code dry --- server/boot/challenge.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/server/boot/challenge.js b/server/boot/challenge.js index a4d15b3412..609d05412b 100644 --- a/server/boot/challenge.js +++ b/server/boot/challenge.js @@ -14,8 +14,9 @@ function buildUserUpdate( completedChallenge, timezone ) { - const updateData = { $set: {} }; let finalChallenge; + let numOfAttempts = 1; + const updateData = { $set: {} }; const { timezone: userTimezone, challengeMap = {} } = user; const oldChallenge = challengeMap[challengeId]; @@ -23,13 +24,14 @@ function buildUserUpdate( if (alreadyCompleted) { // add data from old challenge - const attempts = oldChallenge.numOfAttempts; - const numOfAttempts = attempts ? attempts + 1 : 0; - finalChallenge = { + if (oldChallenge.numOfAttempts) { + numOfAttempts = oldChallenge.numOfAttempts + 1; + } + finalChallenge = { ...completedChallenge, completedDate: oldChallenge.completedDate, lastUpdated: completedChallenge.completedDate, - numOfAttempts: numOfAttempts + numOfAttempts }; } else { updateData.$push = { @@ -40,7 +42,7 @@ function buildUserUpdate( }; finalChallenge = { ...completedChallenge, - numOfAttempts: 1 + numOfAttempts }; }