diff --git a/api/src/quest/domain/models/Eligibility.js b/api/src/quest/domain/models/Eligibility.js index 71f66fb1752..99d805d29b6 100644 --- a/api/src/quest/domain/models/Eligibility.js +++ b/api/src/quest/domain/models/Eligibility.js @@ -5,44 +5,31 @@ export const TYPES = { }; export class Eligibility { - #campaignParticipations; - constructor({ organizationLearner, organization, campaignParticipations = [] }) { this.organizationLearner = { + id: organizationLearner?.id, MEFCode: organizationLearner?.MEFCode, }; this.organization = organization; - this.#campaignParticipations = campaignParticipations; - } - - get campaignParticipations() { - return { - targetProfileIds: this.#campaignParticipations.map(({ targetProfileId }) => targetProfileId), - }; - } - - set campaignParticipations(campaignParticipations) { - this.#campaignParticipations = campaignParticipations; + this.campaignParticipations = campaignParticipations; } hasCampaignParticipation(campaignParticipationId) { return Boolean( - this.#campaignParticipations.find( - (campaignParticipation) => campaignParticipation.id === campaignParticipationId, - ), + this.campaignParticipations.find((campaignParticipation) => campaignParticipation.id === campaignParticipationId), ); } hasCampaignParticipationForTargetProfileId(targetProfileId) { return Boolean( - this.#campaignParticipations.find( + this.campaignParticipations.find( (campaignParticipation) => campaignParticipation.targetProfileId === targetProfileId, ), ); } getTargetProfileForCampaignParticipation(campaignParticipationId) { - const campaignParticipation = this.#campaignParticipations.find( + const campaignParticipation = this.campaignParticipations.find( (campaignParticipation) => campaignParticipation.id === campaignParticipationId, ); diff --git a/api/src/quest/domain/models/Quest.js b/api/src/quest/domain/models/Quest.js index c7279562056..86d2beabf8e 100644 --- a/api/src/quest/domain/models/Quest.js +++ b/api/src/quest/domain/models/Quest.js @@ -24,17 +24,37 @@ class Quest { ); } + #checkCriterion({ criterion, eligibilityData }) { + if (Array.isArray(criterion)) { + if (Array.isArray(eligibilityData)) { + return criterion.every((valueToTest) => eligibilityData.includes(valueToTest)); + } + return criterion.some((valueToTest) => valueToTest === eligibilityData); + } + return eligibilityData === criterion; + } + #checkRequirement(eligibilityRequirement, eligibility) { - const comparaisonFunction = eligibilityRequirement.comparison === COMPARISON.ONE_OF ? 'some' : 'every'; + const comparisonFunction = eligibilityRequirement.comparison === COMPARISON.ONE_OF ? 'some' : 'every'; - return Object.keys(eligibilityRequirement.data)[comparaisonFunction]((key) => { - const eligibilityData = eligibility[eligibilityRequirement.type][key]; - const criterion = eligibilityRequirement.data[key]; + if (Array.isArray(eligibility[eligibilityRequirement.type])) { + return eligibility[eligibilityRequirement.type].some((item) => { + return Object.keys(eligibilityRequirement.data)[comparisonFunction]((key) => { + // TODO: Dés que les quêtes ont été mises à jour il faudra retirer cette ligne + const alterKey = key === 'targetProfileIds' ? 'targetProfileId' : key; + return this.#checkCriterion({ + criterion: eligibilityRequirement.data[key], + eligibilityData: item[alterKey], + }); + }); + }); + } - if (Array.isArray(criterion)) { - return criterion.every((valueToTest) => eligibilityData.includes(valueToTest)); - } - return eligibilityData === criterion; + return Object.keys(eligibilityRequirement.data)[comparisonFunction]((key) => { + return this.#checkCriterion({ + criterion: eligibilityRequirement.data[key], + eligibilityData: eligibility[eligibilityRequirement.type][key], + }); }); } diff --git a/api/tests/quest/unit/domain/models/Eligibility_test.js b/api/tests/quest/unit/domain/models/Eligibility_test.js index 694aaf079c7..cbeb66e387f 100644 --- a/api/tests/quest/unit/domain/models/Eligibility_test.js +++ b/api/tests/quest/unit/domain/models/Eligibility_test.js @@ -2,32 +2,6 @@ import { Eligibility } from '../../../../../src/quest/domain/models/Eligibility. import { expect } from '../../../../test-helper.js'; describe('Quest | Unit | Domain | Models | Eligibility ', function () { - describe('#campaignParticipations', function () { - it('Should return an object with targetProfileIds property', function () { - // given - const campaignParticipations = [{ targetProfileId: 1 }, { targetProfileId: 2 }]; - const eligiblity = new Eligibility({ campaignParticipations }); - - // when - const result = eligiblity.campaignParticipations; - - // then - expect(result.targetProfileIds).to.deep.equal([1, 2]); - }); - - it('Should return an empty array on targetProfileIds property', function () { - // given - const campaignParticipations = []; - const eligiblity = new Eligibility({ campaignParticipations }); - - // when - const result = eligiblity.campaignParticipations; - - // then - expect(result.targetProfileIds).to.deep.equal([]); - }); - }); - describe('#hasCampaignParticipation', function () { it('Should return true if campaign participation exists', function () { // given diff --git a/api/tests/quest/unit/domain/models/Quest_test.js b/api/tests/quest/unit/domain/models/Quest_test.js index aa6fe56bbf4..7897b76c3e7 100644 --- a/api/tests/quest/unit/domain/models/Quest_test.js +++ b/api/tests/quest/unit/domain/models/Quest_test.js @@ -119,7 +119,7 @@ describe('Quest | Unit | Domain | Models | Quest ', function () { let quest; let userTargetProfileId; - before(function () { + beforeEach(function () { // given const eligibleTargetProfileId = 1000; userTargetProfileId = eligibleTargetProfileId; @@ -177,6 +177,62 @@ describe('Quest | Unit | Domain | Models | Quest ', function () { // then expect(quest.isEligible(eligibilityData)).to.equal(false); }); + + it('should return true if all eligibility requirements of a same type are met', function () { + // given + const organization = { type: 'SCO', isManagingStudents: true, tags: ['AEFE'] }; + const organizationLearner = { MEFCode: '10010012110' }; + const campaignParticipations = [{ targetProfileId: 1 }, { targetProfileId: 2 }]; + const eligibilityData = new Eligibility({ organization, organizationLearner, campaignParticipations }); + const eligibilityRequirements = [ + { + type: TYPES.CAMPAIGN_PARTICIPATIONS, + data: { + targetProfileIds: [1], + }, + comparison: COMPARISON.ALL, + }, + { + type: TYPES.CAMPAIGN_PARTICIPATIONS, + data: { + targetProfileIds: [2, 3], + }, + comparison: COMPARISON.ALL, + }, + ]; + quest = new Quest({ eligibilityRequirements }); + + // then + expect(quest.isEligible(eligibilityData)).to.equal(true); + }); + + it('should return false if one of eligibility requirement of a same type is not eligible', function () { + // given + const organization = { type: 'SCO', isManagingStudents: true, tags: ['AEFE'] }; + const organizationLearner = { MEFCode: '10010012110' }; + const campaignParticipations = [{ targetProfileId: 1 }, { targetProfileId: 4 }]; + const eligibilityData = new Eligibility({ organization, organizationLearner, campaignParticipations }); + const eligibilityRequirements = [ + { + type: TYPES.CAMPAIGN_PARTICIPATIONS, + data: { + targetProfileIds: [1], + }, + comparison: COMPARISON.ALL, + }, + { + type: TYPES.CAMPAIGN_PARTICIPATIONS, + data: { + targetProfileIds: [2, 3], + }, + comparison: COMPARISON.ALL, + }, + ]; + quest = new Quest({ eligibilityRequirements }); + + // then + expect(quest.isEligible(eligibilityData)).to.equal(false); + }); }); });