feat(SW-2863): Move contentstack router to trpc package * Add exports to packages and lint rule to prevent relative imports * Add env to trpc package * Add eslint to trpc package * Apply lint rules * Use direct imports from trpc package * Add lint-staged config to trpc * Move lang enum to common * Restructure trpc package folder structure * WIP first step * update internal imports in trpc * Fix most errors in scandic-web Just 100 left... * Move Props type out of trpc * Fix CategorizedFilters types * Move more schemas in hotel router * Fix deps * fix getNonContentstackUrls * Fix import error * Fix entry error handling * Fix generateMetadata metrics * Fix alertType enum * Fix duplicated types * lint:fix * Merge branch 'master' into feat/sw-2863-move-contentstack-router-to-trpc-package * Fix broken imports * Merge branch 'master' into feat/sw-2863-move-contentstack-router-to-trpc-package Approved-by: Linus Flood
100 lines
2.7 KiB
TypeScript
100 lines
2.7 KiB
TypeScript
import { createCounter } from "@scandic-hotels/common/telemetry"
|
|
|
|
import { GetHotelPageCount } from "../../../graphql/Query/HotelPage/HotelPageCount.graphql"
|
|
import { GetHotelPageUrls } from "../../../graphql/Query/HotelPage/HotelPageUrl.graphql"
|
|
import { request } from "../../../graphql/request"
|
|
import { batchedHotelPageUrlsSchema, hotelPageCountSchema } from "./output"
|
|
|
|
import type { Lang } from "@scandic-hotels/common/constants/language"
|
|
|
|
import type {
|
|
GetHotelPageCountData,
|
|
GetHotelPageUrlsData,
|
|
} from "../../../types/hotelPage"
|
|
|
|
export async function getHotelPageCount(lang: Lang) {
|
|
const getHotelPageCountCounter = createCounter(
|
|
"trpc.contentstack",
|
|
"hotelPageCount.get"
|
|
)
|
|
const metricsGetHotelPageCount = getHotelPageCountCounter.init({ lang })
|
|
|
|
metricsGetHotelPageCount.start()
|
|
|
|
const response = await request<GetHotelPageCountData>(
|
|
GetHotelPageCount,
|
|
{
|
|
locale: lang,
|
|
},
|
|
{
|
|
key: `${lang}:hotel_page_count`,
|
|
ttl: "max",
|
|
}
|
|
)
|
|
|
|
if (!response.data) {
|
|
metricsGetHotelPageCount.noDataError()
|
|
return 0
|
|
}
|
|
|
|
const validatedResponse = hotelPageCountSchema.safeParse(response.data)
|
|
|
|
if (!validatedResponse.success) {
|
|
metricsGetHotelPageCount.validationError(validatedResponse.error)
|
|
return 0
|
|
}
|
|
|
|
metricsGetHotelPageCount.success()
|
|
|
|
return validatedResponse.data
|
|
}
|
|
|
|
export async function getHotelPageUrls(lang: Lang) {
|
|
const getHotelPageUrlsCounter = createCounter(
|
|
"trpc.contentstack",
|
|
"hotelPageUrls.get"
|
|
)
|
|
const metricsGetHotelPageUrls = getHotelPageUrlsCounter.init({ lang })
|
|
|
|
metricsGetHotelPageUrls.start()
|
|
|
|
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) {
|
|
metricsGetHotelPageUrls.validationError(validatedResponse.error)
|
|
return []
|
|
}
|
|
|
|
metricsGetHotelPageUrls.success()
|
|
|
|
return validatedResponse.data
|
|
}
|