feat(LOY-154): add expiration date to rewards
This commit is contained in:
@@ -3,49 +3,74 @@ import {
|
||||
REWARD_IDS,
|
||||
REWARD_TYPES,
|
||||
} from "@/constants/rewards"
|
||||
import { dt } from "@/lib/dt"
|
||||
|
||||
import type { Dayjs } from "dayjs"
|
||||
|
||||
import type {
|
||||
RestaurantRewardId,
|
||||
RewardId,
|
||||
RewardType,
|
||||
} from "@/types/components/myPages/rewards"
|
||||
import type { RewardWithRedeem } from "@/server/routers/contentstack/reward/output"
|
||||
import type {
|
||||
Coupon,
|
||||
RewardWithRedeem,
|
||||
} from "@/server/routers/contentstack/reward/output"
|
||||
|
||||
export function isValidRewardId(id: string): id is RewardId {
|
||||
export {
|
||||
getEarliestExpirationDate,
|
||||
getRewardType,
|
||||
isOnSiteTierReward,
|
||||
isRestaurantOnSiteTierReward,
|
||||
isRestaurantReward,
|
||||
isTierType,
|
||||
isValidRewardId,
|
||||
redeemLocationIsOnSite,
|
||||
}
|
||||
|
||||
function isValidRewardId(id: string): id is RewardId {
|
||||
return Object.values<string>(REWARD_IDS).includes(id)
|
||||
}
|
||||
|
||||
export function isRestaurantReward(
|
||||
rewardId: string
|
||||
): rewardId is RestaurantRewardId {
|
||||
function isRestaurantReward(rewardId: string): rewardId is RestaurantRewardId {
|
||||
return RESTAURANT_REWARD_IDS.some((id) => id === rewardId)
|
||||
}
|
||||
|
||||
export function redeemLocationIsOnSite(
|
||||
function redeemLocationIsOnSite(
|
||||
location: RewardWithRedeem["redeemLocation"]
|
||||
): location is "On-site" {
|
||||
return location === "On-site"
|
||||
}
|
||||
|
||||
export function isTierType(
|
||||
type: RewardWithRedeem["rewardType"]
|
||||
): type is "Tier" {
|
||||
function isTierType(type: RewardWithRedeem["rewardType"]): type is "Tier" {
|
||||
return type === "Tier"
|
||||
}
|
||||
|
||||
export function isOnSiteTierReward(reward: RewardWithRedeem): boolean {
|
||||
function isOnSiteTierReward(reward: RewardWithRedeem): boolean {
|
||||
return (
|
||||
redeemLocationIsOnSite(reward.redeemLocation) &&
|
||||
isTierType(reward.rewardType)
|
||||
)
|
||||
}
|
||||
|
||||
export function isRestaurantOnSiteTierReward(
|
||||
reward: RewardWithRedeem
|
||||
): boolean {
|
||||
function isRestaurantOnSiteTierReward(reward: RewardWithRedeem): boolean {
|
||||
return isOnSiteTierReward(reward) && isRestaurantReward(reward.reward_id)
|
||||
}
|
||||
|
||||
export function getRewardType(type?: string): RewardType | null {
|
||||
function getRewardType(type?: string): RewardType | null {
|
||||
return REWARD_TYPES.find((t) => t === type) ?? null
|
||||
}
|
||||
|
||||
function getEarliestExpirationDate(coupons: Coupon[]) {
|
||||
return coupons
|
||||
.map(({ expiresAt }) => expiresAt)
|
||||
.filter((expiresAt): expiresAt is string => !!expiresAt)
|
||||
.reduce((earliestDate: Dayjs | null, expiresAt) => {
|
||||
const expiresAtDate = dt(expiresAt)
|
||||
if (!earliestDate) {
|
||||
return expiresAtDate
|
||||
}
|
||||
|
||||
return earliestDate.isBefore(expiresAtDate) ? earliestDate : expiresAtDate
|
||||
}, null)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user