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 {} export interface StartPage extends z.output {} export type Block = z.output 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( 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, } }), })