Feat/SW-1308 booking codes track b * feat: SW-1308 Booking codes track b * feat: SW-1308 Booking codes Track B implementation * feat: SW-1308 Optimized after rebase Approved-by: Arvid Norlin
152 lines
4.1 KiB
TypeScript
152 lines
4.1 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"
|
|
|
|
export const calculateTotalPrice = (
|
|
selectedRateSummary: Rate[],
|
|
isUserLoggedIn: boolean,
|
|
petRoomPackage: RoomPackage | undefined
|
|
) => {
|
|
return selectedRateSummary.reduce<Price>(
|
|
(total, room, idx) => {
|
|
const rate =
|
|
isUserLoggedIn && room.member && idx + 1 === 1
|
|
? room.member
|
|
: room.public
|
|
|
|
if (!rate) {
|
|
return total
|
|
}
|
|
|
|
const isPetRoom = room.features.find(
|
|
(feature) => feature.code === RoomPackageCodeEnum.PET_ROOM
|
|
)
|
|
|
|
let petRoomPrice = 0
|
|
if (
|
|
petRoomPackage &&
|
|
isPetRoom &&
|
|
room.package === RoomPackageCodeEnum.PET_ROOM
|
|
) {
|
|
petRoomPrice = Number(petRoomPackage.localPrice.totalPrice)
|
|
}
|
|
|
|
const regularPrice = rate.localPrice.regularPricePerStay
|
|
? (total.local.regularPrice || 0) +
|
|
(rate.localPrice.regularPricePerStay || 0)
|
|
: undefined
|
|
|
|
return {
|
|
local: {
|
|
currency: rate.localPrice.currency,
|
|
price:
|
|
total.local.price + rate.localPrice.pricePerStay + petRoomPrice,
|
|
regularPrice,
|
|
},
|
|
requested: rate.requestedPrice
|
|
? {
|
|
currency: rate.requestedPrice.currency,
|
|
price:
|
|
(total.requested?.price ?? 0) +
|
|
rate.requestedPrice.pricePerStay +
|
|
petRoomPrice,
|
|
}
|
|
: undefined,
|
|
}
|
|
},
|
|
{
|
|
local: {
|
|
currency: (selectedRateSummary[0].public?.localPrice.currency ||
|
|
selectedRateSummary[0].member?.localPrice.currency)!,
|
|
price: 0,
|
|
regularPrice: undefined,
|
|
},
|
|
requested: undefined,
|
|
}
|
|
)
|
|
}
|
|
|
|
export const calculateVoucherPrice = (selectedRateSummary: Rate[]) => {
|
|
return selectedRateSummary.reduce<Price>(
|
|
(total, room) => {
|
|
const rate = room.voucher
|
|
if (!rate) {
|
|
return total
|
|
}
|
|
|
|
return <Price>{
|
|
local: {
|
|
currency: total.local.currency,
|
|
price: total.local.price + rate.numberOfVouchers,
|
|
},
|
|
requested: undefined,
|
|
}
|
|
},
|
|
{
|
|
local: {
|
|
currency: CurrencyEnum.Voucher,
|
|
price: 0,
|
|
},
|
|
requested: undefined,
|
|
}
|
|
)
|
|
}
|
|
|
|
export const calculateChequePrice = (selectedRateSummary: Rate[]) => {
|
|
return selectedRateSummary.reduce<Price>(
|
|
(total, room) => {
|
|
const rate = room.bonusCheque
|
|
if (!rate) {
|
|
return total
|
|
}
|
|
|
|
const price = total.local.price + rate.localPrice.numberOfBonusCheques
|
|
|
|
const additionalPrice =
|
|
rate.localPrice.numberOfBonusCheques &&
|
|
(total.local.additionalPrice ?? 0) +
|
|
(rate.localPrice.additionalPricePerStay ?? 0)
|
|
const additionalPriceCurrency = (rate.localPrice.numberOfBonusCheques &&
|
|
rate.localPrice.currency)!
|
|
|
|
const requestedPrice = rate.requestedPrice?.numberOfBonusCheques
|
|
? (total.requested?.price ?? 0) +
|
|
rate.requestedPrice?.numberOfBonusCheques
|
|
: total.requested?.price
|
|
|
|
const requestedAdditionalPrice =
|
|
rate.requestedPrice?.additionalPricePerStay &&
|
|
(total.requested?.additionalPrice ?? 0) +
|
|
(rate.requestedPrice?.additionalPricePerStay ?? 0)
|
|
|
|
return <Price>{
|
|
local: {
|
|
currency: CurrencyEnum.CC,
|
|
price,
|
|
additionalPrice,
|
|
additionalPriceCurrency,
|
|
},
|
|
requested: rate.requestedPrice
|
|
? {
|
|
currency: CurrencyEnum.CC,
|
|
price: requestedPrice,
|
|
additionalPrice: requestedAdditionalPrice,
|
|
additionalPriceCurrency: rate.requestedPrice?.currency,
|
|
}
|
|
: undefined,
|
|
}
|
|
},
|
|
{
|
|
local: {
|
|
currency: CurrencyEnum.CC,
|
|
price: 0,
|
|
},
|
|
requested: undefined,
|
|
}
|
|
)
|
|
}
|