refactor(server/challenge): Make code dry
This commit is contained in:
@ -14,8 +14,9 @@ function buildUserUpdate(
|
|||||||
completedChallenge,
|
completedChallenge,
|
||||||
timezone
|
timezone
|
||||||
) {
|
) {
|
||||||
const updateData = { $set: {} };
|
|
||||||
let finalChallenge;
|
let finalChallenge;
|
||||||
|
let numOfAttempts = 1;
|
||||||
|
const updateData = { $set: {} };
|
||||||
const { timezone: userTimezone, challengeMap = {} } = user;
|
const { timezone: userTimezone, challengeMap = {} } = user;
|
||||||
|
|
||||||
const oldChallenge = challengeMap[challengeId];
|
const oldChallenge = challengeMap[challengeId];
|
||||||
@ -23,13 +24,14 @@ function buildUserUpdate(
|
|||||||
|
|
||||||
if (alreadyCompleted) {
|
if (alreadyCompleted) {
|
||||||
// add data from old challenge
|
// add data from old challenge
|
||||||
const attempts = oldChallenge.numOfAttempts;
|
if (oldChallenge.numOfAttempts) {
|
||||||
const numOfAttempts = attempts ? attempts + 1 : 0;
|
numOfAttempts = oldChallenge.numOfAttempts + 1;
|
||||||
finalChallenge = {
|
}
|
||||||
|
finalChallenge = {
|
||||||
...completedChallenge,
|
...completedChallenge,
|
||||||
completedDate: oldChallenge.completedDate,
|
completedDate: oldChallenge.completedDate,
|
||||||
lastUpdated: completedChallenge.completedDate,
|
lastUpdated: completedChallenge.completedDate,
|
||||||
numOfAttempts: numOfAttempts
|
numOfAttempts
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
updateData.$push = {
|
updateData.$push = {
|
||||||
@ -40,7 +42,7 @@ function buildUserUpdate(
|
|||||||
};
|
};
|
||||||
finalChallenge = {
|
finalChallenge = {
|
||||||
...completedChallenge,
|
...completedChallenge,
|
||||||
numOfAttempts: 1
|
numOfAttempts
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user