Feature/wrap logging * feat: change all logging to go through our own logger function so that we can control log levels * move packages/trpc to using our own logger * merge Approved-by: Linus Flood
62 lines
1.7 KiB
TypeScript
62 lines
1.7 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,
|
|
getHotelIdsByCountry,
|
|
} from "@scandic-hotels/trpc/routers/hotels/utils"
|
|
|
|
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,
|
|
}
|
|
}
|
|
}
|