feat(SW-2033): Added new route for fetching room features, and merged the data with existing availability data * feat(SW-2033): Added new route for fetching room features, and merged the data with existing availability data * fix: issue with total price not including room features * fix: add return null * fix * fix * fixes from PR feedback Approved-by: Arvid Norlin
167 lines
4.9 KiB
TypeScript
167 lines
4.9 KiB
TypeScript
import type { RoomPackageCodeEnum } from "@/types/components/hotelReservation/selectRate/roomFilter"
|
|
import type {
|
|
Child,
|
|
Room,
|
|
} from "@/types/components/hotelReservation/selectRate/selectRate"
|
|
|
|
export function removeMultipleSlashes(pathname: string) {
|
|
return pathname.replaceAll(/\/\/+/g, "/")
|
|
}
|
|
|
|
export function removeTrailingSlash(pathname: string) {
|
|
if (pathname.endsWith("/")) {
|
|
// Remove the trailing slash
|
|
return pathname.slice(0, -1)
|
|
}
|
|
return pathname
|
|
}
|
|
|
|
type PartialRoom = { rooms?: Partial<Room>[] }
|
|
|
|
const keyedSearchParams = new Map([
|
|
["room", "rooms"],
|
|
["ratecode", "rateCode"],
|
|
["counterratecode", "counterRateCode"],
|
|
["roomtype", "roomTypeCode"],
|
|
["fromdate", "fromDate"],
|
|
["todate", "toDate"],
|
|
["hotel", "hotelId"],
|
|
["child", "childrenInRoom"],
|
|
["searchtype", "searchType"],
|
|
])
|
|
|
|
export type SelectHotelParams<T> = Omit<T, "hotel"> & {
|
|
hotelId: string
|
|
} & PartialRoom
|
|
|
|
export function getKeyFromSearchParam(key: string): string {
|
|
return keyedSearchParams.get(key) || key
|
|
}
|
|
|
|
export function getSearchParamFromKey(key: string): string {
|
|
for (const [mapKey, mapValue] of keyedSearchParams.entries()) {
|
|
if (mapValue === key) {
|
|
return mapKey
|
|
}
|
|
}
|
|
return key
|
|
}
|
|
|
|
export function searchParamsToRecord(searchParams: URLSearchParams) {
|
|
return Object.fromEntries(searchParams.entries())
|
|
}
|
|
|
|
export function convertSearchParamsToObj<T extends PartialRoom>(
|
|
searchParams: Record<string, string>
|
|
) {
|
|
const searchParamsObject = Object.entries(searchParams).reduce<
|
|
SelectHotelParams<T>
|
|
>((acc, [key, value]) => {
|
|
// The params are sometimes indexed with a number (for ex: `room[0].adults`),
|
|
// so we need to split them by . or []
|
|
const keys = key.replace(/\]/g, "").split(/\[|\./)
|
|
const firstKey = getKeyFromSearchParam(keys[0])
|
|
|
|
// Room is a special case since it is an array, so we need to handle it separately
|
|
if (firstKey === "rooms") {
|
|
// Rooms are always indexed with a number, so we need to extract the index
|
|
const index = Number(keys[1])
|
|
const roomObject =
|
|
acc.rooms && Array.isArray(acc.rooms) ? acc.rooms : (acc.rooms = [])
|
|
|
|
const roomObjectKey = getKeyFromSearchParam(keys[2]) as keyof Room
|
|
|
|
if (!roomObject[index]) {
|
|
roomObject[index] = {}
|
|
}
|
|
|
|
// Adults should be converted to a number
|
|
if (roomObjectKey === "adults") {
|
|
roomObject[index].adults = Number(value)
|
|
|
|
// Child is an array, so we need to handle it separately
|
|
} else if (roomObjectKey === "childrenInRoom") {
|
|
const childIndex = Number(keys[3])
|
|
const childKey = keys[4] as keyof Child
|
|
|
|
if (
|
|
!("childrenInRoom" in roomObject[index]) ||
|
|
!Array.isArray(roomObject[index].childrenInRoom)
|
|
) {
|
|
roomObject[index].childrenInRoom = []
|
|
}
|
|
|
|
roomObject[index].childrenInRoom![childIndex] = {
|
|
...roomObject[index].childrenInRoom![childIndex],
|
|
[childKey]: Number(value),
|
|
}
|
|
} else if (roomObjectKey === "packages") {
|
|
roomObject[index].packages = value.split(",") as RoomPackageCodeEnum[]
|
|
} else {
|
|
roomObject[index][roomObjectKey] = value
|
|
}
|
|
} else {
|
|
return { ...acc, [firstKey]: value }
|
|
}
|
|
|
|
return acc
|
|
}, {} as SelectHotelParams<T>)
|
|
|
|
return searchParamsObject
|
|
}
|
|
|
|
export function convertObjToSearchParams<T>(
|
|
bookingData: T & PartialRoom,
|
|
intitalSearchParams = {} as URLSearchParams
|
|
) {
|
|
const bookingSearchParams = new URLSearchParams(intitalSearchParams)
|
|
Object.entries(bookingData).forEach(([key, value]) => {
|
|
if (key === "rooms") {
|
|
value.forEach((item, index) => {
|
|
if (item?.adults) {
|
|
bookingSearchParams.set(
|
|
`room[${index}].adults`,
|
|
item.adults.toString()
|
|
)
|
|
}
|
|
if (item?.childrenInRoom) {
|
|
item.childrenInRoom.forEach((child, childIndex) => {
|
|
bookingSearchParams.set(
|
|
`room[${index}].child[${childIndex}].age`,
|
|
child.age.toString()
|
|
)
|
|
bookingSearchParams.set(
|
|
`room[${index}].child[${childIndex}].bed`,
|
|
child.bed.toString()
|
|
)
|
|
})
|
|
}
|
|
if (item?.roomTypeCode) {
|
|
bookingSearchParams.set(`room[${index}].roomtype`, item.roomTypeCode)
|
|
}
|
|
if (item?.rateCode) {
|
|
bookingSearchParams.set(`room[${index}].ratecode`, item.rateCode)
|
|
}
|
|
|
|
if (item?.counterRateCode) {
|
|
bookingSearchParams.set(
|
|
`room[${index}].counterratecode`,
|
|
item.counterRateCode
|
|
)
|
|
}
|
|
|
|
if (item.packages && item.packages.length > 0) {
|
|
bookingSearchParams.set(
|
|
`room[${index}].packages`,
|
|
item.packages.join(",")
|
|
)
|
|
}
|
|
})
|
|
} else {
|
|
bookingSearchParams.set(getSearchParamFromKey(key), value.toString())
|
|
}
|
|
})
|
|
|
|
return bookingSearchParams
|
|
}
|