Unfinished grading attempt at solving it
This commit is contained in:
@@ -3,6 +3,9 @@ import type { NextApiRequest, NextApiResponse } from "next";
|
||||
import { withIronSessionApiRoute } from "iron-session/next";
|
||||
import { sessionOptions } from "@/lib/session";
|
||||
import axios from "axios";
|
||||
import client from "@/lib/mongodb";
|
||||
|
||||
const db = client.db(process.env.MONGODB_DB);
|
||||
|
||||
interface Body {
|
||||
userId: string;
|
||||
@@ -22,13 +25,41 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
return;
|
||||
}
|
||||
|
||||
const { task, ...body} = req.body as Body;
|
||||
const taskNumber = task.toString() !== "1" && task.toString() !== "2" ? "1" : task.toString();
|
||||
const body = req.body as Body;
|
||||
const taskNumber = body.task.toString() !== "1" && body.task.toString() !== "2" ? "1" : body.task.toString();
|
||||
|
||||
// Check if there is one eval for the current exercise
|
||||
const previousEval = await db.collection("evaluation").findOne({
|
||||
user: body.userId,
|
||||
session_id: body.sessionId,
|
||||
exercise_id: body.exerciseId,
|
||||
})
|
||||
|
||||
// If there is delete it
|
||||
if (previousEval) {
|
||||
await db.collection("evaluation").deleteOne({
|
||||
user: body.userId,
|
||||
session_id: body.sessionId,
|
||||
exercise_id: body.exerciseId,
|
||||
})
|
||||
}
|
||||
|
||||
// Insert the new eval for the backend to place it's result
|
||||
await db.collection("evaluation").insertOne(
|
||||
{
|
||||
user: body.userId,
|
||||
session_id: body.sessionId,
|
||||
exercise_id: body.exerciseId,
|
||||
type: "writing",
|
||||
task: body.task,
|
||||
status: "pending"
|
||||
}
|
||||
);
|
||||
|
||||
await axios.post(`${process.env.BACKEND_URL}/grade/writing/${taskNumber}`, body, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${process.env.BACKEND_JWT}`,
|
||||
},
|
||||
});
|
||||
res.status(200).json({ok: true});
|
||||
res.status(200).json({ ok: true });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user