Merged in fix/SW-3198-prices-select-rate (pull request #2763)
fix(SW-3198): fix striketrhough/regular prices, the same in enter details as select rate * fix(SW-3198): fix striketrhough/regular prices, the same in enter details as select rate * fix(SW-3198): remove additonalcost if calculating cost per room * fix(SW-3198): include bookingcode in specialrate * fix(SW-3198): remove console log * fix(SW-3198): add or operator * fix(SW-3198): capture total return value * fix(SW-3198): rename and move function Approved-by: Joakim Jäderberg Approved-by: Hrishikesh Vaipurkar
This commit is contained in:
@@ -230,7 +230,7 @@ export function SelectRateProvider({
|
||||
rate,
|
||||
roomConfiguration: roomAvailability[ix]?.[0],
|
||||
})),
|
||||
useMemberPrices: isUserLoggedIn,
|
||||
isMember: isUserLoggedIn,
|
||||
})
|
||||
|
||||
const getPriceForRoom = useCallback(
|
||||
@@ -249,7 +249,8 @@ export function SelectRateProvider({
|
||||
selectedRates: [
|
||||
{ rate, roomConfiguration: roomAvailability[roomIndex]?.[0] },
|
||||
],
|
||||
useMemberPrices: isUserLoggedIn,
|
||||
isMember: isUserLoggedIn && roomIndex === 0,
|
||||
addAdditionalCost: false,
|
||||
})
|
||||
},
|
||||
[selectedRates, roomAvailability, isUserLoggedIn]
|
||||
|
||||
@@ -6,7 +6,7 @@ describe("getTotalPrice", () => {
|
||||
it("should return null when no rates are selected", () => {
|
||||
const result = getTotalPrice({
|
||||
selectedRates: [],
|
||||
useMemberPrices: false,
|
||||
isMember: false,
|
||||
})
|
||||
|
||||
expect(result).toEqual({
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { CurrencyEnum } from "@scandic-hotels/common/constants/currency"
|
||||
|
||||
import { sumPackages } from "../../utils/SelectRate"
|
||||
import { calculateRegularPrice } from "../../utils/calculateRegularPrice"
|
||||
import { sumPackages, sumPackagesRequestedPrice } from "../../utils/SelectRate"
|
||||
|
||||
import type { RedemptionProduct } from "@scandic-hotels/trpc/types/roomAvailability"
|
||||
|
||||
@@ -26,10 +27,12 @@ type SelectedRate = {
|
||||
|
||||
export function getTotalPrice({
|
||||
selectedRates,
|
||||
useMemberPrices,
|
||||
isMember,
|
||||
addAdditionalCost = true,
|
||||
}: {
|
||||
selectedRates: Array<SelectedRate | null>
|
||||
useMemberPrices: boolean
|
||||
isMember: boolean
|
||||
addAdditionalCost?: boolean
|
||||
}): Price | null {
|
||||
const mainRoom = selectedRates[0]
|
||||
const mainRoomRate = mainRoom?.rate
|
||||
@@ -42,7 +45,7 @@ export function getTotalPrice({
|
||||
}
|
||||
|
||||
if (!mainRoomRate) {
|
||||
return calculateTotalPrice(summaryArray, useMemberPrices)
|
||||
return calculateTotalPrice(summaryArray, isMember, addAdditionalCost)
|
||||
}
|
||||
|
||||
// In case of reward night (redemption) or voucher only single room booking is supported by business rules
|
||||
@@ -59,14 +62,15 @@ export function getTotalPrice({
|
||||
return voucherPrice
|
||||
}
|
||||
|
||||
return calculateTotalPrice(summaryArray, useMemberPrices)
|
||||
return calculateTotalPrice(summaryArray, isMember, addAdditionalCost)
|
||||
}
|
||||
|
||||
function calculateTotalPrice(
|
||||
selectedRateSummary: OneLevelNonNullable<SelectedRate>[],
|
||||
useMemberPrices: boolean
|
||||
isMember: boolean,
|
||||
addAdditionalCost: boolean
|
||||
) {
|
||||
return selectedRateSummary.reduce<Price>(
|
||||
const totalPrice = selectedRateSummary.reduce<Price>(
|
||||
(total, room, idx) => {
|
||||
if (!room.rate || !("member" in room.rate) || !("public" in room.rate)) {
|
||||
return total
|
||||
@@ -75,34 +79,25 @@ function calculateTotalPrice(
|
||||
const roomNr = idx + 1
|
||||
const isMainRoom = roomNr === 1
|
||||
|
||||
const useMemberRate = isMainRoom && useMemberPrices && room.rate.member
|
||||
const useMemberRate = isMainRoom && isMember && room.rate.member
|
||||
const rate = useMemberRate ? room.rate.member : room.rate.public
|
||||
const publicRate = room.rate.public
|
||||
const memberRate = room.rate.member
|
||||
|
||||
if (!rate) {
|
||||
return total
|
||||
}
|
||||
|
||||
const packagesPrice = room.roomConfiguration?.selectedPackages.reduce(
|
||||
(total, pkg) => {
|
||||
total.local = total.local + pkg.localPrice.totalPrice
|
||||
if (pkg.requestedPrice.totalPrice) {
|
||||
total.requested = total.requested + pkg.requestedPrice.totalPrice
|
||||
}
|
||||
return total
|
||||
},
|
||||
{ local: 0, requested: 0 }
|
||||
)
|
||||
const packagesPrice = addAdditionalCost
|
||||
? sumPackages(room.roomConfiguration?.selectedPackages)
|
||||
: { price: 0, currency: undefined }
|
||||
const packagesRequestedPrice = addAdditionalCost
|
||||
? sumPackagesRequestedPrice(room.roomConfiguration?.selectedPackages)
|
||||
: { price: 0, currency: undefined }
|
||||
|
||||
total.local.currency = rate.localPrice.currency
|
||||
total.local.price =
|
||||
total.local.price + rate.localPrice.pricePerStay + packagesPrice.local
|
||||
|
||||
if (rate.localPrice.regularPricePerStay) {
|
||||
total.local.regularPrice =
|
||||
(total.local.regularPrice || 0) +
|
||||
rate.localPrice.regularPricePerStay +
|
||||
packagesPrice.local
|
||||
}
|
||||
total.local.price + rate.localPrice.pricePerStay + packagesPrice.price
|
||||
|
||||
if (rate.requestedPrice) {
|
||||
if (!total.requested) {
|
||||
@@ -119,17 +114,33 @@ function calculateTotalPrice(
|
||||
total.requested.price =
|
||||
total.requested.price +
|
||||
rate.requestedPrice.pricePerStay +
|
||||
packagesPrice.requested
|
||||
packagesRequestedPrice.price
|
||||
|
||||
if (rate.requestedPrice.regularPricePerStay) {
|
||||
total.requested.regularPrice =
|
||||
(total.requested.regularPrice || 0) +
|
||||
rate.requestedPrice.regularPricePerStay +
|
||||
packagesPrice.requested
|
||||
packagesRequestedPrice.price
|
||||
}
|
||||
}
|
||||
return calculateRegularPrice({
|
||||
total,
|
||||
useMemberRate: !!useMemberRate,
|
||||
regularMemberPrice: memberRate
|
||||
? {
|
||||
pricePerStay: memberRate.localPrice.pricePerNight,
|
||||
regularPricePerStay: memberRate.localPrice.regularPricePerStay,
|
||||
}
|
||||
: undefined,
|
||||
regularPublicPrice: publicRate
|
||||
? {
|
||||
pricePerStay: publicRate.localPrice.pricePerNight,
|
||||
regularPricePerStay: publicRate.localPrice.regularPricePerStay,
|
||||
}
|
||||
: undefined,
|
||||
|
||||
return total
|
||||
additionalCost: packagesPrice.price,
|
||||
})
|
||||
},
|
||||
{
|
||||
local: {
|
||||
@@ -140,6 +151,15 @@ function calculateTotalPrice(
|
||||
requested: undefined,
|
||||
}
|
||||
)
|
||||
|
||||
if (
|
||||
totalPrice.local.regularPrice &&
|
||||
totalPrice.local.price >= totalPrice.local.regularPrice
|
||||
) {
|
||||
totalPrice.local.regularPrice = undefined
|
||||
}
|
||||
|
||||
return totalPrice
|
||||
}
|
||||
|
||||
function calculateRedemptionTotalPrice(
|
||||
@@ -196,6 +216,7 @@ function calculateVoucherPrice(
|
||||
local: {
|
||||
currency: CurrencyEnum.Voucher,
|
||||
price: 0,
|
||||
regularPrice: undefined,
|
||||
},
|
||||
requested: undefined,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user