Files
web/apps/scandic-web/server/routers/contentstack/hotelPage/utils.ts
Joakim Jäderberg fa63b20ed0 Merged in feature/redis (pull request #1478)
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
2025-03-14 07:54:21 +00:00

257 lines
6.6 KiB
TypeScript

import { GetHotelPageRefs } from "@/lib/graphql/Query/HotelPage/HotelPage.graphql"
import { GetHotelPageCount } from "@/lib/graphql/Query/HotelPage/HotelPageCount.graphql"
import { GetHotelPageUrls } from "@/lib/graphql/Query/HotelPage/HotelPageUrl.graphql"
import { request } from "@/lib/graphql/request"
import { notFound } from "@/server/errors/trpc"
import { generateTag, generateTagsFromSystem } from "@/utils/generateTag"
import {
batchedHotelPageUrlsSchema,
hotelPageCountSchema,
hotelPageRefsSchema,
} from "./output"
import {
getHotelPageCountCounter,
getHotelPageCountFailCounter,
getHotelPageCountSuccessCounter,
getHotelPageRefsCounter,
getHotelPageRefsFailCounter,
getHotelPageRefsSuccessCounter,
getHotelPageUrlsCounter,
getHotelPageUrlsFailCounter,
getHotelPageUrlsSuccessCounter,
} from "./telemetry"
import { HotelPageEnum } from "@/types/enums/hotelPage"
import type { System } from "@/types/requests/system"
import type {
GetHotelPageCountData,
GetHotelPageRefsSchema,
GetHotelPageUrlsData,
HotelPageRefs,
} from "@/types/trpc/routers/contentstack/hotelPage"
import type { Lang } from "@/constants/languages"
export async function fetchHotelPageRefs(lang: Lang, uid: string) {
getHotelPageRefsCounter.add(1, { lang, uid })
console.info(
"contentstack.hotelPage.refs start",
JSON.stringify({
query: { lang, uid },
})
)
const refsResponse = await request<GetHotelPageRefsSchema>(
GetHotelPageRefs,
{ locale: lang, uid },
{
key: generateTag(lang, uid),
ttl: "max",
}
)
if (!refsResponse.data) {
const notFoundError = notFound(refsResponse)
getHotelPageRefsFailCounter.add(1, {
lang,
uid,
error_type: "http_error",
error: JSON.stringify({
code: notFoundError.code,
}),
})
console.error(
"contentstack.hotelPage.refs not found error",
JSON.stringify({
query: {
lang,
uid,
},
error: { code: notFoundError.code },
})
)
throw notFoundError
}
return refsResponse.data
}
export function validateHotelPageRefs(
data: GetHotelPageRefsSchema,
lang: Lang,
uid: string
) {
const validatedData = hotelPageRefsSchema.safeParse(data)
if (!validatedData.success) {
getHotelPageRefsFailCounter.add(1, {
lang,
uid,
error_type: "validation_error",
error: JSON.stringify(validatedData.error),
})
console.error(
"contentstack.hotelPage.refs validation error",
JSON.stringify({
query: { lang, uid },
error: validatedData.error,
})
)
return null
}
getHotelPageRefsSuccessCounter.add(1, { lang, uid })
console.info(
"contentstack.hotelPage.refs success",
JSON.stringify({
query: { lang, uid },
})
)
return validatedData.data
}
export function generatePageTags(
validatedData: HotelPageRefs,
lang: Lang
): string[] {
const connections = getConnections(validatedData)
return [
generateTagsFromSystem(lang, connections),
generateTag(lang, validatedData.hotel_page.system.uid),
].flat()
}
export function getConnections({ hotel_page }: HotelPageRefs) {
const connections: System["system"][] = [hotel_page.system]
if (hotel_page.content) {
hotel_page.content.forEach((block) => {
switch (block.__typename) {
case HotelPageEnum.ContentStack.blocks.ActivitiesCard: {
if (block.upcoming_activities_card.length) {
connections.push(...block.upcoming_activities_card)
}
break
}
}
if (hotel_page.faq) {
connections.push(...hotel_page.faq)
}
})
}
return connections
}
export async function getHotelPageCount(lang: Lang) {
getHotelPageCountCounter.add(1, { lang })
console.info(
"contentstack.hotelPageCount start",
JSON.stringify({ query: { lang } })
)
const response = await request<GetHotelPageCountData>(
GetHotelPageCount,
{
locale: lang,
},
{
key: `${lang}:hotel_page_count`,
ttl: "max",
}
)
if (!response.data) {
getHotelPageCountFailCounter.add(1, {
lang,
error_type: "not_found",
error: `Hotel pages count not found for lang: ${lang}`,
})
console.error(
"contentstack.hotelPageCount not found error",
JSON.stringify({ query: { lang } })
)
return 0
}
const validatedResponse = hotelPageCountSchema.safeParse(response.data)
if (!validatedResponse.success) {
getHotelPageCountFailCounter.add(1, {
lang,
error_type: "validation_error",
error: JSON.stringify(validatedResponse.error),
})
console.error(
"contentstack.hotelPageCount validation error",
JSON.stringify({
query: { lang },
error: validatedResponse.error,
})
)
return 0
}
getHotelPageCountSuccessCounter.add(1, { lang })
console.info(
"contentstack.hotelPageCount success",
JSON.stringify({ query: { lang } })
)
return validatedResponse.data
}
export async function getHotelPageUrls(lang: Lang) {
getHotelPageUrlsCounter.add(1, { lang })
console.info(
"contentstack.hotelPageUrls start",
JSON.stringify({ query: { lang } })
)
const count = await getHotelPageCount(lang)
if (count === 0) {
return []
}
// Calculating the amount of requests needed to fetch all hotel pages.
// Contentstack has a limit of 100 items per request.
// So we need to make multiple requests to fetch urls to all hotel pages.
// The `batchRequest` function is not working here, because the arrayMerge is
// used for other purposes.
const amountOfRequests = Math.ceil(count / 100)
const requests = Array.from({ length: amountOfRequests }).map((_, i) => ({
document: GetHotelPageUrls,
variables: { locale: lang, skip: i * 100 },
cacheKey: `${lang}:hotel_page_urls_batch_${i}`,
}))
const batchedResponse = await Promise.all(
requests.map((req) =>
request<GetHotelPageUrlsData>(req.document, req.variables, {
key: req.cacheKey,
ttl: "max",
})
)
)
const validatedResponse =
batchedHotelPageUrlsSchema.safeParse(batchedResponse)
if (!validatedResponse.success) {
getHotelPageUrlsFailCounter.add(1, {
lang,
error_type: "validation_error",
error: JSON.stringify(validatedResponse.error),
})
console.error(
"contentstack.hotelPageUrls validation error",
JSON.stringify({
query: { lang },
error: validatedResponse.error,
})
)
return []
}
getHotelPageUrlsSuccessCounter.add(1, { lang })
console.info(
"contentstack.hotelPageUrl success",
JSON.stringify({ query: { lang } })
)
return validatedResponse.data
}