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)
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import deepmerge from "deepmerge"
|
||||
import stringify from "json-stable-stringify-without-jsonify"
|
||||
|
||||
import { REDEMPTION } from "@/constants/booking"
|
||||
import { BookingErrorCodeEnum, REDEMPTION } from "@/constants/booking"
|
||||
import { Lang } from "@/constants/languages"
|
||||
import { selectRate } from "@/constants/routes/hotelReservation"
|
||||
import { env } from "@/env/server"
|
||||
import * as api from "@/lib/api"
|
||||
import { badRequestError } from "@/server/errors/trpc"
|
||||
@@ -43,6 +44,7 @@ import type { PackagesOutput } from "@/types/requests/packages"
|
||||
import type {
|
||||
HotelsAvailabilityInputSchema,
|
||||
HotelsByHotelIdsAvailabilityInputSchema,
|
||||
RoomsAvailabilityExtendedInputSchema,
|
||||
RoomsAvailabilityInputRoom,
|
||||
RoomsAvailabilityOutputSchema,
|
||||
} from "@/types/trpc/routers/hotel/availability"
|
||||
@@ -1245,6 +1247,7 @@ export function getBedTypes(
|
||||
size: matchingRoom.mainBed.widthRange,
|
||||
value: matchingRoom.code,
|
||||
type: matchingRoom.mainBed.type,
|
||||
roomsLeft: availRoom.roomsLeft,
|
||||
extraBed: matchingRoom.fixedExtraBed
|
||||
? {
|
||||
type: matchingRoom.fixedExtraBed.type,
|
||||
@@ -1293,3 +1296,43 @@ export function mergeRoomTypes(roomConfigurations: RoomConfiguration[]) {
|
||||
}
|
||||
return Array.from(roomConfigs.values())
|
||||
}
|
||||
|
||||
export function selectRateRedirectURL(
|
||||
input: RoomsAvailabilityExtendedInputSchema,
|
||||
selectedRooms: boolean[]
|
||||
) {
|
||||
const searchParams = new URLSearchParams({
|
||||
errorCode: BookingErrorCodeEnum.AvailabilityError,
|
||||
fromdate: input.booking.fromDate,
|
||||
hotel: input.booking.hotelId,
|
||||
todate: input.booking.toDate,
|
||||
})
|
||||
if (input.booking.searchType) {
|
||||
searchParams.set("searchtype", input.booking.searchType)
|
||||
}
|
||||
for (const [idx, room] of input.booking.rooms.entries()) {
|
||||
searchParams.set(`room[${idx}].adults`, room.adults.toString())
|
||||
|
||||
if (selectedRooms[idx]) {
|
||||
if (room.counterRateCode) {
|
||||
searchParams.set(`room[${idx}].counterratecode`, room.counterRateCode)
|
||||
}
|
||||
searchParams.set(`room[${idx}].ratecode`, room.rateCode)
|
||||
searchParams.set(`room[${idx}].roomtype`, room.roomTypeCode)
|
||||
}
|
||||
if (room.bookingCode) {
|
||||
searchParams.set(`room[${idx}].bookingCode`, room.bookingCode)
|
||||
}
|
||||
if (room.packages) {
|
||||
searchParams.set(`room[${idx}].packages`, room.packages.join(","))
|
||||
}
|
||||
if (room.childrenInRoom?.length) {
|
||||
for (const [i, kid] of room.childrenInRoom.entries()) {
|
||||
searchParams.set(`room[${idx}].child[${i}].age`, kid.age.toString())
|
||||
searchParams.set(`room[${idx}].child[${i}].bed`, kid.bed.toString())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return `${selectRate(input.lang)}?${searchParams.toString()}`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user