import { Lang } from "@/constants/languages" import { GetContentPage } from "@/lib/graphql/Query/ContentPage.graphql" import { request } from "@/lib/graphql/request" import { notFound } from "@/server/errors/trpc" import { contentstackExtendedProcedureUID, router } from "@/server/trpc" import { generateTag } from "@/utils/generateTag" import { makeImageVaultImage } from "@/utils/imageVault" import { validateContentPageSchema } from "./output" import { TrackingChannelEnum, TrackingSDKPageData, } from "@/types/components/tracking" import { ContentPage, ContentPageDataRaw, } from "@/types/trpc/routers/contentstack/contentPage" export const contentPageQueryRouter = router({ get: contentstackExtendedProcedureUID.query(async ({ ctx }) => { const { lang, uid } = ctx // TODO: Refs request should be done when adding more data to this query // which has references to other pages. const response = await request( GetContentPage, { locale: lang, uid, }, { tags: [generateTag(lang, uid)] } ) if (!response.data) { throw notFound(response) } const validatedContentPage = validateContentPageSchema.safeParse( response.data ) if (!validatedContentPage.success) { console.error( `Failed to validate Contentpage Data - (lang: ${lang}, uid: ${uid})` ) console.error(validatedContentPage.error) return null } const contentPageData = validatedContentPage.data.content_page const contentPage: ContentPage = { ...contentPageData, heroImage: makeImageVaultImage(contentPageData.hero_image), } const tracking: TrackingSDKPageData = { pageId: contentPageData.system.uid, lang: contentPageData.system.locale as Lang, publishedDate: contentPageData.system.updated_at, createdDate: contentPageData.system.created_at, channel: TrackingChannelEnum["static-content-page"], pageType: "staticcontentpage", } return { contentPage, tracking, } }), })