From 7ca86fcb951c14650d48568f7523efefdc44b0df Mon Sep 17 00:00:00 2001 From: Shaun Hamilton Date: Tue, 16 Dec 2025 00:02:26 +0200 Subject: [PATCH] fix(api): upsert moderation record on invalid attempt (#64635) --- .../routes/exam-environment.ts | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/api/src/exam-environment/routes/exam-environment.ts b/api/src/exam-environment/routes/exam-environment.ts index 498643c1d7e..ef2db2bc0fb 100644 --- a/api/src/exam-environment/routes/exam-environment.ts +++ b/api/src/exam-environment/routes/exam-environment.ts @@ -681,23 +681,25 @@ async function postExamAttemptHandler( ); if (maybeValidExamAttempt.hasError) { - logger.warn( - { validExamAttemptError: maybeValidExamAttempt.error }, - 'Invalid exam attempt.' - ); - // As attempt is invalid, create moderation record to investigate - await this.prisma.examEnvironmentExamModeration.create({ - data: { - examAttemptId: latestAttempt.id, - status: ExamEnvironmentExamModerationStatus.Pending - } - }); - - void reply.code(400); const message = maybeValidExamAttempt.error instanceof Error ? maybeValidExamAttempt.error.message : 'Unknown attempt validation error'; + logger.warn({ validExamAttemptError: message }, 'Invalid exam attempt.'); + // As attempt is invalid, create moderation record to investigate or update existing record + await this.prisma.examEnvironmentExamModeration.upsert({ + where: { examAttemptId: latestAttempt.id }, + create: { + examAttemptId: latestAttempt.id, + status: ExamEnvironmentExamModerationStatus.Pending, + feedback: message + }, + update: { + feedback: message + } + }); + + void reply.code(400); return reply.send(ERRORS.FCC_EINVAL_EXAM_ENVIRONMENT_EXAM_ATTEMPT(message)); }