diff --git a/NodeApp/src/managers/DojoBackendManager.ts b/NodeApp/src/managers/DojoBackendManager.ts index b8e8a41ec1beb1172244b281c61d658dc2c923ec..fcd749df9a44d151c202498cb8d5a8dfe5f2df2a 100644 --- a/NodeApp/src/managers/DojoBackendManager.ts +++ b/NodeApp/src/managers/DojoBackendManager.ts @@ -192,6 +192,49 @@ class DojoBackendManager { throw error; } } + + public async linkUpdateCorrection(exerciseIdOrUrl: string, assignment: Assignment, isUpdate: boolean, verbose: boolean = true): Promise<boolean> { + const spinner: ora.Ora = ora(`${ isUpdate ? 'Updating' : 'Linking' } correction`); + + if ( verbose ) { + spinner.start(); + } + + try { + const axiosFunction = isUpdate ? axios.patch : axios.post; + const route = isUpdate ? ApiRoute.ASSIGNMENT_CORRECTION_UPDATE : ApiRoute.ASSIGNMENT_CORRECTION_LINK; + + await axiosFunction(this.getApiUrl(route).replace('{{assignmentNameOrUrl}}', encodeURIComponent(assignment.name)).replace('{{exerciseIdOrUrl}}', encodeURIComponent(exerciseIdOrUrl)), { + exerciseIdOrUrl: exerciseIdOrUrl + }); + + if ( verbose ) { + spinner.succeed(`Correction ${ isUpdate ? 'updated' : 'linked' }`); + } + + return true; + } catch ( error ) { + if ( verbose ) { + if ( error instanceof AxiosError ) { + if ( error.response?.data ) { + if ( error.response.data.code === DojoStatusCode.ASSIGNMENT_EXERCISE_NOT_RELATED ) { + spinner.fail(`The exercise does not belong to the assignment.`); + } else if ( error.response.data.code === DojoStatusCode.EXERCISE_CORRECTION_ALREADY_EXIST ) { + spinner.fail(`This exercise is already labelled as a correction. If you want to update it, please use the update command.`); + } else if ( error.response.data.code === DojoStatusCode.EXERCISE_CORRECTION_NOT_EXIST ) { + spinner.fail(`The exercise is not labelled as a correction so it's not possible to update it.`); + } + } else { + spinner.fail(`Correction ${ isUpdate ? 'update' : 'link' } error: ${ error.response?.statusText }`); + } + } else { + spinner.fail(`Correction ${ isUpdate ? 'update' : 'link' } error: ${ error }`); + } + } + + return false; + } + } }