Files
web/packages/booking-flow/lib/utils/calculateRegularPrice.ts
Anton Gunnarsson b0f3e4afbd Merged in chore/cleanup-booking-flow (pull request #2824)
chore: Cleanup booking-flow after migration

* Remove unused types

* Clean up exports, types, unused files etc in booking-flow


Approved-by: Joakim Jäderberg
2025-09-18 07:28:05 +00:00

79 lines
2.3 KiB
TypeScript

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
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)
}