feat: add included param to hotel call, fetch room data, setup schema, use in hotelpage

This commit is contained in:
Chuma McPhoy
2024-07-10 13:41:39 +02:00
parent f71d0a07d5
commit 0697c8d9ef
15 changed files with 191 additions and 162 deletions

View File

@@ -3,7 +3,7 @@ import { badRequestError } from "@/server/errors/trpc"
import { publicProcedure, router } from "@/server/trpc"
import { getHotelInputSchema, getRatesInputSchema } from "./input"
import { getHotelDataSchema, getRatesSchema } from "./output"
import { getHotelDataSchema, getRatesSchema, RoomSchema } from "./output"
import tempHotelData from "./tempHotelData.json"
import tempRatesData from "./tempRatesData.json"
import { toApiLang } from "./utils"
@@ -12,12 +12,15 @@ export const hotelQueryRouter = router({
getHotel: publicProcedure
.input(getHotelInputSchema)
.query(async ({ input, ctx }) => {
const { hotelId, language } = input
const { hotelId, language, include } = input
const params = new URLSearchParams()
const apiLang = toApiLang(language)
params.set("hotelId", hotelId.toString())
params.set("language", apiLang)
if (include) {
params.set("include", include.join(","))
}
// TODO: Enable once we have authorized API access.
// const apiResponse = await api.get(
@@ -33,10 +36,9 @@ export const hotelQueryRouter = router({
// }
// 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.
// NOTE: We can pass an "include" param to the hotel API to retrieve
// additional data for an individual hotel.
// Example "included" data can be found in our tempHotelData file.
const { included, ...apiJsonWithoutIncluded } = tempHotelData
const validatedHotelData = getHotelDataSchema.safeParse(
apiJsonWithoutIncluded
@@ -48,7 +50,24 @@ export const hotelQueryRouter = router({
throw badRequestError()
}
return validatedHotelData.data.data.attributes
const roomCategories = included
? included
.filter((item) => item.type === "roomcategories")
.map((roomCategory) => {
const validatedRoom = RoomSchema.safeParse(roomCategory)
if (!validatedRoom.success) {
console.info(`Get Room Category Data - Verified Data Error`)
console.error(validatedRoom.error)
throw badRequestError()
}
return validatedRoom.data
})
: []
return {
attributes: validatedHotelData.data.data.attributes,
roomCategories: roomCategories,
}
}),
getRates: publicProcedure
.input(getRatesInputSchema)