Fix/SW-902 update response city availability * fix(SW-902): update response for API changes * fix(SW-902): add total row for pricePerStay * fix(SW-902): fix optional requestedPrice * fix(SW-902): fix bookingCode output * feat(SW-903): fix sorting Approved-by: Pontus Dreij Approved-by: Niclas Edenvin
72 lines
2.1 KiB
TypeScript
72 lines
2.1 KiB
TypeScript
import { getHotelData } from "@/lib/trpc/memoizedRequests"
|
|
import { serverClient } from "@/lib/trpc/server"
|
|
|
|
import { getLang } from "@/i18n/serverContext"
|
|
|
|
import type { AvailabilityInput } from "@/types/components/hotelReservation/selectHotel/availabilityInput"
|
|
import type { HotelData } from "@/types/components/hotelReservation/selectHotel/hotelCardListingProps"
|
|
import type {
|
|
CategorizedFilters,
|
|
Filter,
|
|
} from "@/types/components/hotelReservation/selectHotel/hotelFilters"
|
|
|
|
const hotelSurroundingsFilterNames = [
|
|
"Hotel surroundings",
|
|
"Hotel omgivelser",
|
|
"Hotelumgebung",
|
|
"Hotellia lähellä",
|
|
"Hotellomgivelser",
|
|
"Omgivningar",
|
|
]
|
|
|
|
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 getHotelData({
|
|
hotelId: hotel.hotelId.toString(),
|
|
language,
|
|
})
|
|
|
|
if (!hotelData) throw new Error()
|
|
|
|
return {
|
|
hotelData: hotelData.data.attributes,
|
|
price: hotel.productType,
|
|
}
|
|
})
|
|
|
|
return await Promise.all(hotels)
|
|
}
|
|
|
|
export function getFiltersFromHotels(hotels: HotelData[]): CategorizedFilters {
|
|
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.reduce<CategorizedFilters>(
|
|
(acc, filter) => {
|
|
if (filter.filter && hotelSurroundingsFilterNames.includes(filter.filter))
|
|
return {
|
|
facilityFilters: acc.facilityFilters,
|
|
surroundingsFilters: [...acc.surroundingsFilters, filter],
|
|
}
|
|
|
|
return {
|
|
facilityFilters: [...acc.facilityFilters, filter],
|
|
surroundingsFilters: acc.surroundingsFilters,
|
|
}
|
|
},
|
|
{ facilityFilters: [], surroundingsFilters: [] }
|
|
)
|
|
}
|