From d1d115ddda858643239bc162316ecbf78c93fd45 Mon Sep 17 00:00:00 2001
From: "vincent.steinman" <vincent.steinmann@etu.hesge.ch>
Date: Wed, 20 Mar 2024 23:10:50 +0100
Subject: [PATCH] fix param/body for add, delete

---
 .../migration.sql                             |  8 ++++
 .../migration.sql                             |  8 ++++
 ExpressAPI/prisma/schema.prisma               |  3 +-
 ExpressAPI/src/managers/TagManager.ts         |  6 +--
 .../src/middlewares/ParamsCallbackManager.ts  |  4 +-
 ExpressAPI/src/routes/ApiRoutesManager.ts     |  2 +
 ExpressAPI/src/routes/TagsRoutes.ts           | 40 +++++++++++--------
 7 files changed, 48 insertions(+), 23 deletions(-)
 create mode 100644 ExpressAPI/prisma/migrations/20240320215213_add_correction_to_assignment/migration.sql
 create mode 100644 ExpressAPI/prisma/migrations/20240320220606_add_correction_to_assignment/migration.sql

diff --git a/ExpressAPI/prisma/migrations/20240320215213_add_correction_to_assignment/migration.sql b/ExpressAPI/prisma/migrations/20240320215213_add_correction_to_assignment/migration.sql
new file mode 100644
index 0000000..0176ca0
--- /dev/null
+++ b/ExpressAPI/prisma/migrations/20240320215213_add_correction_to_assignment/migration.sql
@@ -0,0 +1,8 @@
+/*
+  Warnings:
+
+  - Added the required column `state` to the `SubmissionTag` table without a default value. This is not possible if the table is not empty.
+
+*/
+-- AlterTable
+ALTER TABLE `SubmissionTag` ADD COLUMN `state` ENUM('PENDINGAPPROVAL', 'DECLINED', 'APPROVED') NOT NULL;
diff --git a/ExpressAPI/prisma/migrations/20240320220606_add_correction_to_assignment/migration.sql b/ExpressAPI/prisma/migrations/20240320220606_add_correction_to_assignment/migration.sql
new file mode 100644
index 0000000..ece1aea
--- /dev/null
+++ b/ExpressAPI/prisma/migrations/20240320220606_add_correction_to_assignment/migration.sql
@@ -0,0 +1,8 @@
+/*
+  Warnings:
+
+  - You are about to alter the column `state` on the `SubmissionTag` table. The data in that column could be lost. The data in that column will be cast from `Enum(EnumId(3))` to `VarChar(191)`.
+
+*/
+-- AlterTable
+ALTER TABLE `SubmissionTag` MODIFY `state` VARCHAR(191) NOT NULL;
diff --git a/ExpressAPI/prisma/schema.prisma b/ExpressAPI/prisma/schema.prisma
index 452ec47..16f531a 100644
--- a/ExpressAPI/prisma/schema.prisma
+++ b/ExpressAPI/prisma/schema.prisma
@@ -98,5 +98,6 @@ model Tag {
 
 model SubmissionTag {
     name        String   @id @db.Char(36)
-    type        TagType       
+    type        TagType 
+    state       String
 }
\ No newline at end of file
diff --git a/ExpressAPI/src/managers/TagManager.ts b/ExpressAPI/src/managers/TagManager.ts
index 3baa8ef..19ac12c 100644
--- a/ExpressAPI/src/managers/TagManager.ts
+++ b/ExpressAPI/src/managers/TagManager.ts
@@ -2,10 +2,10 @@ import { Prisma, Tag }           from '@prisma/client';
 import db                   from '../helpers/DatabaseHelper';
 
 class TagManager {
-	async get(name: string, include: Prisma.ExerciseInclude | undefined = undefined): Promise<Tag | undefined> {
-        return await db.exercise.findUnique({
+	async get(name: string, include: Prisma.TagInclude | undefined = undefined): Promise<Tag | undefined> {
+        return await db.tag.findUnique({
                                                 where  : {
-                                                    id: name
+                                                    name: name
                                                 },
                                                 include: include
                                             }) as unknown as Tag ?? undefined;
diff --git a/ExpressAPI/src/middlewares/ParamsCallbackManager.ts b/ExpressAPI/src/middlewares/ParamsCallbackManager.ts
index 5027a7e..753c154 100644
--- a/ExpressAPI/src/middlewares/ParamsCallbackManager.ts
+++ b/ExpressAPI/src/middlewares/ParamsCallbackManager.ts
@@ -49,11 +49,11 @@ class ParamsCallbackManager {
         
         this.listenParam('tagId', backend, (TagManager.get as GetFunction).bind(TagManager), [ {
 
-        } ], 'tag');
+        } ], 'tags');
 
         this.listenParam('tagProposalName', backend, (TagManager.get as GetFunction).bind(TagManager), [ {
  
-        } ], 'tag');
+        } ], 'tags');
     }
 }
 
diff --git a/ExpressAPI/src/routes/ApiRoutesManager.ts b/ExpressAPI/src/routes/ApiRoutesManager.ts
index 57a4188..2461b4e 100644
--- a/ExpressAPI/src/routes/ApiRoutesManager.ts
+++ b/ExpressAPI/src/routes/ApiRoutesManager.ts
@@ -5,6 +5,7 @@ import SessionRoutes    from './SessionRoutes.js';
 import AssignmentRoutes from './AssignmentRoutes.js';
 import GitlabRoutes     from './GitlabRoutes.js';
 import ExerciseRoutes   from './ExerciseRoutes.js';
+import TagsRoutes       from './TagsRoutes';
 
 
 class AdminRoutesManager implements RoutesManager {
@@ -14,6 +15,7 @@ class AdminRoutesManager implements RoutesManager {
         GitlabRoutes.registerOnBackend(backend);
         AssignmentRoutes.registerOnBackend(backend);
         ExerciseRoutes.registerOnBackend(backend);
+        TagsRoutes.registerOnBackend(backend);
     }
 }
 
diff --git a/ExpressAPI/src/routes/TagsRoutes.ts b/ExpressAPI/src/routes/TagsRoutes.ts
index bcd9e7b..5d0e97d 100644
--- a/ExpressAPI/src/routes/TagsRoutes.ts
+++ b/ExpressAPI/src/routes/TagsRoutes.ts
@@ -18,7 +18,7 @@ enum SubmitStatus{
 }
 
 class TagRoutes implements RoutesManager {
-    private readonly tagsValidator: ExpressValidator.Schema = {
+    private readonly tagsValidatorNameType: ExpressValidator.Schema = {
         name: {
             trim: true,
             notEmpty: true
@@ -29,21 +29,26 @@ class TagRoutes implements RoutesManager {
         }
     };
 
-    registerOnBackend(backend: Express) {
-        backend.post('/tags', SecurityMiddleware.check(true, SecurityCheckType.TEACHING_STAFF), ParamsValidatorMiddleware.validate(this.tagsValidator), this.addTag.bind(this));
-        backend.delete('/tags/:tagId', SecurityMiddleware.check(true, SecurityCheckType.ADMIN), ParamsValidatorMiddleware.validate(this.tagsValidator), this.deleteTag.bind(this));
-        backend.get('/tags/proposals/state?', SecurityMiddleware.check(true, SecurityCheckType.ADMIN), ParamsValidatorMiddleware.validate(this.tagsValidator), this.getSubmittedTag.bind(this));
-        backend.post('/tags/proposals', SecurityMiddleware.check(true, SecurityCheckType.TEACHING_STAFF), ParamsValidatorMiddleware.validate(this.tagsValidator), this.SubmitTag.bind(this));
-        backend.patch('/tags/proposals/:tagProposalName', SecurityMiddleware.check(true, SecurityCheckType.ADMIN), ParamsValidatorMiddleware.validate(this.tagsValidator), this.validateTag.bind(this));
+    private readonly tagsValidatorStatus: ExpressValidator.Schema = {
+        status: {
+            trim: true,
+            notEmpty: true
+        },
+    };
+
+    registerOnBackend(backend: Express) { 
+        backend.post('/tags', SecurityMiddleware.check(true, SecurityCheckType.TEACHING_STAFF), ParamsValidatorMiddleware.validate(this.tagsValidatorNameType), this.addTag.bind(this));
+        backend.delete('/tags/:tageName', SecurityMiddleware.check(true, SecurityCheckType.ADMIN), this.deleteTag.bind(this));
+        backend.get('/tags/proposals/state', SecurityMiddleware.check(true, SecurityCheckType.ADMIN), this.getSubmittedTag.bind(this)); //Check ?
+        backend.post('/tags/proposals', SecurityMiddleware.check(true, SecurityCheckType.TEACHING_STAFF), ParamsValidatorMiddleware.validate(this.tagsValidatorNameType), this.SubmitTag.bind(this));
+        backend.patch('/tags/proposals/:tagProposalName', SecurityMiddleware.check(true, SecurityCheckType.ADMIN), ParamsValidatorMiddleware.validate(this.tagsValidatorStatus), this.validateTag.bind(this));
     }
 
     private async addTag(req: express.Request, res: express.Response) {
         const tagName = req.body.name
         const tagType = req.body.type
-        console.log("Popipo");
 
         if(tagType != TagType.USERDEFINED && !req.session.profile.isAdmin) {
-            console.log("Hehehehe");
             return req.session.sendResponse(res, StatusCodes.FORBIDDEN);
         }
 
@@ -56,13 +61,12 @@ class TagRoutes implements RoutesManager {
             }
         })
         return req.session.sendResponse(res, StatusCodes.OK, {
-            tag           : (req.boundParams.tag as Tags),
+            tag           : req.body.type,
             name          : req.body.name
         }, "Tag ajouté avec succès");
     }
     private async deleteTag(req: express.Request, res: express.Response) {
-        console.log("DELETE");
-        const tagName = req.body.name
+        const tagName = req.params.name
         
         db.tag.delete({
             where : { name: tagName }
@@ -70,21 +74,22 @@ class TagRoutes implements RoutesManager {
         return req.session.sendResponse(res, StatusCodes.OK, "Tag supprimé avec succès");
     }
     private async getSubmittedTag(req: express.Request, res: express.Response) {
-        const tagName = req.body.name
+        const state = req.params.name
 
         db.submissionTag.findMany({
             where : { 
-                name: tagName 
-            },
+                state: state 
+            }
         })
         return req.session.sendResponse(res, StatusCodes.OK, {
             name          : req.body.name,
-            tag           : (req.boundParams.tag as Tags)
+            tag           : req.body.type
         });
     }
     private async SubmitTag(req: express.Request, res: express.Response) {
         const tagName = req.body.name
         const tagType = req.body.type
+        const tagState = req.body.state
 
         db.submissionTag.upsert({
             where : { name: tagName },
@@ -92,12 +97,13 @@ class TagRoutes implements RoutesManager {
             create: {
                 name            : tagName,
                 type            : tagType,
+                state           : tagState
             }
         })
 
         return req.session.sendResponse(res, StatusCodes.OK, {
             name          : req.body.name,
-            tag           : (req.boundParams.tag as Tags)
+            tag           : req.body.tag
         });        
     }
     private async validateTag(req: express.Request, res: express.Response) {
-- 
GitLab