feat: keep inventory of bedselections
This commit is contained in:
committed by
Michael Zetterberg
parent
39b89c5d51
commit
5ca30d02a0
@@ -64,6 +64,7 @@ import {
|
||||
getRoomsAvailability,
|
||||
getSelectedRoomAvailability,
|
||||
mergeRoomTypes,
|
||||
selectRateRedirectURL,
|
||||
} from "./utils"
|
||||
|
||||
import { AvailabilityEnum } from "@/types/components/hotelReservation/selectHotel/selectHotel"
|
||||
@@ -71,6 +72,7 @@ import { BreakfastPackageEnum } from "@/types/enums/breakfast"
|
||||
import { RateEnum } from "@/types/enums/rate"
|
||||
import { RateTypeEnum } from "@/types/enums/rateType"
|
||||
import type { DestinationPagesHotelData, HotelDataWithUrl } from "@/types/hotel"
|
||||
import type { Room } from "@/types/providers/details/room"
|
||||
import type { CityLocation } from "@/types/trpc/routers/hotel/locations"
|
||||
|
||||
export const hotelQueryRouter = router({
|
||||
@@ -252,7 +254,38 @@ export const hotelQueryRouter = router({
|
||||
})
|
||||
}
|
||||
|
||||
return selectedRooms
|
||||
const totalBedsAvailableForRoomTypeCode: Record<string, number> = {}
|
||||
for (const selectedRoom of selectedRooms) {
|
||||
if (selectedRoom) {
|
||||
if (!totalBedsAvailableForRoomTypeCode[selectedRoom.roomTypeCode]) {
|
||||
totalBedsAvailableForRoomTypeCode[selectedRoom.roomTypeCode] =
|
||||
selectedRoom.bedTypes.reduce(
|
||||
(total, bedType) => total + bedType.roomsLeft,
|
||||
0
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (const [idx, selectedRoom] of selectedRooms.entries()) {
|
||||
if (selectedRoom) {
|
||||
const totalBedsLeft =
|
||||
totalBedsAvailableForRoomTypeCode[selectedRoom.roomTypeCode]
|
||||
if (totalBedsLeft <= 0) {
|
||||
selectedRooms[idx] = null
|
||||
continue
|
||||
}
|
||||
totalBedsAvailableForRoomTypeCode[selectedRoom.roomTypeCode] =
|
||||
totalBedsAvailableForRoomTypeCode[selectedRoom.roomTypeCode] - 1
|
||||
}
|
||||
}
|
||||
|
||||
if (selectedRooms.some((sr) => !sr)) {
|
||||
return selectRateRedirectURL(input, selectedRooms.map(Boolean))
|
||||
}
|
||||
|
||||
// Make TS show appropriate type
|
||||
return selectedRooms.filter((sr): sr is Room => !!sr)
|
||||
}),
|
||||
myStay: safeProtectedServiceProcedure
|
||||
.input(myStayRoomAvailabilityInputSchema)
|
||||
|
||||
Reference in New Issue
Block a user