Skip to content

Commit

Permalink
fix: get the correct completed tier, handle when tiers completed at t…
Browse files Browse the repository at this point in the history
…he same time
  • Loading branch information
dyersituations committed Dec 4, 2024
1 parent 4fb6ae9 commit 7d05a89
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
9 changes: 7 additions & 2 deletions src/composables/useBadgeData.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?? '')}`
};
Expand Down
29 changes: 26 additions & 3 deletions test/unit/specs/composables/useBadgeData.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
});
});

Expand Down

0 comments on commit 7d05a89

Please sign in to comment.