Files
web/server/routers/hotels/query.ts
2024-07-08 12:53:12 +02:00

53 lines
1.9 KiB
TypeScript

import * as api from "@/lib/api"
import { badRequestError } from "@/server/errors/trpc"
import { publicProcedure, router } from "@/server/trpc"
import { getHotelInputSchema } from "./input"
import { getHotelDataSchema } from "./output"
import tempHotelData from "./tempHotelData.json"
import { toApiLang } from "./utils"
export const hotelQueryRouter = router({
getHotel: publicProcedure
.input(getHotelInputSchema)
.query(async ({ input, ctx }) => {
const { hotelId, language } = input
const params = new URLSearchParams()
const apiLang = toApiLang(language)
params.set("hotelId", hotelId.toString())
params.set("language", apiLang)
// TODO: Enable once we have authorized API access.
// const apiResponse = await api.get(
// api.endpoints.v1.hotel,
// {}, // Include token.
// params
// )
//
// if (!apiResponse.ok) {
// console.info(`API Response Failed - Getting Hotel`)
// console.error(apiResponse)
// return null
// }
// const apiJson = await apiResponse.json()
//TODO: We can pass an "included" param to the hotel API to retrieve additional data for an individual hotel.
// - This is out of scope for current work (and I'm unsure if we need it for hotel pages specifically),
// - but if/when we do we can extend the endpoint (and schema) to add necessary requirements.
// - Example "included" data available in our tempHotelData file.
const { included, ...apiJsonWithoutIncluded } = tempHotelData
const validatedHotelData = getHotelDataSchema.safeParse(
apiJsonWithoutIncluded
)
if (!validatedHotelData.success) {
console.info(`Get Individual Hotel Data - Verified Data Error`)
console.error(validatedHotelData.error)
throw badRequestError()
}
return validatedHotelData.data.data.attributes
}),
})