117 lines
3.7 KiB
TypeScript
117 lines
3.7 KiB
TypeScript
import * as api from "@/lib/api"
|
|
import { badRequestError } from "@/server/errors/trpc"
|
|
import { publicProcedure, router } from "@/server/trpc"
|
|
|
|
import {
|
|
getFiltersInputSchema,
|
|
getHotelInputSchema,
|
|
getRatesInputSchema,
|
|
} from "./input"
|
|
import {
|
|
getFiltersSchema,
|
|
getHotelDataSchema,
|
|
getRatesSchema,
|
|
RoomSchema,
|
|
} from "./output"
|
|
import tempFilterData from "./tempFilterData.json"
|
|
import tempHotelData from "./tempHotelData.json"
|
|
import tempRatesData from "./tempRatesData.json"
|
|
import { toApiLang } from "./utils"
|
|
|
|
export const hotelQueryRouter = router({
|
|
getHotel: publicProcedure
|
|
.input(getHotelInputSchema)
|
|
.query(async ({ input, ctx }) => {
|
|
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(
|
|
// 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()
|
|
|
|
// 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
|
|
)
|
|
|
|
if (!validatedHotelData.success) {
|
|
console.error(`Get Individual Hotel Data - Verified Data Error`)
|
|
console.error(validatedHotelData.error)
|
|
throw badRequestError()
|
|
}
|
|
|
|
const roomCategories = included
|
|
? included
|
|
.filter((item) => item.type === "roomcategories")
|
|
.map((roomCategory) => {
|
|
const validatedRoom = RoomSchema.safeParse(roomCategory)
|
|
if (!validatedRoom.success) {
|
|
console.error(`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)
|
|
.query(async ({ input, ctx }) => {
|
|
// TODO: Do a real API call when the endpoint is ready
|
|
// const { hotelId } = input
|
|
|
|
// const params = new URLSearchParams()
|
|
// const apiLang = toApiLang(language)
|
|
// params.set("hotelId", hotelId.toString())
|
|
// params.set("language", apiLang)
|
|
|
|
const validatedHotelData = getRatesSchema.safeParse(tempRatesData)
|
|
|
|
if (!validatedHotelData.success) {
|
|
console.error(`Get Individual Rates Data - Verified Data Error`)
|
|
console.error(validatedHotelData.error)
|
|
throw badRequestError()
|
|
}
|
|
|
|
return validatedHotelData.data
|
|
}),
|
|
getFilters: publicProcedure
|
|
.input(getFiltersInputSchema)
|
|
.query(async ({ input, ctx }) => {
|
|
const validateFilterData = getFiltersSchema.safeParse(tempFilterData)
|
|
|
|
if (!validateFilterData.success) {
|
|
console.info(`Get Individual Filter Data - Verified Data Error`)
|
|
console.error(validateFilterData.error)
|
|
throw badRequestError()
|
|
}
|
|
|
|
return validateFilterData.data
|
|
}),
|
|
})
|