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-route.js b/api/src/prescription/learner-management/application/organization-learners-route.js index 54d4e712425..7c76aba0b00 100644 --- a/api/src/prescription/learner-management/application/organization-learners-route.js +++ b/api/src/prescription/learner-management/application/organization-learners-route.js @@ -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()), @@ -152,7 +152,7 @@ const register = async (server) => { handler: organizationLearnersController.dissociate, validate: { params: Joi.object({ - organizationId: identifiersType.organizationLearnerId, + id: identifiersType.organizationLearnerId, }), }, notes: [ 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/prescription/learner-management/domain/models/OrganizationLearnerList.js b/api/src/prescription/learner-management/domain/models/OrganizationLearnerList.js index a0284a278d3..f0b31baba00 100644 --- a/api/src/prescription/learner-management/domain/models/OrganizationLearnerList.js +++ b/api/src/prescription/learner-management/domain/models/OrganizationLearnerList.js @@ -1,4 +1,5 @@ import { logger } from '../../../../shared/infrastructure/utils/logger.js'; +import { CouldNotDeleteLearnersError } from '../errors.js'; class OrganizationLearnerList { constructor({ organizationId, organizationLearnerIds } = {}) { @@ -13,7 +14,7 @@ class OrganizationLearnerList { logger.error( `User id ${userId} could not delete organization learners because learner id ${result} don't belong to organization id ${this.organizationId} "`, ); - throw new Error('Could not delete organization learners.'); + throw new CouldNotDeleteLearnersError(); } } } diff --git a/api/src/prescription/learner-management/domain/usecases/delete-organization-learners.js b/api/src/prescription/learner-management/domain/usecases/delete-organization-learners.js index 8850bf99a14..260c39afcf0 100644 --- a/api/src/prescription/learner-management/domain/usecases/delete-organization-learners.js +++ b/api/src/prescription/learner-management/domain/usecases/delete-organization-learners.js @@ -7,15 +7,14 @@ const deleteOrganizationLearners = async function ({ organizationLearnerRepository, campaignParticipationRepository, }) { - const learnersBelogingToOrganization = await organizationLearnerRepository.findOrganizationLearnerIdsByOrganizationId( - { + const organizationLearnerIdsFromOrganization = + await organizationLearnerRepository.findOrganizationLearnerIdsByOrganizationId({ organizationId, - }, - ); + }); const organizationLearnerList = new OrganizationLearnerList({ organizationId, - organizationLearnerIds: learnersBelogingToOrganization, + organizationLearnerIds: organizationLearnerIdsFromOrganization, }); organizationLearnerList.canDeleteOrganizationLearners(organizationLearnerIds, userId); diff --git a/api/tests/prescription/learner-management/unit/domain/models/OrganizationLearnerList_test.js b/api/tests/prescription/learner-management/unit/domain/models/OrganizationLearnerList_test.js index 5b66b67f80e..8b0d2b13dc7 100644 --- a/api/tests/prescription/learner-management/unit/domain/models/OrganizationLearnerList_test.js +++ b/api/tests/prescription/learner-management/unit/domain/models/OrganizationLearnerList_test.js @@ -1,3 +1,4 @@ +import { CouldNotDeleteLearnersError } from '../../../../../../src/prescription/learner-management/domain/errors.js'; import { OrganizationLearnerList } from '../../../../../../src/prescription/learner-management/domain/models/OrganizationLearnerList.js'; import { catchErrSync, expect } from '../../../../../test-helper.js'; @@ -31,7 +32,7 @@ describe('Unit | Models | OrganizationLearnerListFormat', function () { userId, ); - expect(result).to.be.instanceof(Error); + expect(result).to.be.instanceof(CouldNotDeleteLearnersError); }); it('should not throw when lists are identical', function () {