From 886c3d373e0bfc58a55119a705217bc864252928 Mon Sep 17 00:00:00 2001 From: Geoffroy Begouaussel Date: Wed, 9 Oct 2024 17:36:52 +0200 Subject: [PATCH] feat(api): sort campaign badges by their certifiable state --- .../repositories/badge-repository.js | 3 +- .../repositories/badge-repository_test.js | 61 +++++++++++++++---- 2 files changed, 52 insertions(+), 12 deletions(-) diff --git a/api/src/evaluation/infrastructure/repositories/badge-repository.js b/api/src/evaluation/infrastructure/repositories/badge-repository.js index 3a500bf6fd9..a9037b76a0a 100644 --- a/api/src/evaluation/infrastructure/repositories/badge-repository.js +++ b/api/src/evaluation/infrastructure/repositories/badge-repository.js @@ -16,7 +16,8 @@ const findByCampaignId = async (campaignId) => { .join('target-profiles', 'target-profiles.id', `${TABLE_NAME}.targetProfileId`) .join('campaigns', 'campaigns.targetProfileId', 'target-profiles.id') .where('campaigns.id', campaignId) - .orderBy('id'); + .orderBy('isCertifiable', 'DESC') + .orderBy('id', 'ASC'); }; const isAssociated = async (badgeId) => { diff --git a/api/tests/evaluation/integration/infrastructure/repositories/badge-repository_test.js b/api/tests/evaluation/integration/infrastructure/repositories/badge-repository_test.js index d7181709543..c91fa748ee4 100644 --- a/api/tests/evaluation/integration/infrastructure/repositories/badge-repository_test.js +++ b/api/tests/evaluation/integration/infrastructure/repositories/badge-repository_test.js @@ -21,6 +21,7 @@ describe('Integration | Repository | Badge', function () { message: 'Congrats, you won the yellow badge!', key: 'YELLOW', targetProfileId: targetProfileWithSeveralBadges.id, + isCertifiable: false, }); badgeWithSameTargetProfile_2 = databaseBuilder.factory.buildBadge({ altMessage: 'You won the GREEN badge!', @@ -28,24 +29,62 @@ describe('Integration | Repository | Badge', function () { message: 'Congrats, you won the green badge!', key: 'GREEN', targetProfileId: targetProfileWithSeveralBadges.id, + isCertifiable: true, }); + databaseBuilder.factory.buildBadge({ key: 'BADGE_WITH_OTHER_TARGET_PROFILE' }); await databaseBuilder.commit(); }); describe('#findByCampaignId', function () { - it('should return two badges for same target profile', async function () { - // given - const targetProfileId = targetProfileWithSeveralBadges.id; - const campaignId = databaseBuilder.factory.buildCampaign({ targetProfileId }).id; - await databaseBuilder.commit(); + describe('when a target profile has badges', function () { + let badges, campaignId; - // when - const badges = await badgeRepository.findByCampaignId(campaignId); + beforeEach(async function () { + // given + const targetProfileId = targetProfileWithSeveralBadges.id; + campaignId = databaseBuilder.factory.buildCampaign({ targetProfileId }).id; + await databaseBuilder.commit(); - // then - expect(badges).to.have.length(2); - expect(badges[0].id).to.equal(badgeWithSameTargetProfile_1.id); - expect(badges[1].id).to.equal(badgeWithSameTargetProfile_2.id); + // when + badges = await badgeRepository.findByCampaignId(campaignId); + }); + + it('should return two badges for same target profile', async function () { + // then + expect(badges).to.have.length(2); + }); + + it('should return certifiable badges first', async function () { + expect(badges[0].id).to.equal(badgeWithSameTargetProfile_2.id); + expect(badges[0].isCertifiable).to.be.true; + + expect(badges[1].id).to.equal(badgeWithSameTargetProfile_1.id); + expect(badges[1].isCertifiable).to.be.false; + }); + + it('should also sort badges by id', async function () { + const badgeWithSameTargetProfile_3 = databaseBuilder.factory.buildBadge({ + id: 1, + targetProfileId: targetProfileWithSeveralBadges.id, + isCertifiable: true, + }); + const badgeWithSameTargetProfile_4 = databaseBuilder.factory.buildBadge({ + id: 2, + targetProfileId: targetProfileWithSeveralBadges.id, + isCertifiable: false, + }); + + await databaseBuilder.commit(); + + // when + badges = await badgeRepository.findByCampaignId(campaignId); + + // then + expect(badges[0].id).to.equal(badgeWithSameTargetProfile_3.id); + expect(badges[1].id).to.equal(badgeWithSameTargetProfile_2.id); + expect(badges[2].id).to.equal(badgeWithSameTargetProfile_4.id); + expect(badges[3].id).to.equal(badgeWithSameTargetProfile_1.id); + }); }); it('should return an empty array when the given campaign has no badges', async function () {