refactor(LOY-175): rewrite reward types according to new api endpoints

This commit is contained in:
Christian Andolf
2025-03-11 16:09:15 +01:00
parent 0ae4c5db17
commit b86347b4f4
15 changed files with 196 additions and 246 deletions

View File

@@ -1,5 +1,4 @@
import { Reward } from "@/server/routers/contentstack/reward/output"
import type { Reward } from "@/types/components/myPages/rewards"
import type { ComparisonLevel } from "@/types/components/overviewTable"
export function getGroupedRewards(levels: ComparisonLevel[]) {

View File

@@ -2,7 +2,6 @@ import {
RESTAURANT_REWARD_IDS,
REWARD_CATEGORIES,
REWARD_IDS,
REWARD_TYPES,
} from "@/constants/rewards"
import { dt } from "@/lib/dt"
@@ -12,16 +11,18 @@ import type {
RestaurantRewardId,
RewardCategory,
RewardId,
RewardType,
} from "@/types/components/myPages/rewards"
import type {
ApiReward,
Coupon,
RewardWithRedeem,
RedeemableCoupon,
RedeemLocation,
} from "@/server/routers/contentstack/reward/output"
export {
getEarliestExpirationDate,
getRewardType,
getFirstRedeemableCoupon,
getReedemableCoupons,
isOnSiteTierReward,
isRestaurantOnSiteTierReward,
isRestaurantReward,
@@ -44,28 +45,41 @@ function isRewardCategory(value: string): value is RewardCategory {
}
function redeemLocationIsOnSite(
location: RewardWithRedeem["redeemLocation"]
location: RedeemLocation
): location is "On-site" {
return location === "On-site"
}
function isTierType(type: RewardWithRedeem["rewardType"]): type is "Tier" {
function isTierType(type: string): type is "Tier" {
return type === "Tier"
}
function isOnSiteTierReward(reward: RewardWithRedeem): boolean {
function isOnSiteTierReward(reward: ApiReward): boolean {
return (
redeemLocationIsOnSite(reward.redeemLocation) &&
isTierType(reward.rewardType)
)
}
function isRestaurantOnSiteTierReward(reward: RewardWithRedeem): boolean {
return isOnSiteTierReward(reward) && isRestaurantReward(reward.reward_id)
function isRestaurantOnSiteTierReward(reward: ApiReward): boolean {
return isOnSiteTierReward(reward) && isRestaurantReward(reward.rewardId)
}
function getRewardType(type?: string): RewardType | null {
return REWARD_TYPES.find((t) => t === type) ?? null
function getReedemableCoupons(reward: ApiReward): RedeemableCoupon[] {
if ("coupon" in reward) {
return reward.coupon.filter(
(coupon): coupon is RedeemableCoupon => coupon.state !== "redeemed"
)
}
return []
}
function getFirstRedeemableCoupon(reward: ApiReward): RedeemableCoupon {
const sortedCoupons = getReedemableCoupons(reward).sort((a, b) => {
// null values used instead of undefined, otherwise it will return current time
return dt(a.expiresAt ?? null).valueOf() - dt(b.expiresAt ?? null).valueOf()
})
return sortedCoupons[0]
}
function getEarliestExpirationDate(coupons: Coupon[]) {