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
94 lines
2.6 KiB
TypeScript
94 lines
2.6 KiB
TypeScript
import { createCounter } from "@scandic-hotels/common/telemetry"
|
|
|
|
import { GetCityPageCount } from "../../../graphql/Query/DestinationCityPage/DestinationCityPageCount.graphql"
|
|
import { GetCityPageUrls } from "../../../graphql/Query/DestinationCityPage/DestinationCityPageUrl.graphql"
|
|
import { request } from "../../../graphql/request"
|
|
import { batchedCityPageUrlsSchema, cityPageCountSchema } from "./output"
|
|
|
|
import type { Lang } from "@scandic-hotels/common/constants/language"
|
|
|
|
import type {
|
|
GetCityPageCountData,
|
|
GetCityPageUrlsData,
|
|
} from "../../../types/destinationCityPage"
|
|
|
|
export async function getCityPageCount(lang: Lang) {
|
|
const getCityPageCountCounter = createCounter(
|
|
"trpc.contentstack.cityPageCount.get"
|
|
)
|
|
const metricsGetCityPageCount = getCityPageCountCounter.init({ lang })
|
|
|
|
metricsGetCityPageCount.start()
|
|
|
|
const response = await request<GetCityPageCountData>(
|
|
GetCityPageCount,
|
|
{
|
|
locale: lang,
|
|
},
|
|
{
|
|
key: `${lang}:city_page_count`,
|
|
ttl: "max",
|
|
}
|
|
)
|
|
|
|
if (!response.data) {
|
|
metricsGetCityPageCount.dataError(
|
|
`Failed to get city pages count for ${lang}`
|
|
)
|
|
return 0
|
|
}
|
|
|
|
const validatedResponse = cityPageCountSchema.safeParse(response.data)
|
|
|
|
if (!validatedResponse.success) {
|
|
metricsGetCityPageCount.validationError(validatedResponse.error)
|
|
return 0
|
|
}
|
|
|
|
metricsGetCityPageCount.success()
|
|
|
|
return validatedResponse.data
|
|
}
|
|
|
|
export async function getCityPageUrls(lang: Lang) {
|
|
const getCityPageUrlsCounter = createCounter(
|
|
"trpc.contentstack.cityPageUrls.get"
|
|
)
|
|
const metricsGetCityPageUrls = getCityPageUrlsCounter.init({ lang })
|
|
|
|
metricsGetCityPageUrls.start()
|
|
|
|
const count = await getCityPageCount(lang)
|
|
|
|
if (count === 0) {
|
|
return []
|
|
}
|
|
|
|
// Calculating the amount of requests needed to fetch all pages.
|
|
// Contentstack has a limit of 100 items per request.
|
|
// So we need to make multiple requests to fetch urls to all pages.
|
|
// The `batchRequest` function is not working here, because the arrayMerge is
|
|
// used for other purposes.
|
|
const amountOfRequests = Math.ceil(count / 100)
|
|
|
|
const batchedResponse = await Promise.all(
|
|
Array.from({ length: amountOfRequests }).map((_, i) =>
|
|
request<GetCityPageUrlsData>(
|
|
GetCityPageUrls,
|
|
{ locale: lang, skip: i * 100 },
|
|
{ key: `${lang}:city_page_urls_batch_${i}`, ttl: "max" }
|
|
)
|
|
)
|
|
)
|
|
const validatedResponse = batchedCityPageUrlsSchema.safeParse(batchedResponse)
|
|
|
|
if (!validatedResponse.success) {
|
|
metricsGetCityPageUrls.validationError(validatedResponse.error)
|
|
return []
|
|
}
|
|
|
|
metricsGetCityPageUrls.success()
|
|
|
|
return validatedResponse.data
|
|
}
|