Browse Source

Merge branch 'development'

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

18
src/helpers/db.ts

@ -3,16 +3,19 @@ import mongoose from 'mongoose';
import log from '../helpers/log'; import log from '../helpers/log';
import userScore from '../models/userScore'; import userScore from '../models/userScore';
export async function updateScore(isCorrect, score, authorId) { export async function updateScore(isCorrect : boolean, score : number, authorId : string) {
if (!isCorrect) { if (!isCorrect) {
return `Nice try! Your score is still ${score}.`; return `Nice try! Your score is still ${score}.`;
} }
else { else {
score += 4; // TODO: Error handling
if (score == 4) { const doc = await userScore.findOne({
authorID: authorId,
});
if (!doc) {
const newUserScore = new userScore({ const newUserScore = new userScore({
authorID: authorId, authorID: authorId,
score: score, score: score + 4,
}); });
newUserScore.save(err => { newUserScore.save(err => {
if (err) { if (err) {
@ -24,15 +27,11 @@ export async function updateScore(isCorrect, score, authorId) {
}); });
} }
else { else {
// TODO: Error handling
const doc = await userScore.findOne({
authorID: authorId,
});
doc.score = doc.score + 4; doc.score = doc.score + 4;
doc.save(); doc.save();
} }
return `Great job! Your score is now ${score}.`; return `Great job! Your score is now ${score + 4}.`;
} }
} }
@ -45,3 +44,4 @@ export async function connect(mongoUri) {
.then(() => log({ logger: 'db', content: `Connected to the database at ${mongoUri}!`, level: 'info' })) .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' })); .catch(err => log({ logger: 'db', content: `Failed to connect to the database at ${mongoUri}: ${err}`, level: 'fatal' }));
} }

Loading…
Cancel
Save