From 059c5252448a78caf1eec799dc070fbbafb04537 Mon Sep 17 00:00:00 2001 From: Alexandre-Monney <101280231+Alexandre-Monney@users.noreply.github.com> Date: Fri, 15 Nov 2024 11:46:33 +0100 Subject: [PATCH] =?UTF-8?q?[BUGFIX]=20Ne=20pas=20afficher=20l'encart=20att?= =?UTF-8?q?estation=20pour=20les=20participations=20a=20des=20campagnes=20?= =?UTF-8?q?sans=20lien=20avec=20les=20qu=C3=AAtes=20(PIX-15308)=20(#10550)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...uest-results-for-campaign-participation.js | 12 +++-- ...results-for-campaign-participation_test.js | 51 +++++++++++++++++++ 2 files changed, 58 insertions(+), 5 deletions(-) diff --git a/api/src/quest/domain/usecases/get-quest-results-for-campaign-participation.js b/api/src/quest/domain/usecases/get-quest-results-for-campaign-participation.js index 30c89b5ca6e..daa3eb1d964 100644 --- a/api/src/quest/domain/usecases/get-quest-results-for-campaign-participation.js +++ b/api/src/quest/domain/usecases/get-quest-results-for-campaign-participation.js @@ -16,11 +16,13 @@ export const getQuestResultsForCampaignParticipation = async ({ if (!eligibility) return []; - eligibility.campaignParticipations.targetProfileIds = [ - // getTargetProfileForCampaignParticipation returns null but this usecase is used for campaign participation result page for now, so the campaign participation ID always exists - // if this usecase is to be used in another context, the edge case must be handled - eligibility.getTargetProfileForCampaignParticipation(campaignParticipationId), - ]; + /* + This effectively overrides the existing campaignParticipations property with a new getter that always returns the updated targetProfileIds array based on the provided campaignParticipationId. + We can't just reassign the getter with the new value, because the getter will still be called and the new value would be ignored + */ + Object.defineProperty(eligibility, 'campaignParticipations', { + get: () => ({ targetProfileIds: [eligibility.getTargetProfileForCampaignParticipation(campaignParticipationId)] }), + }); const questResults = []; for (const quest of quests) { diff --git a/api/tests/quest/integration/domain/usecases/get-quest-results-for-campaign-participation_test.js b/api/tests/quest/integration/domain/usecases/get-quest-results-for-campaign-participation_test.js index 8878a43969f..1acde3d74f4 100644 --- a/api/tests/quest/integration/domain/usecases/get-quest-results-for-campaign-participation_test.js +++ b/api/tests/quest/integration/domain/usecases/get-quest-results-for-campaign-participation_test.js @@ -35,4 +35,55 @@ describe('Quest | Integration | Domain | Usecases | getQuestResultsForCampaignPa expect(result[0].id).to.equal(questId); expect(result[0].reward.id).to.equal(rewardId); }); + + it('should not return quest results for other campaign participation', async function () { + // given + const organizationId = databaseBuilder.factory.buildOrganization({ type: 'SCO' }).id; + const { id: organizationLearnerId, userId } = databaseBuilder.factory.buildOrganizationLearner({ organizationId }); + + const { id: notEligibleParticipationId } = databaseBuilder.factory.buildCampaignParticipation({ + organizationLearnerId, + userId, + }); + const targetProfileId = databaseBuilder.factory.buildTargetProfile({ ownerOrganizationId: organizationId }).id; + const campaignId = databaseBuilder.factory.buildCampaign({ organizationId, targetProfileId }).id; + databaseBuilder.factory.buildCampaignParticipation({ + organizationLearnerId, + campaignId, + userId, + }); + const rewardId = databaseBuilder.factory.buildAttestation().id; + databaseBuilder.factory.buildQuest({ + rewardType: 'attestations', + rewardId, + eligibilityRequirements: [ + { + type: 'organization', + data: { + type: 'SCO', + }, + comparison: COMPARISON.ALL, + }, + { + type: 'campaignParticipations', + data: { + targetProfileIds: [targetProfileId], + }, + comparison: COMPARISON.ALL, + }, + ], + successRequirements: [], + }).id; + + await databaseBuilder.commit(); + + // when + const result = await usecases.getQuestResultsForCampaignParticipation({ + userId, + campaignParticipationId: notEligibleParticipationId, + }); + + // then + expect(result).to.be.empty; + }); });