From 43dd2af7e5329b9608eedc268873fc8e01bf255e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Micha=C3=ABl=20Minelli?= <michael@minelli.me>
Date: Mon, 27 May 2024 19:10:48 +0200
Subject: [PATCH] AssignmentRoute => Add commit and description & remove
 correction

---
 ExpressAPI/src/routes/AssignmentRoutes.ts | 49 ++++++++++++++++++++---
 1 file changed, 43 insertions(+), 6 deletions(-)

diff --git a/ExpressAPI/src/routes/AssignmentRoutes.ts b/ExpressAPI/src/routes/AssignmentRoutes.ts
index 951afe1..583e6aa 100644
--- a/ExpressAPI/src/routes/AssignmentRoutes.ts
+++ b/ExpressAPI/src/routes/AssignmentRoutes.ts
@@ -48,6 +48,14 @@ class AssignmentRoutes implements RoutesManager {
             trim    : true,
             notEmpty: true,
             custom  : DojoValidators.exerciseIdOrUrlValidator
+        },
+        commit         : {
+            trim    : true,
+            notEmpty: false
+        },
+        description    : {
+            trim    : true,
+            notEmpty: false
         }
     };
 
@@ -60,6 +68,7 @@ class AssignmentRoutes implements RoutesManager {
 
         backend.post('/assignments/:assignmentNameOrUrl/corrections', SecurityMiddleware.check(true, SecurityCheckType.ASSIGNMENT_STAFF), ParamsValidatorMiddleware.validate(this.assignmentAddCorrigeValidator), this.linkUpdateAssignmentCorrection(false).bind(this) as RequestHandler);
         backend.patch('/assignments/:assignmentNameOrUrl/corrections/:exerciseIdOrUrl', SecurityMiddleware.check(true, SecurityCheckType.ASSIGNMENT_STAFF), this.linkUpdateAssignmentCorrection(true).bind(this) as RequestHandler);
+        backend.delete('/assignments/:assignmentNameOrUrl/corrections/:exerciseIdOrUrl', SecurityMiddleware.check(true, SecurityCheckType.ASSIGNMENT_STAFF), this.unlinkAssignmentCorrection.bind(this) as RequestHandler);
     }
 
     // Get an assignment by its name or gitlab url
@@ -229,9 +238,9 @@ class AssignmentRoutes implements RoutesManager {
                 return req.session.sendResponse(res, StatusCodes.BAD_REQUEST, undefined, 'This exercise is not a correction', DojoStatusCode.EXERCISE_CORRECTION_NOT_EXIST);
             }
 
-            const lastCommit = await GitlabManager.getRepositoryLastCommit(req.boundParams.exercise!.gitlabId);
+            const commit: Gitlab.CommitSchema | undefined = req.body.commit ? await GitlabManager.getRepositoryCommit(req.boundParams.exercise!.gitlabId, req.body.commit as string) : await GitlabManager.getRepositoryLastCommit(req.boundParams.exercise!.gitlabId);
 
-            if ( lastCommit ) {
+            if ( commit ) {
                 if ( !isUpdate && SharedConfig.production ) { //Disable in dev env because gitlab dev group is private and we can't change visibility of sub projects
                     await GitlabManager.changeRepositoryVisibility(req.boundParams.exercise!.gitlabId, 'internal');
                 }
@@ -240,17 +249,45 @@ class AssignmentRoutes implements RoutesManager {
                                              where: {
                                                  id: req.boundParams.exercise!.id
                                              },
-                                             data : {
-                                                 correctionCommit: lastCommit
-                                             }
+                                             data : Object.assign({
+                                                                      correctionCommit: commit
+                                                                  }, isUpdate && req.body.description === undefined ? {} : {
+                                                 correctionDescription: req.body.description
+                                             })
                                          });
 
                 return req.session.sendResponse(res, StatusCodes.OK);
             } else {
-                return req.session.sendResponse(res, StatusCodes.INTERNAL_SERVER_ERROR, undefined, 'No last commit found');
+                return req.session.sendResponse(res, StatusCodes.NOT_FOUND, undefined, 'Commit not found');
             }
         };
     }
+
+    private async unlinkAssignmentCorrection(req: express.Request, res: express.Response) {
+        if ( req.boundParams.exercise?.assignmentName !== req.boundParams.assignment?.name ) {
+            return req.session.sendResponse(res, StatusCodes.BAD_REQUEST, undefined, 'The exercise does not belong to the assignment', DojoStatusCode.ASSIGNMENT_EXERCISE_NOT_RELATED);
+        }
+
+        if ( !req.boundParams.exercise?.isCorrection ) {
+            return req.session.sendResponse(res, StatusCodes.BAD_REQUEST, undefined, 'This exercise is not a correction', DojoStatusCode.EXERCISE_CORRECTION_NOT_EXIST);
+        }
+
+        if ( SharedConfig.production ) { //Disable in dev env because gitlab dev group is private and we can't change visibility of sub projects
+            await GitlabManager.changeRepositoryVisibility(req.boundParams.exercise.gitlabId, 'private');
+        }
+
+        await db.exercise.update({
+                                     where: {
+                                         id: req.boundParams.exercise.id
+                                     },
+                                     data : {
+                                         correctionCommit     : Prisma.DbNull,
+                                         correctionDescription: null
+                                     }
+                                 });
+
+        return req.session.sendResponse(res, StatusCodes.OK);
+    }
 }
 
 
-- 
GitLab