Skip to content

Commit

Permalink
fix(api): after review fix
Browse files Browse the repository at this point in the history
  • Loading branch information
alicegoarnisson committed Nov 15, 2024
1 parent c2963d5 commit 3d76fa5
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -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 = [
{
Expand All @@ -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 };
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down Expand Up @@ -152,7 +152,7 @@ const register = async (server) => {
handler: organizationLearnersController.dissociate,
validate: {
params: Joi.object({
organizationId: identifiersType.organizationLearnerId,
id: identifiersType.organizationLearnerId,
}),
},
notes: [
Expand Down
7 changes: 7 additions & 0 deletions api/src/prescription/learner-management/domain/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { logger } from '../../../../shared/infrastructure/utils/logger.js';
import { CouldNotDeleteLearnersError } from '../errors.js';

class OrganizationLearnerList {
constructor({ organizationId, organizationLearnerIds } = {}) {
Expand All @@ -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();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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 () {
Expand Down

0 comments on commit 3d76fa5

Please sign in to comment.