Merged in feat/sw-2863-move-contentstack-router-to-trpc-package (pull request #2389)
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
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
import type {
|
||||
ApiReward,
|
||||
RedeemableCoupon,
|
||||
SurpriseReward,
|
||||
} from "../../../types/reward"
|
||||
|
||||
export function getRedeemableRewards(rewards: ApiReward[]) {
|
||||
return rewards
|
||||
.filter((reward) => {
|
||||
if ("coupon" in reward && reward.coupon.length > 0) {
|
||||
if (reward.coupon.every((coupon) => coupon.state === "redeemed")) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
})
|
||||
.filter((reward) => {
|
||||
if (isSurpriseReward(reward)) {
|
||||
return !reward.coupon.some(({ unwrapped }) => !unwrapped)
|
||||
}
|
||||
return true
|
||||
})
|
||||
}
|
||||
|
||||
function isSurpriseReward(reward: ApiReward): reward is SurpriseReward {
|
||||
return reward.rewardType === "Surprise"
|
||||
}
|
||||
|
||||
export function getUnwrappedSurpriseRewards(rewards: ApiReward[]) {
|
||||
return rewards
|
||||
.filter(isSurpriseReward)
|
||||
.filter((reward) => getReedemableCoupons(reward).length)
|
||||
.filter((reward) => {
|
||||
const unwrappedCoupons =
|
||||
reward.coupon.filter((coupon) => !coupon.unwrapped) || []
|
||||
|
||||
return unwrappedCoupons.length
|
||||
})
|
||||
}
|
||||
|
||||
export function getReedemableCoupons(reward: ApiReward): RedeemableCoupon[] {
|
||||
if ("coupon" in reward) {
|
||||
return reward.coupon.filter(
|
||||
(coupon): coupon is RedeemableCoupon => coupon.state !== "redeemed"
|
||||
)
|
||||
}
|
||||
return []
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
import { mergeRouters } from "../../.."
|
||||
import { rewardQueryRouter } from "./query"
|
||||
|
||||
export const rewardRouter = mergeRouters(rewardQueryRouter)
|
||||
@@ -0,0 +1,24 @@
|
||||
import { z } from "zod"
|
||||
|
||||
import { MembershipLevelEnum } from "@scandic-hotels/common/constants/membershipLevels"
|
||||
|
||||
export const rewardsByLevelInput = z.object({
|
||||
level_id: z.nativeEnum(MembershipLevelEnum),
|
||||
unique: z.boolean().default(false),
|
||||
})
|
||||
|
||||
export const rewardsAllInput = z
|
||||
.object({ unique: z.boolean() })
|
||||
.default({ unique: false })
|
||||
|
||||
export const rewardsUpdateInput = z.array(
|
||||
z.object({
|
||||
rewardId: z.string(),
|
||||
couponCode: z.string(),
|
||||
})
|
||||
)
|
||||
|
||||
export const rewardsRedeemInput = z.object({
|
||||
rewardId: z.string(),
|
||||
couponCode: z.string().optional(),
|
||||
})
|
||||
@@ -0,0 +1,175 @@
|
||||
import { z } from "zod"
|
||||
|
||||
import { MembershipLevelEnum } from "@scandic-hotels/common/constants/membershipLevels"
|
||||
|
||||
import {
|
||||
linkRefsUnionSchema,
|
||||
linkUnionSchema,
|
||||
transformPageLink,
|
||||
} from "../schemas/pageLinks"
|
||||
import { systemSchema } from "../schemas/system"
|
||||
|
||||
export {
|
||||
BenefitReward,
|
||||
CouponData,
|
||||
CouponReward,
|
||||
REDEEM_LOCATIONS,
|
||||
REWARD_TYPES,
|
||||
rewardRefsSchema,
|
||||
validateApiAllTiersSchema,
|
||||
validateCategorizedRewardsSchema,
|
||||
validateCmsRewardsSchema,
|
||||
}
|
||||
|
||||
const validateCmsRewardsSchema = z
|
||||
.object({
|
||||
data: z.object({
|
||||
all_reward: z.object({
|
||||
items: z.array(
|
||||
z.object({
|
||||
taxonomies: z.array(
|
||||
z.object({
|
||||
term_uid: z.string().optional().default(""),
|
||||
})
|
||||
),
|
||||
label: z.string().optional(),
|
||||
reward_id: z.string(),
|
||||
grouped_label: z.string().optional(),
|
||||
description: z.string().optional(),
|
||||
redeem_description: z
|
||||
.object({
|
||||
json: z.any(), // JSON
|
||||
embedded_itemsConnection: z.object({
|
||||
edges: z.array(
|
||||
z.object({
|
||||
node: linkUnionSchema.transform((data) => {
|
||||
const link = transformPageLink(data)
|
||||
if (link) {
|
||||
return link
|
||||
}
|
||||
return data
|
||||
}),
|
||||
})
|
||||
),
|
||||
}),
|
||||
})
|
||||
// This is primarily added in order to handle a transition
|
||||
// switching from string to RTE
|
||||
.nullable(),
|
||||
grouped_description: z.string().optional(),
|
||||
value: z.string().optional(),
|
||||
})
|
||||
),
|
||||
}),
|
||||
}),
|
||||
})
|
||||
.transform((data) => data.data.all_reward.items)
|
||||
|
||||
const rewardRefsSchema = z.object({
|
||||
data: z.object({
|
||||
all_reward: z.object({
|
||||
items: z.array(
|
||||
z.object({
|
||||
redeem_description: z
|
||||
.object({
|
||||
embedded_itemsConnection: z.object({
|
||||
edges: z.array(
|
||||
z.object({
|
||||
node: linkRefsUnionSchema,
|
||||
})
|
||||
),
|
||||
}),
|
||||
})
|
||||
// This is primarily added in order to handle a transition
|
||||
// switching from string to RTE
|
||||
.nullable(),
|
||||
system: systemSchema,
|
||||
})
|
||||
),
|
||||
}),
|
||||
}),
|
||||
})
|
||||
|
||||
const REDEEM_LOCATIONS = ["Non-redeemable", "On-site", "Online"] as const
|
||||
const REWARD_CATEGORIES = [
|
||||
"Restaurants",
|
||||
"Bar",
|
||||
"Voucher",
|
||||
"Services and rooms",
|
||||
"Spa and gym",
|
||||
] as const
|
||||
|
||||
const BaseReward = z.object({
|
||||
title: z.string().optional(),
|
||||
id: z.string(),
|
||||
categories: z
|
||||
.array(z.enum(REWARD_CATEGORIES).or(z.literal("")))
|
||||
.optional()
|
||||
// we sometimes receive empty categories, this filters them out
|
||||
.transform((categories = []) =>
|
||||
categories.filter(
|
||||
(c): c is (typeof REWARD_CATEGORIES)[number] => c !== ""
|
||||
)
|
||||
),
|
||||
rewardId: z.string(),
|
||||
redeemLocation: z.enum(REDEEM_LOCATIONS),
|
||||
status: z.enum(["active", "expired"]),
|
||||
})
|
||||
|
||||
const REWARD_TYPES = {
|
||||
Surprise: "Surprise",
|
||||
Campaign: "Campaign",
|
||||
MemberVoucher: "Member-voucher",
|
||||
Tier: "Tier",
|
||||
} as const
|
||||
|
||||
const BenefitReward = BaseReward.merge(
|
||||
z.object({
|
||||
rewardType: z.enum([REWARD_TYPES.Tier]),
|
||||
rewardTierLevel: z.string().optional(),
|
||||
})
|
||||
)
|
||||
|
||||
const CouponData = z.object({
|
||||
couponCode: z.string(),
|
||||
unwrapped: z.boolean().default(false),
|
||||
state: z.enum(["claimed", "redeemed", "viewed"]),
|
||||
expiresAt: z.string().datetime({ offset: true }).optional(),
|
||||
})
|
||||
|
||||
const CouponReward = BaseReward.merge(
|
||||
z.object({
|
||||
rewardType: z.enum([
|
||||
REWARD_TYPES.Surprise,
|
||||
REWARD_TYPES.Campaign,
|
||||
REWARD_TYPES.MemberVoucher,
|
||||
]),
|
||||
operaRewardId: z.string().default(""),
|
||||
coupon: z
|
||||
.array(CouponData)
|
||||
.optional()
|
||||
.transform((val) => val || []),
|
||||
})
|
||||
)
|
||||
|
||||
const validateCategorizedRewardsSchema = z.object({
|
||||
benefits: z.array(BenefitReward),
|
||||
coupons: z.array(CouponReward),
|
||||
})
|
||||
|
||||
const TierKeyMapping = {
|
||||
tier1: MembershipLevelEnum.L1,
|
||||
tier2: MembershipLevelEnum.L2,
|
||||
tier3: MembershipLevelEnum.L3,
|
||||
tier4: MembershipLevelEnum.L4,
|
||||
tier5: MembershipLevelEnum.L5,
|
||||
tier6: MembershipLevelEnum.L6,
|
||||
tier7: MembershipLevelEnum.L7,
|
||||
} as const
|
||||
|
||||
const TierKeys = Object.keys(TierKeyMapping) as [keyof typeof TierKeyMapping]
|
||||
|
||||
const validateApiAllTiersSchema = z.record(
|
||||
z.enum(TierKeys).transform((data) => TierKeyMapping[data]),
|
||||
z.array(BenefitReward)
|
||||
)
|
||||
@@ -0,0 +1,391 @@
|
||||
import { createCounter } from "@scandic-hotels/common/telemetry"
|
||||
|
||||
import { router } from "../../.."
|
||||
import * as api from "../../../api"
|
||||
import { notFound } from "../../../errors"
|
||||
import {
|
||||
contentStackBaseWithProtectedProcedure,
|
||||
contentStackBaseWithServiceProcedure,
|
||||
protectedProcedure,
|
||||
} from "../../../procedures"
|
||||
import { langInput } from "../../../utils"
|
||||
import { getAllLoyaltyLevels, getLoyaltyLevel } from "../loyaltyLevel/query"
|
||||
import { getRedeemableRewards, getUnwrappedSurpriseRewards } from "./helpers"
|
||||
import {
|
||||
rewardsAllInput,
|
||||
rewardsByLevelInput,
|
||||
rewardsRedeemInput,
|
||||
rewardsUpdateInput,
|
||||
} from "./input"
|
||||
import { validateCategorizedRewardsSchema } from "./output"
|
||||
import {
|
||||
getCachedAllTierRewards,
|
||||
getCmsRewards,
|
||||
getUniqueRewardIds,
|
||||
} from "./utils"
|
||||
|
||||
import type { CMSReward } from "../../../types/reward"
|
||||
import type { BaseReward, Surprise } from "../../../types/rewards"
|
||||
import type { LevelWithRewards } from "../loyaltyLevel/output"
|
||||
|
||||
export const rewardQueryRouter = router({
|
||||
all: contentStackBaseWithServiceProcedure
|
||||
.input(rewardsAllInput)
|
||||
.query(async function ({ input, ctx }) {
|
||||
const getContentstackRewardAllCounter = createCounter(
|
||||
"trpc.contentstack",
|
||||
"reward.all"
|
||||
)
|
||||
const metricsGetContentstackRewardAll =
|
||||
getContentstackRewardAllCounter.init()
|
||||
|
||||
metricsGetContentstackRewardAll.start()
|
||||
|
||||
const allApiRewards = await getCachedAllTierRewards(ctx.serviceToken)
|
||||
|
||||
if (!allApiRewards) {
|
||||
return []
|
||||
}
|
||||
|
||||
const rewardIds = Object.values(allApiRewards)
|
||||
.flatMap((level) => level.map((reward) => reward?.rewardId))
|
||||
.filter((id): id is string => Boolean(id))
|
||||
|
||||
const contentStackRewards = await getCmsRewards(
|
||||
ctx.lang,
|
||||
getUniqueRewardIds(rewardIds)
|
||||
)
|
||||
|
||||
if (!contentStackRewards) {
|
||||
return []
|
||||
}
|
||||
|
||||
const loyaltyLevelsConfig = await getAllLoyaltyLevels(ctx.lang)
|
||||
const levelsWithRewards = Object.entries(allApiRewards).map(
|
||||
([level, rewards]) => {
|
||||
const combinedRewards = rewards
|
||||
.filter((reward) =>
|
||||
input.unique ? reward.rewardTierLevel === level : true
|
||||
)
|
||||
.map((reward) => {
|
||||
const contentStackReward = contentStackRewards.find((r) => {
|
||||
return r.reward_id === reward.rewardId
|
||||
})
|
||||
|
||||
if (contentStackReward) {
|
||||
return contentStackReward
|
||||
} else {
|
||||
metricsGetContentstackRewardAll.dataError(
|
||||
`Failed to find reward in CMS for reward ${reward.rewardId} `,
|
||||
{
|
||||
rewardId: reward.rewardId,
|
||||
}
|
||||
)
|
||||
}
|
||||
})
|
||||
.filter((reward): reward is CMSReward => Boolean(reward))
|
||||
|
||||
const levelConfig = loyaltyLevelsConfig.find(
|
||||
(l) => l.level_id === level
|
||||
)
|
||||
|
||||
if (!levelConfig) {
|
||||
metricsGetContentstackRewardAll.dataError(
|
||||
`Failed to matched loyalty level between API and CMS for level ${level}`
|
||||
)
|
||||
throw notFound()
|
||||
}
|
||||
const result: LevelWithRewards = {
|
||||
...levelConfig,
|
||||
rewards: combinedRewards,
|
||||
}
|
||||
return result
|
||||
}
|
||||
)
|
||||
|
||||
metricsGetContentstackRewardAll.success()
|
||||
|
||||
return levelsWithRewards
|
||||
}),
|
||||
byLevel: contentStackBaseWithServiceProcedure
|
||||
.input(rewardsByLevelInput)
|
||||
.query(async function ({ input, ctx }) {
|
||||
const { level_id } = input
|
||||
|
||||
const getRewardByLevelCounter = createCounter(
|
||||
"trpc.contentstack",
|
||||
"reward.byLevel"
|
||||
)
|
||||
const metricsGetRewardByLevel = getRewardByLevelCounter.init({
|
||||
level_id,
|
||||
})
|
||||
|
||||
metricsGetRewardByLevel.start()
|
||||
|
||||
const allUpcomingApiRewards = await getCachedAllTierRewards(
|
||||
ctx.serviceToken
|
||||
)
|
||||
|
||||
if (!allUpcomingApiRewards || !allUpcomingApiRewards[level_id]) {
|
||||
metricsGetRewardByLevel.noDataError()
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
let apiRewards = allUpcomingApiRewards[level_id]!
|
||||
|
||||
if (input.unique) {
|
||||
apiRewards = allUpcomingApiRewards[level_id]!.filter(
|
||||
(reward) => reward?.rewardTierLevel === level_id
|
||||
)
|
||||
}
|
||||
|
||||
const rewardIds = apiRewards
|
||||
.map((reward) => reward?.rewardId)
|
||||
.filter((id): id is string => Boolean(id))
|
||||
|
||||
const [contentStackRewards, loyaltyLevelsConfig] = await Promise.all([
|
||||
getCmsRewards(ctx.lang, rewardIds),
|
||||
getLoyaltyLevel(ctx.lang, input.level_id),
|
||||
])
|
||||
|
||||
if (!contentStackRewards) {
|
||||
return null
|
||||
}
|
||||
|
||||
const levelsWithRewards = apiRewards
|
||||
.map((reward) => {
|
||||
const contentStackReward = contentStackRewards.find((r) => {
|
||||
return r.reward_id === reward.rewardId
|
||||
})
|
||||
|
||||
if (contentStackReward) {
|
||||
return contentStackReward
|
||||
} else {
|
||||
metricsGetRewardByLevel.dataError(
|
||||
`Failed to find reward in Contentstack with rewardId: ${reward.rewardId}`,
|
||||
{
|
||||
rewardId: reward.rewardId,
|
||||
}
|
||||
)
|
||||
}
|
||||
})
|
||||
.filter((reward): reward is CMSReward => Boolean(reward))
|
||||
|
||||
metricsGetRewardByLevel.success()
|
||||
|
||||
return { level: loyaltyLevelsConfig, rewards: levelsWithRewards }
|
||||
}),
|
||||
current: contentStackBaseWithProtectedProcedure
|
||||
.input(langInput.optional()) // lang is required for client, but not for server
|
||||
.query(async function ({ ctx }) {
|
||||
const getCurrentRewardCounter = createCounter(
|
||||
"trpc.contentstack",
|
||||
"reward.current"
|
||||
)
|
||||
const metricsGetCurrentReward = getCurrentRewardCounter.init()
|
||||
|
||||
metricsGetCurrentReward.start()
|
||||
|
||||
const apiResponse = await api.get(
|
||||
api.endpoints.v1.Profile.Reward.reward,
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${ctx.session.token.access_token}`,
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
if (!apiResponse.ok) {
|
||||
await metricsGetCurrentReward.httpError(apiResponse)
|
||||
return null
|
||||
}
|
||||
|
||||
const data = await apiResponse.json()
|
||||
|
||||
const validatedApiRewards =
|
||||
validateCategorizedRewardsSchema.safeParse(data)
|
||||
|
||||
if (!validatedApiRewards.success) {
|
||||
metricsGetCurrentReward.validationError(validatedApiRewards.error)
|
||||
return null
|
||||
}
|
||||
|
||||
const { benefits, coupons } = validatedApiRewards.data
|
||||
const redeemableRewards = getRedeemableRewards([...benefits, ...coupons])
|
||||
const rewardIds = redeemableRewards.map(({ rewardId }) => rewardId).sort()
|
||||
const cmsRewards = await getCmsRewards(ctx.lang, rewardIds)
|
||||
if (!cmsRewards) {
|
||||
return null
|
||||
}
|
||||
|
||||
const rewards: BaseReward[] = cmsRewards.map((cmsReward) => {
|
||||
// Non-null assertion is used here because we know our reward exist
|
||||
const apiReward = redeemableRewards.find(
|
||||
({ rewardId }) => rewardId === cmsReward.reward_id
|
||||
)!
|
||||
|
||||
return {
|
||||
...apiReward,
|
||||
...cmsReward,
|
||||
}
|
||||
})
|
||||
|
||||
metricsGetCurrentReward.success()
|
||||
|
||||
return { rewards }
|
||||
}),
|
||||
surprises: contentStackBaseWithProtectedProcedure
|
||||
.input(langInput.optional()) // lang is required for client, but not for server
|
||||
.query(async ({ ctx }) => {
|
||||
const getSurprisesCounter = createCounter(
|
||||
"trpc.contentstack",
|
||||
"surprises"
|
||||
)
|
||||
const metricsGetSurprises = getSurprisesCounter.init()
|
||||
|
||||
metricsGetSurprises.start()
|
||||
|
||||
const endpoint = api.endpoints.v1.Profile.Reward.reward
|
||||
|
||||
const apiResponse = await api.get(endpoint, {
|
||||
cache: undefined,
|
||||
headers: {
|
||||
Authorization: `Bearer ${ctx.session.token.access_token}`,
|
||||
},
|
||||
})
|
||||
|
||||
if (!apiResponse.ok) {
|
||||
await metricsGetSurprises.httpError(apiResponse)
|
||||
return null
|
||||
}
|
||||
|
||||
const data = await apiResponse.json()
|
||||
const validatedApiRewards =
|
||||
validateCategorizedRewardsSchema.safeParse(data)
|
||||
|
||||
if (!validatedApiRewards.success) {
|
||||
metricsGetSurprises.validationError(validatedApiRewards.error)
|
||||
return null
|
||||
}
|
||||
|
||||
const unwrappedSurpriseRewards = getUnwrappedSurpriseRewards(
|
||||
validatedApiRewards.data.coupons
|
||||
)
|
||||
const rewardIds = unwrappedSurpriseRewards
|
||||
.map(({ rewardId }) => rewardId)
|
||||
.sort()
|
||||
const cmsRewards = await getCmsRewards(ctx.lang, rewardIds)
|
||||
if (!cmsRewards) {
|
||||
return null
|
||||
}
|
||||
|
||||
const surprises: Surprise[] = cmsRewards.map((cmsReward) => {
|
||||
// Non-null assertion is used here because we know our reward exist
|
||||
const apiReward = unwrappedSurpriseRewards.find(
|
||||
({ rewardId }) => rewardId === cmsReward.reward_id
|
||||
)!
|
||||
|
||||
return {
|
||||
...apiReward,
|
||||
...cmsReward,
|
||||
}
|
||||
})
|
||||
|
||||
metricsGetSurprises.success()
|
||||
|
||||
return surprises
|
||||
}),
|
||||
unwrap: protectedProcedure
|
||||
.input(rewardsUpdateInput)
|
||||
.mutation(async ({ input, ctx }) => {
|
||||
const results = await Promise.allSettled(
|
||||
// Execute each unwrap individually
|
||||
input.map(({ rewardId, couponCode }) => {
|
||||
async function handleUnwrap() {
|
||||
const getUnwrapSurpriseCounter = createCounter(
|
||||
"trpc.contentstack",
|
||||
"reward.unwrap"
|
||||
)
|
||||
|
||||
const metricsGetUnwrapSurprise = getUnwrapSurpriseCounter.init({
|
||||
rewardId,
|
||||
couponCode,
|
||||
})
|
||||
|
||||
metricsGetUnwrapSurprise.start()
|
||||
|
||||
const apiResponse = await api.post(
|
||||
api.endpoints.v1.Profile.Reward.unwrap,
|
||||
{
|
||||
body: {
|
||||
rewardId,
|
||||
couponCode,
|
||||
},
|
||||
headers: {
|
||||
Authorization: `Bearer ${ctx.session.token.access_token}`,
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
if (!apiResponse.ok) {
|
||||
metricsGetUnwrapSurprise.httpError(apiResponse)
|
||||
return false
|
||||
}
|
||||
|
||||
metricsGetUnwrapSurprise.success()
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
return handleUnwrap()
|
||||
})
|
||||
)
|
||||
|
||||
if (
|
||||
results.some(
|
||||
(result) => result.status === "rejected" || result.value === false
|
||||
)
|
||||
) {
|
||||
return null
|
||||
}
|
||||
|
||||
return true
|
||||
}),
|
||||
redeem: protectedProcedure
|
||||
.input(rewardsRedeemInput)
|
||||
.mutation(async ({ input, ctx }) => {
|
||||
const { rewardId, couponCode } = input
|
||||
|
||||
const getRedeemCounter = createCounter(
|
||||
"trpc.contentstack",
|
||||
"reward.redeem"
|
||||
)
|
||||
|
||||
const metricGetRedeem = getRedeemCounter.init({ rewardId, couponCode })
|
||||
|
||||
metricGetRedeem.start()
|
||||
|
||||
const apiResponse = await api.post(
|
||||
api.endpoints.v1.Profile.Reward.redeem,
|
||||
{
|
||||
body: {
|
||||
rewardId,
|
||||
couponCode,
|
||||
},
|
||||
headers: {
|
||||
Authorization: `Bearer ${ctx.session.token.access_token}`,
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
if (!apiResponse.ok) {
|
||||
metricGetRedeem.httpError(apiResponse)
|
||||
return null
|
||||
}
|
||||
|
||||
metricGetRedeem.success()
|
||||
|
||||
return true
|
||||
}),
|
||||
})
|
||||
@@ -0,0 +1,166 @@
|
||||
import { getCacheClient } from "@scandic-hotels/common/dataCache"
|
||||
import { createCounter } from "@scandic-hotels/common/telemetry"
|
||||
|
||||
import * as api from "../../../api"
|
||||
import { notFound } from "../../../errors"
|
||||
import {
|
||||
GetRewards as GetRewards,
|
||||
GetRewardsRef as GetRewardsRef,
|
||||
} from "../../../graphql/Query/RewardsWithRedeem.graphql"
|
||||
import { request } from "../../../graphql/request"
|
||||
import {
|
||||
generateLoyaltyConfigTag,
|
||||
generateRefsResponseTag,
|
||||
} from "../../../utils/generateTag"
|
||||
import {
|
||||
rewardRefsSchema,
|
||||
validateApiAllTiersSchema,
|
||||
validateCmsRewardsSchema,
|
||||
} from "./output"
|
||||
|
||||
import type { Lang } from "@scandic-hotels/common/constants/language"
|
||||
|
||||
import type {
|
||||
CMSRewardsResponse,
|
||||
GetRewardRefsSchema,
|
||||
} from "../../../types/reward"
|
||||
|
||||
export function getUniqueRewardIds(rewardIds: string[]) {
|
||||
const uniqueRewardIds = new Set(rewardIds)
|
||||
return Array.from(uniqueRewardIds)
|
||||
}
|
||||
|
||||
/**
|
||||
* Cached for 1 hour.
|
||||
*/
|
||||
export async function getCachedAllTierRewards(token: string) {
|
||||
const cacheClient = await getCacheClient()
|
||||
|
||||
return await cacheClient.cacheOrGet(
|
||||
"getAllTierRewards",
|
||||
async () => {
|
||||
const getApiRewardAllTiersCounter = createCounter(
|
||||
"trpc.api",
|
||||
"reward.allTiers"
|
||||
)
|
||||
const metricsGetApiRewardAllTiers = getApiRewardAllTiersCounter.init()
|
||||
|
||||
metricsGetApiRewardAllTiers.start()
|
||||
|
||||
const apiResponse = await api.get(
|
||||
api.endpoints.v1.Profile.Reward.allTiers,
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
if (!apiResponse.ok) {
|
||||
metricsGetApiRewardAllTiers.httpError(apiResponse)
|
||||
throw apiResponse
|
||||
}
|
||||
|
||||
const data = await apiResponse.json()
|
||||
const validatedApiAllTierRewards =
|
||||
validateApiAllTiersSchema.safeParse(data)
|
||||
|
||||
if (!validatedApiAllTierRewards.success) {
|
||||
metricsGetApiRewardAllTiers.validationError(
|
||||
validatedApiAllTierRewards.error
|
||||
)
|
||||
throw validatedApiAllTierRewards.error
|
||||
}
|
||||
|
||||
metricsGetApiRewardAllTiers.success()
|
||||
|
||||
return validatedApiAllTierRewards.data
|
||||
},
|
||||
"1h"
|
||||
)
|
||||
}
|
||||
|
||||
export async function getCmsRewards(lang: Lang, rewardIds: string[]) {
|
||||
if (!rewardIds.length) {
|
||||
return null
|
||||
}
|
||||
|
||||
const tags = rewardIds.map((id) =>
|
||||
generateLoyaltyConfigTag(lang, "reward", id)
|
||||
)
|
||||
|
||||
const getContentstackRewardAllRefsCounter = createCounter(
|
||||
"trpc.contentstack",
|
||||
"reward.all.refs"
|
||||
)
|
||||
const metricsGetContentstackRewardAllRefs =
|
||||
getContentstackRewardAllRefsCounter.init({ lang, rewardIds })
|
||||
|
||||
metricsGetContentstackRewardAllRefs.start()
|
||||
|
||||
const refsResponse = await request<GetRewardRefsSchema>(
|
||||
GetRewardsRef,
|
||||
{
|
||||
locale: lang,
|
||||
rewardIds,
|
||||
},
|
||||
{
|
||||
key: rewardIds.map((rewardId) => generateRefsResponseTag(lang, rewardId)),
|
||||
ttl: "max",
|
||||
}
|
||||
)
|
||||
|
||||
if (!refsResponse.data) {
|
||||
const notFoundError = notFound(refsResponse)
|
||||
metricsGetContentstackRewardAllRefs.noDataError()
|
||||
throw notFoundError
|
||||
}
|
||||
|
||||
const validatedRefsData = rewardRefsSchema.safeParse(refsResponse)
|
||||
|
||||
if (!validatedRefsData.success) {
|
||||
metricsGetContentstackRewardAllRefs.validationError(validatedRefsData.error)
|
||||
return null
|
||||
}
|
||||
|
||||
metricsGetContentstackRewardAllRefs.success()
|
||||
|
||||
const getContentstackRewardAllCounter = createCounter(
|
||||
"trpc.contentstack",
|
||||
"reward.all"
|
||||
)
|
||||
const metricsGetContentstackRewardAll = getContentstackRewardAllCounter.init({
|
||||
lang,
|
||||
rewardIds,
|
||||
})
|
||||
|
||||
const cmsRewardsResponse = await request<CMSRewardsResponse>(
|
||||
GetRewards,
|
||||
{
|
||||
locale: lang,
|
||||
rewardIds,
|
||||
},
|
||||
{
|
||||
key: tags,
|
||||
ttl: "max",
|
||||
}
|
||||
)
|
||||
|
||||
if (!cmsRewardsResponse.data) {
|
||||
const notFoundError = notFound(cmsRewardsResponse)
|
||||
metricsGetContentstackRewardAll.noDataError()
|
||||
throw notFoundError
|
||||
}
|
||||
|
||||
const validatedCmsRewards =
|
||||
validateCmsRewardsSchema.safeParse(cmsRewardsResponse)
|
||||
|
||||
if (!validatedCmsRewards.success) {
|
||||
metricsGetContentstackRewardAll.validationError(validatedCmsRewards.error)
|
||||
return null
|
||||
}
|
||||
|
||||
metricsGetContentstackRewardAll.success()
|
||||
|
||||
return validatedCmsRewards.data
|
||||
}
|
||||
Reference in New Issue
Block a user