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:
78
packages/booking-flow/lib/utils/calculateRegularPrice.ts
Normal file
78
packages/booking-flow/lib/utils/calculateRegularPrice.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
import type { Price } from "../types/price"
|
||||
|
||||
type RegularPrice = {
|
||||
pricePerStay: number
|
||||
regularPricePerStay?: number
|
||||
}
|
||||
|
||||
// Helper function to calculate regular/strikethrough price
|
||||
export function calculateRegularPrice({
|
||||
total,
|
||||
useMemberRate,
|
||||
regularMemberPrice,
|
||||
regularPublicPrice,
|
||||
additionalCost = 0,
|
||||
}: {
|
||||
total: Price
|
||||
useMemberRate: boolean
|
||||
regularMemberPrice: RegularPrice | undefined
|
||||
regularPublicPrice: RegularPrice | undefined
|
||||
additionalCost?: number
|
||||
}) {
|
||||
if (
|
||||
!total ||
|
||||
(!useMemberRate && !regularPublicPrice) ||
|
||||
(useMemberRate && !regularMemberPrice)
|
||||
) {
|
||||
return total
|
||||
}
|
||||
|
||||
let basePrice = 0
|
||||
// Legend:
|
||||
// - total.local.price = Total Price = Black price, what the user pays
|
||||
// - total.local.regularPrice = Regular Price = Strikethrough price (could potentially be none)
|
||||
// - total.requested.price = Requested Price = EUR approx price
|
||||
|
||||
// We sometimes don't get all the required data to calculate the correct strikethrough total.
|
||||
// Therefore we try these different approach to get a number that is close
|
||||
// enough to the real number if all data would've been present.
|
||||
|
||||
if (useMemberRate && regularMemberPrice) {
|
||||
if (regularPublicPrice) {
|
||||
// #1 Member price uses public price as strikethrough
|
||||
basePrice = regularPublicPrice.pricePerStay
|
||||
} else if (regularMemberPrice.regularPricePerStay) {
|
||||
// #2 Member price uses member regular price as strikethrough
|
||||
basePrice = regularMemberPrice.regularPricePerStay
|
||||
} else {
|
||||
// #3 Member price uses member price as strikethrough
|
||||
basePrice = regularMemberPrice.pricePerStay
|
||||
}
|
||||
} else if (regularPublicPrice) {
|
||||
if (regularPublicPrice.regularPricePerStay) {
|
||||
// #1 Public price uses public regular price as strikethrough
|
||||
basePrice = regularPublicPrice.regularPricePerStay
|
||||
} else {
|
||||
// #2 Public price uses public price as strikethrough
|
||||
basePrice = regularPublicPrice.pricePerStay
|
||||
}
|
||||
}
|
||||
|
||||
total.local.regularPrice = add(
|
||||
total.local.regularPrice,
|
||||
basePrice,
|
||||
additionalCost
|
||||
)
|
||||
return total
|
||||
}
|
||||
|
||||
//copied from enter-details/helpers.ts
|
||||
export function add(...nums: (number | string | undefined)[]) {
|
||||
return nums.reduce((total: number, num) => {
|
||||
if (typeof num === "undefined") {
|
||||
num = 0
|
||||
}
|
||||
total = total + parseInt(`${num}`)
|
||||
return total
|
||||
}, 0)
|
||||
}
|
||||
Reference in New Issue
Block a user