Skip to content

Commit

Permalink
feat(api): add internal api to delete learner while activate import f…
Browse files Browse the repository at this point in the history
…eature
  • Loading branch information
xav-car committed Nov 29, 2024
1 parent beefd5b commit ed758df
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,26 @@ export const hasBeenLearner = async ({ userId }) => {

return isLearner;
};

/**
* delete organization learner before adding import feature
*
* @param {object} params
* @param {number} params.userId - The ID of the user wich request the action
* @param {number} params.organizationId - The ID of the organizationId to find learner to delete
* @returns {Promise<void>}
* @throws TypeError - Throw when params.userId or params.organizationId is not defined
*/
export const deleteOrganizationLearnerBeforeImportFeature = async ({ userId, organizationId }) => {
if (!userId) {
throw new TypeError('userId is required');
}

if (!organizationId) {
throw new TypeError('organizationId is required');
}

const organizationLearnerIds = await usecases.findOrganizationLearnersBeforeImportFeature({ organizationId });

return usecases.deleteOrganizationLearners({ userId, organizationId, organizationLearnerIds });
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { hasBeenLearner } from '../../../../../../src/prescription/learner-management/application/api/learners-api.js';
import {
deleteOrganizationLearnerBeforeImportFeature,
hasBeenLearner,
} from '../../../../../../src/prescription/learner-management/application/api/learners-api.js';
import { usecases } from '../../../../../../src/prescription/learner-management/domain/usecases/index.js';
import { catchErr, expect, sinon } from '../../../../../test-helper.js';

Expand Down Expand Up @@ -29,4 +32,58 @@ describe('Unit | Prescription | learner management | Api | learners', function (
expect(hasBeenLearnerStub).to.have.been.calledOnceWith({ userId });
});
});

describe('#deleteOrganizationLearnerBeforeImportFeature', function () {
let findOrganizationLearnersBeforeImportFeatureStub, deleteOrganizationLearnersStub;

beforeEach(function () {
findOrganizationLearnersBeforeImportFeatureStub = sinon
.stub(usecases, 'findOrganizationLearnersBeforeImportFeature')
.rejects();
deleteOrganizationLearnersStub = sinon.stub(usecases, 'deleteOrganizationLearners').rejects();
});

it('should throw a "TypeError" when "userId" is not defined', async function () {
// when
const error = await catchErr(deleteOrganizationLearnerBeforeImportFeature)({
userId: undefined,
organizationId: Symbol('organizationId'),
});

// then
expect(error).to.be.instanceOf(TypeError);
expect(findOrganizationLearnersBeforeImportFeatureStub).to.not.have.been.called;
expect(deleteOrganizationLearnersStub).to.not.have.been.called;
});

it('should throw a "TypeError" when "organizationId" is not defined', async function () {
// when
const error = await catchErr(deleteOrganizationLearnerBeforeImportFeature)({
userId: Symbol('userId'),
organizationId: undefined,
});

// then
expect(error).to.be.instanceOf(TypeError);
expect(findOrganizationLearnersBeforeImportFeatureStub).to.not.have.been.called;
expect(deleteOrganizationLearnersStub).to.not.have.been.called;
});

it('should call usecase given parameter', async function () {
// given
const userId = Symbol('userId');
const organizationId = Symbol('organizationId');
const organizationLearnerIds = Symbol('organizationLearnerIds');

findOrganizationLearnersBeforeImportFeatureStub.withArgs({ organizationId }).resolves(organizationLearnerIds);
deleteOrganizationLearnersStub.withArgs({ userId, organizationId, organizationLearnerIds }).resolves();

// when
await deleteOrganizationLearnerBeforeImportFeature({ userId, organizationId });

// then
expect(findOrganizationLearnersBeforeImportFeatureStub).to.have.been.calledOnce;
expect(deleteOrganizationLearnersStub).to.have.been.calledOnce;
});
});
});

0 comments on commit ed758df

Please sign in to comment.