feat(SW-2863): Move contentstack router to trpc package * Add exports to packages and lint rule to prevent relative imports * Add env to trpc package * Add eslint to trpc package * Apply lint rules * Use direct imports from trpc package * Add lint-staged config to trpc * Move lang enum to common * Restructure trpc package folder structure * WIP first step * update internal imports in trpc * Fix most errors in scandic-web Just 100 left... * Move Props type out of trpc * Fix CategorizedFilters types * Move more schemas in hotel router * Fix deps * fix getNonContentstackUrls * Fix import error * Fix entry error handling * Fix generateMetadata metrics * Fix alertType enum * Fix duplicated types * lint:fix * Merge branch 'master' into feat/sw-2863-move-contentstack-router-to-trpc-package * Fix broken imports * Merge branch 'master' into feat/sw-2863-move-contentstack-router-to-trpc-package Approved-by: Linus Flood
79 lines
2.2 KiB
TypeScript
79 lines
2.2 KiB
TypeScript
import { dt } from "@scandic-hotels/common/dt"
|
|
import { getReedemableCoupons } from "@scandic-hotels/trpc/routers/contentstack/reward/helpers"
|
|
import {
|
|
RESTAURANT_REWARD_IDS,
|
|
type RestaurantRewardId,
|
|
REWARD_IDS,
|
|
type RewardId,
|
|
} from "@scandic-hotels/trpc/types/rewards"
|
|
|
|
import type {
|
|
ApiReward,
|
|
RedeemableCoupon,
|
|
RedeemLocation,
|
|
} from "@scandic-hotels/trpc/types/reward"
|
|
import type { Dayjs } from "dayjs"
|
|
|
|
export {
|
|
getEarliestExpirationDate,
|
|
getFirstRedeemableCoupon,
|
|
getReedemableCoupons,
|
|
isOnSiteTierReward,
|
|
isRestaurantOnSiteTierReward,
|
|
isRestaurantReward,
|
|
isTierType,
|
|
isValidRewardId,
|
|
redeemLocationIsOnSite,
|
|
}
|
|
|
|
function isValidRewardId(id: string): id is RewardId {
|
|
return Object.values<string>(REWARD_IDS).includes(id)
|
|
}
|
|
|
|
function isRestaurantReward(rewardId: string): rewardId is RestaurantRewardId {
|
|
return RESTAURANT_REWARD_IDS.some((id) => id === rewardId)
|
|
}
|
|
|
|
function redeemLocationIsOnSite(
|
|
location: RedeemLocation
|
|
): location is "On-site" {
|
|
return location === "On-site"
|
|
}
|
|
|
|
function isTierType(type: string): type is "Tier" {
|
|
return type === "Tier"
|
|
}
|
|
|
|
function isOnSiteTierReward(reward: ApiReward): boolean {
|
|
return (
|
|
redeemLocationIsOnSite(reward.redeemLocation) &&
|
|
isTierType(reward.rewardType)
|
|
)
|
|
}
|
|
|
|
function isRestaurantOnSiteTierReward(reward: ApiReward): boolean {
|
|
return isOnSiteTierReward(reward) && isRestaurantReward(reward.rewardId)
|
|
}
|
|
|
|
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(reward: ApiReward) {
|
|
return getReedemableCoupons(reward)
|
|
.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)
|
|
}
|