got public stats to work

This commit is contained in:
Miodec 2021-08-02 16:21:14 +01:00
parent d4ad8afacd
commit 0cf495e127
2 changed files with 16 additions and 10 deletions

View file

@ -1,6 +1,6 @@
const ResultDAO = require("../../dao/result");
const UserDAO = require("../../dao/user");
// const PublicStatsDAO = require("../../dao/public-stats");
const PublicStatsDAO = require("../../dao/public-stats");
const {
validateObjectValues,
validateResult,
@ -168,6 +168,8 @@ class ResultController {
await UserDAO.updateTypingStats(uid, result.restartCount, tt);
await PublicStatsDAO.updateStats(result.restartCount, tt);
let addedResult = await ResultDAO.addResult(uid, result);
return res.status(200).json({

View file

@ -4,17 +4,21 @@ const { roundTo2 } = require("../handlers/misc");
class PublicStatsDAO {
//needs to be rewritten, this is public stats not user stats
static async increment(started, completed, time) {
static async updateStats(restartCount, time) {
time = roundTo2(time);
await mongoDB()
.collection("users")
.updateOne({ name: "startedTests" }, { $inc: { value: started } });
await mongoDB()
.collection("users")
.updateOne({ name: "completedTests" }, { $inc: { value: completed } });
await mongoDB()
.collection("users")
.updateOne({ name: "timeTyping" }, { $inc: { value: time } });
.collection("public")
.updateOne(
{ type: "stats" },
{
$inc: {
testsCompleted: 1,
testsStarted: restartCount + 1,
timeTyping: time,
},
},
{ upsert: true }
);
return true;
}
}