Skip to content

Commit

Permalink
Light refacto on get v2 user certif eligibility
Browse files Browse the repository at this point in the history
Co-authored-by: Guillaume Lagorce <guillaume.lagorce@pix.fr>
Co-authored-by: Alexandre Coin <alexandre.coin@pix.fr>
Co-authored-by: Jérémy Pluquet <jeremy.pluquet@pix.fr>
  • Loading branch information
4 people committed Sep 12, 2024
1 parent ba75223 commit 177d38d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ class ComplementaryCertificationCourseWithResults {
this.complementaryCertificationBadgeId = complementaryCertificationBadgeId;
}

isAcquiredByPixSource() {
return this.results.some(({ source, acquired }) => source === sources.PIX && acquired);
}

isAcquiredExpectedLevelByPixSource() {
return _.some(this.results, {
source: sources.PIX,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,18 @@ async function _getTargetProfileHistory({ targetProfileHistoryRepository, certif
return targetProfileHistory;
}

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

/**
* @param {Object} params
* @param {CertificationBadgesService} params.certificationBadgesService
Expand All @@ -206,18 +218,14 @@ async function _getComplementaryCertificationsEligibles({
userId,
});

const acquiredComplementaryCertifications = complementaryCertificationsTakenByUser.filter(
(complementaryCertification) => complementaryCertification.isAcquiredByPixSource(),
);

const complementaryCertificationsEligibles = [];
for (const stillValidBadgeAcquisition of stillValidBadgeAcquisitions) {
const stillValidBadgeAcquisitionComplementaryCertificationBadgeId =
stillValidBadgeAcquisition.complementaryCertificationBadgeId;

const stillValidAcquiredComplementaryCertification = acquiredComplementaryCertifications.find(
({ complementaryCertificationBadgeId }) =>
complementaryCertificationBadgeId === stillValidBadgeAcquisition.complementaryCertificationBadgeId,
const isAcquiredExpectedLevel = _hasAcquiredComplementaryCertificationForExpectedLevel(
complementaryCertificationsTakenByUser,
stillValidBadgeAcquisitionComplementaryCertificationBadgeId,
);

const complementaryCertificationVersioning = await _getComplementaryCertificationVersioningForOutdatedBadge({
Expand All @@ -230,16 +238,12 @@ async function _getComplementaryCertificationsEligibles({
_isAcquiredByPixSourceOrOutdatedByMoreThanOneVersion({
stillValidBadgeAcquisitionComplementaryCertificationBadgeId,
complementaryCertificationVersioning,
stillValidAcquiredComplementaryCertification,
isAcquiredExpectedLevel,
})
) {
continue;
}

const isAcquiredExpectedLevel = Boolean(
stillValidAcquiredComplementaryCertification?.isAcquiredExpectedLevelByPixSource(),
);

complementaryCertificationsEligibles.push({
label: stillValidBadgeAcquisition.complementaryCertificationBadgeLabel,
imageUrl: stillValidBadgeAcquisition.complementaryCertificationBadgeImageUrl,
Expand All @@ -253,7 +257,7 @@ async function _getComplementaryCertificationsEligibles({
function _isAcquiredByPixSourceOrOutdatedByMoreThanOneVersion({
stillValidBadgeAcquisitionComplementaryCertificationBadgeId,
complementaryCertificationVersioning,
stillValidAcquiredComplementaryCertification,
isAcquiredExpectedLevel,
}) {
return (
// TODO: Remove check on isPixPlusLowerLeverEnabled if enabled in PROD
Expand All @@ -262,6 +266,6 @@ function _isAcquiredByPixSourceOrOutdatedByMoreThanOneVersion({
stillValidBadgeAcquisitionComplementaryCertificationBadgeId,
outdatedVersions: complementaryCertificationVersioning,
})) ||
stillValidAcquiredComplementaryCertification?.isAcquiredExpectedLevelByPixSource()
isAcquiredExpectedLevel
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,6 @@ import { sources } from '../../../../../../src/certification/shared/domain/model
import { expect } from '../../../../../test-helper.js';

describe('Unit | Certification | Enrolment | Domain | Models | ComplementaryCertificationCourseWithResults', function () {
describe('#isAcquiredByPixSource', function () {
describe('when the certification is acquired by PIX', function () {
it('should return true', function () {
// given
const complementaryCertificationCourseWithResults = new ComplementaryCertificationCourseWithResults({
complementaryCertificationBadgeId: 4,
results: [{ complementaryCertificationBadgeId: 3, source: sources.PIX, acquired: true }],
});

// when
const result = complementaryCertificationCourseWithResults.isAcquiredByPixSource();

// then
expect(result).to.be.true;
});
});

describe('when the certification is not acquired by PIX', function () {
it('should return false', function () {
// given
const complementaryCertificationCourseWithResults = new ComplementaryCertificationCourseWithResults({
complementaryCertificationBadgeId: 4,
results: [{ complementaryCertificationBadgeId: 3, source: sources.PIX, acquired: false }],
});

// when
const result = complementaryCertificationCourseWithResults.isAcquiredByPixSource();

// then
expect(result).to.be.false;
});
});
});

describe('#isAcquiredExpectedLevelByPixSource', function () {
context(' on expected level', function () {
describe('when the certification is acquired by PIX', function () {
Expand Down

0 comments on commit 177d38d

Please sign in to comment.