Files
web/apps/scandic-web/server/routers/contentstack/hotelPage/utils.ts
Anton Gunnarsson 846fd904a6 Merged in feat/sw-2859-set-up-shared-trpc-package (pull request #2319)
feat(SW-2859): Create trpc package

* Add isEdge, safeTry and dataCache to new common package

* Add eslint and move prettier config

* Clean up tests

* Create trpc package and move initialization

* Move errors and a few procedures

* Move telemetry to common package

* Move tokenManager to common package

* Add Sentry to procedures

* Clean up procedures

* Fix self-referencing imports

* 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

* Fix lang imports


Approved-by: Linus Flood
2025-06-18 12:14:20 +00:00

101 lines
2.7 KiB
TypeScript

import { createCounter } from "@scandic-hotels/common/telemetry"
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 { batchedHotelPageUrlsSchema, hotelPageCountSchema } from "./output"
import type { Lang } from "@scandic-hotels/common/constants/language"
import type {
GetHotelPageCountData,
GetHotelPageUrlsData,
} from "@/types/trpc/routers/contentstack/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
}