Browse Source

Attempt to fix static score error if DB value is 0

pull/41/head
Abheek Dhawan 3 years ago
parent
commit
c63ccd834d
Signed by: abheekd GPG Key ID: 7BE81B8C14475B67
  1. 16
      src/helpers/db.ts

16
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' }));
}

Loading…
Cancel
Save