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
98 lines
3.0 KiB
TypeScript
98 lines
3.0 KiB
TypeScript
import { createCounter } from "@scandic-hotels/common/telemetry"
|
|
|
|
import { router } from "../../.."
|
|
import { notFoundError } from "../../../errors"
|
|
import { GetDestinationCountryPage } from "../../../graphql/Query/DestinationCountryPage/DestinationCountryPage.graphql"
|
|
import { request } from "../../../graphql/request"
|
|
import {
|
|
contentStackBaseWithServiceProcedure,
|
|
contentstackExtendedProcedureUID,
|
|
} from "../../../procedures"
|
|
import { ApiCountry } from "../../../types/country"
|
|
import { generateTag } from "../../../utils/generateTag"
|
|
import { getCityPagesInput } from "./input"
|
|
import { destinationCountryPageSchema } from "./output"
|
|
import { getCityPages } from "./utils"
|
|
|
|
import type { GetDestinationCountryPageData } from "../../../types/destinationCountryPage"
|
|
import type { TrackingPageData } from "../../types"
|
|
|
|
export const destinationCountryPageQueryRouter = router({
|
|
get: contentstackExtendedProcedureUID.query(async ({ ctx }) => {
|
|
const { lang, uid } = ctx
|
|
|
|
const cacheKey = generateTag(lang, uid)
|
|
|
|
const getDestinationCountryPageCounter = createCounter(
|
|
"trpc.contentstack.destinationCountryPage.get"
|
|
)
|
|
const metricsGetDestinationCountryPage =
|
|
getDestinationCountryPageCounter.init({ lang, uid })
|
|
|
|
metricsGetDestinationCountryPage.start()
|
|
|
|
const variables = { locale: lang, uid }
|
|
const response = await request<GetDestinationCountryPageData>(
|
|
GetDestinationCountryPage,
|
|
variables,
|
|
{
|
|
key: `${cacheKey}:destinationCountryPage`,
|
|
ttl: "max",
|
|
}
|
|
)
|
|
if (!response.data) {
|
|
metricsGetDestinationCountryPage.noDataError()
|
|
throw notFoundError({
|
|
message: "GetDestinationCountryPage returned no data",
|
|
errorDetails: variables,
|
|
})
|
|
}
|
|
|
|
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
|
|
}),
|
|
})
|