From 3ed58df55e12be5f1cfa48f360b54396cb7bfa7f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Micha=C3=ABl=20Minelli?= <michael@minelli.me>
Date: Mon, 27 May 2024 19:16:56 +0200
Subject: [PATCH] Add correction unlink command

---
 .../correction/AssignmentCorrectionCommand.ts |  2 +
 .../AssignmentCorrectionUnlinkCommand.ts      | 54 +++++++++++++++++++
 NodeApp/src/managers/DojoBackendManager.ts    | 25 +++++++++
 3 files changed, 81 insertions(+)
 create mode 100644 NodeApp/src/commander/assignment/subcommands/correction/subcommands/AssignmentCorrectionUnlinkCommand.ts

diff --git a/NodeApp/src/commander/assignment/subcommands/correction/AssignmentCorrectionCommand.ts b/NodeApp/src/commander/assignment/subcommands/correction/AssignmentCorrectionCommand.ts
index 1ff39af..3eb929a 100644
--- a/NodeApp/src/commander/assignment/subcommands/correction/AssignmentCorrectionCommand.ts
+++ b/NodeApp/src/commander/assignment/subcommands/correction/AssignmentCorrectionCommand.ts
@@ -1,6 +1,7 @@
 import CommanderCommand                  from '../../../CommanderCommand.js';
 import AssignmentCorrectionLinkCommand   from './subcommands/AssignmentCorrectionLinkCommand.js';
 import AssignmentCorrectionUpdateCommand from './subcommands/AssignmentCorrectionUpdateCommand.js';
+import AssignmentCorrectionUnlinkCommand from './subcommands/AssignmentCorrectionUnlinkCommand.js';
 
 
 class AssignmentCorrectionCommand extends CommanderCommand {
@@ -14,6 +15,7 @@ class AssignmentCorrectionCommand extends CommanderCommand {
     protected defineSubCommands() {
         AssignmentCorrectionLinkCommand.registerOnCommand(this.command);
         AssignmentCorrectionUpdateCommand.registerOnCommand(this.command);
+        AssignmentCorrectionUnlinkCommand.registerOnCommand(this.command);
     }
 
     protected async commandAction(): Promise<void> {
diff --git a/NodeApp/src/commander/assignment/subcommands/correction/subcommands/AssignmentCorrectionUnlinkCommand.ts b/NodeApp/src/commander/assignment/subcommands/correction/subcommands/AssignmentCorrectionUnlinkCommand.ts
new file mode 100644
index 0000000..ceb10cd
--- /dev/null
+++ b/NodeApp/src/commander/assignment/subcommands/correction/subcommands/AssignmentCorrectionUnlinkCommand.ts
@@ -0,0 +1,54 @@
+import Assignment         from '../../../../../sharedByClients/models/Assignment.js';
+import ora                from 'ora';
+import TextStyle          from '../../../../../types/TextStyle.js';
+import DojoBackendManager from '../../../../../managers/DojoBackendManager.js';
+import SessionManager     from '../../../../../managers/SessionManager.js';
+import CommanderCommand   from '../../../../CommanderCommand.js';
+
+
+class AssignmentCorrectionLinkCommand extends CommanderCommand {
+    protected commandName: string = 'unlink';
+
+    protected defineCommand() {
+        this.command
+            .description('remove a correction of an assignment')
+            .argument('<string>', 'id or url of the exercise that is the correction')
+            .requiredOption('-a, --assignment <string>', 'id or url of the assignment of the correction')
+            .action(this.commandAction.bind(this));
+    }
+
+    protected async commandAction(exerciseIdOrUrl: string, options: { assignment: string }): Promise<void> {
+        let assignment!: Assignment | undefined;
+
+        // Check access
+        {
+            console.log(TextStyle.BLOCK('Please wait while we check access...'));
+
+            const assignmentGetSpinner: ora.Ora = ora('Checking if assignment exists').start();
+            assignment = await DojoBackendManager.getAssignment(options.assignment);
+            if ( !assignment ) {
+                assignmentGetSpinner.fail(`The assignment doesn't exists`);
+                return;
+            }
+            assignmentGetSpinner.succeed(`The assignment exists`);
+
+
+            const assignmentAccessSpinner: ora.Ora = ora('Checking assignment access').start();
+            if ( assignment.staff.find(staff => staff.id === SessionManager.profile?.id) === undefined ) {
+                assignmentAccessSpinner.fail(`You are not in the staff of the assignment`);
+                return;
+            }
+            assignmentAccessSpinner.succeed(`You are in the staff of the assignment`);
+        }
+
+        // Link the exercise
+        {
+            console.log(TextStyle.BLOCK('Please wait while we unlink the exercise...'));
+
+            await DojoBackendManager.unlinkCorrection(exerciseIdOrUrl, assignment);
+        }
+    }
+}
+
+
+export default new AssignmentCorrectionLinkCommand();
\ No newline at end of file
diff --git a/NodeApp/src/managers/DojoBackendManager.ts b/NodeApp/src/managers/DojoBackendManager.ts
index b341a94..8c8a5a9 100644
--- a/NodeApp/src/managers/DojoBackendManager.ts
+++ b/NodeApp/src/managers/DojoBackendManager.ts
@@ -224,6 +224,31 @@ class DojoBackendManager {
             return false;
         }
     }
+
+    public async unlinkCorrection(exerciseIdOrUrl: string, assignment: Assignment, verbose: boolean = true): Promise<boolean> {
+        const spinner: ora.Ora = ora(`Unlinking correction`);
+
+        if ( verbose ) {
+            spinner.start();
+        }
+
+        try {
+            await axios.delete(DojoBackendHelper.getApiUrl(ApiRoute.ASSIGNMENT_CORRECTION_UPDATE_DELETE, {
+                assignmentNameOrUrl: assignment.name,
+                exerciseIdOrUrl    : exerciseIdOrUrl
+            }));
+
+            if ( verbose ) {
+                spinner.succeed(`Correction unlinked`);
+            }
+
+            return true;
+        } catch ( error ) {
+            this.handleApiError(error, spinner, verbose, `Correction unlink error: ${ error }`);
+
+            return false;
+        }
+    }
 }
 
 
-- 
GitLab