Skip to content

Commit

Permalink
fix(api): reassign campaignParticipation getter in Eligibility object
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandre-Monney committed Nov 15, 2024
1 parent 07ef282 commit 47d5fcf
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,52 @@ 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 () {
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();

const result = await usecases.getQuestResultsForCampaignParticipation({
userId,
campaignParticipationId: notEligibleParticipationId,
});

expect(result).to.be.empty;
});
});

0 comments on commit 47d5fcf

Please sign in to comment.