Files
web/apps/scandic-web/utils/rewards.ts
Joakim Jäderberg 8ebc48b138 Merged in feat/SW-3461-setup-auth-with-sas-eurobonus (pull request #2825)
Feat/SW-3461 setup auth with sas eurobonus

* feat(SW-3461): Setup auth for sas eurobonus

* .

* feat: setup auth towards SAS

* Fix auth via SAS and add logout route

* .

* merge

* auth via SAS

* fix powered by scandic logo

* Merge branch 'master' of bitbucket.org:scandic-swap/web into feat/SW-3461-setup-auth-with-sas-eurobonus

* Include access_token in jwt after successful login

* merge


Approved-by: Anton Gunnarsson
2025-09-22 09:30:36 +00:00

74 lines
2.1 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,
isRestaurantOnSiteTierReward,
isValidRewardId,
}
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"
}
export 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)
}