feat(SW-2859): Create trpc package * Add isEdge, safeTry and dataCache to new common package * Add eslint and move prettier config * Clean up tests * Create trpc package and move initialization * Move errors and a few procedures * Move telemetry to common package * Move tokenManager to common package * Add Sentry to procedures * Clean up procedures * Fix self-referencing imports * Add exports to packages and lint rule to prevent relative imports * Add env to trpc package * Add eslint to trpc package * Apply lint rules * Use direct imports from trpc package * Add lint-staged config to trpc * Move lang enum to common * Restructure trpc package folder structure * Fix lang imports Approved-by: Linus Flood
83 lines
2.4 KiB
TypeScript
83 lines
2.4 KiB
TypeScript
import { createCounter } from "@scandic-hotels/common/telemetry"
|
|
import { router } from "@scandic-hotels/trpc"
|
|
import { contentstackExtendedProcedureUID } from "@scandic-hotels/trpc/procedures"
|
|
|
|
import { GetCollectionPage } from "@/lib/graphql/Query/CollectionPage/CollectionPage.graphql"
|
|
import { request } from "@/lib/graphql/request"
|
|
|
|
import { collectionPageSchema } from "./output"
|
|
import {
|
|
fetchCollectionPageRefs,
|
|
generatePageTags,
|
|
validateCollectionPageRefs,
|
|
} from "./utils"
|
|
|
|
import {
|
|
TrackingChannelEnum,
|
|
type TrackingSDKPageData,
|
|
} from "@/types/components/tracking"
|
|
import type { GetCollectionPageSchema } from "@/types/trpc/routers/contentstack/collectionPage"
|
|
|
|
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: TrackingSDKPageData = {
|
|
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: TrackingChannelEnum["collection-page"],
|
|
pageType: "collectionpage",
|
|
pageName: collectionPage.data.trackingProps.url,
|
|
siteSections: collectionPage.data.trackingProps.url,
|
|
siteVersion: "new-web",
|
|
}
|
|
|
|
return {
|
|
collectionPage: collectionPage.data.collection_page,
|
|
tracking,
|
|
}
|
|
}),
|
|
})
|