Feat/SW-2113 allow feature combinations * feat(SW-2113): Refactor features data to be fetched on filter room filter change * feat(SW-2113): added loading state * fix: now clear room selection when applying filter and room doesnt exists. And added room features to mobile summary * fix * fix: add package to price details * feat(SW-2113): added buttons to room filter * fix: active room * fix: remove console log * fix: added form and close handler to room package filter * fix: add restriction so you cannot select pet room with allergy room and vice versa * fix: fixes from review feedback * fix * fix: hide modify button if on nextcoming rooms if no selection is made, and adjust filter logic in togglePackage * fix: forgot to use roomFeatureCodes from input.. * fix: naming Approved-by: Simon.Emanuelsson
227 lines
6.3 KiB
TypeScript
227 lines
6.3 KiB
TypeScript
import type { Price } from "@/types/components/hotelReservation/price"
|
|
import {
|
|
type RoomPackage,
|
|
RoomPackageCodeEnum,
|
|
} from "@/types/components/hotelReservation/selectRate/roomFilter"
|
|
import type { Rate } from "@/types/components/hotelReservation/selectRate/selectRate"
|
|
import { CurrencyEnum } from "@/types/enums/currency"
|
|
import type { Packages } from "@/types/requests/packages"
|
|
import type { RedemptionProduct } from "@/types/trpc/routers/hotel/roomAvailability"
|
|
|
|
export function calculateTotalPrice(
|
|
selectedRateSummary: Rate[],
|
|
isUserLoggedIn: boolean,
|
|
petRoomPackage: RoomPackage | undefined
|
|
) {
|
|
return selectedRateSummary.reduce<Price>(
|
|
(total, room, idx) => {
|
|
if (!("member" in room.product) || !("public" in room.product)) {
|
|
return total
|
|
}
|
|
|
|
const roomNr = idx + 1
|
|
const isMainRoom = roomNr === 1
|
|
let rate
|
|
if (isUserLoggedIn && isMainRoom && room.product.member) {
|
|
rate = room.product.member
|
|
} else if (room.product.public) {
|
|
rate = room.product.public
|
|
}
|
|
|
|
if (!rate) {
|
|
return total
|
|
}
|
|
|
|
const isPetRoom = room.features.find(
|
|
(feature) => feature.code === RoomPackageCodeEnum.PET_ROOM
|
|
)
|
|
let petRoomPriceLocal = 0
|
|
if (
|
|
petRoomPackage &&
|
|
isPetRoom &&
|
|
room.packages.includes(RoomPackageCodeEnum.PET_ROOM)
|
|
) {
|
|
petRoomPriceLocal = Number(petRoomPackage.localPrice.totalPrice)
|
|
}
|
|
let petRoomPriceRequested = 0
|
|
if (
|
|
petRoomPackage &&
|
|
isPetRoom &&
|
|
room.packages.includes(RoomPackageCodeEnum.PET_ROOM)
|
|
) {
|
|
petRoomPriceRequested = Number(petRoomPackage.requestedPrice.totalPrice)
|
|
}
|
|
|
|
total.local.currency = rate.localPrice.currency
|
|
total.local.price =
|
|
total.local.price + rate.localPrice.pricePerStay + petRoomPriceLocal
|
|
|
|
if (rate.localPrice.regularPricePerStay) {
|
|
total.local.regularPrice =
|
|
(total.local.regularPrice || 0) +
|
|
rate.localPrice.regularPricePerStay +
|
|
petRoomPriceLocal
|
|
}
|
|
|
|
if (rate.requestedPrice) {
|
|
if (!total.requested) {
|
|
total.requested = {
|
|
currency: rate.requestedPrice.currency,
|
|
price: 0,
|
|
}
|
|
}
|
|
|
|
if (!total.requested.currency) {
|
|
total.requested.currency = rate.requestedPrice.currency
|
|
}
|
|
|
|
total.requested.price =
|
|
total.requested.price +
|
|
rate.requestedPrice.pricePerStay +
|
|
petRoomPriceRequested
|
|
|
|
if (rate.requestedPrice.regularPricePerStay) {
|
|
total.requested.regularPrice =
|
|
(total.requested.regularPrice || 0) +
|
|
rate.requestedPrice.regularPricePerStay +
|
|
petRoomPriceRequested
|
|
}
|
|
}
|
|
|
|
return total
|
|
},
|
|
{
|
|
local: {
|
|
currency: CurrencyEnum.Unknown,
|
|
price: 0,
|
|
regularPrice: undefined,
|
|
},
|
|
requested: undefined,
|
|
}
|
|
)
|
|
}
|
|
|
|
export function calculateRedemptionTotalPrice(
|
|
redemption: RedemptionProduct["redemption"]
|
|
) {
|
|
return {
|
|
local: {
|
|
additionalPrice: redemption.localPrice.additionalPricePerStay
|
|
? redemption.localPrice.additionalPricePerStay
|
|
: undefined,
|
|
additionalPriceCurrency: redemption.localPrice.currency
|
|
? redemption.localPrice.currency
|
|
: undefined,
|
|
currency: CurrencyEnum.POINTS,
|
|
price: redemption.localPrice.pointsPerStay,
|
|
},
|
|
}
|
|
}
|
|
|
|
export function calculateVoucherPrice(selectedRateSummary: Rate[]) {
|
|
return selectedRateSummary.reduce<Price>(
|
|
(total, room) => {
|
|
if (!("voucher" in room.product)) {
|
|
return total
|
|
}
|
|
const rate = room.product.voucher
|
|
|
|
return {
|
|
local: {
|
|
currency: total.local.currency,
|
|
price: total.local.price + rate.numberOfVouchers,
|
|
},
|
|
requested: undefined,
|
|
}
|
|
},
|
|
{
|
|
local: {
|
|
currency: CurrencyEnum.Voucher,
|
|
price: 0,
|
|
},
|
|
requested: undefined,
|
|
}
|
|
)
|
|
}
|
|
|
|
export function calculateCorporateChequePrice(selectedRateSummary: Rate[]) {
|
|
return selectedRateSummary.reduce<Price>(
|
|
(total, room) => {
|
|
if (!("corporateCheque" in room.product)) {
|
|
return total
|
|
}
|
|
const rate = room.product.corporateCheque
|
|
|
|
total.local.price = total.local.price + rate.localPrice.numberOfCheques
|
|
if (rate.localPrice.additionalPricePerStay) {
|
|
total.local.additionalPrice =
|
|
(total.local.additionalPrice || 0) +
|
|
rate.localPrice.additionalPricePerStay
|
|
}
|
|
if (rate.localPrice.currency) {
|
|
total.local.additionalPriceCurrency = rate.localPrice.currency
|
|
}
|
|
|
|
if (rate.requestedPrice) {
|
|
if (!total.requested) {
|
|
total.requested = {
|
|
currency: CurrencyEnum.CC,
|
|
price: 0,
|
|
}
|
|
}
|
|
|
|
total.requested.price =
|
|
total.requested.price + rate.requestedPrice.numberOfCheques
|
|
|
|
if (rate.requestedPrice.additionalPricePerStay) {
|
|
total.requested.additionalPrice =
|
|
(total.requested.additionalPrice || 0) +
|
|
rate.requestedPrice.additionalPricePerStay
|
|
}
|
|
|
|
if (rate.requestedPrice.currency) {
|
|
total.requested.additionalPriceCurrency = rate.requestedPrice.currency
|
|
}
|
|
}
|
|
|
|
return total
|
|
},
|
|
{
|
|
local: {
|
|
currency: CurrencyEnum.CC,
|
|
price: 0,
|
|
},
|
|
requested: undefined,
|
|
}
|
|
)
|
|
}
|
|
|
|
export function getTotalPrice(
|
|
mainRoomProduct: Rate | null,
|
|
rateSummary: Array<Rate | null>,
|
|
isUserLoggedIn: boolean,
|
|
petRoomPackage: NonNullable<Packages>[number] | undefined
|
|
): Price | null {
|
|
const summaryArray = rateSummary.filter((rate): rate is Rate => rate !== null)
|
|
|
|
if (summaryArray.some((rate) => "corporateCheque" in rate.product)) {
|
|
return calculateCorporateChequePrice(summaryArray)
|
|
}
|
|
|
|
if (!mainRoomProduct) {
|
|
return calculateTotalPrice(summaryArray, isUserLoggedIn, petRoomPackage)
|
|
}
|
|
|
|
const { product } = mainRoomProduct
|
|
|
|
// In case of reward night (redemption) or voucher only single room booking is supported by business rules
|
|
if ("redemption" in product) {
|
|
return calculateRedemptionTotalPrice(product.redemption)
|
|
}
|
|
if ("voucher" in product) {
|
|
return calculateVoucherPrice(summaryArray)
|
|
}
|
|
|
|
return calculateTotalPrice(summaryArray, isUserLoggedIn, petRoomPackage)
|
|
}
|