feat: Add TRPC procedure for hotel API, schemas, and use in hotel content page

This commit is contained in:
Chuma McPhoy
2024-07-02 15:27:07 +02:00
parent 965e093100
commit 0d5b775c24
6 changed files with 11510 additions and 36 deletions

View File

@@ -1,40 +1,52 @@
import * as api from "@/lib/api"
import { protectedProcedure, publicProcedure, router } from "@/server/trpc"
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"
export const hotelQueryRouter = router({
// TODO: Should be public.
getHotel: protectedProcedure
getHotel: publicProcedure
.input(getHotelInputSchema)
.query(async ({ input, ctx }) => {
const { hotelId } = input
const { hotelId, language } = input
const params = new URLSearchParams()
params.set("hotelId", hotelId.toString())
console.log("hotel fetch start")
const apiResponse = await api.get(
api.endpoints.v1.hotel,
{
cache: "no-store",
headers: {
Authorization: `Bearer ${ctx.session.token.access_token}`,
},
},
params
)
console.log("apiResponse", apiResponse)
params.set("language", language.toString())
if (!apiResponse.ok) {
console.info(`API Response Failed - Getting Hotel`)
console.error(apiResponse)
return null
// 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
console.log("hotel apiJson: ", apiJsonWithoutIncluded)
const validatedHotelData = getHotelDataSchema.safeParse(
apiJsonWithoutIncluded
)
if (!validatedHotelData.success) {
console.info(`Get Individual Hotel Data - Verified Data Error`)
console.error(validatedHotelData.error)
throw badRequestError()
}
const apiJson = await apiResponse.json()
console.log("apiJson", apiJson)
// return null
// TODO: validate apiJson.
return apiJson
console.log("validatedHotelData.data: ", validatedHotelData)
return validatedHotelData.data
}),
})