From 638e2334370cd82b0b8dc4933163a4dba36886ff Mon Sep 17 00:00:00 2001 From: alicegoarnisson Date: Fri, 15 Nov 2024 10:59:57 +0100 Subject: [PATCH] feat(api): update route and add error mapper --- .../application/http-error-mapper-configuration.js | 8 +++++++- .../application/organization-learners-controller.js | 2 ++ .../application/organization-learners-route.js | 4 ++-- api/src/prescription/learner-management/domain/errors.js | 7 +++++++ api/src/shared/application/security-pre-handlers.js | 2 +- .../application/organization-learners-route_test.js | 2 +- 6 files changed, 20 insertions(+), 5 deletions(-) diff --git a/api/src/prescription/learner-management/application/http-error-mapper-configuration.js b/api/src/prescription/learner-management/application/http-error-mapper-configuration.js index 989d60fdc45..9e000b7e44f 100644 --- a/api/src/prescription/learner-management/application/http-error-mapper-configuration.js +++ b/api/src/prescription/learner-management/application/http-error-mapper-configuration.js @@ -1,5 +1,5 @@ import { HttpErrors } from '../../../shared/application/http-errors.js'; -import { AggregateImportError } from '../domain/errors.js'; +import { AggregateImportError, CouldNotDeleteLearnersError } from '../domain/errors.js'; const learnerManagementDomainErrorMappingConfiguration = [ { @@ -8,6 +8,12 @@ const learnerManagementDomainErrorMappingConfiguration = [ return new HttpErrors.PreconditionFailedError(error.message, error.code, error.meta); }, }, + { + name: CouldNotDeleteLearnersError.name, + httpErrorFn: (error) => { + return new HttpErrors.PreconditionFailedError(error.message); + }, + }, ]; export { learnerManagementDomainErrorMappingConfiguration }; diff --git a/api/src/prescription/learner-management/application/organization-learners-controller.js b/api/src/prescription/learner-management/application/organization-learners-controller.js index 54820a579b2..2943263207d 100644 --- a/api/src/prescription/learner-management/application/organization-learners-controller.js +++ b/api/src/prescription/learner-management/application/organization-learners-controller.js @@ -6,11 +6,13 @@ import { usecases } from '../domain/usecases/index.js'; const deleteOrganizationLearners = async function (request, h) { const authenticatedUserId = request.auth.credentials.userId; const listLearners = request.payload.listLearners; + const organizationId = request.params.organizationId; await DomainTransaction.execute(async () => { await usecases.deleteOrganizationLearners({ organizationLearnerIds: listLearners, userId: authenticatedUserId, + organizationId, }); }); return h.response().code(200); diff --git a/api/src/prescription/learner-management/application/organization-learners-route.js b/api/src/prescription/learner-management/application/organization-learners-route.js index dee2d613c1c..1df4563b0e1 100644 --- a/api/src/prescription/learner-management/application/organization-learners-route.js +++ b/api/src/prescription/learner-management/application/organization-learners-route.js @@ -13,7 +13,7 @@ const register = async (server) => { server.route([ { method: 'DELETE', - path: '/api/organizations/{id}/organization-learners', + path: '/api/organizations/{organizationId}/organization-learners', config: { pre: [ { @@ -27,7 +27,7 @@ const register = async (server) => { ], validate: { params: Joi.object({ - id: identifiersType.organizationId, + organizationId: identifiersType.organizationId, }), payload: Joi.object({ listLearners: Joi.array().required().items(Joi.number().required()), diff --git a/api/src/prescription/learner-management/domain/errors.js b/api/src/prescription/learner-management/domain/errors.js index 02b8f1ae740..10d4bb40bf2 100644 --- a/api/src/prescription/learner-management/domain/errors.js +++ b/api/src/prescription/learner-management/domain/errors.js @@ -41,8 +41,15 @@ class ReconcileCommonOrganizationLearnerError extends DomainError { } } +class CouldNotDeleteLearnersError extends DomainError { + constructor() { + super(`Could not delete the following organization learners.`); + } +} + export { AggregateImportError, + CouldNotDeleteLearnersError, OrganizationDoesNotHaveFeatureEnabledError, OrganizationLearnerImportFormatNotFoundError, OrganizationLearnersCouldNotBeSavedError, diff --git a/api/src/shared/application/security-pre-handlers.js b/api/src/shared/application/security-pre-handlers.js index b0ccc995ef2..35feb8b9a4a 100644 --- a/api/src/shared/application/security-pre-handlers.js +++ b/api/src/shared/application/security-pre-handlers.js @@ -468,7 +468,7 @@ async function checkUserDoesNotBelongsToScoOrganizationManagingStudents( return _replyForbiddenError(h); } - const organizationId = request.params.id; + const organizationId = request.params.organizationId || request.params.id; const isOrganizationScoManagingStudent = await dependencies.checkOrganizationIsScoAndManagingStudentUsecase.execute({ organizationId, diff --git a/api/tests/prescription/learner-management/integration/application/organization-learners-route_test.js b/api/tests/prescription/learner-management/integration/application/organization-learners-route_test.js index 2d2536664a3..3eb5bcce94e 100644 --- a/api/tests/prescription/learner-management/integration/application/organization-learners-route_test.js +++ b/api/tests/prescription/learner-management/integration/application/organization-learners-route_test.js @@ -10,7 +10,7 @@ import { } from '../../../../test-helper.js'; describe('Integration | Application | Organization Learners Management | Routes', function () { - describe('DELETE /organizations/{id}/organization-learners', function () { + describe('DELETE /organizations/{organizationId}/organization-learners', function () { const method = 'DELETE'; let headers, httpTestServer, organizationId, url, payload;