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(