Files
web/packages/trpc/lib/routers/contentstack/collectionPage/query.ts
Joakim Jäderberg 8b94540d19 Merged in chore/redirect-counter (pull request #3302)
Counter name is now searchable and add counter for redirects

* refactor: createCounter() only takes one argument, the name of the counter. Makes it easier to search for

* feat: add counter when we do a redirect from redirect-service


Approved-by: Linus Flood
2025-12-08 10:24:05 +00:00

78 lines
2.3 KiB
TypeScript

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 { collectionPageSchema } from "./output"
import {
fetchCollectionPageRefs,
generatePageTags,
validateCollectionPageRefs,
} from "./utils"
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 collectionPageRefsData = await fetchCollectionPageRefs(lang, uid)
const collectionPageRefs = validateCollectionPageRefs(
collectionPageRefsData,
lang,
uid
)
if (!collectionPageRefs) {
return null
}
const tags = generatePageTags(collectionPageRefs, lang)
const getCollectionPageCounter = createCounter(
"trpc.contentstack.collectionPage.get"
)
const metricsGetCollectionPage = getCollectionPageCounter.init({
lang,
uid,
})
metricsGetCollectionPage.start()
const response = await request<GetCollectionPageSchema>(
GetCollectionPage,
{ locale: lang, uid },
{
key: tags,
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,
}
}),
})