Files
web/packages/trpc/lib/routers/contentstack/destinationCountryPage/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

142 lines
4.0 KiB
TypeScript

import { createCounter } from "@scandic-hotels/common/telemetry"
import { router } from "../../.."
import { notFound } from "../../../errors"
import {
GetDestinationCountryPage,
GetDestinationCountryPageRefs,
} from "../../../graphql/Query/DestinationCountryPage/DestinationCountryPage.graphql"
import { request } from "../../../graphql/request"
import {
contentStackBaseWithServiceProcedure,
contentstackExtendedProcedureUID,
} from "../../../procedures"
import { ApiCountry } from "../../../types/country"
import { generateRefsResponseTag } from "../../../utils/generateTag"
import { getCityPagesInput } from "./input"
import {
destinationCountryPageRefsSchema,
destinationCountryPageSchema,
} from "./output"
import { generatePageTags, getCityPages } from "./utils"
import type {
GetDestinationCountryPageData,
GetDestinationCountryPageRefsSchema,
} from "../../../types/destinationCountryPage"
import type { TrackingPageData } from "../../types"
export const destinationCountryPageQueryRouter = router({
get: contentstackExtendedProcedureUID.query(async ({ ctx }) => {
const { lang, uid } = ctx
const getDestinationCountryPageRefsCounter = createCounter(
"trpc.contentstack.destinationCountryPage.get.refs"
)
const metricsGetDestinationCountryPageRefs =
getDestinationCountryPageRefsCounter.init({ lang, uid })
metricsGetDestinationCountryPageRefs.start()
const refsResponse = await request<GetDestinationCountryPageRefsSchema>(
GetDestinationCountryPageRefs,
{ locale: lang, uid },
{
key: generateRefsResponseTag(lang, uid),
ttl: "max",
}
)
if (!refsResponse.data) {
const notFoundError = notFound(refsResponse)
metricsGetDestinationCountryPageRefs.noDataError()
throw notFoundError
}
const validatedRefsData = destinationCountryPageRefsSchema.safeParse(
refsResponse.data
)
if (!validatedRefsData.success) {
metricsGetDestinationCountryPageRefs.validationError(
validatedRefsData.error
)
return null
}
metricsGetDestinationCountryPageRefs.success()
const tags = generatePageTags(validatedRefsData.data, lang)
const getDestinationCountryPageCounter = createCounter(
"trpc.contentstack.destinationCountryPage.get"
)
const metricsGetDestinationCountryPage =
getDestinationCountryPageCounter.init({ lang, uid })
metricsGetDestinationCountryPage.start()
const response = await request<GetDestinationCountryPageData>(
GetDestinationCountryPage,
{
locale: lang,
uid,
},
{
key: tags,
ttl: "max",
}
)
if (!response.data) {
const notFoundError = notFound(response)
metricsGetDestinationCountryPage.noDataError()
throw notFoundError
}
const validatedResponse = destinationCountryPageSchema.safeParse(
response.data
)
if (!validatedResponse.success) {
metricsGetDestinationCountryPage.validationError(validatedResponse.error)
return null
}
const destinationCountryPage =
validatedResponse.data.destination_country_page
const country = destinationCountryPage.destination_settings.country
metricsGetDestinationCountryPage.success()
const system = destinationCountryPage.system
const pageName = `destinations|${country}`
const tracking: TrackingPageData = {
pageId: system.uid,
domainLanguage: system.locale,
publishDate: system.updated_at,
createDate: system.created_at,
channel: "hotels",
pageType: "countrypage",
pageName,
siteSections: pageName,
siteVersion: "new-web",
}
return {
destinationCountryPage,
translatedCountry: ApiCountry[lang][country],
tracking,
}
}),
cityPages: contentStackBaseWithServiceProcedure
.input(getCityPagesInput)
.query(async ({ ctx, input }) => {
const { lang, serviceToken } = ctx
const { country } = input
const cities = await getCityPages(lang, serviceToken, country)
return cities
}),
})