Skip to content

Commit

Permalink
feat(api): prevent reward if user already has one
Browse files Browse the repository at this point in the history
  • Loading branch information
Libouk authored and La-toile-cosmique committed Sep 23, 2024
1 parent e35c257 commit 2c3dabc
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
7 changes: 7 additions & 0 deletions api/src/quest/domain/usecases/reward-user.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,20 @@ export const rewardUser = async ({

const eligibilities = await eligibilityRepository.find({ userId });

const rewards = await rewardRepository.getByUserId({ userId });
const rewardIds = rewards.map((reward) => reward.rewardId);

for (const quest of quests) {
const isEligibleForQuest = eligibilities.some((eligibility) => quest.isEligible(eligibility));

if (!isEligibleForQuest) {
continue;
}

if (rewardIds.includes(quest.rewardId)) {
continue;
}

const success = await successRepository.find({ userId, skillIds: quest.successRequirements[0].data.ids });
const userHasSucceedQuest = quest.isSuccessful(success);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export const reward = async ({ userId, rewardId, profileRewardApi }) => {
return profileRewardApi.save(userId, rewardId);
};

export const getByUserId = async ({ userId, profileRewardApi }) => {
return profileRewardApi.getByUserId(userId);
};
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ describe('Profile | Integration | Repository | profile-reward', function () {
});

describe('#getByUserId', function () {

it('should return all profile rewards for the user', async function () {
// given
const { id: userId } = databaseBuilder.factory.buildUser();
Expand Down
25 changes: 25 additions & 0 deletions api/tests/quest/unit/domain/usecases/reward-user_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,29 @@ describe('Quest | Unit | Domain | Usecases | RewardUser', function () {
expect(rewardRepository.save).to.have.not.been.called;
});
});

context('when the user is eligible but already got the reward', function () {
it('should not call success repository', async function () {
// given
const questRewardId = Symbol('questRewardId');
const quest = { isEligible: () => true, rewardId: questRewardId };
questRepository.findAll.resolves([quest]);
eligibilityRepository.find.resolves([Symbol('eligibility')]);
rewardRepository.getByUserId.resolves([
{
rewardId: questRewardId,
},
]);

// when
await rewardUser({
userId,
questRepository,
eligibilityRepository,
successRepository,
rewardRepository,
});
expect(successRepository.find).to.not.have.been.called;
});
});
});

0 comments on commit 2c3dabc

Please sign in to comment.