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
90 lines
2.1 KiB
TypeScript
90 lines
2.1 KiB
TypeScript
import { RoomPackageCodeEnum } from "@/types/components/hotelReservation/selectRate/roomFilter"
|
|
import type { AvailabilityError } from "@/types/stores/rates"
|
|
import type {
|
|
Product,
|
|
RoomConfiguration,
|
|
} from "@/types/trpc/routers/hotel/roomAvailability"
|
|
|
|
export function findProduct(rateCode: string, product: Product) {
|
|
if ("corporateCheque" in product) {
|
|
return product.corporateCheque.rateCode === rateCode
|
|
}
|
|
|
|
if ("redemption" in product) {
|
|
return product.redemption.rateCode === rateCode
|
|
}
|
|
|
|
if ("voucher" in product) {
|
|
return product.voucher.rateCode === rateCode
|
|
}
|
|
|
|
if ("public" in product && product.public) {
|
|
return product.public.rateCode === rateCode
|
|
}
|
|
|
|
if ("member" in product && product.member) {
|
|
return product.member.rateCode === rateCode
|
|
}
|
|
|
|
return null
|
|
}
|
|
|
|
export function findProductInRoom(rateCode: string, room: RoomConfiguration) {
|
|
if (room.campaign.length) {
|
|
const campaignProduct = room.campaign.find((product) =>
|
|
findProduct(rateCode, product)
|
|
)
|
|
if (campaignProduct) {
|
|
return campaignProduct
|
|
}
|
|
}
|
|
if (room.code.length) {
|
|
const codeProduct = room.code.find((product) =>
|
|
findProduct(rateCode, product)
|
|
)
|
|
if (codeProduct) {
|
|
return codeProduct
|
|
}
|
|
}
|
|
if (room.redemptions.length) {
|
|
const redemptionProduct = room.redemptions.find((product) =>
|
|
findProduct(rateCode, product)
|
|
)
|
|
if (redemptionProduct) {
|
|
return redemptionProduct
|
|
}
|
|
}
|
|
if (room.regular.length) {
|
|
const regularProduct = room.regular.find((product) =>
|
|
findProduct(rateCode, product)
|
|
)
|
|
if (regularProduct) {
|
|
return regularProduct
|
|
}
|
|
}
|
|
}
|
|
|
|
export function findSelectedRate(
|
|
rateCode: string,
|
|
roomTypeCode: string,
|
|
rooms: RoomConfiguration[] | AvailabilityError
|
|
) {
|
|
if (!Array.isArray(rooms)) {
|
|
return null
|
|
}
|
|
return rooms.find((room) => {
|
|
if (room.roomTypeCode !== roomTypeCode) {
|
|
return false
|
|
}
|
|
return findProductInRoom(rateCode, room)
|
|
})
|
|
}
|
|
|
|
export function isRoomPackageCode(
|
|
code: string | null
|
|
): code is RoomPackageCodeEnum {
|
|
return Object.values(RoomPackageCodeEnum).includes(
|
|
code as RoomPackageCodeEnum
|
|
)
|
|
}
|