From f6ae3bbb231d236cba20a158795d0aa19a00cade Mon Sep 17 00:00:00 2001 From: AndreiaPena Date: Fri, 15 Nov 2024 09:43:27 +0100 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=94=A5=20api:=20remove=20usecase?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../get-V2-user-certification-eligibility.js | 267 -------- ...-V2-user-certification-eligibility_test.js | 587 ------------------ 2 files changed, 854 deletions(-) delete mode 100644 api/src/certification/enrolment/domain/usecases/get-V2-user-certification-eligibility.js delete mode 100644 api/tests/certification/enrolment/unit/domain/usecases/get-V2-user-certification-eligibility_test.js diff --git a/api/src/certification/enrolment/domain/usecases/get-V2-user-certification-eligibility.js b/api/src/certification/enrolment/domain/usecases/get-V2-user-certification-eligibility.js deleted file mode 100644 index 2ba5dd42636..00000000000 --- a/api/src/certification/enrolment/domain/usecases/get-V2-user-certification-eligibility.js +++ /dev/null @@ -1,267 +0,0 @@ -/** - * @typedef {import ('./index.js').CertificationBadgesService} CertificationBadgesService - * @typedef {import ('./index.js').ComplementaryCertificationCourseRepository} ComplementaryCertificationCourseRepository - * @typedef {import ('./index.js').ComplementaryCertificationRepository} ComplementaryCertificationRepository - * @typedef {import ('./index.js').PlacementProfileService} PlacementProfileService - * @typedef {import ('../models/CertifiableBadgeAcquisition.js').CertifiableBadgeAcquisition} CertifiableBadgeAcquisition - * @typedef {import ('../../../src/certification/complementary-certification/domain/read-models/ComplementaryCertificationVersioning.js').ComplementaryCertificationVersioning} ComplementaryCertificationVersioning - */ - -/** - * @typedef {Object} ComplementaryCertificationsEligibles - * @property {string} label - * @property {string} imageUrl - * @property {boolean} isOutdated - * @property {boolean} isAcquired - */ - -import _ from 'lodash'; - -import { ComplementaryCertificationBadgesHistory } from '../../../complementary-certification/application/api/models/ComplementaryCertificationBadgesHistory.js'; -import { - ComplementaryCertificationBadge, - ComplementaryCertificationVersioning, -} from '../../../complementary-certification/domain/read-models/ComplementaryCertificationVersioning.js'; -import { CertificationEligibility } from '../read-models/CertificationEligibility.js'; - -/** - * @param {Object} params - * @param {CertificationBadgesService} params.certificationBadgesService - * @param {ComplementaryCertificationCourseRepository} params.complementaryCertificationCourseRepository - * @param {TargetProfileHistoryRepository} params.targetProfileHistoryRepository - * @param {PlacementProfileService} params.placementProfileService - * - * @returns {CertificationEligibility} - */ -const getV2UserCertificationEligibility = async function ({ - userId, - limitDate = new Date(), - placementProfileService, - certificationBadgesService, - complementaryCertificationCourseRepository, - targetProfileHistoryRepository, -}) { - const placementProfile = await placementProfileService.getPlacementProfile({ userId, limitDate }); - const pixCertificationEligible = placementProfile.isCertifiable(); - - if (!pixCertificationEligible) { - return CertificationEligibility.notCertifiable({ userId }); - } - - const complementaryCertificationsEligibles = await _getComplementaryCertificationsEligibles({ - certificationBadgesService, - userId, - limitDate, - complementaryCertificationCourseRepository, - targetProfileHistoryRepository, - }); - - return new CertificationEligibility({ - id: userId, - pixCertificationEligible, - complementaryCertifications: complementaryCertificationsEligibles, - }); -}; - -export { getV2UserCertificationEligibility }; - -/** - * @param {Object} params - * @param {CertifiableBadgeAcquisition} params.certifiableBadgeAcquisition - * @param {TargetProfileHistoryRepository} params.targetProfileHistoryRepository - * @returns {Array} - complementaryCertificationBadgeIds - */ -const _getComplementaryCertificationVersioningForOutdatedBadge = async ({ - certifiableBadgeAcquisition, - targetProfileHistoryRepository, -}) => { - if (certifiableBadgeAcquisition.isOutdated) { - const targetProfileHistory = await _getTargetProfileHistory({ - targetProfileHistoryRepository, - certifiableBadgeAcquisition, - }); - - const complementaryCertificationBadgesHistories = _getComplementaryCertificationBadgesHistories({ - targetProfileHistory, - }); - - const complementaryVersioning = _toComplementaryCertificationVersioning({ - complementaryCertificationId: certifiableBadgeAcquisition.complementaryCertificationId, - complementaryCertificationBadgesHistories, - }); - - return _.chain(complementaryVersioning.complementaryCertificationBadges) - .filter(({ deactivationDate }) => !!deactivationDate) - .groupBy('deactivationDate') - .values() - .value(); - } - - return []; -}; - -/** - * @param {Object} params - * @param {number} params.stillValidBadgeAcquisitionComplementaryCertificationBadgeId - * @param {Array} params.outdatedVersions - * @returns {Boolean} - */ -const _isOutdatedBadgeAcquisitionByMoreThanOneVersion = ({ - stillValidBadgeAcquisitionComplementaryCertificationBadgeId, - outdatedVersions, -}) => { - if (!outdatedVersions.length) { - return false; - } - const lastOutedVersion = outdatedVersions[0]; - const isBadgeAcquisitionFoundInPreviousVersion = lastOutedVersion.some( - ({ id }) => id === stillValidBadgeAcquisitionComplementaryCertificationBadgeId, - ); - return outdatedVersions.length > 1 && !isBadgeAcquisitionFoundInPreviousVersion; -}; - -/** - * @param {Object} params - * @param {number} params.complementaryCertificationId - * @param {Array} params.complementaryCertificationBadgesHistories - * @returns {ComplementaryCertificationVersioning} - */ -const _toComplementaryCertificationVersioning = ({ - complementaryCertificationId, - complementaryCertificationBadgesHistories, -}) => { - const complementaryCertificationBadges = complementaryCertificationBadgesHistories.flatMap( - (complementaryCertificationBadgesHistory) => { - return complementaryCertificationBadgesHistory.complementaryCertificationBadgeIds.map( - (complementaryCertificationBadgeId) => { - return new ComplementaryCertificationBadge({ - ...complementaryCertificationBadgesHistory, - id: complementaryCertificationBadgeId, - }); - }, - ); - }, - ); - - return new ComplementaryCertificationVersioning({ complementaryCertificationId, complementaryCertificationBadges }); -}; - -/** - * @param {Object} params - * @param {Array} params.targetProfileHistory - * @returns {ComplementaryCertificationBadgesHistory} - */ -function _getComplementaryCertificationBadgesHistories({ targetProfileHistory }) { - return targetProfileHistory.map((complementaryCertificationBadge) => { - const complementaryCertificationBadgeIds = complementaryCertificationBadge.badges.map( - ({ complementaryCertificationBadgeId }) => complementaryCertificationBadgeId, - ); - - return new ComplementaryCertificationBadgesHistory({ - complementaryCertificationBadgeIds, - activationDate: complementaryCertificationBadge.attachedAt, - deactivationDate: complementaryCertificationBadge.detachedAt, - }); - }); -} - -async function _getTargetProfileHistory({ targetProfileHistoryRepository, certifiableBadgeAcquisition }) { - const currentTargetProfilesHistoryWithBadges = - await targetProfileHistoryRepository.getCurrentTargetProfilesHistoryWithBadgesByComplementaryCertificationId({ - complementaryCertificationId: certifiableBadgeAcquisition.complementaryCertificationId, - }); - - const detachedTargetProfilesHistory = - await targetProfileHistoryRepository.getDetachedTargetProfilesHistoryByComplementaryCertificationId({ - complementaryCertificationId: certifiableBadgeAcquisition.complementaryCertificationId, - }); - - const targetProfileHistory = [...currentTargetProfilesHistoryWithBadges, ...detachedTargetProfilesHistory]; - return targetProfileHistory; -} - -function _hasAcquiredComplementaryCertificationForExpectedLevel( - complementaryCertificationsTakenByUser, - stillValidBadgeAcquisitionComplementaryCertificationBadgeId, -) { - return complementaryCertificationsTakenByUser.some( - (certificationTakenByUser) => - certificationTakenByUser.isAcquiredExpectedLevelByPixSource() && - stillValidBadgeAcquisitionComplementaryCertificationBadgeId === - certificationTakenByUser.complementaryCertificationBadgeId, - ); -} - -/** - * @param {Object} params - * @param {CertificationBadgesService} params.certificationBadgesService - * @param {number} params.userId - * @param {date} params.limitDate - * @param {complementaryCertificationCourseRepository} params.complementaryCertificationCourseRepository - * @param {TargetProfileHistoryRepository} params.targetProfileHistoryRepository - * @returns {Array} - */ -async function _getComplementaryCertificationsEligibles({ - certificationBadgesService, - userId, - limitDate, - complementaryCertificationCourseRepository, - targetProfileHistoryRepository, -}) { - const stillValidBadgeAcquisitions = await certificationBadgesService.findLatestBadgeAcquisitions({ - userId, - limitDate, - }); - - const complementaryCertificationsTakenByUser = await complementaryCertificationCourseRepository.findByUserId({ - userId, - }); - - const complementaryCertificationsEligibles = []; - for (const stillValidBadgeAcquisition of stillValidBadgeAcquisitions) { - const stillValidBadgeAcquisitionComplementaryCertificationBadgeId = - stillValidBadgeAcquisition.complementaryCertificationBadgeId; - - const isAcquiredExpectedLevel = _hasAcquiredComplementaryCertificationForExpectedLevel( - complementaryCertificationsTakenByUser, - stillValidBadgeAcquisitionComplementaryCertificationBadgeId, - ); - - const complementaryCertificationVersioning = await _getComplementaryCertificationVersioningForOutdatedBadge({ - certifiableBadgeAcquisition: stillValidBadgeAcquisition, - targetProfileHistoryRepository, - }); - - if ( - stillValidBadgeAcquisition.isOutdated && - _isAcquiredByPixSourceOrOutdatedByMoreThanOneVersion({ - stillValidBadgeAcquisitionComplementaryCertificationBadgeId, - complementaryCertificationVersioning, - isAcquiredExpectedLevel, - }) - ) { - continue; - } - - complementaryCertificationsEligibles.push({ - label: stillValidBadgeAcquisition.complementaryCertificationBadgeLabel, - imageUrl: stillValidBadgeAcquisition.complementaryCertificationBadgeImageUrl, - isOutdated: stillValidBadgeAcquisition.isOutdated, - isAcquiredExpectedLevel, - }); - } - return complementaryCertificationsEligibles; -} - -function _isAcquiredByPixSourceOrOutdatedByMoreThanOneVersion({ - stillValidBadgeAcquisitionComplementaryCertificationBadgeId, - complementaryCertificationVersioning, - isAcquiredExpectedLevel, -}) { - return ( - _isOutdatedBadgeAcquisitionByMoreThanOneVersion({ - stillValidBadgeAcquisitionComplementaryCertificationBadgeId, - outdatedVersions: complementaryCertificationVersioning, - }) || isAcquiredExpectedLevel - ); -} diff --git a/api/tests/certification/enrolment/unit/domain/usecases/get-V2-user-certification-eligibility_test.js b/api/tests/certification/enrolment/unit/domain/usecases/get-V2-user-certification-eligibility_test.js deleted file mode 100644 index 07cd5307e3a..00000000000 --- a/api/tests/certification/enrolment/unit/domain/usecases/get-V2-user-certification-eligibility_test.js +++ /dev/null @@ -1,587 +0,0 @@ -import { getV2UserCertificationEligibility } from '../../../../../../src/certification/enrolment/domain/usecases/get-V2-user-certification-eligibility.js'; -import { domainBuilder, expect, sinon } from '../../../../../test-helper.js'; - -describe('Unit | UseCase | get-v2-user-certification-eligibility', function () { - let clock, - placementProfileService, - certificationBadgesService, - complementaryCertificationCourseRepository, - targetProfileHistoryRepository; - const now = new Date(2020, 1, 1); - - beforeEach(function () { - clock = sinon.useFakeTimers({ now, toFake: ['Date'] }); - complementaryCertificationCourseRepository = { - findByUserId: sinon.stub(), - }; - targetProfileHistoryRepository = { - getCurrentTargetProfilesHistoryWithBadgesByComplementaryCertificationId: sinon.stub(), - getDetachedTargetProfilesHistoryByComplementaryCertificationId: sinon.stub(), - }; - certificationBadgesService = { - findLatestBadgeAcquisitions: sinon.stub(), - }; - placementProfileService = { - getPlacementProfile: sinon.stub(), - }; - }); - - afterEach(function () { - clock.restore(); - }); - - context('when pix certification is not eligible', function () { - it('should return the user certification eligibility without eligible complementary certifications', async function () { - // given - const placementProfile = { isCertifiable: () => false }; - placementProfileService.getPlacementProfile.withArgs({ userId: 2, limitDate: now }).resolves(placementProfile); - certificationBadgesService.findLatestBadgeAcquisitions.throws(new Error('I should not be called')); - - // when - const certificationEligibility = await getV2UserCertificationEligibility({ - userId: 2, - placementProfileService, - certificationBadgesService, - targetProfileHistoryRepository, - }); - - // then - const expectedCertificationEligibility = domainBuilder.certification.enrolment.buildCertificationEligibility({ - id: 2, - pixCertificationEligible: false, - }); - expect(certificationEligibility).to.deep.equal(expectedCertificationEligibility); - }); - }); - - context(`when badge is not acquired`, function () { - it('should return the user certification eligibility without eligible badge', async function () { - // given - const placementProfile = { isCertifiable: () => true }; - placementProfileService.getPlacementProfile.withArgs({ userId: 2, limitDate: now }).resolves(placementProfile); - certificationBadgesService.findLatestBadgeAcquisitions.resolves([]); - complementaryCertificationCourseRepository.findByUserId.resolves([]); - - // when - const certificationEligibility = await getV2UserCertificationEligibility({ - userId: 2, - placementProfileService, - certificationBadgesService, - complementaryCertificationCourseRepository, - targetProfileHistoryRepository, - }); - - // then - expect(certificationEligibility.complementaryCertifications).to.be.empty; - }); - }); - - context('when badge is acquired', function () { - context('when the complementary certification is not acquired', function () { - it('should return the user certification eligibility with the acquired badge information', async function () { - // given - const placementProfile = { isCertifiable: () => true }; - placementProfileService.getPlacementProfile.withArgs({ userId: 2, limitDate: now }).resolves(placementProfile); - const badgeAcquisition = _getOutdatedBadgeAcquisition({ complementaryCertificationBadgeId: 34 }); - const detachedTargetProfileHistory = domainBuilder.buildTargetProfileHistoryForAdmin({ - badges: [ - domainBuilder.buildComplementaryCertificationBadgeForAdmin({ - id: 3, - complementaryCertificationBadgeId: 34, - }), - ], - attachedAt: new Date('2021-01-01'), - detachedAt: new Date('2022-01-01'), - }); - const attachedTargetProfileHistory = domainBuilder.buildTargetProfileHistoryForAdmin({ - detachedAt: null, - attachedAt: new Date('2024-01-01'), - badges: [ - domainBuilder.buildComplementaryCertificationBadgeForAdmin({ - id: 3, - complementaryCertificationBadgeId: 35, - }), - ], - }); - certificationBadgesService.findLatestBadgeAcquisitions.resolves([badgeAcquisition]); - targetProfileHistoryRepository.getCurrentTargetProfilesHistoryWithBadgesByComplementaryCertificationId - .withArgs({ complementaryCertificationId: 1 }) - .resolves([attachedTargetProfileHistory]); - - targetProfileHistoryRepository.getDetachedTargetProfilesHistoryByComplementaryCertificationId - .withArgs({ complementaryCertificationId: 1 }) - .resolves([detachedTargetProfileHistory]); - - complementaryCertificationCourseRepository.findByUserId.resolves([ - domainBuilder.certification.enrolment.buildComplementaryCertificationCourseWithResults({ - id: 1, - hasExternalJury: false, - complementaryCertificationBadgeId: 34, - results: [ - { - id: 3, - acquired: false, - source: 'PIX', - }, - ], - }), - ]); - - // when - const certificationEligibility = await getV2UserCertificationEligibility({ - userId: 2, - placementProfileService, - certificationBadgesService, - complementaryCertificationCourseRepository, - targetProfileHistoryRepository, - }); - - // then - expect(certificationEligibility.complementaryCertifications).to.deep.equal([ - { - label: 'BADGE_LABEL', - imageUrl: 'http://www.image-url.com', - isOutdated: true, - isAcquiredExpectedLevel: false, - }, - ]); - }); - }); - - context('when the complementary certification is acquired', function () { - it('should return the user certification eligibility with an acquired badge', async function () { - // given - const placementProfile = { isCertifiable: () => true }; - placementProfileService.getPlacementProfile.withArgs({ userId: 2, limitDate: now }).resolves(placementProfile); - const badgeAcquisition = _getBadgeAcquisition({ complementaryCertificationBadgeId: 34 }); - certificationBadgesService.findLatestBadgeAcquisitions.resolves([badgeAcquisition]); - - complementaryCertificationCourseRepository.findByUserId.resolves([ - domainBuilder.certification.enrolment.buildComplementaryCertificationCourseWithResults({ - id: 1, - hasExternalJury: false, - complementaryCertificationBadgeId: 34, - results: [ - { - id: 3, - acquired: true, - source: 'PIX', - complementaryCertificationBadgeId: 34, - }, - ], - }), - ]); - - // when - const certificationEligibility = await getV2UserCertificationEligibility({ - userId: 2, - placementProfileService, - certificationBadgesService, - complementaryCertificationCourseRepository, - targetProfileHistoryRepository, - }); - - // then - expect(certificationEligibility.complementaryCertifications).to.deep.equal([ - { - label: 'BADGE_LABEL', - imageUrl: 'http://www.image-url.com', - isOutdated: false, - isAcquiredExpectedLevel: true, - }, - ]); - }); - - context('when the complementary certification badge has been deprecated', function () { - context('when the acquired complementary certification badge is lower level', function () { - context('when there is only one new version', function () { - it('should return the user certification eligibility with complementary certification', async function () { - // given - const detachedTargetProfileHistory = domainBuilder.buildTargetProfileHistoryForAdmin({ - badges: [ - domainBuilder.buildComplementaryCertificationBadgeForAdmin({ - id: 3, - complementaryCertificationBadgeId: 34, - }), - ], - attachedAt: new Date('2021-01-01'), - detachedAt: new Date('2022-01-01'), - }); - const attachedTargetProfileHistory = domainBuilder.buildTargetProfileHistoryForAdmin({ - detachedAt: null, - attachedAt: new Date('2024-01-01'), - badges: [ - domainBuilder.buildComplementaryCertificationBadgeForAdmin({ - id: 3, - complementaryCertificationBadgeId: 35, - }), - domainBuilder.buildComplementaryCertificationBadgeForAdmin({ - id: 3, - complementaryCertificationBadgeId: 36, - }), - ], - }); - targetProfileHistoryRepository.getCurrentTargetProfilesHistoryWithBadgesByComplementaryCertificationId - .withArgs({ complementaryCertificationId: 1 }) - .resolves([attachedTargetProfileHistory]); - - targetProfileHistoryRepository.getDetachedTargetProfilesHistoryByComplementaryCertificationId - .withArgs({ complementaryCertificationId: 1 }) - .resolves([detachedTargetProfileHistory]); - - const placementProfile = { isCertifiable: () => true }; - placementProfileService.getPlacementProfile - .withArgs({ userId: 2, limitDate: now }) - .resolves(placementProfile); - const badgeAcquisition = _getOutdatedBadgeAcquisition({ complementaryCertificationBadgeId: 34 }); - certificationBadgesService.findLatestBadgeAcquisitions.resolves([badgeAcquisition]); - - complementaryCertificationCourseRepository.findByUserId.resolves([ - domainBuilder.certification.enrolment.buildComplementaryCertificationCourseWithResults({ - id: 1, - hasExternalJury: false, - complementaryCertificationBadgeId: 36, - results: [ - { - id: 3, - acquired: true, - source: 'PIX', - complementaryCertificationBadgeId: 35, - }, - ], - }), - ]); - - // when - const certificationEligibility = await getV2UserCertificationEligibility({ - userId: 2, - placementProfileService, - certificationBadgesService, - complementaryCertificationCourseRepository, - targetProfileHistoryRepository, - }); - - // then - expect(certificationEligibility.complementaryCertifications).to.deep.equal([ - { - label: 'BADGE_LABEL', - imageUrl: 'http://www.image-url.com', - isOutdated: true, - isAcquiredExpectedLevel: false, - }, - ]); - }); - }); - - context('when there is more than one new version', function () { - it('should return the user certification eligibility without complementary certification', async function () { - // given - const detachedTargetProfileHistory1 = domainBuilder.buildTargetProfileHistoryForAdmin({ - badges: [ - domainBuilder.buildComplementaryCertificationBadgeForAdmin({ - id: 3, - complementaryCertificationBadgeId: 32, - }), - domainBuilder.buildComplementaryCertificationBadgeForAdmin({ - id: 3, - complementaryCertificationBadgeId: 33, - }), - ], - attachedAt: new Date('2020-01-01'), - detachedAt: new Date('2021-01-01'), - }); - const detachedTargetProfileHistory2 = domainBuilder.buildTargetProfileHistoryForAdmin({ - badges: [ - domainBuilder.buildComplementaryCertificationBadgeForAdmin({ - id: 3, - complementaryCertificationBadgeId: 34, - }), - ], - attachedAt: new Date('2021-01-01'), - detachedAt: new Date('2022-01-01'), - }); - const attachedTargetProfileHistory = domainBuilder.buildTargetProfileHistoryForAdmin({ - detachedAt: null, - attachedAt: new Date('2024-01-01'), - badges: [ - domainBuilder.buildComplementaryCertificationBadgeForAdmin({ - id: 3, - complementaryCertificationBadgeId: 35, - }), - ], - }); - - targetProfileHistoryRepository.getCurrentTargetProfilesHistoryWithBadgesByComplementaryCertificationId - .withArgs({ complementaryCertificationId: 1 }) - .resolves([attachedTargetProfileHistory]); - - targetProfileHistoryRepository.getDetachedTargetProfilesHistoryByComplementaryCertificationId - .withArgs({ complementaryCertificationId: 1 }) - .resolves([detachedTargetProfileHistory2, detachedTargetProfileHistory1]); - - const placementProfile = { isCertifiable: () => true }; - placementProfileService.getPlacementProfile - .withArgs({ userId: 2, limitDate: now }) - .resolves(placementProfile); - - const badgeAcquisition = _getOutdatedBadgeAcquisition({ complementaryCertificationBadgeId: 33 }); - - certificationBadgesService.findLatestBadgeAcquisitions.resolves([badgeAcquisition]); - - complementaryCertificationCourseRepository.findByUserId.resolves([ - domainBuilder.certification.enrolment.buildComplementaryCertificationCourseWithResults({ - id: 1, - hasExternalJury: false, - complementaryCertificationBadgeId: 33, - results: [ - { - id: 3, - acquired: true, - source: 'PIX', - complementaryCertificationBadgeId: 32, - }, - ], - }), - ]); - - // when - const certificationEligibility = await getV2UserCertificationEligibility({ - userId: 2, - placementProfileService, - certificationBadgesService, - complementaryCertificationCourseRepository, - targetProfileHistoryRepository, - }); - - // then - expect(certificationEligibility.complementaryCertifications).to.deep.equal([]); - }); - }); - }); - - context('when the acquired complementary certification badge is current level', function () { - it('should return the user certification eligibility without complementary certification', async function () { - // given - const attachedTargetProfileHistory = domainBuilder.buildTargetProfileHistoryForAdmin({ - detachedAt: null, - attachedAt: new Date('2024-01-01'), - badges: [ - domainBuilder.buildComplementaryCertificationBadgeForAdmin({ - id: 3, - complementaryCertificationBadgeId: 35, - }), - ], - }); - - const detachedTargetProfileHistory = domainBuilder.buildTargetProfileHistoryForAdmin({ - detachedAt: null, - attachedAt: new Date('2024-01-01'), - badges: [ - domainBuilder.buildComplementaryCertificationBadgeForAdmin({ - id: 3, - complementaryCertificationBadgeId: 34, - }), - ], - }); - - targetProfileHistoryRepository.getCurrentTargetProfilesHistoryWithBadgesByComplementaryCertificationId - .withArgs({ complementaryCertificationId: 1 }) - .resolves([attachedTargetProfileHistory]); - - targetProfileHistoryRepository.getDetachedTargetProfilesHistoryByComplementaryCertificationId - .withArgs({ complementaryCertificationId: 1 }) - .resolves([detachedTargetProfileHistory]); - - const placementProfile = { isCertifiable: () => true }; - placementProfileService.getPlacementProfile - .withArgs({ userId: 2, limitDate: now }) - .resolves(placementProfile); - - const badgeAcquisition = _getOutdatedBadgeAcquisition({ complementaryCertificationBadgeId: 35 }); - - certificationBadgesService.findLatestBadgeAcquisitions.resolves([badgeAcquisition]); - - complementaryCertificationCourseRepository.findByUserId.resolves([ - domainBuilder.certification.enrolment.buildComplementaryCertificationCourseWithResults({ - id: 1, - hasExternalJury: false, - complementaryCertificationBadgeId: 35, - results: [ - { - id: 3, - acquired: true, - source: 'PIX', - complementaryCertificationBadgeId: 35, - }, - ], - }), - ]); - - // when - const certificationEligibility = await getV2UserCertificationEligibility({ - userId: 2, - placementProfileService, - certificationBadgesService, - complementaryCertificationCourseRepository, - targetProfileHistoryRepository, - }); - - // then - expect(certificationEligibility.complementaryCertifications).to.deep.equal([]); - }); - }); - }); - }); - - context('when the complementary certification has not been taken', function () { - it('should return the user certification eligibility with the acquired badge information', async function () { - // given - const attachedTargetProfileHistory = domainBuilder.buildTargetProfileHistoryForAdmin({ - detachedAt: null, - attachedAt: new Date('2024-01-01'), - badges: [ - domainBuilder.buildComplementaryCertificationBadgeForAdmin({ - id: 3, - complementaryCertificationBadgeId: 35, - }), - ], - }); - - targetProfileHistoryRepository.getCurrentTargetProfilesHistoryWithBadgesByComplementaryCertificationId - .withArgs({ complementaryCertificationId: 1 }) - .resolves([attachedTargetProfileHistory]); - - targetProfileHistoryRepository.getDetachedTargetProfilesHistoryByComplementaryCertificationId - .withArgs({ complementaryCertificationId: 1 }) - .resolves([]); - const placementProfile = { isCertifiable: () => true }; - placementProfileService.getPlacementProfile.withArgs({ userId: 2, limitDate: now }).resolves(placementProfile); - const badgeAcquisition = _getBadgeAcquisition({}); - certificationBadgesService.findLatestBadgeAcquisitions.resolves([badgeAcquisition]); - - complementaryCertificationCourseRepository.findByUserId.resolves([ - domainBuilder.certification.enrolment.buildComplementaryCertificationCourseWithResults({ - id: 1, - hasExternalJury: false, - complementaryCertificationBadgeId: 2, - results: [], - }), - ]); - - // when - const certificationEligibility = await getV2UserCertificationEligibility({ - userId: 2, - placementProfileService, - certificationBadgesService, - complementaryCertificationCourseRepository, - targetProfileHistoryRepository, - }); - - // then - expect(certificationEligibility.complementaryCertifications).to.deep.equal([ - { - label: 'BADGE_LABEL', - imageUrl: 'http://www.image-url.com', - isOutdated: false, - isAcquiredExpectedLevel: false, - }, - ]); - }); - }); - - context('when the complementary certification has an external', function () { - context('when the pix jury is acquired', function () { - context('when the external jury is not acquired', function () { - it('should return the user certification eligibility with acquired badge', async function () { - // given - const attachedTargetProfileHistory = domainBuilder.buildTargetProfileHistoryForAdmin({ - detachedAt: null, - attachedAt: new Date('2024-01-01'), - badges: [ - domainBuilder.buildComplementaryCertificationBadgeForAdmin({ - id: 3, - complementaryCertificationBadgeId: 35, - }), - ], - }); - - targetProfileHistoryRepository.getCurrentTargetProfilesHistoryWithBadgesByComplementaryCertificationId - .withArgs({ complementaryCertificationId: 1 }) - .resolves([attachedTargetProfileHistory]); - - targetProfileHistoryRepository.getDetachedTargetProfilesHistoryByComplementaryCertificationId - .withArgs({ complementaryCertificationId: 1 }) - .resolves([]); - - const placementProfile = { isCertifiable: () => true }; - placementProfileService.getPlacementProfile - .withArgs({ userId: 2, limitDate: now }) - .resolves(placementProfile); - const badgeAcquisition = _getBadgeAcquisition({ complementaryCertificationBadgeId: 33 }); - certificationBadgesService.findLatestBadgeAcquisitions.resolves([badgeAcquisition]); - - complementaryCertificationCourseRepository.findByUserId.resolves([ - domainBuilder.certification.enrolment.buildComplementaryCertificationCourseWithResults({ - id: 1, - hasExternalJury: true, - complementaryCertificationBadgeId: 33, - results: [ - { - id: 3, - acquired: true, - source: 'PIX', - complementaryCertificationBadgeId: 33, - }, - { - id: 4, - acquired: false, - source: 'EXTERNAL', - complementaryCertificationBadgeId: 33, - }, - ], - }), - ]); - - // when - const certificationEligibility = await getV2UserCertificationEligibility({ - userId: 2, - placementProfileService, - certificationBadgesService, - complementaryCertificationCourseRepository, - targetProfileHistoryRepository, - }); - - // then - expect(certificationEligibility.complementaryCertifications).to.deep.equals([ - { - label: 'BADGE_LABEL', - imageUrl: 'http://www.image-url.com', - isOutdated: false, - isAcquiredExpectedLevel: true, - }, - ]); - }); - }); - }); - }); - }); -}); - -function _getBadgeAcquisition({ - complementaryCertificationId = 1, - complementaryCertificationBadgeId = 2, - outdated = false, -}) { - return domainBuilder.buildCertifiableBadgeAcquisition({ - badgeKey: 'BADGE_KEY', - complementaryCertificationId, - complementaryCertificationBadgeId, - complementaryCertificationBadgeLabel: 'BADGE_LABEL', - complementaryCertificationBadgeImageUrl: 'http://www.image-url.com', - isOutdated: outdated, - }); -} - -function _getOutdatedBadgeAcquisition({ complementaryCertificationId = 1, complementaryCertificationBadgeId = 2 }) { - return _getBadgeAcquisition({ complementaryCertificationId, complementaryCertificationBadgeId, outdated: true }); -} From 150c6733dff08722f11ff6337a3ba7a359d09d90 Mon Sep 17 00:00:00 2001 From: AndreiaPena Date: Fri, 15 Nov 2024 09:43:45 +0100 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=94=A5=20api:=20remove=20unused=20scr?= =?UTF-8?q?ipt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../get-user-certification-eligibility.js | 89 -- ...get-user-certification-eligibility_test.js | 1420 ----------------- 2 files changed, 1509 deletions(-) delete mode 100644 api/scripts/certification/get-user-certification-eligibility.js delete mode 100644 api/tests/certification/enrolment/unit/domain/usecases/get-user-certification-eligibility_test.js diff --git a/api/scripts/certification/get-user-certification-eligibility.js b/api/scripts/certification/get-user-certification-eligibility.js deleted file mode 100644 index 67a63b11075..00000000000 --- a/api/scripts/certification/get-user-certification-eligibility.js +++ /dev/null @@ -1,89 +0,0 @@ -import * as url from 'node:url'; - -const __dirname = url.fileURLToPath(new URL('.', import.meta.url)); -import * as dotenv from 'dotenv'; - -dotenv.config({ path: `${__dirname}/../.env` }); - -import { disconnect } from '../../db/knex-database-connection.js'; -import { usecases } from '../../lib/domain/usecases/index.js'; -import * as certificationBadgesService from '../../src/certification/shared/domain/services/certification-badges-service.js'; -import * as placementProfileService from '../../src/shared/domain/services/placement-profile-service.js'; -import { learningContentCache as cache } from '../../src/shared/infrastructure/caches/learning-content-cache.js'; -import { temporaryStorage } from '../../src/shared/infrastructure/temporary-storage/index.js'; -import { logger } from '../../src/shared/infrastructure/utils/logger.js'; - -/** - * DESCRIPTION - * Will display certifications eligibility for a user at a specific date - * Note: by default date is full today's date at 23h 59m 59s - * - * USAGE - * $ node ./scripts/certification/get-user-certification-eligibility.js [] [HH:mm:ss] - * - * EXAMPLES: - * # Today at 23h59:59 - * $ node get-user-certification-eligibility.js 147114 - * - * # On 26/07/2023 at 23h 59m 59s - * $ node get-user-certification-eligibility.js 147114 2023-07-26 - * - * # On 22/01/2000 at 11h 52m 00s - * $ node get-user-certification-eligibility.js 147114 2000-01-22 11:52 - */ - -// TODO : add "outdated" info display (PIX-9023)) -async function getUserCertificationEligibility({ userId, limitDate }) { - logger.info('Starting script get-user-certification-eligibility'); - - const { pixCertificationEligible, complementaryCertifications } = await usecases.getV2UserCertificationEligibility({ - userId, - placementProfileService, - certificationBadgesService, - limitDate, - }); - - console.log(`--------------------------------------------------------------`); - console.log(`Eligibilité utilisateur ${userId} à ${limitDate.toISOString()}`); - console.log(`PIX: ${pixCertificationEligible ? '✅' : '❌'}`); - if (pixCertificationEligible) { - const complementaryCertificationsInfo = - complementaryCertifications - .map(({ label, isOutdated }) => { - const outdatedIcon = isOutdated ? ' 💀' : ''; - return `${label}${outdatedIcon}`; - }) - .join(', ') || '❌'; - console.log(`COMPLEMENTAIRES: ${complementaryCertificationsInfo}`); - } - console.log(`--------------------------------------------------------------`); -} - -const modulePath = url.fileURLToPath(import.meta.url); -const isLaunchedFromCommandLine = process.argv[1] === modulePath; - -async function main() { - const userId = process.argv[2]; - const limitDay = process.argv[3]; - const limitHours = process.argv[4] ?? '23:59:59'; - const limitDate = limitDay ? new Date(`${limitDay} ${limitHours}`) : new Date(); - - await getUserCertificationEligibility({ userId, limitDate }); -} - -(async () => { - if (isLaunchedFromCommandLine) { - try { - await main(); - } catch (error) { - logger.error(error); - process.exitCode = 1; - } finally { - await disconnect(); - await cache.quit(); - await temporaryStorage.quit(); - } - } -})(); - -export { getUserCertificationEligibility }; diff --git a/api/tests/certification/enrolment/unit/domain/usecases/get-user-certification-eligibility_test.js b/api/tests/certification/enrolment/unit/domain/usecases/get-user-certification-eligibility_test.js deleted file mode 100644 index 5780307c09a..00000000000 --- a/api/tests/certification/enrolment/unit/domain/usecases/get-user-certification-eligibility_test.js +++ /dev/null @@ -1,1420 +0,0 @@ -import { getUserCertificationEligibility } from '../../../../../../src/certification/enrolment/domain/usecases/get-user-certification-eligibility.js'; -import { sources } from '../../../../../../src/certification/shared/domain/models/ComplementaryCertificationCourseResult.js'; -import { ComplementaryCertificationKeys } from '../../../../../../src/certification/shared/domain/models/ComplementaryCertificationKeys.js'; -import { AssessmentResult } from '../../../../../../src/shared/domain/models/index.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 = {}; - const pixCertificationRepository = {}; - const complementaryCertificationBadgeWithOffsetVersionRepository = {}; - - beforeEach(function () { - placementProfileService.getPlacementProfile = sinon.stub(); - certificationBadgesService.findLatestBadgeAcquisitions = sinon.stub(); - complementaryCertificationCourseRepository.findByUserId = sinon.stub(); - pixCertificationRepository.findByUserId = sinon.stub(); - complementaryCertificationBadgeWithOffsetVersionRepository.getAllWithSameTargetProfile = sinon.stub(); - dependencies = { - userId, - limitDate, - placementProfileService, - certificationBadgesService, - complementaryCertificationCourseRepository, - pixCertificationRepository, - complementaryCertificationBadgeWithOffsetVersionRepository, - }; - }); - - context('certificability', function () { - beforeEach(function () { - certificationBadgesService.findLatestBadgeAcquisitions.resolves([]); - complementaryCertificationCourseRepository.findByUserId.resolves([]); - pixCertificationRepository.findByUserId.resolves([]); - complementaryCertificationBadgeWithOffsetVersionRepository.getAllWithSameTargetProfile.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( - domainBuilder.buildPlacementProfile.buildCertifiable({ - profileDate: limitDate, - userId, - }), - ); - - const userCertificationEligibility = await getUserCertificationEligibility(dependencies); - - expect(userCertificationEligibility).to.deep.equal( - domainBuilder.certification.enrolment.buildUserCertificationEligibility({ - id: userId, - isCertifiable: true, - certificationEligibilities: [], - }), - ); - }); - }); - context('when user is not certifiable', function () { - it('returns a user certification eligibility with is certifiable set to false', async function () { - placementProfileService.getPlacementProfile.withArgs({ userId, limitDate }).resolves( - domainBuilder.buildPlacementProfile({ - profileDate: limitDate, - userId, - userCompetences: [domainBuilder.buildUserCompetence({ estimatedLevel: 1, pixScore: 1 })], - }), - ); - - const userCertificationEligibility = await getUserCertificationEligibility(dependencies); - - expect(userCertificationEligibility).to.deep.equal( - domainBuilder.certification.enrolment.buildUserCertificationEligibility({ - id: userId, - isCertifiable: false, - certificationEligibilities: [], - }), - ); - }); - }); - }); - context('eligibility', function () { - const complementaryCertificationBadgeId = 123; - - context('when user has a badge acquisition', function () { - context('when user has the required pix score', function () { - it('returns that user is eligible for current level', async function () { - // given - placementProfileService.getPlacementProfile.withArgs({ userId, limitDate }).resolves( - domainBuilder.buildPlacementProfile.buildCertifiable({ - profileDate: limitDate, - userId, - }), - ); - - const complementaryBadgeAcquired = domainBuilder.buildCertifiableBadgeAcquisition(); - certificationBadgesService.findLatestBadgeAcquisitions - .withArgs({ - userId, - limitDate, - }) - .resolves([complementaryBadgeAcquired]); - - complementaryCertificationCourseRepository.findByUserId.resolves([]); - - const pixCertificationRequiredScore = 300; - pixCertificationRepository.findByUserId - .withArgs({ - userId, - }) - .resolves([ - domainBuilder.certification.enrolment.buildPixCertification({ - pixScore: pixCertificationRequiredScore, - status: AssessmentResult.status.VALIDATED, - isCancelled: false, - isRejectedForFraud: false, - }), - ]); - - const complementaryBadgeWithOffset = - domainBuilder.certification.enrolment.buildComplementaryCertificationBadge({ - id: complementaryBadgeAcquired.complementaryCertificationBadgeId, - requiredPixScore: pixCertificationRequiredScore, - level: 1, - offsetVersion: 0, - }); - complementaryCertificationBadgeWithOffsetVersionRepository.getAllWithSameTargetProfile - .withArgs({ - complementaryCertificationBadgeId: complementaryBadgeAcquired.complementaryCertificationBadgeId, - }) - .resolves([complementaryBadgeWithOffset]); - - // when - const userCertificationEligibility = await getUserCertificationEligibility(dependencies); - - // then - expect(userCertificationEligibility).to.deep.equal( - domainBuilder.certification.enrolment.buildUserCertificationEligibility({ - id: userId, - isCertifiable: true, - certificationEligibilities: [ - domainBuilder.certification.enrolment.buildV3CertificationEligibility({ - label: complementaryBadgeAcquired.complementaryCertificationBadgeLabel, - imageUrl: complementaryBadgeAcquired.complementaryCertificationBadgeImageUrl, - isAcquiredExpectedLevel: false, - isOutdated: complementaryBadgeAcquired.isOutdated, - }), - ], - }), - ); - }); - }); - - context('when user has not the required pix score', function () { - it('returns that user is not eligible for current level', async function () { - // given - placementProfileService.getPlacementProfile.withArgs({ userId, limitDate }).resolves( - domainBuilder.buildPlacementProfile.buildCertifiable({ - profileDate: limitDate, - userId, - }), - ); - - const complementaryBadgeAcquired = domainBuilder.buildCertifiableBadgeAcquisition(); - certificationBadgesService.findLatestBadgeAcquisitions - .withArgs({ - userId, - limitDate, - }) - .resolves([complementaryBadgeAcquired]); - - complementaryCertificationCourseRepository.findByUserId.resolves([]); - - const pixCertificationRequiredScore = 300; - pixCertificationRepository.findByUserId - .withArgs({ - userId, - }) - .resolves([ - domainBuilder.certification.enrolment.buildPixCertification({ - pixScore: pixCertificationRequiredScore - 1, - status: AssessmentResult.status.VALIDATED, - isCancelled: false, - isRejectedForFraud: false, - }), - ]); - - const complementaryBadgeWithOffset = - domainBuilder.certification.enrolment.buildComplementaryCertificationBadge({ - id: complementaryBadgeAcquired.complementaryCertificationBadgeId, - requiredPixScore: pixCertificationRequiredScore, - level: 1, - offsetVersion: 0, - }); - complementaryCertificationBadgeWithOffsetVersionRepository.getAllWithSameTargetProfile - .withArgs({ - complementaryCertificationBadgeId: complementaryBadgeAcquired.complementaryCertificationBadgeId, - }) - .resolves([complementaryBadgeWithOffset]); - - // when - const userCertificationEligibility = await getUserCertificationEligibility(dependencies); - - // then - expect(userCertificationEligibility).to.deep.equal( - domainBuilder.certification.enrolment.buildUserCertificationEligibility({ - id: userId, - isCertifiable: true, - certificationEligibilities: [], - }), - ); - }); - }); - - context('when there is a lower level', function () { - context('when the user has not the required pix score for the lower level', function () { - it('returns that user is not eligible for lower level', async function () { - // given - placementProfileService.getPlacementProfile.withArgs({ userId, limitDate }).resolves( - domainBuilder.buildPlacementProfile.buildCertifiable({ - profileDate: limitDate, - userId, - }), - ); - - const complementaryBadgeAcquired = domainBuilder.buildCertifiableBadgeAcquisition(); - certificationBadgesService.findLatestBadgeAcquisitions - .withArgs({ - userId, - limitDate, - }) - .resolves([complementaryBadgeAcquired]); - - complementaryCertificationCourseRepository.findByUserId.resolves([]); - - const pixCertificationRequiredScore = 300; - pixCertificationRepository.findByUserId - .withArgs({ - userId, - }) - .resolves([ - domainBuilder.certification.enrolment.buildPixCertification({ - pixScore: pixCertificationRequiredScore / 2 - 1, - status: AssessmentResult.status.VALIDATED, - isCancelled: false, - isRejectedForFraud: false, - }), - ]); - - const complementaryBadgesWithOffset = [ - domainBuilder.certification.enrolment.buildComplementaryCertificationBadge({ - id: 999, - requiredPixScore: pixCertificationRequiredScore / 2, - level: 1, - offsetVersion: 0, - }), - domainBuilder.certification.enrolment.buildComplementaryCertificationBadge({ - id: complementaryBadgeAcquired.complementaryCertificationBadgeId, - requiredPixScore: pixCertificationRequiredScore, - level: 2, - offsetVersion: 0, - }), - ]; - complementaryCertificationBadgeWithOffsetVersionRepository.getAllWithSameTargetProfile - .withArgs({ - complementaryCertificationBadgeId: complementaryBadgeAcquired.complementaryCertificationBadgeId, - }) - .resolves(complementaryBadgesWithOffset); - - // when - const userCertificationEligibility = await getUserCertificationEligibility(dependencies); - - // then - expect(userCertificationEligibility).to.deep.equal( - domainBuilder.certification.enrolment.buildUserCertificationEligibility({ - id: userId, - isCertifiable: true, - certificationEligibilities: [], - }), - ); - }); - }); - - context('when the user has the required pix score for the lower level', function () { - it('returns that user is eligible for lower level', async function () { - // given - placementProfileService.getPlacementProfile.withArgs({ userId, limitDate }).resolves( - domainBuilder.buildPlacementProfile.buildCertifiable({ - profileDate: limitDate, - userId, - }), - ); - - const complementaryBadgeAcquired = domainBuilder.buildCertifiableBadgeAcquisition(); - const lowerLevelComplementaryBadgeAcquired = domainBuilder.buildCertifiableBadgeAcquisition({ - badgeId: 999, - complementaryCertificationId: complementaryBadgeAcquired.complementaryCertificationId, - complementaryCertificationKey: complementaryBadgeAcquired.complementaryCertificationKey, - complementaryCertificationBadgeImageUrl: 'pix+.toto.initié.fr', - complementaryCertificationBadgeLabel: 'Pix+ Toto Initié', - isOutdated: false, - }); - certificationBadgesService.findLatestBadgeAcquisitions - .withArgs({ - userId, - limitDate, - }) - .resolves([complementaryBadgeAcquired]); - - complementaryCertificationCourseRepository.findByUserId.resolves([]); - - const pixCertificationRequiredScore = 300; - pixCertificationRepository.findByUserId - .withArgs({ - userId, - }) - .resolves([ - domainBuilder.certification.enrolment.buildPixCertification({ - pixScore: pixCertificationRequiredScore / 2 + 1, - status: AssessmentResult.status.VALIDATED, - isCancelled: false, - isRejectedForFraud: false, - }), - ]); - - const complementaryBadgesWithOffset = [ - domainBuilder.certification.enrolment.buildComplementaryCertificationBadge({ - id: 999, - requiredPixScore: pixCertificationRequiredScore / 2, - level: 1, - offsetVersion: 0, - label: 'Pix+ Toto Initié', - imageUrl: 'pix+.toto.initié.fr', - isOutdated: false, - }), - domainBuilder.certification.enrolment.buildComplementaryCertificationBadge({ - id: complementaryBadgeAcquired.complementaryCertificationBadgeId, - requiredPixScore: pixCertificationRequiredScore, - level: 2, - offsetVersion: 0, - label: 'Pix+ Toto Avancé', - imageUrl: 'pix+.toto.avancé.fr', - isOutdated: false, - }), - ]; - complementaryCertificationBadgeWithOffsetVersionRepository.getAllWithSameTargetProfile - .withArgs({ - complementaryCertificationBadgeId: complementaryBadgeAcquired.complementaryCertificationBadgeId, - }) - .resolves(complementaryBadgesWithOffset); - - // when - const userCertificationEligibility = await getUserCertificationEligibility(dependencies); - - // then - expect(userCertificationEligibility).to.deep.equal( - domainBuilder.certification.enrolment.buildUserCertificationEligibility({ - id: userId, - isCertifiable: true, - certificationEligibilities: [ - domainBuilder.certification.enrolment.buildV3CertificationEligibility({ - label: lowerLevelComplementaryBadgeAcquired.complementaryCertificationBadgeLabel, - imageUrl: lowerLevelComplementaryBadgeAcquired.complementaryCertificationBadgeImageUrl, - isAcquiredExpectedLevel: false, - isOutdated: lowerLevelComplementaryBadgeAcquired.isOutdated, - }), - ], - }), - ); - }); - }); - }); - }); - - context('when user has acquired a badge', function () { - let complementaryCertificationKey; - const requiredPixScore = 150; - context('CLEA', function () { - beforeEach(function () { - complementaryCertificationKey = ComplementaryCertificationKeys.CLEA; - }); - context('when acquired badge is outdated', function () { - const isOutdated = true; - beforeEach(function () { - complementaryCertificationBadgeWithOffsetVersionRepository.getAllWithSameTargetProfile.resolves([ - domainBuilder.certification.enrolment.buildComplementaryCertificationBadge({ - id: '1234', - requiredPixScore, - offsetVersion: 0, - }), - domainBuilder.certification.enrolment.buildComplementaryCertificationBadge({ - id: complementaryCertificationBadgeId, - requiredPixScore, - offsetVersion: 1, - }), - ]); - - pixCertificationRepository.findByUserId.withArgs({ userId }).resolves([ - domainBuilder.certification.enrolment.buildPixCertification({ - pixScore: requiredPixScore, - status: AssessmentResult.status.VALIDATED, - isCancelled: false, - isRejectedForFraud: false, - }), - ]); - }); - context('when user is certifiable', function () { - const isCertifiable = true; - beforeEach(function () { - placementProfileService.getPlacementProfile.withArgs({ userId, limitDate }).resolves( - domainBuilder.buildPlacementProfile.buildCertifiable({ - profileDate: limitDate, - userId, - }), - ); - }); - context('when user has an acquired certification for this badge', function () { - it('should not be added in the eligibilities of the model', async function () { - // given - 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, - complementaryCertificationKey, - complementaryCertificationBadgeImageUrl: 'monImageUrl', - complementaryCertificationBadgeLabel: 'monLabel', - isOutdated, - }), - ]); - - // when - const userCertificationEligibility = await getUserCertificationEligibility(dependencies); - - // then - expect(userCertificationEligibility).to.deep.equal( - domainBuilder.certification.enrolment.buildUserCertificationEligibility({ - id: userId, - isCertifiable, - certificationEligibilities: [], - }), - ); - }); - }); - context('when user has not an acquired certification for this badge', function () { - beforeEach(function () { - complementaryCertificationCourseRepository.findByUserId.withArgs({ userId }).resolves([]); - }); - - context('when badge is outdated by more than one version', function () { - beforeEach(function () { - complementaryCertificationBadgeWithOffsetVersionRepository.getAllWithSameTargetProfile.resolves([ - domainBuilder.certification.enrolment.buildComplementaryCertificationBadge({ - id: '1234', - requiredPixScore, - offsetVersion: 0, - }), - domainBuilder.certification.enrolment.buildComplementaryCertificationBadge({ - id: '5678', - requiredPixScore, - offsetVersion: 1, - }), - domainBuilder.certification.enrolment.buildComplementaryCertificationBadge({ - id: complementaryCertificationBadgeId, - requiredPixScore, - offsetVersion: 2, - }), - ]); - }); - it('should not be added in the eligibilities of the model', async function () { - // given - certificationBadgesService.findLatestBadgeAcquisitions - .withArgs({ - userId, - limitDate, - }) - .resolves([ - domainBuilder.buildCertifiableBadgeAcquisition({ - complementaryCertificationBadgeId, - complementaryCertificationKey, - complementaryCertificationBadgeImageUrl: 'monImageUrl', - complementaryCertificationBadgeLabel: 'monLabel', - isOutdated, - }), - ]); - - // when - const userCertificationEligibility = await getUserCertificationEligibility(dependencies); - - // then - expect(userCertificationEligibility).to.deep.equal( - domainBuilder.certification.enrolment.buildUserCertificationEligibility({ - id: userId, - isCertifiable, - certificationEligibilities: [], - }), - ); - }); - }); - context('when badge is outdated by exactly one version', function () { - const offsetVersion = 1; - it('returns a UserCertificationEligibility model with the outdated eligibility inside', async function () { - // given - certificationBadgesService.findLatestBadgeAcquisitions - .withArgs({ - userId, - limitDate, - }) - .resolves([ - domainBuilder.buildCertifiableBadgeAcquisition({ - complementaryCertificationBadgeId, - complementaryCertificationKey, - complementaryCertificationBadgeImageUrl: 'monImageUrl', - complementaryCertificationBadgeLabel: 'monLabel', - isOutdated, - offsetVersion, - }), - ]); - - // when - const userCertificationEligibility = await getUserCertificationEligibility(dependencies); - - // then - expect(userCertificationEligibility).to.deep.equal( - domainBuilder.certification.enrolment.buildUserCertificationEligibility({ - id: userId, - isCertifiable, - certificationEligibilities: [ - domainBuilder.certification.enrolment.buildV3CertificationEligibility({ - label: 'monLabel', - imageUrl: 'monImageUrl', - isAcquiredExpectedLevel: false, - isOutdated: true, - }), - ], - }), - ); - }); - }); - }); - }); - context('when user is not certifiable', function () { - const isCertifiable = false; - beforeEach(function () { - placementProfileService.getPlacementProfile.withArgs({ userId, limitDate }).resolves( - domainBuilder.buildPlacementProfile({ - profileDate: limitDate, - userId, - userCompetences: [domainBuilder.buildUserCompetence({ estimatedLevel: 1, pixScore: 1 })], - }), - ); - }); - context('when user has an acquired certification for this badge', function () { - it('should not be added in the eligibilities of the model', async function () { - // given - 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, - complementaryCertificationKey, - complementaryCertificationBadgeImageUrl: 'monImageUrl', - complementaryCertificationBadgeLabel: 'monLabel', - isOutdated: true, - offsetVersion: 1, - }), - ]); - - // when - const userCertificationEligibility = await getUserCertificationEligibility(dependencies); - - // then - expect(userCertificationEligibility).to.deep.equal( - domainBuilder.certification.enrolment.buildUserCertificationEligibility({ - id: userId, - isCertifiable, - certificationEligibilities: [], - }), - ); - }); - }); - context('when user has not an acquired certification for this badge', function () { - beforeEach(function () { - complementaryCertificationCourseRepository.findByUserId.withArgs({ userId }).resolves([]); - }); - - context('when badge is outdated by more than one version', function () { - const offsetVersion = 2; - it('should not be added in the eligibilities of the model', async function () { - // given - certificationBadgesService.findLatestBadgeAcquisitions - .withArgs({ - userId, - limitDate, - }) - .resolves([ - domainBuilder.buildCertifiableBadgeAcquisition({ - complementaryCertificationBadgeId, - complementaryCertificationKey, - complementaryCertificationBadgeImageUrl: 'monImageUrl', - complementaryCertificationBadgeLabel: 'monLabel', - isOutdated, - offsetVersion, - }), - ]); - - // when - const userCertificationEligibility = await getUserCertificationEligibility(dependencies); - - // then - expect(userCertificationEligibility).to.deep.equal( - domainBuilder.certification.enrolment.buildUserCertificationEligibility({ - id: userId, - isCertifiable, - certificationEligibilities: [], - }), - ); - }); - }); - context('when badge is outdated by exactly one version', function () { - const offsetVersion = 1; - it('should not be added in the eligibilities of the model', async function () { - // given - certificationBadgesService.findLatestBadgeAcquisitions - .withArgs({ - userId, - limitDate, - }) - .resolves([ - domainBuilder.buildCertifiableBadgeAcquisition({ - complementaryCertificationBadgeId, - complementaryCertificationKey, - complementaryCertificationBadgeImageUrl: 'monImageUrl', - complementaryCertificationBadgeLabel: 'monLabel', - isOutdated, - offsetVersion, - }), - ]); - - // when - const userCertificationEligibility = await getUserCertificationEligibility(dependencies); - - // then - expect(userCertificationEligibility).to.deep.equal( - domainBuilder.certification.enrolment.buildUserCertificationEligibility({ - id: userId, - isCertifiable, - certificationEligibilities: [], - }), - ); - }); - }); - }); - }); - }); - context('when acquired badge is not outdated', function () { - const isOutdated = false; - beforeEach(function () { - complementaryCertificationBadgeWithOffsetVersionRepository.getAllWithSameTargetProfile.resolves([ - domainBuilder.certification.enrolment.buildComplementaryCertificationBadge({ - id: complementaryCertificationBadgeId, - requiredPixScore, - offsetVersion: 0, - }), - ]); - }); - context('when user is certifiable', function () { - const isCertifiable = true; - beforeEach(function () { - placementProfileService.getPlacementProfile.withArgs({ userId, limitDate }).resolves( - domainBuilder.buildPlacementProfile.buildCertifiable({ - profileDate: limitDate, - userId, - }), - ); - - pixCertificationRepository.findByUserId.withArgs({ userId }).resolves([ - domainBuilder.certification.enrolment.buildPixCertification({ - pixScore: requiredPixScore, - status: AssessmentResult.status.VALIDATED, - isCancelled: false, - isRejectedForFraud: false, - }), - ]); - }); - - context('when user has an acquired certification for this badge', function () { - it('returns a UserCertificationEligibility model with the corresponding eligibility', async function () { - // given - 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, - complementaryCertificationKey, - complementaryCertificationBadgeImageUrl: 'monImageUrl', - complementaryCertificationBadgeLabel: 'monLabel', - isOutdated, - }), - ]); - - // when - const userCertificationEligibility = await getUserCertificationEligibility(dependencies); - - // then - expect(userCertificationEligibility).to.deep.equal( - domainBuilder.certification.enrolment.buildUserCertificationEligibility({ - id: userId, - isCertifiable, - certificationEligibilities: [ - domainBuilder.certification.enrolment.buildV3CertificationEligibility({ - label: 'monLabel', - imageUrl: 'monImageUrl', - isAcquiredExpectedLevel: true, - isOutdated, - }), - ], - }), - ); - }); - }); - context('when user has not an acquired certification for this badge', function () { - beforeEach(function () { - complementaryCertificationCourseRepository.findByUserId.withArgs({ userId }).resolves([]); - }); - it('returns a UserCertificationEligibility model with the corresponding eligibility', async function () { - // given - certificationBadgesService.findLatestBadgeAcquisitions - .withArgs({ - userId, - limitDate, - }) - .resolves([ - domainBuilder.buildCertifiableBadgeAcquisition({ - complementaryCertificationBadgeId, - complementaryCertificationKey, - complementaryCertificationBadgeImageUrl: 'monImageUrl', - complementaryCertificationBadgeLabel: 'monLabel', - isOutdated, - }), - ]); - - // when - const userCertificationEligibility = await getUserCertificationEligibility(dependencies); - - // then - expect(userCertificationEligibility).to.deep.equal( - domainBuilder.certification.enrolment.buildUserCertificationEligibility({ - id: userId, - isCertifiable, - certificationEligibilities: [ - domainBuilder.certification.enrolment.buildV3CertificationEligibility({ - label: 'monLabel', - imageUrl: 'monImageUrl', - isAcquiredExpectedLevel: false, - isOutdated, - }), - ], - }), - ); - }); - }); - }); - context('when user is not certifiable', function () { - const isCertifiable = false; - beforeEach(function () { - placementProfileService.getPlacementProfile.withArgs({ userId, limitDate }).resolves( - domainBuilder.buildPlacementProfile({ - profileDate: limitDate, - userId, - userCompetences: [domainBuilder.buildUserCompetence({ estimatedLevel: 1, pixScore: 1 })], - }), - ); - - pixCertificationRepository.findByUserId.withArgs({ userId }).resolves([ - domainBuilder.certification.enrolment.buildPixCertification({ - pixScore: requiredPixScore, - status: AssessmentResult.status.VALIDATED, - isCancelled: false, - isRejectedForFraud: false, - }), - ]); - }); - context('when user has an acquired certification for this badge', function () { - it('should not be added in the eligibilities of the model', async function () { - // given - 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, - complementaryCertificationKey, - complementaryCertificationBadgeImageUrl: 'monImageUrl', - complementaryCertificationBadgeLabel: 'monLabel', - isOutdated, - }), - ]); - - // when - const userCertificationEligibility = await getUserCertificationEligibility(dependencies); - - // then - expect(userCertificationEligibility).to.deep.equal( - domainBuilder.certification.enrolment.buildUserCertificationEligibility({ - id: userId, - isCertifiable, - certificationEligibilities: [], - }), - ); - }); - }); - context('when user has not an acquired certification for this badge', function () { - beforeEach(function () { - complementaryCertificationCourseRepository.findByUserId.withArgs({ userId }).resolves([]); - }); - - it('should not be added in the eligibilities of the model', async function () { - // given - 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, - complementaryCertificationKey, - complementaryCertificationBadgeImageUrl: 'monImageUrl', - complementaryCertificationBadgeLabel: 'monLabel', - isOutdated, - }), - ]); - - // when - const userCertificationEligibility = await getUserCertificationEligibility(dependencies); - - // then - expect(userCertificationEligibility).to.deep.equal( - domainBuilder.certification.enrolment.buildUserCertificationEligibility({ - id: userId, - isCertifiable, - certificationEligibilities: [], - }), - ); - }); - }); - }); - }); - }); - context('not CLEA', function () { - const isCertifiable = true; - beforeEach(function () { - placementProfileService.getPlacementProfile.withArgs({ userId, limitDate }).resolves( - domainBuilder.buildPlacementProfile.buildCertifiable({ - profileDate: limitDate, - userId, - }), - ); - complementaryCertificationKey = 'NOT CLEA'; - complementaryCertificationBadgeWithOffsetVersionRepository.getAllWithSameTargetProfile.resolves([ - domainBuilder.certification.enrolment.buildComplementaryCertificationBadge({ - id: complementaryCertificationBadgeId, - requiredPixScore, - offsetVersion: 0, - }), - ]); - }); - - context('when user has no pix certification', function () { - const isOutdated = false; - beforeEach(function () { - pixCertificationRepository.findByUserId.withArgs({ userId }).resolves([]); - }); - it('should not be added in the eligibilities of the model', async function () { - complementaryCertificationCourseRepository.findByUserId.withArgs({ userId }).resolves([]); - certificationBadgesService.findLatestBadgeAcquisitions - .withArgs({ - userId, - limitDate, - }) - .resolves([ - domainBuilder.buildCertifiableBadgeAcquisition({ - complementaryCertificationBadgeId, - complementaryCertificationKey, - complementaryCertificationBadgeImageUrl: 'monImageUrl', - complementaryCertificationBadgeLabel: 'monLabel', - isOutdated, - }), - ]); - - const userCertificationEligibility = await getUserCertificationEligibility(dependencies); - - expect(userCertificationEligibility).to.deep.equal( - domainBuilder.certification.enrolment.buildUserCertificationEligibility({ - id: userId, - isCertifiable, - certificationEligibilities: [], - }), - ); - }); - }); - context( - 'when user has a validated certification that is not cancelled not rejected and without the required pixscore', - function () { - const isOutdated = false; - beforeEach(function () { - pixCertificationRepository.findByUserId.withArgs({ userId }).resolves([ - domainBuilder.certification.enrolment.buildPixCertification({ - pixScore: requiredPixScore - 1, - status: AssessmentResult.status.VALIDATED, - isCancelled: false, - isRejectedForFraud: false, - }), - ]); - }); - it('should not be added in the eligibilities of the model', async function () { - complementaryCertificationCourseRepository.findByUserId.withArgs({ userId }).resolves([]); - certificationBadgesService.findLatestBadgeAcquisitions - .withArgs({ - userId, - limitDate, - }) - .resolves([ - domainBuilder.buildCertifiableBadgeAcquisition({ - complementaryCertificationBadgeId, - complementaryCertificationKey, - complementaryCertificationBadgeImageUrl: 'monImageUrl', - complementaryCertificationBadgeLabel: 'monLabel', - isOutdated, - }), - ]); - - const userCertificationEligibility = await getUserCertificationEligibility(dependencies); - - expect(userCertificationEligibility).to.deep.equal( - domainBuilder.certification.enrolment.buildUserCertificationEligibility({ - id: userId, - isCertifiable, - certificationEligibilities: [], - }), - ); - }); - }, - ); - context( - 'when user has a validated and cancelled certification that is not rejected for fraud and with the required pixscore', - function () { - const isOutdated = false; - beforeEach(function () { - pixCertificationRepository.findByUserId.withArgs({ userId }).resolves([ - domainBuilder.certification.enrolment.buildPixCertification({ - pixScore: requiredPixScore, - status: AssessmentResult.status.VALIDATED, - isCancelled: true, - isRejectedForFraud: false, - }), - ]); - }); - it('should not be added in the eligibilities of the model', async function () { - complementaryCertificationCourseRepository.findByUserId.withArgs({ userId }).resolves([]); - certificationBadgesService.findLatestBadgeAcquisitions - .withArgs({ - userId, - limitDate, - }) - .resolves([ - domainBuilder.buildCertifiableBadgeAcquisition({ - complementaryCertificationBadgeId, - complementaryCertificationKey, - complementaryCertificationBadgeImageUrl: 'monImageUrl', - complementaryCertificationBadgeLabel: 'monLabel', - isOutdated, - }), - ]); - - const userCertificationEligibility = await getUserCertificationEligibility(dependencies); - - expect(userCertificationEligibility).to.deep.equal( - domainBuilder.certification.enrolment.buildUserCertificationEligibility({ - id: userId, - isCertifiable, - certificationEligibilities: [], - }), - ); - }); - }, - ); - context( - 'when user has not cancelled but rejected for fraud validated pix certification with the required pix score', - function () { - const isOutdated = false; - beforeEach(function () { - pixCertificationRepository.findByUserId.withArgs({ userId }).resolves([ - domainBuilder.certification.enrolment.buildPixCertification({ - pixScore: requiredPixScore, - status: AssessmentResult.status.VALIDATED, - isCancelled: false, - isRejectedForFraud: true, - }), - ]); - }); - it('should not be added in the eligibilities of the model', async function () { - complementaryCertificationCourseRepository.findByUserId.withArgs({ userId }).resolves([]); - certificationBadgesService.findLatestBadgeAcquisitions - .withArgs({ - userId, - limitDate, - }) - .resolves([ - domainBuilder.buildCertifiableBadgeAcquisition({ - complementaryCertificationBadgeId, - complementaryCertificationKey, - complementaryCertificationBadgeImageUrl: 'monImageUrl', - complementaryCertificationBadgeLabel: 'monLabel', - isOutdated, - }), - ]); - - const userCertificationEligibility = await getUserCertificationEligibility(dependencies); - - expect(userCertificationEligibility).to.deep.equal( - domainBuilder.certification.enrolment.buildUserCertificationEligibility({ - id: userId, - isCertifiable, - certificationEligibilities: [], - }), - ); - }); - }, - ); - context('when user has not cancelled not rejected for fraud but not validated pix certification', function () { - const isOutdated = false; - beforeEach(function () { - pixCertificationRepository.findByUserId.withArgs({ userId }).resolves([ - domainBuilder.certification.enrolment.buildPixCertification({ - pixScore: requiredPixScore, - status: AssessmentResult.status.REJECTED, - isCancelled: true, - isRejectedForFraud: false, - }), - ]); - }); - it('should not be added in the eligibilities of the model', async function () { - complementaryCertificationCourseRepository.findByUserId.withArgs({ userId }).resolves([]); - certificationBadgesService.findLatestBadgeAcquisitions - .withArgs({ - userId, - limitDate, - }) - .resolves([ - domainBuilder.buildCertifiableBadgeAcquisition({ - complementaryCertificationBadgeId, - complementaryCertificationKey, - complementaryCertificationBadgeImageUrl: 'monImageUrl', - complementaryCertificationBadgeLabel: 'monLabel', - isOutdated, - }), - ]); - - const userCertificationEligibility = await getUserCertificationEligibility(dependencies); - - expect(userCertificationEligibility).to.deep.equal( - domainBuilder.certification.enrolment.buildUserCertificationEligibility({ - id: userId, - isCertifiable, - certificationEligibilities: [], - }), - ); - }); - }); - context('when user has a pix certification delivered validated with the required pix score', function () { - beforeEach(function () { - pixCertificationRepository.findByUserId.withArgs({ userId }).resolves([ - domainBuilder.certification.enrolment.buildPixCertification({ - pixScore: requiredPixScore, - status: AssessmentResult.status.VALIDATED, - isCancelled: false, - isRejectedForFraud: false, - }), - ]); - }); - context('when acquired badge is not outdated', function () { - const isOutdated = false; - context('when user has an acquired certification for this badge', function () { - it('returns a UserCertificationEligibility model with the outdated eligibility inside', async function () { - // given - 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, - complementaryCertificationKey, - complementaryCertificationBadgeImageUrl: 'monImageUrl', - complementaryCertificationBadgeLabel: 'monLabel', - isOutdated, - }), - ]); - - // when - const userCertificationEligibility = await getUserCertificationEligibility(dependencies); - - // then - expect(userCertificationEligibility).to.deep.equal( - domainBuilder.certification.enrolment.buildUserCertificationEligibility({ - id: userId, - isCertifiable, - certificationEligibilities: [ - domainBuilder.certification.enrolment.buildV3CertificationEligibility({ - label: 'monLabel', - imageUrl: 'monImageUrl', - isAcquiredExpectedLevel: true, - isOutdated: false, - }), - ], - }), - ); - }); - }); - context('when user has not an acquired certification for this badge', function () { - beforeEach(function () { - complementaryCertificationCourseRepository.findByUserId.withArgs({ userId }).resolves([]); - }); - it('returns a UserCertificationEligibility model with the outdated eligibility inside', async function () { - // given - certificationBadgesService.findLatestBadgeAcquisitions - .withArgs({ - userId, - limitDate, - }) - .resolves([ - domainBuilder.buildCertifiableBadgeAcquisition({ - complementaryCertificationBadgeId, - complementaryCertificationKey, - complementaryCertificationBadgeImageUrl: 'monImageUrl', - complementaryCertificationBadgeLabel: 'monLabel', - isOutdated, - }), - ]); - - // when - const userCertificationEligibility = await getUserCertificationEligibility(dependencies); - - // then - expect(userCertificationEligibility).to.deep.equal( - domainBuilder.certification.enrolment.buildUserCertificationEligibility({ - id: userId, - isCertifiable, - certificationEligibilities: [ - domainBuilder.certification.enrolment.buildV3CertificationEligibility({ - label: 'monLabel', - imageUrl: 'monImageUrl', - isAcquiredExpectedLevel: false, - isOutdated: false, - }), - ], - }), - ); - }); - }); - }); - context('when acquired badge is outdated by one version', function () { - const isOutdated = true; - context('when user has an acquired certification for this badge', function () { - it('should not be added in the eligibilities of the model', async function () { - // given - complementaryCertificationBadgeWithOffsetVersionRepository.getAllWithSameTargetProfile.resolves([ - domainBuilder.certification.enrolment.buildComplementaryCertificationBadge({ - id: '1234', - requiredPixScore, - offsetVersion: 0, - }), - domainBuilder.certification.enrolment.buildComplementaryCertificationBadge({ - id: complementaryCertificationBadgeId, - requiredPixScore, - offsetVersion: 1, - }), - ]); - - 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, - complementaryCertificationKey, - complementaryCertificationBadgeImageUrl: 'monImageUrl', - complementaryCertificationBadgeLabel: 'monLabel', - isOutdated, - }), - ]); - - // when - const userCertificationEligibility = await getUserCertificationEligibility(dependencies); - - // then - expect(userCertificationEligibility).to.deep.equal( - domainBuilder.certification.enrolment.buildUserCertificationEligibility({ - id: userId, - isCertifiable, - certificationEligibilities: [], - }), - ); - }); - }); - context('when user has not an acquired certification for this badge', function () { - beforeEach(function () { - complementaryCertificationCourseRepository.findByUserId.withArgs({ userId }).resolves([]); - }); - it('returns a UserCertificationEligibility model with the outdated eligibility inside', async function () { - // given - certificationBadgesService.findLatestBadgeAcquisitions - .withArgs({ - userId, - limitDate, - }) - .resolves([ - domainBuilder.buildCertifiableBadgeAcquisition({ - complementaryCertificationBadgeId, - complementaryCertificationKey, - complementaryCertificationBadgeImageUrl: 'monImageUrl', - complementaryCertificationBadgeLabel: 'monLabel', - isOutdated, - }), - ]); - - // when - const userCertificationEligibility = await getUserCertificationEligibility(dependencies); - - // then - expect(userCertificationEligibility).to.deep.equal( - domainBuilder.certification.enrolment.buildUserCertificationEligibility({ - id: userId, - isCertifiable, - certificationEligibilities: [ - domainBuilder.certification.enrolment.buildV3CertificationEligibility({ - label: 'monLabel', - imageUrl: 'monImageUrl', - isAcquiredExpectedLevel: false, - isOutdated, - }), - ], - }), - ); - }); - }); - }); - context('when acquired badge is outdated by more than one version', function () { - const isOutdated = true; - it('should not be added in the eligibilities of the model', async function () { - // given - complementaryCertificationBadgeWithOffsetVersionRepository.getAllWithSameTargetProfile.resolves([ - domainBuilder.certification.enrolment.buildComplementaryCertificationBadge({ - id: '1234', - requiredPixScore, - offsetVersion: 0, - }), - domainBuilder.certification.enrolment.buildComplementaryCertificationBadge({ - id: '5678', - requiredPixScore, - offsetVersion: 1, - }), - domainBuilder.certification.enrolment.buildComplementaryCertificationBadge({ - id: complementaryCertificationBadgeId, - requiredPixScore, - offsetVersion: 2, - }), - ]); - - 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, - complementaryCertificationKey, - complementaryCertificationBadgeImageUrl: 'monImageUrl', - complementaryCertificationBadgeLabel: 'monLabel', - isOutdated, - }), - ]); - - // when - const userCertificationEligibility = await getUserCertificationEligibility(dependencies); - - // then - expect(userCertificationEligibility).to.deep.equal( - domainBuilder.certification.enrolment.buildUserCertificationEligibility({ - id: userId, - isCertifiable, - certificationEligibilities: [], - }), - ); - }); - }); - }); - }); - }); - }); -});