From c3849518fb1dc75af6faeb21524cf3075633af7d Mon Sep 17 00:00:00 2001 From: Joao Correia Date: Wed, 5 Feb 2025 19:14:18 +0000 Subject: [PATCH] amend last commit --- src/pages/api/exam/[module]/index.ts | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/pages/api/exam/[module]/index.ts b/src/pages/api/exam/[module]/index.ts index af9a7783..86e2e7d6 100644 --- a/src/pages/api/exam/[module]/index.ts +++ b/src/pages/api/exam/[module]/index.ts @@ -62,18 +62,13 @@ async function POST(req: NextApiRequest, res: NextApiResponse) { // 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 || []); + const existingExamOwners = docSnap?.owners ?? []; + const newExamOwners = exam.owners ?? []; - if (docSnap !== null) { - if (docSnap.owners === undefined) { - docSnap.owners = []; - } - if (exam.owners === undefined) { - exam.owners = []; - } - if (docSnap.owners?.length !== exam.owners?.length || !exam.owners?.every((e: string) => ownersSet.has(e))) { - throw new Error("Name already exists"); - } + const ownersSet = new Set(existingExamOwners); + + if (docSnap !== null && (existingExamOwners.length !== newExamOwners.length || !newExamOwners.every((e: string) => ownersSet.has(e)))) { + throw new Error("Name already exists"); } await db.collection(module).updateOne(