Skip to content

Commit

Permalink
fix(api): changed error to log helpful variables and not be too expli…
Browse files Browse the repository at this point in the history
…cit frontend side
  • Loading branch information
alicegoarnisson committed Nov 14, 2024
1 parent e102081 commit c2963d5
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 22 deletions.
7 changes: 0 additions & 7 deletions api/src/prescription/learner-management/domain/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,8 @@ class ReconcileCommonOrganizationLearnerError extends DomainError {
}
}

class CouldNotDeleteLearnersError extends DomainError {
constructor(organizationLearnerIds) {
super(`Could not delete the following organization learners : ${organizationLearnerIds}`);
}
}

export {
AggregateImportError,
CouldNotDeleteLearnersError,
OrganizationDoesNotHaveFeatureEnabledError,
OrganizationLearnerImportFormatNotFoundError,
OrganizationLearnersCouldNotBeSavedError,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { CouldNotDeleteLearnersError } from '../errors.js';
import { logger } from '../../../../shared/infrastructure/utils/logger.js';

class OrganizationLearnerList {
constructor({ organizationId, organizationLearnerIds } = {}) {
this.organizationId = organizationId;
this.organizationLearnerIds = organizationLearnerIds;
}
canDeleteOrganizationLearners(organizationLearnerIdsToValidate) {
canDeleteOrganizationLearners(organizationLearnerIdsToValidate, userId) {
const result = organizationLearnerIdsToValidate.filter((organizationLearnerId) => {
return this.organizationLearnerIds.includes(organizationLearnerId);
return !this.organizationLearnerIds.includes(organizationLearnerId);
});
if (result.length !== organizationLearnerIdsToValidate.length) {
throw new CouldNotDeleteLearnersError(this.organizationLearnerIds);
if (result.length !== 0) {
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.');
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const deleteOrganizationLearners = async function ({
organizationLearnerIds: learnersBelogingToOrganization,
});

organizationLearnerList.canDeleteOrganizationLearners(organizationLearnerIds);
organizationLearnerList.canDeleteOrganizationLearners(organizationLearnerIds, userId);
await campaignParticipationRepository.removeByOrganizationLearnerIds({
organizationLearnerIds,
userId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
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 All @@ -18,6 +17,7 @@ describe('Unit | Models | OrganizationLearnerListFormat', function () {

describe('#can delete organization learners ', function () {
it('should throw when lists are different', function () {
const userId = Symbol('123');
//when
const payload = {
organizationId: Symbol('organizationId'),
Expand All @@ -26,23 +26,25 @@ describe('Unit | Models | OrganizationLearnerListFormat', function () {

const organizationLearnerList = new OrganizationLearnerList(payload);

const result = catchErrSync(
organizationLearnerList.canDeleteOrganizationLearners,
organizationLearnerList,
)([456, 123]);
const result = catchErrSync(organizationLearnerList.canDeleteOrganizationLearners, organizationLearnerList)(
[456, 123],
userId,
);

expect(result).to.be.instanceof(CouldNotDeleteLearnersError);
expect(result).to.be.instanceof(Error);
});

it('should not throw when lists are identical', function () {
const userId = Symbol('123');

const payload = {
organizationId: Symbol('organizationId'),
organizationLearnerIds: [123, 345],
};

expect(() => {
const organizationLearnerList = new OrganizationLearnerList(payload);
organizationLearnerList.canDeleteOrganizationLearners([123, 345]);
organizationLearnerList.canDeleteOrganizationLearners([123, 345], userId);
}).to.not.throw();
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('Unit | UseCase | Organization Learners Management | Delete Organizatio
organizationLearnerRepository,
});

expect(canDeleteStub).to.have.been.calledWith(organizationLearnerIds);
expect(canDeleteStub).to.have.been.calledWith(organizationLearnerIds, userId);

expect(organizationLearnerRepository.findOrganizationLearnerIdsByOrganizationId).to.have.been.calledWithExactly({
organizationId,
Expand Down Expand Up @@ -70,7 +70,7 @@ describe('Unit | UseCase | Organization Learners Management | Delete Organizatio
organizationLearnerRepository,
});

expect(canDeleteStub).to.have.been.calledWith(organizationLearnerIdsPayload);
expect(canDeleteStub).to.have.been.calledWith(organizationLearnerIdsPayload, userId);

expect(organizationLearnerRepository.findOrganizationLearnerIdsByOrganizationId).to.have.been.calledWithExactly({
organizationId,
Expand Down

0 comments on commit c2963d5

Please sign in to comment.