Distributed cache * cache deleteKey now uses an options object instead of a lonely argument variable fuzzy * merge * remove debug logs and cleanup * cleanup * add fault handling * add fault handling * add pid when logging redis client creation * add identifier when logging redis client creation * cleanup * feat: add redis-api as it's own app * feature: use http wrapper for redis * feat: add the possibility to fallback to unstable_cache * Add error handling if redis cache is unresponsive * add logging for unstable_cache * merge * don't cache errors * fix: metadatabase on branchdeploys * Handle when /en/destinations throws add ErrorBoundary * Add sentry-logging when ErrorBoundary catches exception * Fix error handling for distributed cache * cleanup code * Added Application Insights back * Update generateApiKeys script and remove duplicate * Merge branch 'feature/redis' of bitbucket.org:scandic-swap/web into feature/redis * merge Approved-by: Linus Flood
280 lines
7.8 KiB
TypeScript
280 lines
7.8 KiB
TypeScript
import {
|
|
GetDestinationOverviewPage,
|
|
GetDestinationOverviewPageRefs,
|
|
} from "@/lib/graphql/Query/DestinationOverviewPage/DestinationOverviewPage.graphql"
|
|
import { request } from "@/lib/graphql/request"
|
|
import { notFound } from "@/server/errors/trpc"
|
|
import {
|
|
contentstackExtendedProcedureUID,
|
|
router,
|
|
serviceProcedure,
|
|
} from "@/server/trpc"
|
|
|
|
import { generateTag } from "@/utils/generateTag"
|
|
import { safeTry } from "@/utils/safeTry"
|
|
|
|
import {
|
|
getCitiesByCountry,
|
|
getCountries,
|
|
getHotelIdsByCityId,
|
|
} from "../../hotels/utils"
|
|
import { getCityPageUrls } from "../destinationCityPage/utils"
|
|
import { getCountryPageUrls } from "../destinationCountryPage/utils"
|
|
import {
|
|
destinationOverviewPageRefsSchema,
|
|
destinationOverviewPageSchema,
|
|
} from "./output"
|
|
import {
|
|
getDestinationOverviewPageCounter,
|
|
getDestinationOverviewPageFailCounter,
|
|
getDestinationOverviewPageRefsCounter,
|
|
getDestinationOverviewPageRefsFailCounter,
|
|
getDestinationOverviewPageRefsSuccessCounter,
|
|
getDestinationOverviewPageSuccessCounter,
|
|
} from "./telemetry"
|
|
|
|
import type {
|
|
Cities,
|
|
DestinationsData,
|
|
} from "@/types/components/destinationOverviewPage/destinationsList/destinationsData"
|
|
import {
|
|
TrackingChannelEnum,
|
|
type TrackingSDKPageData,
|
|
} from "@/types/components/tracking"
|
|
import type {
|
|
GetDestinationOverviewPageData,
|
|
GetDestinationOverviewPageRefsSchema,
|
|
} from "@/types/trpc/routers/contentstack/destinationOverviewPage"
|
|
|
|
export const destinationOverviewPageQueryRouter = router({
|
|
get: contentstackExtendedProcedureUID.query(async ({ ctx }) => {
|
|
const { lang, uid } = ctx
|
|
|
|
getDestinationOverviewPageRefsCounter.add(1, { lang, uid: `${uid}` })
|
|
console.info(
|
|
"contentstack.destinationOverviewPage.refs start",
|
|
JSON.stringify({
|
|
query: { lang, uid },
|
|
})
|
|
)
|
|
const refsResponse = await request<GetDestinationOverviewPageRefsSchema>(
|
|
GetDestinationOverviewPageRefs,
|
|
{
|
|
locale: lang,
|
|
uid,
|
|
},
|
|
{
|
|
key: generateTag(lang, uid),
|
|
ttl: "max",
|
|
}
|
|
)
|
|
if (!refsResponse.data) {
|
|
const notFoundError = notFound(refsResponse)
|
|
getDestinationOverviewPageRefsFailCounter.add(1, {
|
|
lang,
|
|
uid,
|
|
error_type: "not_found",
|
|
error: JSON.stringify({ code: notFoundError.code }),
|
|
})
|
|
console.error(
|
|
"contentstack.destinationOverviewPage.refs not found error",
|
|
JSON.stringify({
|
|
query: { lang, uid },
|
|
error: { code: notFoundError.code },
|
|
})
|
|
)
|
|
throw notFoundError
|
|
}
|
|
|
|
const validatedRefsData = destinationOverviewPageRefsSchema.safeParse(
|
|
refsResponse.data
|
|
)
|
|
|
|
if (!validatedRefsData.success) {
|
|
getDestinationOverviewPageRefsFailCounter.add(1, {
|
|
lang,
|
|
uid,
|
|
error_type: "validation_error",
|
|
error: JSON.stringify(validatedRefsData.error),
|
|
})
|
|
console.error(
|
|
"contentstack.destinationOverviewPage.refs validation error",
|
|
JSON.stringify({
|
|
query: { lang, uid },
|
|
error: validatedRefsData.error,
|
|
})
|
|
)
|
|
return null
|
|
}
|
|
|
|
getDestinationOverviewPageRefsSuccessCounter.add(1, { lang, uid: `${uid}` })
|
|
console.info(
|
|
"contentstack.destinationOverviewPage.refs success",
|
|
JSON.stringify({
|
|
query: { lang, uid },
|
|
})
|
|
)
|
|
|
|
getDestinationOverviewPageCounter.add(1, { lang, uid: `${uid}` })
|
|
console.info(
|
|
"contentstack.destinationOverviewPage start",
|
|
JSON.stringify({
|
|
query: { lang, uid },
|
|
})
|
|
)
|
|
const response = await request<GetDestinationOverviewPageData>(
|
|
GetDestinationOverviewPage,
|
|
{
|
|
locale: lang,
|
|
uid,
|
|
},
|
|
{
|
|
key: generateTag(lang, uid),
|
|
ttl: "max",
|
|
}
|
|
)
|
|
if (!response.data) {
|
|
const notFoundError = notFound(response)
|
|
getDestinationOverviewPageFailCounter.add(1, {
|
|
lang,
|
|
uid: `${uid}`,
|
|
error_type: "not_found",
|
|
error: JSON.stringify({ code: notFoundError.code }),
|
|
})
|
|
console.error(
|
|
"contentstack.destinationOverviewPage not found error",
|
|
JSON.stringify({
|
|
query: { lang, uid },
|
|
error: { code: notFoundError.code },
|
|
})
|
|
)
|
|
throw notFoundError
|
|
}
|
|
|
|
const destinationOverviewPage = destinationOverviewPageSchema.safeParse(
|
|
response.data
|
|
)
|
|
|
|
if (!destinationOverviewPage.success) {
|
|
getDestinationOverviewPageFailCounter.add(1, {
|
|
lang,
|
|
uid: `${uid}`,
|
|
error_type: "validation_error",
|
|
error: JSON.stringify(destinationOverviewPage.error),
|
|
})
|
|
console.error(
|
|
"contentstack.destinationOverviewPage validation error",
|
|
JSON.stringify({
|
|
query: { lang, uid },
|
|
error: destinationOverviewPage.error,
|
|
})
|
|
)
|
|
return null
|
|
}
|
|
|
|
getDestinationOverviewPageSuccessCounter.add(1, { lang, uid: `${uid}` })
|
|
console.info(
|
|
"contentstack.destinationOverviewPage success",
|
|
JSON.stringify({
|
|
query: { lang, uid },
|
|
})
|
|
)
|
|
|
|
const system = destinationOverviewPage.data.destination_overview_page.system
|
|
const tracking: TrackingSDKPageData = {
|
|
pageId: system.uid,
|
|
domainLanguage: lang,
|
|
publishDate: system.updated_at,
|
|
createDate: system.created_at,
|
|
channel: TrackingChannelEnum.hotels,
|
|
pageType: "destinationoverviewpage",
|
|
pageName: "destination|overview",
|
|
siteSections: "destination|overview",
|
|
siteVersion: "new-web",
|
|
}
|
|
|
|
return {
|
|
destinationOverviewPage:
|
|
destinationOverviewPage.data.destination_overview_page,
|
|
tracking,
|
|
}
|
|
}),
|
|
destinations: router({
|
|
get: serviceProcedure.query(async function ({ ctx }) {
|
|
const countries = await getCountries({
|
|
lang: ctx.lang,
|
|
serviceToken: ctx.serviceToken,
|
|
})
|
|
|
|
const countryPages = await getCountryPageUrls(ctx.lang)
|
|
|
|
if (!countries) {
|
|
return null
|
|
}
|
|
|
|
const countryNames = countries.data.map((country) => country.name)
|
|
|
|
const citiesByCountry = await getCitiesByCountry({
|
|
lang: ctx.lang,
|
|
countries: countryNames,
|
|
serviceToken: ctx.serviceToken,
|
|
onlyPublished: true,
|
|
})
|
|
|
|
const cityPages = await getCityPageUrls(ctx.lang)
|
|
|
|
const destinations: DestinationsData = await Promise.all(
|
|
Object.entries(citiesByCountry).map(async ([country, cities]) => {
|
|
const citiesWithHotelCount = await Promise.all(
|
|
cities.map(async (city) => {
|
|
const [hotels] = await safeTry(
|
|
getHotelIdsByCityId({
|
|
cityId: city.id,
|
|
serviceToken: ctx.serviceToken,
|
|
})
|
|
)
|
|
|
|
const cityPage = cityPages.find(
|
|
(cityPage) => cityPage.city === city.cityIdentifier
|
|
)
|
|
|
|
if (!cityPage) {
|
|
return null
|
|
}
|
|
|
|
return {
|
|
id: city.id,
|
|
name: city.name,
|
|
hotelIds: hotels || [],
|
|
hotelCount: hotels?.length ?? 0,
|
|
url: cityPage.url,
|
|
}
|
|
})
|
|
)
|
|
|
|
const activeCitiesWithHotelCount: Cities =
|
|
citiesWithHotelCount.filter(
|
|
(city): city is Cities[number] => !!city
|
|
)
|
|
|
|
const countryPage = countryPages.find(
|
|
(countryPage) => countryPage.country === country
|
|
)
|
|
|
|
return {
|
|
country,
|
|
countryUrl: countryPage?.url,
|
|
numberOfHotels: activeCitiesWithHotelCount.reduce(
|
|
(acc, city) => acc + city.hotelCount,
|
|
0
|
|
),
|
|
cities: activeCitiesWithHotelCount,
|
|
}
|
|
})
|
|
)
|
|
|
|
return destinations.sort((a, b) => a.country.localeCompare(b.country))
|
|
}),
|
|
}),
|
|
})
|