Files
web/apps/scandic-web/server/routers/contentstack/startPage/query.ts
Anton Gunnarsson 846fd904a6 Merged in feat/sw-2859-set-up-shared-trpc-package (pull request #2319)
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
2025-06-18 12:14:20 +00:00

129 lines
3.2 KiB
TypeScript

import { createCounter } from "@scandic-hotels/common/telemetry"
import { router } from "@scandic-hotels/trpc"
import { notFound } from "@scandic-hotels/trpc/errors"
import { contentstackExtendedProcedureUID } from "@scandic-hotels/trpc/procedures"
import {
GetStartPage,
GetStartPageRefs,
} from "@/lib/graphql/Query/StartPage/StartPage.graphql"
import { request } from "@/lib/graphql/request"
import {
generateRefsResponseTag,
generateTag,
generateTagsFromSystem,
} from "@/utils/generateTag"
import { startPageRefsSchema, startPageSchema } from "./output"
import { getConnections } from "./utils"
import {
TrackingChannelEnum,
type TrackingSDKPageData,
} from "@/types/components/tracking"
import type {
GetStartPageData,
GetStartPageRefsSchema,
} from "@/types/trpc/routers/contentstack/startPage"
export const startPageQueryRouter = router({
get: contentstackExtendedProcedureUID.query(async ({ ctx }) => {
const { lang, uid } = ctx
const getStartPageRefsCounter = createCounter(
"trpc.contentstack",
"startPage.get.refs"
)
const metricsGetStartPageRefs = getStartPageRefsCounter.init({ lang, uid })
metricsGetStartPageRefs.start()
const refsResponse = await request<GetStartPageRefsSchema>(
GetStartPageRefs,
{
locale: lang,
uid,
},
{
key: generateRefsResponseTag(lang, uid),
ttl: "max",
}
)
if (!refsResponse.data) {
const notFoundError = notFound(refsResponse)
metricsGetStartPageRefs.noDataError()
throw notFoundError
}
const validatedRefsData = startPageRefsSchema.safeParse(refsResponse.data)
if (!validatedRefsData.success) {
metricsGetStartPageRefs.validationError(validatedRefsData.error)
return null
}
metricsGetStartPageRefs.success()
const getStartPageCounter = createCounter(
"trpc.contentstack",
"startPage.get"
)
const metricsGetStartPage = getStartPageCounter.init({ lang, uid })
metricsGetStartPage.start()
const connections = getConnections(validatedRefsData.data)
const tags = [
generateTagsFromSystem(lang, connections),
generateTag(lang, validatedRefsData.data.start_page.system.uid),
].flat()
const response = await request<GetStartPageData>(
GetStartPage,
{
locale: lang,
uid,
},
{
key: tags,
ttl: "max",
}
)
if (!response.data) {
const notFoundError = notFound(response)
metricsGetStartPage.noDataError()
throw notFoundError
}
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: TrackingSDKPageData = {
pageName: "startpage",
pageType: "startpage",
pageId: system.uid,
channel: TrackingChannelEnum["homepage"],
domainLanguage: lang,
createDate: system.created_at,
publishDate: system.updated_at,
siteSections: "startpage",
siteVersion: "new-web",
}
return {
startPage: startPage.data.start_page,
tracking,
}
}),
})