More bugs, some updates where not using set
This commit is contained in:
@@ -1,11 +1,11 @@
|
|||||||
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
||||||
import type {NextApiRequest, NextApiResponse} from "next";
|
import type { NextApiRequest, NextApiResponse } from "next";
|
||||||
import client from "@/lib/mongodb";
|
import client from "@/lib/mongodb";
|
||||||
import {withIronSessionApiRoute} from "iron-session/next";
|
import { withIronSessionApiRoute } from "iron-session/next";
|
||||||
import {sessionOptions} from "@/lib/session";
|
import { sessionOptions } from "@/lib/session";
|
||||||
import axios, {AxiosResponse} from "axios";
|
import axios, { AxiosResponse } from "axios";
|
||||||
import {Stat} from "@/interfaces/user";
|
import { Stat } from "@/interfaces/user";
|
||||||
import {writingReverseMarking} from "@/utils/score";
|
import { writingReverseMarking } from "@/utils/score";
|
||||||
|
|
||||||
interface Body {
|
interface Body {
|
||||||
question: string;
|
question: string;
|
||||||
@@ -24,7 +24,7 @@ export default withIronSessionApiRoute(handler, sessionOptions);
|
|||||||
|
|
||||||
async function handler(req: NextApiRequest, res: NextApiResponse) {
|
async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
if (!req.session.user) {
|
if (!req.session.user) {
|
||||||
res.status(401).json({ok: false});
|
res.status(401).json({ ok: false });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -36,27 +36,29 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
|
|
||||||
const correspondingStat = await getCorrespondingStat(req.body.id, 1);
|
const correspondingStat = await getCorrespondingStat(req.body.id, 1);
|
||||||
|
|
||||||
const solutions = correspondingStat.solutions.map((x) => ({...x, evaluation: backendRequest.data}));
|
const solutions = correspondingStat.solutions.map((x) => ({ ...x, evaluation: backendRequest.data }));
|
||||||
await db.collection("stats").updateOne(
|
await db.collection("stats").updateOne(
|
||||||
{ id: (req.body as Body).id},
|
{ id: (req.body as Body).id },
|
||||||
{
|
{
|
||||||
id: (req.body as Body).id,
|
$set: {
|
||||||
solutions,
|
id: (req.body as Body).id,
|
||||||
score: {
|
solutions,
|
||||||
correct: writingReverseMarking[backendRequest.data.overall],
|
score: {
|
||||||
total: 100,
|
correct: writingReverseMarking[backendRequest.data.overall],
|
||||||
missing: 0,
|
total: 100,
|
||||||
},
|
missing: 0,
|
||||||
isDisabled: false,
|
},
|
||||||
|
isDisabled: false,
|
||||||
|
}
|
||||||
},
|
},
|
||||||
{upsert: true},
|
{ upsert: true },
|
||||||
);
|
);
|
||||||
console.log("🌱 - Updated the DB");
|
console.log("🌱 - Updated the DB");
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getCorrespondingStat(id: string, index: number): Promise<Stat> {
|
async function getCorrespondingStat(id: string, index: number): Promise<Stat> {
|
||||||
console.log(`🌱 - Try number ${index} - ${id}`);
|
console.log(`🌱 - Try number ${index} - ${id}`);
|
||||||
const correspondingStat = await db.collection("stats").findOne<Stat>({ id: id});
|
const correspondingStat = await db.collection("stats").findOne<Stat>({ id: id });
|
||||||
|
|
||||||
if (correspondingStat) return correspondingStat;
|
if (correspondingStat) return correspondingStat;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user