feat(SW-312): fix "2-for-1-breakfast" only unlocked for level 5

This commit is contained in:
Matilda Landström
2024-08-29 14:36:26 +02:00
committed by Christel Westerberg
parent 00fc2af3dd
commit 41827f0fe0
15 changed files with 97 additions and 93 deletions

View File

@@ -1,13 +1,21 @@
import { ComparisonLevel } from "@/types/components/loyalty/blocks"
import type {
Benefit,
ComparisonLevel,
} from "@/types/components/loyalty/blocks"
export function getHighestLevel(levels: ComparisonLevel[]) {
return levels.reduce(
(acc: ComparisonLevel | null, level: ComparisonLevel) => {
if (!acc) {
return level
}
return level.level > acc.level ? level : acc
},
null
export function getUnlockedBenefits(levels: ComparisonLevel[]) {
const allBenefits = levels
.map((level) => {
return level.benefits.filter((benefit) => benefit.unlocked)
})
.flat()
/* Remove duplicate benefits based on the name property */
return Array.from(
new Map(allBenefits.map((benefit) => [benefit.name, benefit])).values()
)
}
export function findBenefit(benefit: Benefit, level: ComparisonLevel) {
return level.benefits.find((b) => b.name === benefit.name) as Benefit
}