feat(SW-1717): rewrite select-rate to show all variants of rate-cards
This commit is contained in:
committed by
Michael Zetterberg
parent
adde77eaa9
commit
ebaea78fb3
@@ -80,156 +80,187 @@ export function subtract(...nums: (number | string | undefined)[]) {
|
||||
}
|
||||
|
||||
export function getCurrency(roomRate: RoomRate) {
|
||||
const requestedCurrency = <CurrencyEnum>(
|
||||
(roomRate.publicRate?.requestedPrice?.currency ??
|
||||
(roomRate.chequeRate && CurrencyEnum.CC))
|
||||
)
|
||||
const localCurrency = <CurrencyEnum>(
|
||||
(roomRate.publicRate?.localPrice.currency ??
|
||||
(roomRate.voucherRate && CurrencyEnum.Voucher) ??
|
||||
(roomRate.chequeRate && CurrencyEnum.CC))
|
||||
)
|
||||
if ("corporateCheque" in roomRate) {
|
||||
return {
|
||||
localCurrency: CurrencyEnum.CC,
|
||||
requestedCurrency: CurrencyEnum.CC,
|
||||
}
|
||||
} else if ("redemption" in roomRate) {
|
||||
return {
|
||||
localCurrency: CurrencyEnum.POINTS,
|
||||
requestedCurrency: CurrencyEnum.POINTS,
|
||||
}
|
||||
} else if ("voucher" in roomRate) {
|
||||
return {
|
||||
localCurrency: CurrencyEnum.Voucher,
|
||||
requestedCurrency: CurrencyEnum.Voucher,
|
||||
}
|
||||
} else if ("public" in roomRate && roomRate.public) {
|
||||
return {
|
||||
localCurrency: roomRate.public.localPrice.currency,
|
||||
requestedCurrency: roomRate.public.requestedPrice?.currency,
|
||||
}
|
||||
} else if ("member" in roomRate && roomRate.member) {
|
||||
return {
|
||||
localCurrency: roomRate.member.localPrice.currency,
|
||||
requestedCurrency: roomRate.member.requestedPrice?.currency,
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
requestedCurrency,
|
||||
localCurrency,
|
||||
localCurrency: CurrencyEnum.Unknown,
|
||||
requestedCurrency: CurrencyEnum.Unknown,
|
||||
}
|
||||
}
|
||||
|
||||
export function getRoomPrice(roomRate: RoomRate, isMember: boolean) {
|
||||
if (isMember && roomRate.memberRate) {
|
||||
if (isMember && "member" in roomRate && roomRate.member) {
|
||||
return {
|
||||
perNight: {
|
||||
requested: roomRate.memberRate.requestedPrice && {
|
||||
currency: roomRate.memberRate.requestedPrice.currency,
|
||||
price: roomRate.memberRate.requestedPrice.pricePerNight,
|
||||
},
|
||||
requested: roomRate.member.requestedPrice
|
||||
? {
|
||||
currency: roomRate.member.requestedPrice.currency,
|
||||
price: roomRate.member.requestedPrice.pricePerNight,
|
||||
}
|
||||
: undefined,
|
||||
local: {
|
||||
currency: roomRate.memberRate.localPrice.currency,
|
||||
price: roomRate.memberRate.localPrice.pricePerNight,
|
||||
currency: roomRate.member.localPrice.currency,
|
||||
price: roomRate.member.localPrice.pricePerNight,
|
||||
},
|
||||
},
|
||||
perStay: {
|
||||
requested: roomRate.memberRate.requestedPrice && {
|
||||
currency: roomRate.memberRate.requestedPrice.currency,
|
||||
price: roomRate.memberRate.requestedPrice.pricePerStay,
|
||||
},
|
||||
requested: roomRate.member.requestedPrice
|
||||
? {
|
||||
currency: roomRate.member.requestedPrice.currency,
|
||||
price: roomRate.member.requestedPrice.pricePerStay,
|
||||
}
|
||||
: undefined,
|
||||
local: {
|
||||
currency: roomRate.memberRate.localPrice.currency,
|
||||
price: roomRate.memberRate.localPrice.pricePerStay,
|
||||
currency: roomRate.member.localPrice.currency,
|
||||
price: roomRate.member.localPrice.pricePerStay,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
if (roomRate.publicRate) {
|
||||
if ("public" in roomRate && roomRate.public) {
|
||||
return {
|
||||
perNight: {
|
||||
requested: roomRate.publicRate.requestedPrice && {
|
||||
currency: roomRate.publicRate.requestedPrice.currency,
|
||||
price: roomRate.publicRate.requestedPrice.pricePerNight,
|
||||
},
|
||||
requested: roomRate.public.requestedPrice
|
||||
? {
|
||||
currency: roomRate.public.requestedPrice.currency,
|
||||
price: roomRate.public.requestedPrice.pricePerNight,
|
||||
}
|
||||
: undefined,
|
||||
local: {
|
||||
currency: roomRate.publicRate.localPrice.currency,
|
||||
price: roomRate.publicRate.localPrice.pricePerNight,
|
||||
regularPrice: roomRate.publicRate.localPrice.regularPricePerNight,
|
||||
currency: roomRate.public.localPrice.currency,
|
||||
price: roomRate.public.localPrice.pricePerNight,
|
||||
regularPrice: roomRate.public.localPrice.regularPricePerNight,
|
||||
},
|
||||
},
|
||||
perStay: {
|
||||
requested: roomRate.publicRate.requestedPrice && {
|
||||
currency: roomRate.publicRate.requestedPrice.currency,
|
||||
price: roomRate.publicRate.requestedPrice.pricePerStay,
|
||||
},
|
||||
requested: roomRate.public.requestedPrice
|
||||
? {
|
||||
currency: roomRate.public.requestedPrice.currency,
|
||||
price: roomRate.public.requestedPrice.pricePerStay,
|
||||
}
|
||||
: undefined,
|
||||
local: {
|
||||
currency: roomRate.publicRate.localPrice.currency,
|
||||
price: roomRate.publicRate.localPrice.pricePerStay,
|
||||
regularPrice: roomRate.publicRate.localPrice.regularPricePerStay,
|
||||
currency: roomRate.public.localPrice.currency,
|
||||
price: roomRate.public.localPrice.pricePerStay,
|
||||
regularPrice: roomRate.public.localPrice.regularPricePerStay,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
if (roomRate.chequeRate) {
|
||||
if ("corporateCheque" in roomRate) {
|
||||
return {
|
||||
perNight: {
|
||||
requested: roomRate.chequeRate.requestedPrice && {
|
||||
currency: CurrencyEnum.CC,
|
||||
price: roomRate.chequeRate.requestedPrice.numberOfBonusCheques,
|
||||
additionalPrice:
|
||||
roomRate.chequeRate.requestedPrice.additionalPricePerStay,
|
||||
additionalPriceCurrency: roomRate.chequeRate.requestedPrice.currency,
|
||||
},
|
||||
requested: roomRate.corporateCheque.requestedPrice
|
||||
? {
|
||||
currency: CurrencyEnum.CC,
|
||||
price: roomRate.corporateCheque.requestedPrice.numberOfCheques,
|
||||
additionalPrice:
|
||||
roomRate.corporateCheque.requestedPrice.additionalPricePerStay,
|
||||
additionalPriceCurrency:
|
||||
roomRate.corporateCheque.requestedPrice.currency ?? undefined,
|
||||
}
|
||||
: undefined,
|
||||
local: {
|
||||
currency: CurrencyEnum.CC,
|
||||
price: roomRate.chequeRate.localPrice.numberOfBonusCheques,
|
||||
price: roomRate.corporateCheque.localPrice.numberOfCheques,
|
||||
additionalPrice:
|
||||
roomRate.chequeRate.localPrice.additionalPricePerStay,
|
||||
additionalPriceCurrency: roomRate.chequeRate.localPrice.currency,
|
||||
roomRate.corporateCheque.localPrice.additionalPricePerStay,
|
||||
additionalPriceCurrency:
|
||||
roomRate.corporateCheque.localPrice.currency ?? undefined,
|
||||
},
|
||||
},
|
||||
perStay: {
|
||||
requested: roomRate.chequeRate.requestedPrice && {
|
||||
currency: CurrencyEnum.CC,
|
||||
price: roomRate.chequeRate.requestedPrice.numberOfBonusCheques,
|
||||
additionalPrice:
|
||||
roomRate.chequeRate.requestedPrice.additionalPricePerStay,
|
||||
additionalPriceCurrency: roomRate.chequeRate.requestedPrice.currency,
|
||||
},
|
||||
requested: roomRate.corporateCheque.requestedPrice
|
||||
? {
|
||||
currency: CurrencyEnum.CC,
|
||||
price: roomRate.corporateCheque.requestedPrice.numberOfCheques,
|
||||
additionalPrice:
|
||||
roomRate.corporateCheque.requestedPrice.additionalPricePerStay,
|
||||
additionalPriceCurrency:
|
||||
roomRate.corporateCheque.requestedPrice.currency ?? undefined,
|
||||
}
|
||||
: undefined,
|
||||
local: {
|
||||
currency: CurrencyEnum.CC,
|
||||
price: roomRate.chequeRate.localPrice.numberOfBonusCheques,
|
||||
price: roomRate.corporateCheque.localPrice.numberOfCheques,
|
||||
additionalPrice:
|
||||
roomRate.chequeRate.localPrice.additionalPricePerStay,
|
||||
additionalPriceCurrency: roomRate.chequeRate.localPrice.currency,
|
||||
roomRate.corporateCheque.localPrice.additionalPricePerStay,
|
||||
additionalPriceCurrency:
|
||||
roomRate.corporateCheque.localPrice.currency ?? undefined,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
if (roomRate.voucherRate) {
|
||||
if ("voucher" in roomRate) {
|
||||
return {
|
||||
perNight: {
|
||||
requested: undefined,
|
||||
local: {
|
||||
currency: CurrencyEnum.Voucher,
|
||||
price: roomRate.voucherRate.numberOfVouchers,
|
||||
price: roomRate.voucher.numberOfVouchers,
|
||||
},
|
||||
},
|
||||
perStay: {
|
||||
requested: undefined,
|
||||
local: {
|
||||
currency: CurrencyEnum.Voucher,
|
||||
price: roomRate.voucherRate.numberOfVouchers,
|
||||
price: roomRate.voucher.numberOfVouchers,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
if (roomRate.redemptionRate) {
|
||||
if ("redemption" in roomRate) {
|
||||
return {
|
||||
// ToDo Handle perNight as undefined
|
||||
perNight: {
|
||||
requested: undefined,
|
||||
local: {
|
||||
currency:
|
||||
roomRate.redemptionRate.localPrice.currency ?? CurrencyEnum.POINTS,
|
||||
price: roomRate.redemptionRate.localPrice.pointsPerStay,
|
||||
currency: CurrencyEnum.POINTS,
|
||||
price: roomRate.redemption.localPrice.pointsPerStay,
|
||||
additionalPrice:
|
||||
roomRate.redemptionRate.localPrice.additionalPricePerStay,
|
||||
roomRate.redemption.localPrice.additionalPricePerStay,
|
||||
additionalPriceCurrency:
|
||||
roomRate.redemptionRate.localPrice.additionalPriceCurrency,
|
||||
roomRate.redemption.localPrice.currency ?? undefined,
|
||||
},
|
||||
},
|
||||
perStay: {
|
||||
requested: undefined,
|
||||
local: {
|
||||
currency:
|
||||
roomRate.redemptionRate.localPrice.currency ?? CurrencyEnum.POINTS,
|
||||
price: roomRate.redemptionRate.localPrice.pointsPerStay,
|
||||
currency: CurrencyEnum.POINTS,
|
||||
price: roomRate.redemption.localPrice.pointsPerStay,
|
||||
additionalPrice:
|
||||
roomRate.redemptionRate.localPrice.additionalPricePerStay,
|
||||
roomRate.redemption.localPrice.additionalPricePerStay,
|
||||
additionalPriceCurrency:
|
||||
roomRate.redemptionRate.localPrice.additionalPriceCurrency,
|
||||
roomRate.redemption.localPrice.currency ?? undefined,
|
||||
},
|
||||
},
|
||||
}
|
||||
@@ -243,58 +274,64 @@ export function getRoomPrice(roomRate: RoomRate, isMember: boolean) {
|
||||
export function getTotalPrice(roomRates: RoomRate[], isMember: boolean) {
|
||||
return roomRates.reduce<Price>(
|
||||
(total, roomRate, idx) => {
|
||||
const isFirstRoom = idx === 0
|
||||
const rate =
|
||||
isFirstRoom && isMember && roomRate.memberRate
|
||||
? roomRate.memberRate
|
||||
: roomRate.publicRate
|
||||
|
||||
const isMainRoom = idx === 0
|
||||
let rate
|
||||
if (isMainRoom && isMember && "member" in roomRate && roomRate.member) {
|
||||
rate = roomRate.member
|
||||
} else if ("public" in roomRate && roomRate.public) {
|
||||
rate = roomRate.public
|
||||
}
|
||||
// TODO: Handle other products?
|
||||
if (!rate) {
|
||||
return total
|
||||
}
|
||||
|
||||
return {
|
||||
requested: rate.requestedPrice
|
||||
? {
|
||||
currency: rate.requestedPrice.currency,
|
||||
price: add(
|
||||
total.requested?.price ?? 0,
|
||||
rate.requestedPrice.pricePerStay
|
||||
),
|
||||
}
|
||||
: total.requested,
|
||||
local: {
|
||||
currency: rate.localPrice.currency,
|
||||
price: add(total.local.price ?? 0, rate.localPrice.pricePerStay),
|
||||
regularPrice: rate.localPrice.regularPricePerStay
|
||||
? add(total.local.regularPrice, rate.localPrice.regularPricePerStay)
|
||||
: total.local.regularPrice,
|
||||
},
|
||||
total.local.currency = rate.localPrice.currency
|
||||
total.local.price = add(total.local.price, rate.localPrice.pricePerStay)
|
||||
if (rate.localPrice.regularPricePerStay) {
|
||||
total.local.regularPrice = add(
|
||||
total.local.regularPrice,
|
||||
rate.localPrice.regularPricePerStay
|
||||
)
|
||||
}
|
||||
|
||||
if (rate.requestedPrice) {
|
||||
if (total.requested) {
|
||||
total.requested.price = add(
|
||||
total.requested.price,
|
||||
rate.requestedPrice.pricePerStay
|
||||
)
|
||||
} else {
|
||||
total.requested = {
|
||||
currency: rate.requestedPrice.currency,
|
||||
price: rate.requestedPrice.pricePerStay,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return total
|
||||
},
|
||||
{
|
||||
requested: undefined,
|
||||
local: {
|
||||
currency: (roomRates[0].publicRate?.localPrice.currency ||
|
||||
roomRates[0].memberRate?.localPrice.currency)!,
|
||||
currency: CurrencyEnum.Unknown,
|
||||
price: 0,
|
||||
},
|
||||
requested: undefined,
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
export const calculateVoucherPrice = (roomRates: RoomRate[]) => {
|
||||
export function calculateVoucherPrice(roomRates: RoomRate[]) {
|
||||
return roomRates.reduce<Price>(
|
||||
(total, room) => {
|
||||
const rate = room.voucherRate
|
||||
if (!rate) {
|
||||
if (!("voucher" in room)) {
|
||||
return total
|
||||
}
|
||||
|
||||
return <Price>{
|
||||
return {
|
||||
local: {
|
||||
currency: total.local.currency,
|
||||
price: total.local.price + rate.numberOfVouchers,
|
||||
price: total.local.price + room.voucher.numberOfVouchers,
|
||||
},
|
||||
requested: undefined,
|
||||
}
|
||||
@@ -309,50 +346,56 @@ export const calculateVoucherPrice = (roomRates: RoomRate[]) => {
|
||||
)
|
||||
}
|
||||
|
||||
export const calculateChequePrice = (roomRates: RoomRate[]) => {
|
||||
export function calculateCorporateChequePrice(roomRates: RoomRate[]) {
|
||||
return roomRates.reduce<Price>(
|
||||
(total, room) => {
|
||||
const rate = room.chequeRate
|
||||
if (!rate) {
|
||||
if (!("corporateCheque" in room)) {
|
||||
return total
|
||||
}
|
||||
|
||||
const price = total.local.price + rate.localPrice.numberOfBonusCheques
|
||||
const rate = room.corporateCheque
|
||||
|
||||
const additionalPrice =
|
||||
rate.localPrice.numberOfBonusCheques &&
|
||||
(total.local.additionalPrice ?? 0) +
|
||||
(rate.localPrice.additionalPricePerStay ?? 0)
|
||||
const additionalPriceCurrency = (rate.localPrice.numberOfBonusCheques &&
|
||||
rate.localPrice.currency)!
|
||||
total.local.price = add(
|
||||
total.local.price,
|
||||
rate.localPrice.numberOfCheques
|
||||
)
|
||||
|
||||
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,
|
||||
if (rate.localPrice.additionalPricePerStay) {
|
||||
total.local.additionalPrice = add(
|
||||
total.local.additionalPrice,
|
||||
rate.localPrice.additionalPricePerStay
|
||||
)
|
||||
}
|
||||
if (rate.localPrice.currency) {
|
||||
total.local.additionalPriceCurrency = rate.localPrice.currency
|
||||
}
|
||||
|
||||
if (rate.requestedPrice) {
|
||||
if (total.requested) {
|
||||
total.requested.price = add(
|
||||
total.requested.price,
|
||||
rate.requestedPrice.numberOfCheques
|
||||
)
|
||||
} else {
|
||||
total.requested = {
|
||||
currency: CurrencyEnum.CC,
|
||||
price: rate.requestedPrice.numberOfCheques,
|
||||
}
|
||||
}
|
||||
|
||||
if (rate.requestedPrice.additionalPricePerStay) {
|
||||
total.requested.additionalPrice = add(
|
||||
total.requested.additionalPrice,
|
||||
rate.requestedPrice.additionalPricePerStay
|
||||
)
|
||||
}
|
||||
|
||||
if (rate.requestedPrice.currency) {
|
||||
total.requested.additionalPriceCurrency = rate.requestedPrice.currency
|
||||
}
|
||||
}
|
||||
|
||||
return total
|
||||
},
|
||||
{
|
||||
local: {
|
||||
@@ -437,8 +480,10 @@ export function calcTotalPrice(
|
||||
breakfastLocalPrice * room.adults * nights,
|
||||
roomFeaturesTotal?.local ?? 0
|
||||
),
|
||||
additionalPriceCurrency:
|
||||
roomPrice.perStay.local.additionalPriceCurrency,
|
||||
additionalPriceCurrency: roomPrice.perStay.local
|
||||
.additionalPriceCurrency
|
||||
? roomPrice.perStay.local.additionalPriceCurrency
|
||||
: undefined,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user