From 7d05a89f2b48d704db6d537f0800c1f801cfc0a2 Mon Sep 17 00:00:00 2001 From: Casey Dyer Date: Wed, 4 Dec 2024 15:22:09 -0800 Subject: [PATCH] fix: get the correct completed tier, handle when tiers completed at the same time --- src/composables/useBadgeData.js | 9 ++++-- .../specs/composables/useBadgeData.spec.js | 29 +++++++++++++++++-- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/composables/useBadgeData.js b/src/composables/useBadgeData.js index aa40abf81b..a100f6cfbc 100644 --- a/src/composables/useBadgeData.js +++ b/src/composables/useBadgeData.js @@ -346,13 +346,18 @@ export default function useBadgeData() { } } else if (badge?.achievementData?.tiers?.length) { const tiers = JSON.parse(JSON.stringify(badge.achievementData.tiers)); - tiers.sort((a, b) => new Date(a.completedDate) - new Date(b.completedDate)); + tiers.sort((a, b) => { + if (a.completedDate === b.completedDate) { + return b.level - a.level; + } + return new Date(b.completedDate) - new Date(a.completedDate); + }); const levelIndex = tiers[0].level - 1; const contentfulData = badge.contentfulData[levelIndex]; return { ...badge, contentfulData, - achievementData: tiers[levelIndex], + achievementData: tiers[0], // eslint-disable-next-line max-len levelName: `${(contentfulData.challengeName ?? '')}${(contentfulData.levelName ? ' ' : '')}${(contentfulData.levelName ?? '')}` }; diff --git a/test/unit/specs/composables/useBadgeData.spec.js b/test/unit/specs/composables/useBadgeData.spec.js index 2ba53f21e8..b87a18fec5 100644 --- a/test/unit/specs/composables/useBadgeData.spec.js +++ b/test/unit/specs/composables/useBadgeData.spec.js @@ -452,9 +452,32 @@ describe('useBadgeData.js', () => { expect(getLastCompletedBadgeLevelData(badge)).toEqual({ ...badge, - contentfulData: badge.contentfulData[0], - achievementData: badge.achievementData.tiers[0], - levelName: 'Basic needs 1' + contentfulData: badge.contentfulData[1], + achievementData: badge.achievementData.tiers[1], + levelName: 'Basic needs 2' + }); + }); + + it('should return the last completed badge level data when tiers have same completed date', () => { + const { getLastCompletedBadgeLevelData } = useBadgeData(); + const badge = { + achievementData: { + tiers: [ + { level: 1, completedDate: '2024-10-22T18:49:21Z' }, + { level: 2, completedDate: '2024-10-22T18:49:21Z' } + ] + }, + contentfulData: [ + { challengeName: 'Basic needs', levelName: '1' }, + { challengeName: 'Basic needs', levelName: '2' } + ] + }; + + expect(getLastCompletedBadgeLevelData(badge)).toEqual({ + ...badge, + contentfulData: badge.contentfulData[1], + achievementData: badge.achievementData.tiers[1], + levelName: 'Basic needs 2' }); });