Files
web/apps/scandic-web/services/warmup/warmupHotelIdsByCountry.ts
Anton Gunnarsson 048a477e52 Merged in feat/common-package (pull request #2333)
feat: Add common package

* Add isEdge, safeTry and dataCache to new common package

* Add eslint and move prettier config

* Fix yarn lock

* Clean up tests

* Add lint-staged config to common

* Add missing dependencies


Approved-by: Joakim Jäderberg
2025-06-11 13:08:39 +00:00

61 lines
1.5 KiB
TypeScript

import { safeTry } from "@scandic-hotels/common/utils/safeTry"
import { Lang } from "@/constants/languages"
import {
getCountries,
getHotelIdsByCountry,
} from "@/server/routers/hotels/utils"
import { getServiceToken } from "@/server/tokenManager"
import type { WarmupFunction, WarmupResult } from "."
export const warmupHotelIdsByCountry =
(): WarmupFunction => async (): Promise<WarmupResult> => {
try {
let serviceToken = await getServiceToken()
const [countries, countriesError] = await safeTry(
getCountries({
lang: Lang.en,
serviceToken: serviceToken.access_token,
warmup: true,
})
)
if (!countries || countriesError) {
return {
status: "error",
error: new Error("Unable to get countries"),
}
}
const countryNames = countries.data.map((country) => country.name)
for (const countryName of countryNames) {
serviceToken = await getServiceToken()
const [_, error] = await safeTry(
getHotelIdsByCountry({
country: countryName,
serviceToken: serviceToken.access_token,
})
)
if (error) {
console.error(
`[Warmup]: Error fetching hotel IDs for ${countryName}:`,
error
)
continue
}
}
return {
status: "completed",
}
} catch (error) {
return {
status: "error",
error: error as Error,
}
}
}