112 lines
3.1 KiB
TypeScript
112 lines
3.1 KiB
TypeScript
import { notFound } from "next/navigation"
|
|
|
|
import { getLocations } from "@/lib/trpc/memoizedRequests"
|
|
|
|
import { generateChildrenString } from "@/components/HotelReservation/utils"
|
|
import { safeTry } from "@/utils/safeTry"
|
|
import { convertSearchParamsToObj, type SelectHotelParams } from "@/utils/url"
|
|
|
|
import type {
|
|
AlternativeHotelsSearchParams,
|
|
SelectHotelSearchParams,
|
|
} from "@/types/components/hotelReservation/selectHotel/selectHotelSearchParams"
|
|
import type {
|
|
Child,
|
|
SelectRateSearchParams,
|
|
} from "@/types/components/hotelReservation/selectRate/selectRate"
|
|
import {
|
|
type HotelLocation,
|
|
isHotelLocation,
|
|
type Location,
|
|
} from "@/types/trpc/routers/hotel/locations"
|
|
|
|
interface HotelSearchDetails<T> {
|
|
city: Location | null
|
|
hotel: HotelLocation | null
|
|
selectHotelParams: SelectHotelParams<T> & { city: string | undefined }
|
|
adultsInRoom: number[]
|
|
childrenInRoomString?: string
|
|
childrenInRoom?: Child[]
|
|
bookingCode?: string
|
|
redemption?: boolean
|
|
}
|
|
|
|
export async function getHotelSearchDetails<
|
|
T extends
|
|
| SelectHotelSearchParams
|
|
| SelectRateSearchParams
|
|
| AlternativeHotelsSearchParams,
|
|
>(
|
|
{
|
|
searchParams,
|
|
}: {
|
|
searchParams: T & {
|
|
[key: string]: string
|
|
}
|
|
},
|
|
isAlternativeHotels?: boolean
|
|
): Promise<HotelSearchDetails<T> | null> {
|
|
const selectHotelParams = convertSearchParamsToObj<T>(searchParams)
|
|
|
|
const [locations, error] = await safeTry(getLocations())
|
|
if (!locations || error) {
|
|
return null
|
|
}
|
|
|
|
const hotel =
|
|
("hotelId" in selectHotelParams &&
|
|
(locations.find(
|
|
(location) =>
|
|
isHotelLocation(location) &&
|
|
"operaId" in location &&
|
|
location.operaId === selectHotelParams.hotelId
|
|
) as HotelLocation | undefined)) ||
|
|
null
|
|
|
|
if (isAlternativeHotels && !hotel) {
|
|
return notFound()
|
|
}
|
|
|
|
const cityName = isAlternativeHotels
|
|
? hotel?.relationships.city.name
|
|
: "city" in selectHotelParams
|
|
? (selectHotelParams.city as string | undefined)
|
|
: undefined
|
|
|
|
const city =
|
|
(typeof cityName === "string" &&
|
|
locations.find(
|
|
(location) => location.name.toLowerCase() === cityName.toLowerCase()
|
|
)) ||
|
|
null
|
|
|
|
if (!city && !hotel) return notFound()
|
|
if (isAlternativeHotels && (!city || !hotel)) return notFound()
|
|
|
|
let adultsInRoom: number[] = []
|
|
let childrenInRoomString: HotelSearchDetails<T>["childrenInRoomString"] =
|
|
undefined
|
|
let childrenInRoom: HotelSearchDetails<T>["childrenInRoom"] = undefined
|
|
|
|
const { rooms } = selectHotelParams
|
|
|
|
if (rooms && rooms.length > 0) {
|
|
adultsInRoom = rooms.map((room) => room.adults ?? 0)
|
|
childrenInRoomString = rooms[0].childrenInRoom
|
|
? generateChildrenString(rooms[0].childrenInRoom)
|
|
: undefined // TODO: Handle multiple rooms
|
|
childrenInRoom = rooms[0].childrenInRoom // TODO: Handle multiple rooms
|
|
}
|
|
|
|
return {
|
|
city,
|
|
hotel,
|
|
selectHotelParams: { city: cityName, ...selectHotelParams },
|
|
adultsInRoom,
|
|
childrenInRoomString,
|
|
childrenInRoom,
|
|
bookingCode: selectHotelParams.bookingCode ?? undefined,
|
|
redemption: selectHotelParams.searchType === "redemption",
|
|
}
|
|
}
|