From c8590e80cd6912487e248274186340c48a3a562a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Micha=C3=ABl=20Minelli?= <michael@minelli.me>
Date: Wed, 21 Feb 2024 00:47:29 +0100
Subject: [PATCH] Prisma => Add extensions with corrections informations

---
 .../Extensions/AssignmentResultExtension.ts   | 36 +++++++++++++++++++
 .../Extensions/ExerciseResultExtension.ts     | 19 ++++++++++
 2 files changed, 55 insertions(+)
 create mode 100644 ExpressAPI/src/helpers/Prisma/Extensions/AssignmentResultExtension.ts
 create mode 100644 ExpressAPI/src/helpers/Prisma/Extensions/ExerciseResultExtension.ts

diff --git a/ExpressAPI/src/helpers/Prisma/Extensions/AssignmentResultExtension.ts b/ExpressAPI/src/helpers/Prisma/Extensions/AssignmentResultExtension.ts
new file mode 100644
index 0000000..56819c9
--- /dev/null
+++ b/ExpressAPI/src/helpers/Prisma/Extensions/AssignmentResultExtension.ts
@@ -0,0 +1,36 @@
+import { Prisma }   from '@prisma/client';
+import { Exercise } from '../../../types/DatabaseTypes';
+import db           from '../../DatabaseHelper';
+import LazyVal      from '../../../shared/helpers/LazyVal';
+
+
+async function getCorrections(assignment: { name: string }): Promise<Array<Exercise> | undefined> {
+    try {
+        return await db.exercise.findMany({
+                                              where: {
+                                                  assignmentName  : assignment.name,
+                                                  correctionCommit: {
+                                                      not: Prisma.JsonNull
+                                                  }
+                                              }
+                                          }) as Array<Exercise> ?? undefined;
+    } catch ( e ) {
+        return undefined;
+    }
+}
+
+export default Prisma.defineExtension(client => {
+    return client.$extends({
+                               result: {
+                                   assignment: {
+                                       corrections: {
+                                           compute(assignment) {
+                                               return new LazyVal<Array<Exercise> | undefined>(() => {
+                                                   return getCorrections(assignment);
+                                               });
+                                           }
+                                       }
+                                   }
+                               }
+                           });
+});
\ No newline at end of file
diff --git a/ExpressAPI/src/helpers/Prisma/Extensions/ExerciseResultExtension.ts b/ExpressAPI/src/helpers/Prisma/Extensions/ExerciseResultExtension.ts
new file mode 100644
index 0000000..e11b4e2
--- /dev/null
+++ b/ExpressAPI/src/helpers/Prisma/Extensions/ExerciseResultExtension.ts
@@ -0,0 +1,19 @@
+import { Prisma } from '@prisma/client';
+
+
+export default Prisma.defineExtension(client => {
+    return client.$extends({
+                               result: {
+                                   exercise: {
+                                       isCorrection: {
+                                           needs: {
+                                               correctionCommit: true
+                                           },
+                                           compute(exercise) {
+                                               return exercise.correctionCommit != null;
+                                           }
+                                       }
+                                   }
+                               }
+                           });
+});
\ No newline at end of file
-- 
GitLab