feat: SW-1577 Implemented booking code city search * feat: SW-1577 Implemented booking code city search * feat: SW-1557 Strict comparison * feat: SW-1557 Review comments fix Approved-by: Michael Zetterberg Approved-by: Pontus Dreij
60 lines
2.0 KiB
TypeScript
60 lines
2.0 KiB
TypeScript
import type { HotelData } from "@/types/components/hotelReservation/selectHotel/hotelCardListingProps"
|
|
import { SortOrder } from "@/types/components/hotelReservation/selectHotel/hotelSorter"
|
|
|
|
export function getSortedHotels({
|
|
hotels,
|
|
sortBy,
|
|
bookingCode,
|
|
}: {
|
|
hotels: HotelData[]
|
|
sortBy: string
|
|
bookingCode: string | null
|
|
}) {
|
|
const getPricePerNight = (hotel: HotelData): number =>
|
|
hotel.price?.member?.localPrice?.pricePerNight ??
|
|
hotel.price?.public?.localPrice?.pricePerNight ??
|
|
Infinity
|
|
const availableHotels = hotels.filter((hotel) => !!hotel?.price)
|
|
const unAvailableHotels = hotels.filter((hotel) => !hotel?.price)
|
|
|
|
const sortingStrategies: Record<
|
|
string,
|
|
(a: HotelData, b: HotelData) => number
|
|
> = {
|
|
[SortOrder.Name]: (a: HotelData, b: HotelData) =>
|
|
a.hotelData.name.localeCompare(b.hotelData.name),
|
|
[SortOrder.TripAdvisorRating]: (a: HotelData, b: HotelData) =>
|
|
(b.hotelData.ratings?.tripAdvisor.rating ?? 0) -
|
|
(a.hotelData.ratings?.tripAdvisor.rating ?? 0),
|
|
[SortOrder.Price]: (a: HotelData, b: HotelData) =>
|
|
getPricePerNight(a) - getPricePerNight(b),
|
|
[SortOrder.Distance]: (a: HotelData, b: HotelData) =>
|
|
a.hotelData.location.distanceToCentre -
|
|
b.hotelData.location.distanceToCentre,
|
|
}
|
|
|
|
const sortStrategy =
|
|
sortingStrategies[sortBy] ?? sortingStrategies[SortOrder.Distance]
|
|
|
|
if (bookingCode) {
|
|
const bookingCodeHotels = hotels.filter(
|
|
(hotel) =>
|
|
(hotel?.price?.public?.rateType?.toLowerCase() !== "regular" ||
|
|
hotel?.price?.member?.rateType?.toLowerCase() !== "regular") &&
|
|
!!hotel?.price
|
|
)
|
|
const regularHotels = hotels.filter(
|
|
(hotel) => hotel?.price?.public?.rateType?.toLowerCase() === "regular"
|
|
)
|
|
|
|
return [...bookingCodeHotels]
|
|
.sort(sortStrategy)
|
|
.concat([...regularHotels].sort(sortStrategy))
|
|
.concat([...unAvailableHotels].sort(sortStrategy))
|
|
}
|
|
|
|
return [...availableHotels]
|
|
.sort(sortStrategy)
|
|
.concat([...unAvailableHotels].sort(sortStrategy))
|
|
}
|