From 0618b10870b30cb1ae0f16ff40ebb8ea8a4594c4 Mon Sep 17 00:00:00 2001 From: Pontus Dreij Date: Tue, 12 Nov 2024 15:52:24 +0100 Subject: [PATCH 1/7] feat(SW-874): Show correct rooms --- .../SelectRate/Rooms/index.tsx | 25 ++++--- .../SelectRate/Rooms/utils.ts | 71 +++++++++++++++++++ 2 files changed, 87 insertions(+), 9 deletions(-) create mode 100644 components/HotelReservation/SelectRate/Rooms/utils.ts diff --git a/components/HotelReservation/SelectRate/Rooms/index.tsx b/components/HotelReservation/SelectRate/Rooms/index.tsx index 8c122a9c0..a3454818a 100644 --- a/components/HotelReservation/SelectRate/Rooms/index.tsx +++ b/components/HotelReservation/SelectRate/Rooms/index.tsx @@ -2,10 +2,9 @@ import { useCallback, useState } from "react" -import { RoomsAvailability } from "@/server/routers/hotels/output" - import RoomFilter from "../RoomFilter" import RoomSelection from "../RoomSelection" +import { getLowestPricedRooms } from "./utils" import styles from "./rooms.module.css" @@ -14,6 +13,10 @@ import { type RoomPackageCodes, } from "@/types/components/hotelReservation/selectRate/roomFilter" import type { RoomSelectionProps } from "@/types/components/hotelReservation/selectRate/roomSelection" +import type { + RoomConfiguration, + RoomsAvailability, +} from "@/server/routers/hotels/output" export default function Rooms({ roomsAvailability, @@ -21,7 +24,12 @@ export default function Rooms({ user, packages, }: Omit) { - const defaultRooms = roomsAvailability.roomConfigurations + console.log("roomsAvailability", roomsAvailability) + const visibleRooms: RoomConfiguration[] = getLowestPricedRooms( + roomsAvailability.roomConfigurations + ) + + const defaultRooms = visibleRooms const [rooms, setRooms] = useState({ ...roomsAvailability, roomConfigurations: defaultRooms, @@ -46,15 +54,14 @@ export default function Rooms({ return } - const filteredRooms = roomsAvailability.roomConfigurations.filter( - (room) => - filteredPackages.every((filteredPackage) => - room.features.some((feature) => feature.code === filteredPackage) - ) + const filteredRooms = visibleRooms.filter((room) => + filteredPackages.every((filteredPackage) => + room.features.some((feature) => feature.code === filteredPackage) + ) ) setRooms({ ...roomsAvailability, roomConfigurations: filteredRooms }) }, - [roomsAvailability, defaultRooms] + [roomsAvailability, defaultRooms, visibleRooms] ) return ( diff --git a/components/HotelReservation/SelectRate/Rooms/utils.ts b/components/HotelReservation/SelectRate/Rooms/utils.ts new file mode 100644 index 000000000..bb1ff48c9 --- /dev/null +++ b/components/HotelReservation/SelectRate/Rooms/utils.ts @@ -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 + ) + + 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()) +} From 70fbe44e51716dc8c639d8f07642475888f14a40 Mon Sep 17 00:00:00 2001 From: Pontus Dreij Date: Wed, 13 Nov 2024 08:17:53 +0100 Subject: [PATCH 2/7] feat(SW-874): Don't add petRoomPrice if filter is not selected --- .../SelectRate/RoomSelection/FlexibilityOption/index.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/HotelReservation/SelectRate/RoomSelection/FlexibilityOption/index.tsx b/components/HotelReservation/SelectRate/RoomSelection/FlexibilityOption/index.tsx index a0fd30b9e..9bc6ca1f3 100644 --- a/components/HotelReservation/SelectRate/RoomSelection/FlexibilityOption/index.tsx +++ b/components/HotelReservation/SelectRate/RoomSelection/FlexibilityOption/index.tsx @@ -56,7 +56,7 @@ export default function FlexibilityOption({ priceName: name, public: publicPrice, member: memberPrice, - features, + features: petRoomPackage ? features : [], } handleSelectRate(rate) } From 3869c41f58371e0706f81bcf5e59883d0d52c7a3 Mon Sep 17 00:00:00 2001 From: Pontus Dreij Date: Wed, 13 Nov 2024 08:21:47 +0100 Subject: [PATCH 3/7] feat(SW-874): Rename function --- components/HotelReservation/SelectRate/Rooms/index.tsx | 4 ++-- components/HotelReservation/SelectRate/Rooms/utils.ts | 8 +++++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/components/HotelReservation/SelectRate/Rooms/index.tsx b/components/HotelReservation/SelectRate/Rooms/index.tsx index a3454818a..d4c8ec50b 100644 --- a/components/HotelReservation/SelectRate/Rooms/index.tsx +++ b/components/HotelReservation/SelectRate/Rooms/index.tsx @@ -4,7 +4,7 @@ import { useCallback, useState } from "react" import RoomFilter from "../RoomFilter" import RoomSelection from "../RoomSelection" -import { getLowestPricedRooms } from "./utils" +import { getLowestPricedDuplicateRooms } from "./utils" import styles from "./rooms.module.css" @@ -25,7 +25,7 @@ export default function Rooms({ packages, }: Omit) { console.log("roomsAvailability", roomsAvailability) - const visibleRooms: RoomConfiguration[] = getLowestPricedRooms( + const visibleRooms: RoomConfiguration[] = getLowestPricedDuplicateRooms( roomsAvailability.roomConfigurations ) diff --git a/components/HotelReservation/SelectRate/Rooms/utils.ts b/components/HotelReservation/SelectRate/Rooms/utils.ts index bb1ff48c9..3c0507371 100644 --- a/components/HotelReservation/SelectRate/Rooms/utils.ts +++ b/components/HotelReservation/SelectRate/Rooms/utils.ts @@ -1,6 +1,12 @@ import { RoomConfiguration } from "@/server/routers/hotels/output" -export function getLowestPricedRooms(roomConfigurations: RoomConfiguration[]) { +/** + * Get the lowest priced room for each room type that appears more than once. + */ + +export function getLowestPricedDuplicateRooms( + roomConfigurations: RoomConfiguration[] +) { const roomTypeCount = roomConfigurations.reduce( (acc, room) => { acc[room.roomType] = (acc[room.roomType] || 0) + 1 From a7ef5857eee9cc4c01bf530d5520ce9330f2b428 Mon Sep 17 00:00:00 2001 From: Pontus Dreij Date: Wed, 13 Nov 2024 08:23:08 +0100 Subject: [PATCH 4/7] feat(SW-874): Removed log --- components/HotelReservation/SelectRate/Rooms/index.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/components/HotelReservation/SelectRate/Rooms/index.tsx b/components/HotelReservation/SelectRate/Rooms/index.tsx index d4c8ec50b..5e982a10f 100644 --- a/components/HotelReservation/SelectRate/Rooms/index.tsx +++ b/components/HotelReservation/SelectRate/Rooms/index.tsx @@ -24,7 +24,6 @@ export default function Rooms({ user, packages, }: Omit) { - console.log("roomsAvailability", roomsAvailability) const visibleRooms: RoomConfiguration[] = getLowestPricedDuplicateRooms( roomsAvailability.roomConfigurations ) From 7ee7d60307894d64aa120a4d3eb328f5029145eb Mon Sep 17 00:00:00 2001 From: Pontus Dreij Date: Wed, 13 Nov 2024 14:25:15 +0100 Subject: [PATCH 5/7] feat(SW-874): removed defaultRooms since we have visibleRooms --- components/HotelReservation/SelectRate/Rooms/index.tsx | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/components/HotelReservation/SelectRate/Rooms/index.tsx b/components/HotelReservation/SelectRate/Rooms/index.tsx index 5e982a10f..c6d271550 100644 --- a/components/HotelReservation/SelectRate/Rooms/index.tsx +++ b/components/HotelReservation/SelectRate/Rooms/index.tsx @@ -28,10 +28,9 @@ export default function Rooms({ roomsAvailability.roomConfigurations ) - const defaultRooms = visibleRooms const [rooms, setRooms] = useState({ ...roomsAvailability, - roomConfigurations: defaultRooms, + roomConfigurations: visibleRooms, }) const [selectedPackages, setSelectedPackages] = useState( [] @@ -48,7 +47,7 @@ export default function Rooms({ if (filteredPackages.length === 0) { setRooms({ ...roomsAvailability, - roomConfigurations: defaultRooms, + roomConfigurations: visibleRooms, }) return } @@ -60,7 +59,7 @@ export default function Rooms({ ) setRooms({ ...roomsAvailability, roomConfigurations: filteredRooms }) }, - [roomsAvailability, defaultRooms, visibleRooms] + [roomsAvailability, visibleRooms] ) return ( From 2beba213231e2fd7f5995109af886ee3ee3355f1 Mon Sep 17 00:00:00 2001 From: Pontus Dreij Date: Wed, 13 Nov 2024 20:27:53 +0100 Subject: [PATCH 6/7] feat(SW-874): Updated logic for getLowestPricedDuplicateRooms --- .../SelectRate/Rooms/utils.ts | 47 ++++++++++++++----- 1 file changed, 36 insertions(+), 11 deletions(-) diff --git a/components/HotelReservation/SelectRate/Rooms/utils.ts b/components/HotelReservation/SelectRate/Rooms/utils.ts index 3c0507371..d7f7d2d85 100644 --- a/components/HotelReservation/SelectRate/Rooms/utils.ts +++ b/components/HotelReservation/SelectRate/Rooms/utils.ts @@ -46,7 +46,7 @@ export function getLowestPricedDuplicateRooms( localPrice: memberLocalPrice, } = memberProduct - const currentLowest = roomMap.get(roomType) + const previousLowest = roomMap.get(roomType) const currentRequestedPrice = Math.min( Number(publicRequestedPrice.pricePerNight) ?? Infinity, @@ -58,17 +58,42 @@ export function getLowestPricedDuplicateRooms( ) if ( - !currentLowest || - currentRequestedPrice < currentLowest.requestedPrice || - (currentRequestedPrice === currentLowest.requestedPrice && - currentLocalPrice < currentLowest.localPrice) + !previousLowest || + currentRequestedPrice < + Math.min( + Number( + previousLowest.products[0].productType.public.requestedPrice + .pricePerNight + ) ?? Infinity, + Number( + previousLowest.products[0].productType.member?.requestedPrice + ?.pricePerNight + ) ?? Infinity + ) || + (currentRequestedPrice === + Math.min( + Number( + previousLowest.products[0].productType.public.requestedPrice + .pricePerNight + ) ?? Infinity, + Number( + previousLowest.products[0].productType.member?.requestedPrice + ?.pricePerNight + ) ?? Infinity + ) && + currentLocalPrice < + Math.min( + Number( + previousLowest.products[0].productType.public.localPrice + .pricePerNight + ) ?? Infinity, + Number( + previousLowest.products[0].productType.member?.localPrice + ?.pricePerNight + ) ?? Infinity + )) ) { - roomMap.set(roomType, { - ...room, - product, - requestedPrice: currentRequestedPrice, - localPrice: currentLocalPrice, - }) + roomMap.set(roomType, room) } }) }) From 768bd40ad8052801ec8b3f6852b8975185d416b7 Mon Sep 17 00:00:00 2001 From: Pontus Dreij Date: Wed, 13 Nov 2024 20:41:48 +0100 Subject: [PATCH 7/7] feat(SW-874): Update function name --- components/HotelReservation/SelectRate/Rooms/index.tsx | 7 +++---- components/HotelReservation/SelectRate/Rooms/utils.ts | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/components/HotelReservation/SelectRate/Rooms/index.tsx b/components/HotelReservation/SelectRate/Rooms/index.tsx index c6d271550..8e56555a2 100644 --- a/components/HotelReservation/SelectRate/Rooms/index.tsx +++ b/components/HotelReservation/SelectRate/Rooms/index.tsx @@ -4,7 +4,7 @@ import { useCallback, useState } from "react" import RoomFilter from "../RoomFilter" import RoomSelection from "../RoomSelection" -import { getLowestPricedDuplicateRooms } from "./utils" +import { filterDuplicateRoomTypesByLowestPrice } from "./utils" import styles from "./rooms.module.css" @@ -24,9 +24,8 @@ export default function Rooms({ user, packages, }: Omit) { - const visibleRooms: RoomConfiguration[] = getLowestPricedDuplicateRooms( - roomsAvailability.roomConfigurations - ) + const visibleRooms: RoomConfiguration[] = + filterDuplicateRoomTypesByLowestPrice(roomsAvailability.roomConfigurations) const [rooms, setRooms] = useState({ ...roomsAvailability, diff --git a/components/HotelReservation/SelectRate/Rooms/utils.ts b/components/HotelReservation/SelectRate/Rooms/utils.ts index d7f7d2d85..9d6f24e00 100644 --- a/components/HotelReservation/SelectRate/Rooms/utils.ts +++ b/components/HotelReservation/SelectRate/Rooms/utils.ts @@ -4,7 +4,7 @@ import { RoomConfiguration } from "@/server/routers/hotels/output" * Get the lowest priced room for each room type that appears more than once. */ -export function getLowestPricedDuplicateRooms( +export function filterDuplicateRoomTypesByLowestPrice( roomConfigurations: RoomConfiguration[] ) { const roomTypeCount = roomConfigurations.reduce(