From c63ccd834d3da6040dc52890dd519e05c287c34e Mon Sep 17 00:00:00 2001 From: Abheek Dhawan Date: Mon, 4 Apr 2022 13:10:58 -0500 Subject: [PATCH 1/2] Attempt to fix static score error if DB value is 0 --- src/helpers/db.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/helpers/db.ts b/src/helpers/db.ts index fac2f83..d7ff2fa 100644 --- a/src/helpers/db.ts +++ b/src/helpers/db.ts @@ -3,16 +3,19 @@ import mongoose from 'mongoose'; import log from '../helpers/log'; import userScore from '../models/userScore'; -export async function updateScore(isCorrect, score, authorId) { +export async function updateScore(isCorrect : boolean, score : number, authorId : string) { if (!isCorrect) { return `Nice try! Your score is still ${score}.`; } else { - score += 4; - if (score == 4) { + // TODO: Error handling + const doc = await userScore.findOne({ + authorID: authorId, + }); + if (!doc) { const newUserScore = new userScore({ authorID: authorId, - score: score, + score: score + 4, }); newUserScore.save(err => { if (err) { @@ -24,10 +27,6 @@ export async function updateScore(isCorrect, score, authorId) { }); } else { - // TODO: Error handling - const doc = await userScore.findOne({ - authorID: authorId, - }); doc.score = doc.score + 4; doc.save(); } @@ -45,3 +44,4 @@ export async function connect(mongoUri) { .then(() => log({ logger: 'db', content: `Connected to the database at ${mongoUri}!`, level: 'info' })) .catch(err => log({ logger: 'db', content: `Failed to connect to the database at ${mongoUri}: ${err}`, level: 'fatal' })); } + From 38b0b0d3a9f9ca3ea4994edac5c66915b76fe92e Mon Sep 17 00:00:00 2001 From: Abheek Dhawan Date: Mon, 4 Apr 2022 13:28:33 -0500 Subject: [PATCH 2/2] Update score that is displayed to the user --- src/helpers/db.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/helpers/db.ts b/src/helpers/db.ts index d7ff2fa..e74d282 100644 --- a/src/helpers/db.ts +++ b/src/helpers/db.ts @@ -31,7 +31,7 @@ export async function updateScore(isCorrect : boolean, score : number, authorId doc.save(); } - return `Great job! Your score is now ${score}.`; + return `Great job! Your score is now ${score + 4}.`; } }