Skip to content

Commit

Permalink
handle checks for not outdated badge acquisitions
Browse files Browse the repository at this point in the history
  • Loading branch information
laura-bergoens committed Sep 12, 2024
1 parent 20152eb commit 608f02a
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,55 @@
import { UserCertificationEligibility } from '../read-models/UserCertificationEligibility.js';
import { CertificationEligibility, UserCertificationEligibility } from '../read-models/UserCertificationEligibility.js';

const getUserCertificationEligibility = async function ({ userId, limitDate, placementProfileService }) {
const getUserCertificationEligibility = async function ({
userId,
limitDate,
placementProfileService,
certificationBadgesService,
complementaryCertificationCourseRepository,
}) {
const placementProfile = await placementProfileService.getPlacementProfile({ userId, limitDate });
const isCertifiable = placementProfile.isCertifiable();

const userAcquiredBadges = await certificationBadgesService.findLatestBadgeAcquisitions({
userId,
limitDate,
});
const certificationEligibilities = [];
const complementaryCertificationAcquiredByUser = await complementaryCertificationCourseRepository.findByUserId({
userId,
});
for (const acquiredBadge of userAcquiredBadges) {
if (!acquiredBadge.isOutdated) {
const isAcquiredExpectedLevel = _hasAcquiredComplementaryCertificationForExpectedLevel(
complementaryCertificationAcquiredByUser,
acquiredBadge,
);
certificationEligibilities.push(
new CertificationEligibility({
label: acquiredBadge.complementaryCertificationBadgeLabel,
imageUrl: acquiredBadge.complementaryCertificationBadgeImageUrl,
isOutdated: acquiredBadge.isOutdated,
isAcquiredExpectedLevel,
}),
);
}
}
return new UserCertificationEligibility({
id: userId,
isCertifiable: placementProfile.isCertifiable(),
certificationEligibilities: [],
isCertifiable,
certificationEligibilities,
});
};

function _hasAcquiredComplementaryCertificationForExpectedLevel(
complementaryCertificationAcquiredByUser,
acquiredBadge,
) {
return complementaryCertificationAcquiredByUser.some(
(certificationTakenByUser) =>
certificationTakenByUser.isAcquiredExpectedLevelByPixSource() &&
acquiredBadge.complementaryCertificationBadgeId === certificationTakenByUser.complementaryCertificationBadgeId,
);
}

export { getUserCertificationEligibility };
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
import { getUserCertificationEligibility } from '../../../../../../src/certification/enrolment/domain/usecases/get-user-certification-eligibility.js';
import { sources } from '../../../../../../src/certification/shared/domain/models/ComplementaryCertificationCourseResult.js';
import { domainBuilder, expect, sinon } from '../../../../../test-helper.js';

describe('Certification | Enrolment | Unit | Usecases | get-user-certification-eligibility', function () {
const userId = 123;
const limitDate = new Date('2024-09-06');
let dependencies;
const placementProfileService = {};
const certificationBadgesService = {};
const complementaryCertificationCourseRepository = {};

beforeEach(function () {
placementProfileService.getPlacementProfile = sinon.stub();
certificationBadgesService.findLatestBadgeAcquisitions = sinon.stub();
complementaryCertificationCourseRepository.findByUserId = sinon.stub();
dependencies = {
userId,
limitDate,
placementProfileService,
certificationBadgesService,
complementaryCertificationCourseRepository,
};
});

context('certificability', function () {
beforeEach(function () {
certificationBadgesService.findLatestBadgeAcquisitions.resolves([]);
complementaryCertificationCourseRepository.findByUserId.resolves([]);
});
context('when user is certifiable', function () {
it('returns a user certification eligibility with is certifiable set to true', async function () {
placementProfileService.getPlacementProfile.withArgs({ userId, limitDate }).resolves(
Expand Down Expand Up @@ -59,4 +70,148 @@ describe('Certification | Enrolment | Unit | Usecases | get-user-certification-e
});
});
});
context('eligibility', function () {
const isCertifiable = true;
beforeEach(function () {
placementProfileService.getPlacementProfile.withArgs({ userId, limitDate }).resolves(
domainBuilder.buildPlacementProfile.buildCertifiable({
profileDate: limitDate,
userId,
}),
);
});

context('when user has not acquired any certifiable badge', function () {
it('returns a UserCertificationEligibility model with no eligibilities ', async function () {
certificationBadgesService.findLatestBadgeAcquisitions
.withArgs({
userId,
limitDate,
})
.resolves([]);
complementaryCertificationCourseRepository.findByUserId.resolves([]);

const userCertificationEligibility = await getUserCertificationEligibility(dependencies);

expect(userCertificationEligibility).to.deep.equal(
domainBuilder.certification.enrolment.buildUserCertificationEligibility({
id: userId,
isCertifiable,
certificationEligibilities: [],
}),
);
});
});

context('when user has acquired a badge', function () {
context('when acquired badge is outdated', function () {
context('when user has an acquired certification for this badge', function () {
it('should not be added in the eligibilities of the model', async function () {});
});
context('when user has not an acquired certification for this badge', function () {
context('when badge is outdated by more than one version', function () {
it('should not be added in the eligibilities of the model', async function () {});
});
context('when badge is outdated by exactly one version', function () {
it('returns a UserCertificationEligibility model with the outdated eligibility inside', async function () {});
});
});
});
context('when acquired badge is not outdated', function () {
const complementaryCertificationBadgeId = 123;
context('when user has acquired a certification for this badge', function () {
it('returns a UserCertificationEligibility model with the corresponding eligibility', async function () {
complementaryCertificationCourseRepository.findByUserId.withArgs({ userId }).resolves([
domainBuilder.certification.enrolment.buildComplementaryCertificationCourseWithResults({
complementaryCertificationBadgeId,
results: [
{
source: sources.PIX,
acquired: true,
complementaryCertificationBadgeId,
},
],
}),
]);
certificationBadgesService.findLatestBadgeAcquisitions
.withArgs({
userId,
limitDate,
})
.resolves([
domainBuilder.buildCertifiableBadgeAcquisition({
complementaryCertificationBadgeId,
complementaryCertificationBadgeImageUrl: 'monImageUrl',
complementaryCertificationBadgeLabel: 'monLabel',
isOutdated: false,
}),
]);

const userCertificationEligibility = await getUserCertificationEligibility(dependencies);

expect(userCertificationEligibility).to.deep.equal(
domainBuilder.certification.enrolment.buildUserCertificationEligibility({
id: userId,
isCertifiable,
certificationEligibilities: [
domainBuilder.certification.enrolment.buildV3CertificationEligibility({
label: 'monLabel',
imageUrl: 'monImageUrl',
isOutdated: false,
isAcquiredExpectedLevel: true,
}),
],
}),
);
});
});
context('when user has not acquired a certification for this badge', function () {
it('returns a UserCertificationEligibility model with the corresponding eligibility', async function () {
complementaryCertificationCourseRepository.findByUserId.withArgs({ userId }).resolves([
domainBuilder.certification.enrolment.buildComplementaryCertificationCourseWithResults({
complementaryCertificationBadgeId,
results: [
{
source: sources.PIX,
acquired: true,
complementaryCertificationBadgeId: 'someOtherBadgeId',
},
],
}),
]);
certificationBadgesService.findLatestBadgeAcquisitions
.withArgs({
userId,
limitDate,
})
.resolves([
domainBuilder.buildCertifiableBadgeAcquisition({
complementaryCertificationBadgeId,
complementaryCertificationBadgeImageUrl: 'monImageUrl',
complementaryCertificationBadgeLabel: 'monLabel',
isOutdated: false,
}),
]);

const userCertificationEligibility = await getUserCertificationEligibility(dependencies);

expect(userCertificationEligibility).to.deep.equal(
domainBuilder.certification.enrolment.buildUserCertificationEligibility({
id: userId,
isCertifiable,
certificationEligibilities: [
domainBuilder.certification.enrolment.buildV3CertificationEligibility({
label: 'monLabel',
imageUrl: 'monImageUrl',
isOutdated: false,
isAcquiredExpectedLevel: false,
}),
],
}),
);
});
});
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { domainBuilder } from '../../../domain-builder.js';
const buildUserCertificationEligibility = function ({
id = 123,
isCertifiable = false,
certificationEligibilities = [domainBuilder.certification.enrolment.buildV3UserCertificationEligibility()],
certificationEligibilities = [domainBuilder.certification.enrolment.buildV3CertificationEligibility()],
} = {}) {
return new UserCertificationEligibility({
id,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CertificationEligibility } from '../../../../../../src/certification/enrolment/domain/read-models/UserCertificationEligibility.js';

const buildV3UserCertificationEligibility = function ({
const buildV3CertificationEligibility = function ({
label = "Label d'éligibilité",
imageUrl = "url d'image",
isOutdated = false,
Expand All @@ -14,4 +14,4 @@ const buildV3UserCertificationEligibility = function ({
});
};

export { buildV3UserCertificationEligibility };
export { buildV3CertificationEligibility };
2 changes: 2 additions & 0 deletions api/tests/tooling/domain-builder/factory/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ import {
} from './certification/enrolment/build-subscription.js';
import { buildUserEnrolment } from './certification/enrolment/build-user.js';
import { buildUserCertificationEligibility } from './certification/enrolment/build-user-certification-eligibility.js';
import { buildV3CertificationEligibility } from './certification/enrolment/build-v3-certification-eligibility.js';
import { buildFlashAssessmentAlgorithm } from './certification/flash-certification/build-flash-assessment-algorithm.js';
import { buildAssessmentResult as buildCertificationScoringAssessmentResult } from './certification/scoring/build-assessment-result.js';
import { buildCertificationAssessmentHistory } from './certification/scoring/build-certification-assessment-history.js';
Expand Down Expand Up @@ -223,6 +224,7 @@ const certification = {
buildCertificationEligibility: buildCertificationEligibilityEnrolment,
buildComplementaryCertificationCourseWithResults: buildComplementaryCertificationCourseWithResultsEnrolment,
buildUserCertificationEligibility,
buildV3CertificationEligibility,
},
sessionManagement: {
buildCertificationSessionComplementaryCertification,
Expand Down

0 comments on commit 608f02a

Please sign in to comment.