Skip to content

Commit

Permalink
feat(api): updated usecase to validate list from organization id
Browse files Browse the repository at this point in the history
  • Loading branch information
alicegoarnisson authored and xav-car committed Nov 15, 2024
1 parent 3ed4a9c commit 7fa587e
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ async function deleteOrganizationLearnersFromOrganization(organizationId, date)
await usecases.deleteOrganizationLearners({
organizationLearnerIds: organizationLearnerToDeleteIds,
userId: engineeringUserId,
organizationId,
});

await _anonymizeOrganizationLearners({ organizationId });
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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;
});
});

0 comments on commit 7fa587e

Please sign in to comment.