Merged in feat/rework-contentstack (pull request #3493)
Feat(SW-3708): refactor contentstack fetching (removing all refs) and cache invalidation * Remove all REFS * Revalidate correct language * PR fixes * PR fixes * Throw when errors from contentstack api Approved-by: Joakim Jäderberg
This commit is contained in:
@@ -2,12 +2,7 @@ import { z } from "zod"
|
||||
|
||||
import { MembershipLevelEnum } from "@scandic-hotels/common/constants/membershipLevels"
|
||||
|
||||
import {
|
||||
linkRefsUnionSchema,
|
||||
linkUnionSchema,
|
||||
transformPageLink,
|
||||
} from "../schemas/pageLinks"
|
||||
import { systemSchema } from "../schemas/system"
|
||||
import { linkUnionSchema, transformPageLink } from "../schemas/pageLinks"
|
||||
|
||||
export {
|
||||
BenefitReward,
|
||||
@@ -15,7 +10,6 @@ export {
|
||||
CouponReward,
|
||||
REDEEM_LOCATIONS,
|
||||
REWARD_TYPES,
|
||||
rewardRefsSchema,
|
||||
validateApiAllTiersSchema,
|
||||
validateCategorizedRewardsSchema,
|
||||
validateCmsRewardsSchema,
|
||||
@@ -65,31 +59,6 @@ const validateCmsRewardsSchema = z
|
||||
})
|
||||
.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",
|
||||
|
||||
@@ -3,27 +3,14 @@ import { createCounter } from "@scandic-hotels/common/telemetry"
|
||||
|
||||
import * as api from "../../../api"
|
||||
import { notFoundError } from "../../../errors"
|
||||
import {
|
||||
GetRewards as GetRewards,
|
||||
GetRewardsRef as GetRewardsRef,
|
||||
} from "../../../graphql/Query/RewardsWithRedeem.graphql"
|
||||
import { GetRewards } from "../../../graphql/Query/RewardsWithRedeem.graphql"
|
||||
import { request } from "../../../graphql/request"
|
||||
import {
|
||||
generateLoyaltyConfigTag,
|
||||
generateRefsResponseTag,
|
||||
} from "../../../utils/generateTag"
|
||||
import {
|
||||
rewardRefsSchema,
|
||||
validateApiAllTiersSchema,
|
||||
validateCmsRewardsSchema,
|
||||
} from "./output"
|
||||
import { generateLoyaltyConfigTag } from "../../../utils/generateTag"
|
||||
import { validateApiAllTiersSchema, validateCmsRewardsSchema } from "./output"
|
||||
|
||||
import type { Lang } from "@scandic-hotels/common/constants/language"
|
||||
|
||||
import type {
|
||||
CMSRewardsResponse,
|
||||
GetRewardRefsSchema,
|
||||
} from "../../../types/reward"
|
||||
import type { CMSRewardsResponse } from "../../../types/reward"
|
||||
|
||||
export function getUniqueRewardIds(rewardIds: string[]) {
|
||||
const uniqueRewardIds = new Set(rewardIds)
|
||||
@@ -88,44 +75,6 @@ export async function getCmsRewards(lang: Lang, rewardIds: string[]) {
|
||||
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) {
|
||||
metricsGetContentstackRewardAllRefs.noDataError()
|
||||
|
||||
throw notFoundError({
|
||||
message: "GetRewardsRef returned no data",
|
||||
errorDetails: { lang, rewardIds },
|
||||
})
|
||||
}
|
||||
|
||||
const validatedRefsData = rewardRefsSchema.safeParse(refsResponse)
|
||||
|
||||
if (!validatedRefsData.success) {
|
||||
metricsGetContentstackRewardAllRefs.validationError(validatedRefsData.error)
|
||||
return null
|
||||
}
|
||||
|
||||
metricsGetContentstackRewardAllRefs.success()
|
||||
|
||||
const getContentstackRewardAllCounter = createCounter(
|
||||
"trpc.contentstack.reward.all"
|
||||
)
|
||||
@@ -134,6 +83,8 @@ export async function getCmsRewards(lang: Lang, rewardIds: string[]) {
|
||||
rewardIds,
|
||||
})
|
||||
|
||||
metricsGetContentstackRewardAll.start()
|
||||
|
||||
const cmsRewardsResponse = await request<CMSRewardsResponse>(
|
||||
GetRewards,
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user