Chore/refactor hotel trpc routes * chore(SW-3519): refactor trpc hotel routers * chore(SW-3519): refactor trpc hotel routers * refactor * merge * Merge branch 'master' of bitbucket.org:scandic-swap/web into chore/refactor-hotel-trpc-routes Approved-by: Linus Flood
60 lines
1.8 KiB
TypeScript
60 lines
1.8 KiB
TypeScript
import { Lang } from "@scandic-hotels/common/constants/language"
|
|
import { createLogger } from "@scandic-hotels/common/logger/createLogger"
|
|
import { getServiceToken } from "@scandic-hotels/common/tokenManager"
|
|
import { safeTry } from "@scandic-hotels/common/utils/safeTry"
|
|
import { getCountries } from "@scandic-hotels/trpc/routers/hotels/services/getCountries"
|
|
import { getHotelIdsByCountry } from "@scandic-hotels/trpc/routers/hotels/services/getHotelIdsByCountry"
|
|
|
|
import type { WarmupFunction, WarmupResult } from "."
|
|
|
|
export const warmupHotelIdsByCountry =
|
|
(): WarmupFunction => async (): Promise<WarmupResult> => {
|
|
const warmupLogger = createLogger("warmupHotelIdsByCountry")
|
|
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) {
|
|
warmupLogger.error(
|
|
`[Warmup]: Error fetching hotel IDs for ${countryName}:`,
|
|
error
|
|
)
|
|
continue
|
|
}
|
|
}
|
|
|
|
return {
|
|
status: "completed",
|
|
}
|
|
} catch (error) {
|
|
return {
|
|
status: "error",
|
|
error: error as Error,
|
|
}
|
|
}
|
|
}
|