From c63ccd834d3da6040dc52890dd519e05c287c34e Mon Sep 17 00:00:00 2001 From: Abheek Dhawan Date: Mon, 4 Apr 2022 13:10:58 -0500 Subject: [PATCH] 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' })); } +