From ed7e1c9dd14c3b92179435b3f99fde49b03f57c7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Micha=C3=ABl=20Minelli?= <michael@minelli.me>
Date: Wed, 21 Feb 2024 00:36:40 +0100
Subject: [PATCH] Commands => Add correction link and update commands

---
 .../commander/assignment/AssignmentCommand.ts | 14 +++--
 .../correction/AssignmentCorrectionCommand.ts | 23 +++++++
 .../AssignmentCorrectionLinkCommand.ts        | 10 +++
 .../AssignmentCorrectionLinkUpdateCommand.ts  | 62 +++++++++++++++++++
 .../AssignmentCorrectionUpdateCommand.ts      | 10 +++
 5 files changed, 113 insertions(+), 6 deletions(-)
 create mode 100644 NodeApp/src/commander/assignment/subcommands/correction/AssignmentCorrectionCommand.ts
 create mode 100644 NodeApp/src/commander/assignment/subcommands/correction/subcommands/AssignmentCorrectionLinkCommand.ts
 create mode 100644 NodeApp/src/commander/assignment/subcommands/correction/subcommands/AssignmentCorrectionLinkUpdateCommand.ts
 create mode 100644 NodeApp/src/commander/assignment/subcommands/correction/subcommands/AssignmentCorrectionUpdateCommand.ts

diff --git a/NodeApp/src/commander/assignment/AssignmentCommand.ts b/NodeApp/src/commander/assignment/AssignmentCommand.ts
index 90ae616..b54c2cb 100644
--- a/NodeApp/src/commander/assignment/AssignmentCommand.ts
+++ b/NodeApp/src/commander/assignment/AssignmentCommand.ts
@@ -1,9 +1,10 @@
-import CommanderCommand           from '../CommanderCommand';
-import AssignmentCreateCommand    from './subcommands/AssignmentCreateCommand';
-import AssignmentPublishCommand   from './subcommands/AssignmentPublishCommand';
-import AssignmentUnpublishCommand from './subcommands/AssignmentUnpublishCommand';
-import AssignmentCheckCommand     from './subcommands/AssignmentCheckCommand';
-import AssignmentRunCommand       from './subcommands/AssignmentRunCommand';
+import CommanderCommand            from '../CommanderCommand';
+import AssignmentCreateCommand     from './subcommands/AssignmentCreateCommand';
+import AssignmentPublishCommand    from './subcommands/AssignmentPublishCommand';
+import AssignmentUnpublishCommand  from './subcommands/AssignmentUnpublishCommand';
+import AssignmentCheckCommand      from './subcommands/AssignmentCheckCommand';
+import AssignmentRunCommand        from './subcommands/AssignmentRunCommand';
+import AssignmentCorrectionCommand from './subcommands/correction/AssignmentCorrectionCommand';
 
 
 class AssignmentCommand extends CommanderCommand {
@@ -20,6 +21,7 @@ class AssignmentCommand extends CommanderCommand {
         AssignmentRunCommand.registerOnCommand(this.command);
         AssignmentPublishCommand.registerOnCommand(this.command);
         AssignmentUnpublishCommand.registerOnCommand(this.command);
+        AssignmentCorrectionCommand.registerOnCommand(this.command);
     }
 
     protected async commandAction(): Promise<void> { }
diff --git a/NodeApp/src/commander/assignment/subcommands/correction/AssignmentCorrectionCommand.ts b/NodeApp/src/commander/assignment/subcommands/correction/AssignmentCorrectionCommand.ts
new file mode 100644
index 0000000..3f30a99
--- /dev/null
+++ b/NodeApp/src/commander/assignment/subcommands/correction/AssignmentCorrectionCommand.ts
@@ -0,0 +1,23 @@
+import CommanderCommand                  from '../../../CommanderCommand';
+import AssignmentCorrectionLinkCommand   from './subcommands/AssignmentCorrectionLinkCommand';
+import AssignmentCorrectionUpdateCommand from './subcommands/AssignmentCorrectionUpdateCommand';
+
+
+class AssignmentCorrectionCommand extends CommanderCommand {
+    protected commandName: string = 'correction';
+
+    protected defineCommand() {
+        this.command
+        .description('manage corrections of an assignment');
+    }
+
+    protected defineSubCommands() {
+        AssignmentCorrectionLinkCommand.registerOnCommand(this.command);
+        AssignmentCorrectionUpdateCommand.registerOnCommand(this.command);
+    }
+
+    protected async commandAction(): Promise<void> { }
+}
+
+
+export default new AssignmentCorrectionCommand();
\ No newline at end of file
diff --git a/NodeApp/src/commander/assignment/subcommands/correction/subcommands/AssignmentCorrectionLinkCommand.ts b/NodeApp/src/commander/assignment/subcommands/correction/subcommands/AssignmentCorrectionLinkCommand.ts
new file mode 100644
index 0000000..5d17320
--- /dev/null
+++ b/NodeApp/src/commander/assignment/subcommands/correction/subcommands/AssignmentCorrectionLinkCommand.ts
@@ -0,0 +1,10 @@
+import AssignmentCorrectionLinkUpdateCommand from './AssignmentCorrectionLinkUpdateCommand';
+
+
+class AssignmentCorrectionLinkCommand extends AssignmentCorrectionLinkUpdateCommand {
+    protected commandName: string = 'link';
+    protected isUpdate: boolean = false;
+}
+
+
+export default new AssignmentCorrectionLinkCommand();
\ No newline at end of file
diff --git a/NodeApp/src/commander/assignment/subcommands/correction/subcommands/AssignmentCorrectionLinkUpdateCommand.ts b/NodeApp/src/commander/assignment/subcommands/correction/subcommands/AssignmentCorrectionLinkUpdateCommand.ts
new file mode 100644
index 0000000..cd7b4f8
--- /dev/null
+++ b/NodeApp/src/commander/assignment/subcommands/correction/subcommands/AssignmentCorrectionLinkUpdateCommand.ts
@@ -0,0 +1,62 @@
+import CommanderCommand   from '../../../../CommanderCommand';
+import chalk              from 'chalk';
+import ora                from 'ora';
+import DojoBackendManager from '../../../../../managers/DojoBackendManager';
+import SessionManager     from '../../../../../managers/SessionManager';
+import Assignment         from '../../../../../sharedByClients/models/Assignment';
+
+
+abstract class AssignmentCorrectionLinkUpdateCommand extends CommanderCommand {
+    protected abstract isUpdate: boolean;
+
+    protected defineCommand() {
+        this.command
+        .description(this.isUpdate ? 'update a correction of an assignment' : 'link an exercise repo as a correction for 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(chalk.cyan('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`);
+
+
+            const assignmentPublishedSpinner: ora.Ora = ora('Checking assignment').start();
+            if ( !assignment.published ) {
+                assignmentPublishedSpinner.fail(`Assignment is not published`);
+                return;
+            }
+            assignmentPublishedSpinner.succeed(`Assignment is published`);
+        }
+
+        // Link the exercise
+        {
+            console.log(chalk.cyan('Please wait while we link the exercise...'));
+
+            await DojoBackendManager.linkUpdateCorrection(exerciseIdOrUrl, assignment, this.isUpdate);
+        }
+    }
+}
+
+
+export default AssignmentCorrectionLinkUpdateCommand;
\ No newline at end of file
diff --git a/NodeApp/src/commander/assignment/subcommands/correction/subcommands/AssignmentCorrectionUpdateCommand.ts b/NodeApp/src/commander/assignment/subcommands/correction/subcommands/AssignmentCorrectionUpdateCommand.ts
new file mode 100644
index 0000000..88a9f48
--- /dev/null
+++ b/NodeApp/src/commander/assignment/subcommands/correction/subcommands/AssignmentCorrectionUpdateCommand.ts
@@ -0,0 +1,10 @@
+import AssignmentCorrectionLinkUpdateCommand from './AssignmentCorrectionLinkUpdateCommand';
+
+
+class AssignmentCorrectionUpdateCommand extends AssignmentCorrectionLinkUpdateCommand {
+    protected commandName: string = 'update';
+    protected isUpdate: boolean = true;
+}
+
+
+export default new AssignmentCorrectionUpdateCommand();
\ No newline at end of file
-- 
GitLab