Files
Linus Flood 5fc93472f4 Merged in feat/rework-contentstack (pull request #3493)
Feat(SW-3708): refactor contentstack fetching (removing all refs) and cache invalidation

* Remove all REFS

* Revalidate correct language

* PR fixes

* PR fixes

* Throw when errors from contentstack api


Approved-by: Joakim Jäderberg
2026-01-27 12:38:36 +00:00

103 lines
2.9 KiB
TypeScript

import { createCounter } from "@scandic-hotels/common/telemetry"
import { router } from "../../.."
import { notFoundError } from "../../../errors"
import { GetDestinationCityPage } from "../../../graphql/Query/DestinationCityPage/DestinationCityPage.graphql"
import { request } from "../../../graphql/request"
import { contentStackUidWithServiceProcedure } from "../../../procedures"
import { generateTag } from "../../../utils/generateTag"
import { getCityByCityIdentifier } from "../../hotels/services/getCityByCityIdentifier"
import { destinationCityPageSchema } from "./output"
import type { GetDestinationCityPageData } from "../../../types/destinationCityPage"
import type { TrackingPageData } from "../../types"
export const destinationCityPageQueryRouter = router({
get: contentStackUidWithServiceProcedure.query(async ({ ctx }) => {
const { lang, uid, serviceToken } = ctx
const cacheKey = generateTag(lang, uid)
const getDestinationCityPageCounter = createCounter(
"trpc.contentstack.destinationCityPage.get"
)
const metricsGetDestinationCityPage = getDestinationCityPageCounter.init({
lang,
uid,
})
metricsGetDestinationCityPage.start()
const variables = { locale: lang, uid }
const response = await request<GetDestinationCityPageData>(
GetDestinationCityPage,
variables,
{
key: `${cacheKey}:destinationCityPage`,
ttl: "max",
}
)
if (!response.data) {
metricsGetDestinationCityPage.noDataError()
throw notFoundError({
message: "GetDestinationCityPage returned no data",
errorDetails: variables,
})
}
const validatedResponse = destinationCityPageSchema.safeParse(response.data)
if (!validatedResponse.success) {
metricsGetDestinationCityPage.validationError(validatedResponse.error)
return null
}
const destinationCityPage = validatedResponse.data.destination_city_page
const cityIdentifier = destinationCityPage.destination_settings.city
if (!cityIdentifier) {
return null
}
const city = await getCityByCityIdentifier({
cityIdentifier,
lang,
serviceToken,
})
if (!city) {
metricsGetDestinationCityPage.dataError(
`Failed to get city data for ${cityIdentifier}`,
{
cityIdentifier,
}
)
return null
}
metricsGetDestinationCityPage.success()
const system = destinationCityPage.system
const pageName = `destinations|${city.country}|${city.name}`
const tracking: TrackingPageData = {
pageId: system.uid,
domainLanguage: system.locale,
publishDate: system.updated_at,
createDate: system.created_at,
channel: "hotels",
pageType: "citypage",
pageName,
siteSections: pageName,
siteVersion: "new-web",
}
return {
destinationCityPage,
cityIdentifier,
city,
tracking,
}
}),
})