import { createCounter } from "@scandic-hotels/common/telemetry" import { router } from "../../.." import { GetCollectionPage } from "../../../graphql/Query/CollectionPage/CollectionPage.graphql" import { request } from "../../../graphql/request" import { contentstackExtendedProcedureUID } from "../../../procedures" import { generateTag } from "../../../utils/generateTag" import { collectionPageSchema } from "./output" import type { GetCollectionPageSchema } from "../../../types/collectionPage" import type { TrackingPageData } from "../../types" export const collectionPageQueryRouter = router({ get: contentstackExtendedProcedureUID.query(async ({ ctx }) => { const { lang, uid } = ctx const cacheKey = generateTag(lang, uid) const getCollectionPageCounter = createCounter( "trpc.contentstack.collectionPage.get" ) const metricsGetCollectionPage = getCollectionPageCounter.init({ lang, uid, }) metricsGetCollectionPage.start() const response = await request( GetCollectionPage, { locale: lang, uid }, { key: `${cacheKey}:collectionPage`, ttl: "max", } ) const collectionPage = collectionPageSchema.safeParse(response.data) if (!collectionPage.success) { metricsGetCollectionPage.validationError(collectionPage.error) return null } metricsGetCollectionPage.success() const tracking: TrackingPageData = { pageId: collectionPage.data.collection_page.system.uid, domainLanguage: lang, publishDate: collectionPage.data.collection_page.system.updated_at, createDate: collectionPage.data.collection_page.system.created_at, channel: "collection-page", pageType: "collectionpage", pageName: collectionPage.data.trackingProps.url, siteSections: collectionPage.data.trackingProps.url, siteVersion: "new-web", } return { collectionPage: collectionPage.data.collection_page, tracking, } }), })