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
74 lines
2.2 KiB
TypeScript
74 lines
2.2 KiB
TypeScript
import { createCounter } from "@scandic-hotels/common/telemetry"
|
|
|
|
import { router } from "../../.."
|
|
import { notFoundError } from "../../../errors"
|
|
import { GetLoyaltyPage } from "../../../graphql/Query/LoyaltyPage/LoyaltyPage.graphql"
|
|
import { request } from "../../../graphql/request"
|
|
import { contentstackExtendedProcedureUID } from "../../../procedures"
|
|
import { generateTag } from "../../../utils/generateTag"
|
|
import { loyaltyPageSchema } from "./output"
|
|
|
|
import type { GetLoyaltyPageSchema } from "../../../types/loyaltyPage"
|
|
import type { TrackingPageData } from "../../types"
|
|
|
|
export const loyaltyPageQueryRouter = router({
|
|
get: contentstackExtendedProcedureUID.query(async ({ ctx }) => {
|
|
const { lang, uid } = ctx
|
|
|
|
const cacheKey = generateTag(lang, uid)
|
|
|
|
const getLoyaltyPageCounter = createCounter(
|
|
"trpc.contentstack.loyaltyPage.get"
|
|
)
|
|
const metricsGetLoyaltyPage = getLoyaltyPageCounter.init({ lang, uid })
|
|
|
|
metricsGetLoyaltyPage.start()
|
|
|
|
const variables = { locale: lang, uid }
|
|
const response = await request<GetLoyaltyPageSchema>(
|
|
GetLoyaltyPage,
|
|
variables,
|
|
{
|
|
key: `${cacheKey}:loyaltyPage`,
|
|
ttl: "max",
|
|
}
|
|
)
|
|
|
|
if (!response.data) {
|
|
metricsGetLoyaltyPage.noDataError()
|
|
throw notFoundError({
|
|
message: "GetLoyaltyPage returned no data",
|
|
errorDetails: { lang, uid },
|
|
})
|
|
}
|
|
|
|
const validatedLoyaltyPage = loyaltyPageSchema.safeParse(response.data)
|
|
if (!validatedLoyaltyPage.success) {
|
|
metricsGetLoyaltyPage.validationError(validatedLoyaltyPage.error)
|
|
return null
|
|
}
|
|
|
|
const loyaltyPage = validatedLoyaltyPage.data.loyalty_page
|
|
|
|
const tracking: TrackingPageData = {
|
|
pageId: loyaltyPage.system.uid,
|
|
domainLanguage: lang,
|
|
publishDate: loyaltyPage.system.updated_at,
|
|
createDate: loyaltyPage.system.created_at,
|
|
channel: "scandic-friends",
|
|
pageType: "loyaltycontentpage",
|
|
pageName: validatedLoyaltyPage.data.trackingProps.url,
|
|
siteSections: validatedLoyaltyPage.data.trackingProps.url,
|
|
siteVersion: "new-web",
|
|
}
|
|
|
|
metricsGetLoyaltyPage.success()
|
|
|
|
// Assert LoyaltyPage type to get correct typings for RTE fields
|
|
return {
|
|
loyaltyPage,
|
|
tracking,
|
|
}
|
|
}),
|
|
})
|