import { getCacheClient } from "@scandic-hotels/common/dataCache" import { createCounter } from "@scandic-hotels/common/telemetry" import { env } from "../../../../env/server" import * as api from "../../../api" import { getHotelIdsSchema } from "../output" export async function getHotelIdsByCountry({ country, serviceToken, }: { country: string serviceToken: string }) { const getHotelIdsByCountryCounter = createCounter( "hotel", "getHotelIdsByCountry" ) const metricsGetHotelIdsByCountry = getHotelIdsByCountryCounter.init({ country, }) metricsGetHotelIdsByCountry.start() const cacheClient = await getCacheClient() const result = await cacheClient.cacheOrGet( `${country}:hotelsByCountry`, async () => { const hotelIdsParams = new URLSearchParams({ country, }) const apiResponse = await api.get( api.endpoints.v1.Hotel.hotels, { headers: { Authorization: `Bearer ${serviceToken}`, }, }, hotelIdsParams ) if (!apiResponse.ok) { await metricsGetHotelIdsByCountry.httpError(apiResponse) throw new Error("Unable to fetch hotelIds by country") } const apiJson = await apiResponse.json() const validatedHotelIds = getHotelIdsSchema.safeParse(apiJson) if (!validatedHotelIds.success) { metricsGetHotelIdsByCountry.validationError(validatedHotelIds.error) throw new Error("Unable to parse hotelIds by country") } return validatedHotelIds.data }, env.CACHE_TIME_HOTELS ) metricsGetHotelIdsByCountry.success() return result }