From 00085bce9b27e0044f38e976fdf733e4fdc139f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Minelli?= <michael@minelli.me> Date: Mon, 27 May 2024 19:40:49 +0200 Subject: [PATCH] Sonar => Fix issues --- .../AssignmentCorrectionLinkUpdateCommand.ts | 17 ++---------- .../AssignmentCorrectionUnlinkCommand.ts | 18 ++----------- .../subcommands/ExerciseCorrectionCommand.ts | 2 +- .../subcommands/ExerciseCreateCommand.ts | 2 +- NodeApp/src/helpers/GlobalHelper.ts | 27 +++++++++++++++++++ 5 files changed, 33 insertions(+), 33 deletions(-) diff --git a/NodeApp/src/commander/assignment/subcommands/correction/subcommands/AssignmentCorrectionLinkUpdateCommand.ts b/NodeApp/src/commander/assignment/subcommands/correction/subcommands/AssignmentCorrectionLinkUpdateCommand.ts index cc790d2..30c3f40 100644 --- a/NodeApp/src/commander/assignment/subcommands/correction/subcommands/AssignmentCorrectionLinkUpdateCommand.ts +++ b/NodeApp/src/commander/assignment/subcommands/correction/subcommands/AssignmentCorrectionLinkUpdateCommand.ts @@ -1,9 +1,9 @@ import CommanderCommand from '../../../../CommanderCommand.js'; import ora from 'ora'; import DojoBackendManager from '../../../../../managers/DojoBackendManager.js'; -import SessionManager from '../../../../../managers/SessionManager.js'; import Assignment from '../../../../../sharedByClients/models/Assignment.js'; import TextStyle from '../../../../../types/TextStyle.js'; +import GlobalHelper from '../../../../../helpers/GlobalHelper.js'; abstract class AssignmentCorrectionLinkUpdateCommand extends CommanderCommand { @@ -32,23 +32,10 @@ abstract class AssignmentCorrectionLinkUpdateCommand extends CommanderCommand { // 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); + assignment = await GlobalHelper.checkAssignmentCorrectionAccess(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(); diff --git a/NodeApp/src/commander/assignment/subcommands/correction/subcommands/AssignmentCorrectionUnlinkCommand.ts b/NodeApp/src/commander/assignment/subcommands/correction/subcommands/AssignmentCorrectionUnlinkCommand.ts index ceb10cd..78cf1cb 100644 --- a/NodeApp/src/commander/assignment/subcommands/correction/subcommands/AssignmentCorrectionUnlinkCommand.ts +++ b/NodeApp/src/commander/assignment/subcommands/correction/subcommands/AssignmentCorrectionUnlinkCommand.ts @@ -1,9 +1,8 @@ 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'; +import GlobalHelper from '../../../../../helpers/GlobalHelper.js'; class AssignmentCorrectionLinkCommand extends CommanderCommand { @@ -22,23 +21,10 @@ class AssignmentCorrectionLinkCommand extends CommanderCommand { // 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); + assignment = await GlobalHelper.checkAssignmentCorrectionAccess(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 diff --git a/NodeApp/src/commander/exercise/subcommands/ExerciseCorrectionCommand.ts b/NodeApp/src/commander/exercise/subcommands/ExerciseCorrectionCommand.ts index 5e6310b..0a7aac7 100644 --- a/NodeApp/src/commander/exercise/subcommands/ExerciseCorrectionCommand.ts +++ b/NodeApp/src/commander/exercise/subcommands/ExerciseCorrectionCommand.ts @@ -42,7 +42,7 @@ class ExerciseCorrectionCommand extends CommanderCommand { const authors = correction.name.replace(correction.assignmentName, '').split('-')[2].trim(); return { - name : (correction.correctionDescription && correction.correctionDescription != '' ? `${ correction.correctionDescription } (${ authors })` : authors), + name : (correction.correctionDescription && correction.correctionDescription !== '' ? `${ correction.correctionDescription } (${ authors })` : authors), value: correction.correctionCommit?.web_url?.replace('/commit/', '/tree/') ?? '' }; }); diff --git a/NodeApp/src/commander/exercise/subcommands/ExerciseCreateCommand.ts b/NodeApp/src/commander/exercise/subcommands/ExerciseCreateCommand.ts index b0675ba..73a67c5 100644 --- a/NodeApp/src/commander/exercise/subcommands/ExerciseCreateCommand.ts +++ b/NodeApp/src/commander/exercise/subcommands/ExerciseCreateCommand.ts @@ -72,7 +72,7 @@ class ExerciseCreateCommand extends CommanderCommand { }).start().info(); }; - oraInfo(`${ TextStyle.WARNING('You already created ' + this.assignment.myExercises.length + ' exercises for this assignment:') }`, 4); + oraInfo(TextStyle.WARNING(`You already created ${ this.assignment.myExercises.length } exercises for this assignment:`), 4); for ( const exercise of this.assignment.myExercises ) { oraInfo(`${ TextStyle.LIST_ITEM_NAME('Exercice Id:') } ${ exercise.id }`, 8); diff --git a/NodeApp/src/helpers/GlobalHelper.ts b/NodeApp/src/helpers/GlobalHelper.ts index d14735a..d79317f 100644 --- a/NodeApp/src/helpers/GlobalHelper.ts +++ b/NodeApp/src/helpers/GlobalHelper.ts @@ -1,6 +1,10 @@ import { Command, Option } from 'commander'; import Config from '../config/Config.js'; import SessionManager from '../managers/SessionManager.js'; +import TextStyle from '../types/TextStyle.js'; +import ora from 'ora'; +import DojoBackendManager from '../managers/DojoBackendManager.js'; +import Assignment from '../sharedByClients/models/Assignment.js'; class GlobalHelper { @@ -20,6 +24,29 @@ class GlobalHelper { return SessionManager.gitlabCredentials.accessToken ?? ''; }; + + + public async checkAssignmentCorrectionAccess(assignmentName: string): Promise<Assignment | undefined> { + console.log(TextStyle.BLOCK('Please wait while we check access...')); + + const assignmentGetSpinner: ora.Ora = ora('Checking if assignment exists').start(); + const assignment = await DojoBackendManager.getAssignment(assignmentName); + if ( !assignment ) { + assignmentGetSpinner.fail(`The assignment doesn't exists`); + return undefined; + } + 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 undefined; + } + assignmentAccessSpinner.succeed(`You are in the staff of the assignment`); + + return assignment; + } } -- GitLab