Improved part of the performance of the dashboards
This commit is contained in:
@@ -1,15 +1,15 @@
|
||||
import {MODULES} from "@/constants/ielts";
|
||||
import {app} from "@/firebase";
|
||||
import {Module} from "@/interfaces";
|
||||
import {Stat, User} from "@/interfaces/user";
|
||||
import {sessionOptions} from "@/lib/session";
|
||||
import {calculateBandScore} from "@/utils/score";
|
||||
import {groupByModule, groupBySession} from "@/utils/stats";
|
||||
import { MODULES } from "@/constants/ielts";
|
||||
import { app } from "@/firebase";
|
||||
import { Module } from "@/interfaces";
|
||||
import { Stat, User } from "@/interfaces/user";
|
||||
import { sessionOptions } from "@/lib/session";
|
||||
import { calculateAverageLevel, calculateBandScore } from "@/utils/score";
|
||||
import { groupByModule, groupBySession } from "@/utils/stats";
|
||||
import { MODULE_ARRAY } from "@/utils/moduleUtils";
|
||||
import client from "@/lib/mongodb";
|
||||
import {withIronSessionApiRoute} from "iron-session/next";
|
||||
import {groupBy} from "lodash";
|
||||
import {NextApiRequest, NextApiResponse} from "next";
|
||||
import { withIronSessionApiRoute } from "iron-session/next";
|
||||
import { groupBy } from "lodash";
|
||||
import { NextApiRequest, NextApiResponse } from "next";
|
||||
import { requestUser } from "@/utils/api";
|
||||
|
||||
const db = client.db(process.env.MONGODB_DB);
|
||||
@@ -29,8 +29,8 @@ async function update(req: NextApiRequest, res: NextApiResponse) {
|
||||
const stats = await db.collection("stats").find<Stat>({ user: user.id }).toArray();
|
||||
|
||||
const groupedStats = groupBySession(stats);
|
||||
const sessionLevels: {[key in Module]: {correct: number; total: number}}[] = Object.keys(groupedStats).map((key) => {
|
||||
const sessionStats = groupedStats[key].map((stat) => ({module: stat.module, correct: stat.score.correct, total: stat.score.total}));
|
||||
const sessionLevels: { [key in Module]: { correct: number; total: number } }[] = Object.keys(groupedStats).map((key) => {
|
||||
const sessionStats = groupedStats[key].map((stat) => ({ module: stat.module, correct: stat.score.correct, total: stat.score.total }));
|
||||
const sessionLevels = {
|
||||
reading: {
|
||||
correct: 0,
|
||||
@@ -59,8 +59,8 @@ async function update(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (moduleStats.length === 0) return;
|
||||
|
||||
const moduleScore = moduleStats.reduce(
|
||||
(accumulator, current) => ({correct: accumulator.correct + current.correct, total: accumulator.total + current.total}),
|
||||
{correct: 0, total: 0},
|
||||
(accumulator, current) => ({ correct: accumulator.correct + current.correct, total: accumulator.total + current.total }),
|
||||
{ correct: 0, total: 0 },
|
||||
);
|
||||
|
||||
sessionLevels[module] = moduleScore;
|
||||
@@ -72,24 +72,24 @@ async function update(req: NextApiRequest, res: NextApiResponse) {
|
||||
const readingLevel = sessionLevels
|
||||
.map((x) => x.reading)
|
||||
.filter((x) => x.total > 0)
|
||||
.reduce((acc, cur) => ({total: acc.total + cur.total, correct: acc.correct + cur.correct}), {total: 0, correct: 0});
|
||||
.reduce((acc, cur) => ({ total: acc.total + cur.total, correct: acc.correct + cur.correct }), { total: 0, correct: 0 });
|
||||
const listeningLevel = sessionLevels
|
||||
.map((x) => x.listening)
|
||||
.filter((x) => x.total > 0)
|
||||
.reduce((acc, cur) => ({total: acc.total + cur.total, correct: acc.correct + cur.correct}), {total: 0, correct: 0});
|
||||
.reduce((acc, cur) => ({ total: acc.total + cur.total, correct: acc.correct + cur.correct }), { total: 0, correct: 0 });
|
||||
const writingLevel = sessionLevels
|
||||
.map((x) => x.writing)
|
||||
.filter((x) => x.total > 0)
|
||||
.reduce((acc, cur) => ({total: acc.total + cur.total, correct: acc.correct + cur.correct}), {total: 0, correct: 0});
|
||||
.reduce((acc, cur) => ({ total: acc.total + cur.total, correct: acc.correct + cur.correct }), { total: 0, correct: 0 });
|
||||
const speakingLevel = sessionLevels
|
||||
.map((x) => x.speaking)
|
||||
.filter((x) => x.total > 0)
|
||||
.reduce((acc, cur) => ({total: acc.total + cur.total, correct: acc.correct + cur.correct}), {total: 0, correct: 0});
|
||||
.reduce((acc, cur) => ({ total: acc.total + cur.total, correct: acc.correct + cur.correct }), { total: 0, correct: 0 });
|
||||
|
||||
const levelLevel = sessionLevels
|
||||
const levelLevel = sessionLevels
|
||||
.map((x) => x.level)
|
||||
.filter((x) => x.total > 0)
|
||||
.reduce((acc, cur) => ({total: acc.total + cur.total, correct: acc.correct + cur.correct}), {total: 0, correct: 0});
|
||||
.reduce((acc, cur) => ({ total: acc.total + cur.total, correct: acc.correct + cur.correct }), { total: 0, correct: 0 });
|
||||
|
||||
|
||||
const levels = {
|
||||
@@ -100,12 +100,14 @@ async function update(req: NextApiRequest, res: NextApiResponse) {
|
||||
level: calculateBandScore(levelLevel.correct, levelLevel.total, "level", user.focus),
|
||||
};
|
||||
|
||||
const averageLevel = calculateAverageLevel(levels)
|
||||
|
||||
await db.collection("users").updateOne(
|
||||
{ id: user.id},
|
||||
{ $set: {levels} }
|
||||
{ id: user.id },
|
||||
{ $set: { levels, averageLevel } }
|
||||
);
|
||||
|
||||
res.status(200).json({ok: true});
|
||||
res.status(200).json({ ok: true });
|
||||
} else {
|
||||
res.status(401).json(undefined);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user