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