Feat/SW-711 update children params * feat(SW-711): add new child params for availability * feat(SW-711): fix children schema * feat(SW-711): fix optional values * feat(SW-711): add children as parameter iff not undefined * feat(SW-711): add bedType enum * feat(SW-711): remove optional number type * feat(SW-711): fix wrong slash * feat(SW-711): remove optional Approved-by: Hrishikesh Vaipurkar
62 lines
2.0 KiB
TypeScript
62 lines
2.0 KiB
TypeScript
import { serverClient } from "@/lib/trpc/server"
|
|
|
|
import { getLang } from "@/i18n/serverContext"
|
|
|
|
import { BedTypeEnum } from "@/types/components/bookingWidget/enums"
|
|
import { AvailabilityInput } from "@/types/components/hotelReservation/selectHotel/availabilityInput"
|
|
import { HotelData } from "@/types/components/hotelReservation/selectHotel/hotelCardListingProps"
|
|
import { Filter } from "@/types/components/hotelReservation/selectHotel/hotelFilters"
|
|
import { Child } from "@/types/components/hotelReservation/selectRate/selectRate"
|
|
|
|
export async function fetchAvailableHotels(
|
|
input: AvailabilityInput
|
|
): Promise<HotelData[]> {
|
|
const availableHotels = await serverClient().hotel.availability.hotels(input)
|
|
|
|
if (!availableHotels) throw new Error()
|
|
|
|
const language = getLang()
|
|
const hotels = availableHotels.availability.map(async (hotel) => {
|
|
const hotelData = await serverClient().hotel.hotelData.get({
|
|
hotelId: hotel.hotelId.toString(),
|
|
language,
|
|
})
|
|
|
|
if (!hotelData) throw new Error()
|
|
|
|
return {
|
|
hotelData: hotelData.data.attributes,
|
|
price: hotel.bestPricePerNight,
|
|
}
|
|
})
|
|
|
|
return await Promise.all(hotels)
|
|
}
|
|
|
|
export function getFiltersFromHotels(hotels: HotelData[]) {
|
|
const filters = hotels.flatMap((hotel) => hotel.hotelData.detailedFacilities)
|
|
|
|
const uniqueFilterIds = [...new Set(filters.map((filter) => filter.id))]
|
|
const filterList: Filter[] = uniqueFilterIds
|
|
.map((filterId) => filters.find((filter) => filter.id === filterId))
|
|
.filter((filter): filter is Filter => filter !== undefined)
|
|
|
|
return filterList
|
|
}
|
|
|
|
const bedTypeMap: Record<number, string> = {
|
|
[BedTypeEnum.IN_ADULTS_BED]: "ParentsBed",
|
|
[BedTypeEnum.IN_CRIB]: "Crib",
|
|
[BedTypeEnum.IN_EXTRA_BED]: "ExtraBed",
|
|
}
|
|
|
|
export function generateChildrenString(children: Child[]): string {
|
|
return `[${children
|
|
?.map((child) => {
|
|
const age = child.age
|
|
const bedType = bedTypeMap[+child.bed]
|
|
return `${age}:${bedType}`
|
|
})
|
|
.join(",")}]`
|
|
}
|