127 lines
3.4 KiB
TypeScript
127 lines
3.4 KiB
TypeScript
import { getHotel } from "@/lib/trpc/memoizedRequests"
|
|
import { serverClient } from "@/lib/trpc/server"
|
|
|
|
import { getLang } from "@/i18n/serverContext"
|
|
|
|
import type {
|
|
AlternativeHotelsAvailabilityInput,
|
|
AvailabilityInput,
|
|
} from "@/types/components/hotelReservation/selectHotel/availabilityInput"
|
|
import type {
|
|
HotelData,
|
|
NullableHotelData,
|
|
} from "@/types/components/hotelReservation/selectHotel/hotelCardListingProps"
|
|
import type {
|
|
CategorizedFilters,
|
|
Filter,
|
|
} from "@/types/components/hotelReservation/selectHotel/hotelFilters"
|
|
import type { HotelsAvailabilityItem } from "@/server/routers/hotels/output"
|
|
|
|
const hotelSurroundingsFilterNames = [
|
|
"Hotel surroundings",
|
|
"Hotel omgivelser",
|
|
"Hotelumgebung",
|
|
"Hotellia lähellä",
|
|
"Hotellomgivelser",
|
|
"Omgivningar",
|
|
]
|
|
|
|
const hotelFacilitiesFilterNames = [
|
|
"Hotel facilities",
|
|
"Hotellfaciliteter",
|
|
"Hotelfaciliteter",
|
|
"Hotel faciliteter",
|
|
"Hotel-Infos",
|
|
"Hotellin palvelut",
|
|
]
|
|
|
|
export async function fetchAvailableHotels(
|
|
input: AvailabilityInput
|
|
): Promise<NullableHotelData[]> {
|
|
const availableHotels =
|
|
await serverClient().hotel.availability.hotelsByCity(input)
|
|
|
|
if (!availableHotels) return []
|
|
|
|
return enhanceHotels(availableHotels)
|
|
}
|
|
|
|
export async function fetchAlternativeHotels(
|
|
hotelId: string,
|
|
input: AlternativeHotelsAvailabilityInput
|
|
): Promise<NullableHotelData[]> {
|
|
const alternativeHotelIds = await serverClient().hotel.nearbyHotelIds({
|
|
hotelId,
|
|
})
|
|
|
|
if (!alternativeHotelIds) return []
|
|
|
|
const availableHotels =
|
|
await serverClient().hotel.availability.hotelsByHotelIds({
|
|
...input,
|
|
hotelIds: alternativeHotelIds,
|
|
})
|
|
|
|
if (!availableHotels) return []
|
|
|
|
return enhanceHotels(availableHotels)
|
|
}
|
|
|
|
async function enhanceHotels(hotels: {
|
|
availability: HotelsAvailabilityItem[]
|
|
}) {
|
|
const language = getLang()
|
|
|
|
const hotelFetchers = hotels.availability.map(async (hotel) => {
|
|
const hotelData = await getHotel({
|
|
hotelId: hotel.hotelId.toString(),
|
|
isCardOnlyPayment: false,
|
|
language,
|
|
})
|
|
|
|
if (!hotelData) return { hotelData: null, price: hotel.productType }
|
|
|
|
return {
|
|
hotelData: hotelData.hotel,
|
|
price: hotel.productType,
|
|
}
|
|
})
|
|
|
|
return await Promise.all(hotelFetchers)
|
|
}
|
|
|
|
export function getFiltersFromHotels(hotels: HotelData[]): CategorizedFilters {
|
|
if (hotels.length === 0)
|
|
return { facilityFilters: [], surroundingsFilters: [] }
|
|
|
|
const filters = hotels.flatMap((hotel) => {
|
|
if (!hotel.hotelData) return []
|
|
return 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)
|
|
.sort((a, b) => b.sortOrder - a.sortOrder)
|
|
|
|
return filterList.reduce<CategorizedFilters>(
|
|
(acc, filter) => {
|
|
if (filter.filter && hotelSurroundingsFilterNames.includes(filter.filter))
|
|
return {
|
|
facilityFilters: acc.facilityFilters,
|
|
surroundingsFilters: [...acc.surroundingsFilters, filter],
|
|
}
|
|
|
|
if (filter.filter && hotelFacilitiesFilterNames.includes(filter.filter))
|
|
return {
|
|
facilityFilters: [...acc.facilityFilters, filter],
|
|
surroundingsFilters: acc.surroundingsFilters,
|
|
}
|
|
|
|
return acc
|
|
},
|
|
{ facilityFilters: [], surroundingsFilters: [] }
|
|
)
|
|
}
|