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
80 lines
2.2 KiB
TypeScript
80 lines
2.2 KiB
TypeScript
import { createCounter } from "@scandic-hotels/common/telemetry"
|
|
|
|
import { router } from "../../.."
|
|
import { notFoundError } from "../../../errors"
|
|
import { GetStartPage } from "../../../graphql/Query/StartPage/StartPage.graphql"
|
|
import { request } from "../../../graphql/request"
|
|
import { contentstackExtendedProcedureUID } from "../../../procedures"
|
|
import { generateTag } from "../../../utils/generateTag"
|
|
import { startPageSchema } from "./output"
|
|
|
|
import type { z } from "zod"
|
|
|
|
import type { TrackingPageData } from "../../types"
|
|
import type { blocksSchema } from "./output"
|
|
|
|
export interface GetStartPageData extends z.input<typeof startPageSchema> {}
|
|
export interface StartPage extends z.output<typeof startPageSchema> {}
|
|
|
|
export type Block = z.output<typeof blocksSchema>
|
|
|
|
export const startPageQueryRouter = router({
|
|
get: contentstackExtendedProcedureUID.query(async ({ ctx }) => {
|
|
const { lang, uid } = ctx
|
|
|
|
const cacheKey = generateTag(lang, uid)
|
|
|
|
const getStartPageCounter = createCounter("trpc.contentstack.startPage.get")
|
|
const metricsGetStartPage = getStartPageCounter.init({ lang, uid })
|
|
|
|
metricsGetStartPage.start()
|
|
|
|
const response = await request<GetStartPageData>(
|
|
GetStartPage,
|
|
{
|
|
locale: lang,
|
|
uid,
|
|
},
|
|
{
|
|
key: `${cacheKey}`,
|
|
ttl: "max",
|
|
}
|
|
)
|
|
|
|
if (!response.data) {
|
|
metricsGetStartPage.noDataError()
|
|
throw notFoundError({
|
|
message: "StartPage not found",
|
|
errorDetails: { lang, uid },
|
|
})
|
|
}
|
|
|
|
const startPage = startPageSchema.safeParse(response.data)
|
|
|
|
if (!startPage.success) {
|
|
metricsGetStartPage.validationError(startPage.error)
|
|
return null
|
|
}
|
|
|
|
metricsGetStartPage.success()
|
|
|
|
const system = startPage.data.start_page.system
|
|
const tracking: TrackingPageData = {
|
|
pageName: "startpage",
|
|
pageType: "startpage",
|
|
pageId: system.uid,
|
|
channel: "homepage",
|
|
domainLanguage: lang,
|
|
createDate: system.created_at,
|
|
publishDate: system.updated_at,
|
|
siteSections: "startpage",
|
|
siteVersion: "new-web",
|
|
}
|
|
|
|
return {
|
|
startPage: startPage.data.start_page,
|
|
tracking,
|
|
}
|
|
}),
|
|
})
|