feat(SW-874): Show correct rooms
This commit is contained in:
71
components/HotelReservation/SelectRate/Rooms/utils.ts
Normal file
71
components/HotelReservation/SelectRate/Rooms/utils.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
import { RoomConfiguration } from "@/server/routers/hotels/output"
|
||||
|
||||
export function getLowestPricedRooms(roomConfigurations: RoomConfiguration[]) {
|
||||
const roomTypeCount = roomConfigurations.reduce(
|
||||
(acc, room) => {
|
||||
acc[room.roomType] = (acc[room.roomType] || 0) + 1
|
||||
return acc
|
||||
},
|
||||
{} as Record<string, number>
|
||||
)
|
||||
|
||||
const duplicateRoomTypes = new Set(
|
||||
Object.keys(roomTypeCount).filter((roomType) => roomTypeCount[roomType] > 1)
|
||||
)
|
||||
|
||||
const roomMap = new Map()
|
||||
|
||||
roomConfigurations.forEach((room) => {
|
||||
const { roomType, products } = room
|
||||
|
||||
if (!duplicateRoomTypes.has(roomType)) {
|
||||
roomMap.set(roomType, room)
|
||||
return
|
||||
}
|
||||
|
||||
products.forEach((product) => {
|
||||
const { productType } = product
|
||||
const publicProduct = productType.public
|
||||
const memberProduct = productType.member || {
|
||||
requestedPrice: null,
|
||||
localPrice: null,
|
||||
}
|
||||
|
||||
const {
|
||||
requestedPrice: publicRequestedPrice,
|
||||
localPrice: publicLocalPrice,
|
||||
} = publicProduct
|
||||
const {
|
||||
requestedPrice: memberRequestedPrice,
|
||||
localPrice: memberLocalPrice,
|
||||
} = memberProduct
|
||||
|
||||
const currentLowest = roomMap.get(roomType)
|
||||
|
||||
const currentRequestedPrice = Math.min(
|
||||
Number(publicRequestedPrice.pricePerNight) ?? Infinity,
|
||||
Number(memberRequestedPrice?.pricePerNight) ?? Infinity
|
||||
)
|
||||
const currentLocalPrice = Math.min(
|
||||
Number(publicLocalPrice.pricePerNight) ?? Infinity,
|
||||
Number(memberLocalPrice?.pricePerNight) ?? Infinity
|
||||
)
|
||||
|
||||
if (
|
||||
!currentLowest ||
|
||||
currentRequestedPrice < currentLowest.requestedPrice ||
|
||||
(currentRequestedPrice === currentLowest.requestedPrice &&
|
||||
currentLocalPrice < currentLowest.localPrice)
|
||||
) {
|
||||
roomMap.set(roomType, {
|
||||
...room,
|
||||
product,
|
||||
requestedPrice: currentRequestedPrice,
|
||||
localPrice: currentLocalPrice,
|
||||
})
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
return Array.from(roomMap.values())
|
||||
}
|
||||
Reference in New Issue
Block a user