From 25323ccde3e608fcfa45e08cf9d84bd5c4bf866b Mon Sep 17 00:00:00 2001 From: zkldi <20380519+zkldi@users.noreply.github.com> Date: Wed, 30 Mar 2022 23:36:27 +0100 Subject: [PATCH] Handle case where user goal becomes unachieved but goal is marked as instantlyAchieved --- .../lib/score-import/framework/goals/goals.ts | 31 ++++++++++++++----- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/server/src/lib/score-import/framework/goals/goals.ts b/server/src/lib/score-import/framework/goals/goals.ts index b47e0105c..234721477 100644 --- a/server/src/lib/score-import/framework/goals/goals.ts +++ b/server/src/lib/score-import/framework/goals/goals.ts @@ -132,18 +132,33 @@ export async function ProcessGoal( }; } + const setData = { + ...newData, + timeAchieved: newData.achieved ? Date.now() : null, + // we're guaranteed that this works, because things + // that haven't changed return nothing instead of + // getting to this point. + lastInteraction: Date.now(), + } as Partial; + + // If this goal was achieved, and is now *not* achieved, we need to unset + // some things. + if (goalSub.achieved && !res.achieved) { + logger.info(`User ${userID} lost their achieved status on ${goal.name}.`, { + goal, + res, + goalSub, + }); + + // This goal can't be marked as instantly achieved, since it was lost. + setData.wasInstantlyAchieved = false; + } + const bulkWrite = { updateOne: { filter: { _id: goalSub._id! }, update: { - $set: { - ...newData, - timeAchieved: newData.achieved ? Date.now() : null, - // we're guaranteed that this works, because things - // that haven't changed return nothing instead of - // getting to this point. - lastInteraction: Date.now(), - } as Partial, + $set: setData, }, }, };