ENCOA-253, ENCOA-248, ENCOA-246
This commit is contained in:
@@ -3,7 +3,7 @@ import type { NextApiRequest, NextApiResponse } from "next";
|
||||
import client from "@/lib/mongodb";
|
||||
import { withIronSessionApiRoute } from "iron-session/next";
|
||||
import { sessionOptions } from "@/lib/session";
|
||||
import { Exam, InstructorGender, Variant } from "@/interfaces/exam";
|
||||
import { Exam, ExamBase, InstructorGender, Variant } from "@/interfaces/exam";
|
||||
import { getExams } from "@/utils/exams.be";
|
||||
import { Module } from "@/interfaces";
|
||||
import { getUserCorporate } from "@/utils/groups.be";
|
||||
@@ -60,18 +60,25 @@ async function POST(req: NextApiRequest, res: NextApiResponse) {
|
||||
};
|
||||
|
||||
await session.withTransaction(async () => {
|
||||
const docSnap = await db.collection(module).findOne({ id: req.body.id }, { session });
|
||||
const docSnap = await db.collection(module).findOne<ExamBase>({ id: req.body.id }, { session });
|
||||
|
||||
if (docSnap) {
|
||||
// Check whether the id of the exam matches another exam with different
|
||||
// owners, throw exception if there is, else allow editing
|
||||
const ownersSet = new Set(docSnap?.owners || []);
|
||||
if (docSnap?.owners?.length === exam.owners.lenght && exam.owners.every((e: string) => ownersSet.has(e))) {
|
||||
throw new Error("Name already exists");
|
||||
}
|
||||
|
||||
await db.collection(module).insertOne(
|
||||
{ id: req.body.id, ...exam },
|
||||
{ session }
|
||||
await db.collection(module).updateOne(
|
||||
{ id: req.body.id },
|
||||
{ $set: { id: req.body.id, ...exam } },
|
||||
{
|
||||
upsert: true,
|
||||
session
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
res.status(200).json(exam);
|
||||
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user