diff --git a/api/scripts/prod/delete-organization-learners-from-organization.js b/api/scripts/prod/delete-organization-learners-from-organization.js index cba1616c4da..3add27346af 100644 --- a/api/scripts/prod/delete-organization-learners-from-organization.js +++ b/api/scripts/prod/delete-organization-learners-from-organization.js @@ -29,6 +29,7 @@ async function deleteOrganizationLearnersFromOrganization(organizationId, date) await usecases.deleteOrganizationLearners({ organizationLearnerIds: organizationLearnerToDeleteIds, userId: engineeringUserId, + organizationId, }); await _anonymizeOrganizationLearners({ organizationId }); 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 1d9e325a065..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 @@ -1,9 +1,23 @@ +import { OrganizationLearnerList } from '../models/OrganizationLearnerList.js'; + const deleteOrganizationLearners = async function ({ organizationLearnerIds, userId, + organizationId, organizationLearnerRepository, campaignParticipationRepository, }) { + const organizationLearnerIdsFromOrganization = + await organizationLearnerRepository.findOrganizationLearnerIdsByOrganizationId({ + organizationId, + }); + + const organizationLearnerList = new OrganizationLearnerList({ + organizationId, + organizationLearnerIds: organizationLearnerIdsFromOrganization, + }); + + organizationLearnerList.canDeleteOrganizationLearners(organizationLearnerIds, userId); await campaignParticipationRepository.removeByOrganizationLearnerIds({ organizationLearnerIds, userId, diff --git a/api/tests/prescription/learner-management/unit/domain/usecases/delete-organization-learners_test.js b/api/tests/prescription/learner-management/unit/domain/usecases/delete-organization-learners_test.js index 68b12c4456e..03417e66096 100644 --- a/api/tests/prescription/learner-management/unit/domain/usecases/delete-organization-learners_test.js +++ b/api/tests/prescription/learner-management/unit/domain/usecases/delete-organization-learners_test.js @@ -1,32 +1,49 @@ +import { OrganizationLearnerList } from '../../../../../../src/prescription/learner-management/domain/models/OrganizationLearnerList.js'; import { deleteOrganizationLearners } from '../../../../../../src/prescription/learner-management/domain/usecases/delete-organization-learners.js'; -import { expect, sinon } from '../../../../../test-helper.js'; +import { catchErr, expect, sinon } from '../../../../../test-helper.js'; describe('Unit | UseCase | Organization Learners Management | Delete Organization Learners', function () { let campaignParticipationRepository; let organizationLearnerRepository; + let organizationLearnerIds; + let organizationId; + let userId; + let canDeleteStub; beforeEach(function () { + userId = 777; + organizationId = 123; + organizationLearnerIds = [123, 456, 789]; + canDeleteStub = sinon.stub(OrganizationLearnerList.prototype, 'canDeleteOrganizationLearners'); campaignParticipationRepository = { removeByOrganizationLearnerIds: sinon.stub(), }; organizationLearnerRepository = { removeByIds: sinon.stub(), + findOrganizationLearnerIdsByOrganizationId: sinon.stub().returns(organizationLearnerIds), }; + organizationLearnerRepository.findOrganizationLearnerIdsByOrganizationId.resolves(organizationLearnerIds); }); - it('should delete organization learners and their participations', async function () { + it('should delete organization learners and their participations when all learners belong to organization', async function () { // given - const userId = 777; - const organizationLearnerIds = [123, 456, 789]; + canDeleteStub.withArgs(organizationLearnerIds); // when await deleteOrganizationLearners({ organizationLearnerIds, userId, + organizationId, campaignParticipationRepository, organizationLearnerRepository, }); + expect(canDeleteStub).to.have.been.calledWith(organizationLearnerIds, userId); + + expect(organizationLearnerRepository.findOrganizationLearnerIdsByOrganizationId).to.have.been.calledWithExactly({ + organizationId, + }); + // then expect(campaignParticipationRepository.removeByOrganizationLearnerIds).to.have.been.calledWithExactly({ organizationLearnerIds, @@ -38,4 +55,29 @@ describe('Unit | UseCase | Organization Learners Management | Delete Organizatio userId, }); }); + + it('should not delete organization learners and their participations when all learners do not belong to organization', async function () { + // given + const organizationLearnerIdsPayload = [123, 456, 789, 101]; + canDeleteStub.withArgs(organizationLearnerIdsPayload).throws(); + + // when + await catchErr(deleteOrganizationLearners)({ + organizationLearnerIds: organizationLearnerIdsPayload, + userId, + organizationId, + campaignParticipationRepository, + organizationLearnerRepository, + }); + + expect(canDeleteStub).to.have.been.calledWith(organizationLearnerIdsPayload, userId); + + expect(organizationLearnerRepository.findOrganizationLearnerIdsByOrganizationId).to.have.been.calledWithExactly({ + organizationId, + }); + + expect(campaignParticipationRepository.removeByOrganizationLearnerIds).to.not.have.been.called; + + expect(organizationLearnerRepository.removeByIds).to.not.have.been.called; + }); });