114 lines
3.4 KiB
TypeScript
114 lines
3.4 KiB
TypeScript
import * as api from "@/lib/api"
|
|
import { badRequestError } from "@/server/errors/trpc"
|
|
import { publicProcedure, router, serviceProcedure } from "@/server/trpc"
|
|
import { toApiLang } from "@/server/utils"
|
|
|
|
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"
|
|
|
|
export const hotelQueryRouter = router({
|
|
getHotel: serviceProcedure
|
|
.input(getHotelInputSchema)
|
|
.query(async ({ input, ctx }) => {
|
|
const { hotelId, language, include } = input
|
|
|
|
const params = new URLSearchParams()
|
|
|
|
const apiLang = toApiLang(language)
|
|
params.set("language", apiLang)
|
|
|
|
if (include) {
|
|
params.set("include", include.join(","))
|
|
}
|
|
|
|
const apiResponse = await api.get(
|
|
`${api.endpoints.v1.hotels}/${hotelId}`,
|
|
{
|
|
cache: "no-store",
|
|
headers: {
|
|
Authorization: `Bearer ${ctx.serviceToken}`,
|
|
},
|
|
},
|
|
params
|
|
)
|
|
|
|
if (!apiResponse.ok) {
|
|
console.info(`API Response Failed - Getting Hotel`)
|
|
console.error(apiResponse)
|
|
return null
|
|
}
|
|
const apiJson = await apiResponse.json()
|
|
const validatedHotelData = getHotelDataSchema.safeParse(apiJson)
|
|
if (!validatedHotelData.success) {
|
|
console.error(`Get Individual Hotel Data - Verified Data Error`)
|
|
console.error(validatedHotelData.error)
|
|
throw badRequestError()
|
|
}
|
|
|
|
const included = validatedHotelData.data.included || []
|
|
|
|
const roomCategories = 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 {
|
|
hotel: 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
|
|
}),
|
|
})
|