Files
web/packages/trpc/lib/routers/hotels/services/getHotelIdsByCityId.ts
Joakim Jäderberg f6a807758e Merged in chore/remove-enrichHotel-call (pull request #3244)
Chore/remove enrichHotel call

* chore: remove enrichHotel api call

* include cityId when errors happen

* remove unused funciton and cleanup code

* tests no longer expect us to have called a function that is not used

* remove commented code


Approved-by: Linus Flood
2025-12-01 07:49:59 +00:00

68 lines
1.7 KiB
TypeScript

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 getHotelIdsByCityId({
cityId,
serviceToken,
}: {
cityId: string
serviceToken: string
}) {
const getHotelIdsByCityIdCounter = createCounter(
"hotel",
"getHotelIdsByCityId"
)
const metricsGetHotelIdsByCityId = getHotelIdsByCityIdCounter.init({
cityId,
})
metricsGetHotelIdsByCityId.start()
const cacheClient = await getCacheClient()
const result = await cacheClient.cacheOrGet(
`${cityId}:hotelsByCityId`,
async () => {
const searchParams = new URLSearchParams({
city: cityId,
})
const apiResponse = await api.get(
api.endpoints.v1.Hotel.hotels,
{
headers: {
Authorization: `Bearer ${serviceToken}`,
},
},
searchParams
)
if (!apiResponse.ok) {
await metricsGetHotelIdsByCityId.httpError(apiResponse)
throw new Error(`Unable to fetch hotelIds by cityId`, {
cause: { cityId },
})
}
const apiJson = await apiResponse.json()
const validatedHotelIds = getHotelIdsSchema.safeParse(apiJson)
if (!validatedHotelIds.success) {
metricsGetHotelIdsByCityId.validationError(validatedHotelIds.error)
throw new Error(`Unable to parse data for hotelIds by cityId`, {
cause: { cityId, errors: validatedHotelIds.error },
})
}
return validatedHotelIds.data
},
env.CACHE_TIME_HOTELS
)
metricsGetHotelIdsByCityId.success()
return result
}