chore(SW-3321): Moved Select rate context to booking-flow package * chore(SW-3321): Moved Select rate context to booking-flow package * chore(SW-3321): Optimised code Approved-by: Joakim Jäderberg
141 lines
4.0 KiB
TypeScript
141 lines
4.0 KiB
TypeScript
import { sumPackages } from "@scandic-hotels/booking-flow/utils/SelectRate"
|
|
import { logger } from "@scandic-hotels/common/logger"
|
|
|
|
import type { RoomState } from "@/types/stores/enter-details"
|
|
|
|
export function mapToPrice(rooms: RoomState[], isMember: boolean) {
|
|
return rooms
|
|
.filter((room) => room && room.room.roomRate)
|
|
.map(({ room }, idx) => {
|
|
const isMainRoom = idx === 0
|
|
|
|
const pkgsSum = sumPackages(room.roomFeatures)
|
|
|
|
const roomWithoutPrice = {
|
|
...room,
|
|
packages: room.roomFeatures,
|
|
rateDefinition: {
|
|
isMemberRate: false,
|
|
},
|
|
}
|
|
|
|
if ("corporateCheque" in room.roomRate) {
|
|
if (
|
|
room.roomRate.corporateCheque.localPrice.additionalPricePerStay ||
|
|
pkgsSum.price
|
|
) {
|
|
return {
|
|
...roomWithoutPrice,
|
|
price: {
|
|
corporateCheque: {
|
|
...room.roomRate.corporateCheque.localPrice,
|
|
additionalPricePerStay:
|
|
room.roomRate.corporateCheque.localPrice
|
|
.additionalPricePerStay || 0,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
return {
|
|
...roomWithoutPrice,
|
|
price: {
|
|
corporateCheque: room.roomRate.corporateCheque.localPrice,
|
|
},
|
|
}
|
|
}
|
|
|
|
if ("redemption" in room.roomRate) {
|
|
if (
|
|
room.roomRate.redemption.localPrice.additionalPricePerStay ||
|
|
pkgsSum.price
|
|
) {
|
|
return {
|
|
...roomWithoutPrice,
|
|
price: {
|
|
redemption: {
|
|
...room.roomRate.redemption.localPrice,
|
|
additionalPricePerStay:
|
|
room.roomRate.redemption.localPrice.additionalPricePerStay ||
|
|
0,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
return {
|
|
...roomWithoutPrice,
|
|
price: {
|
|
redemption: room.roomRate.redemption.localPrice,
|
|
},
|
|
}
|
|
}
|
|
|
|
if ("voucher" in room.roomRate) {
|
|
return {
|
|
...roomWithoutPrice,
|
|
price: {
|
|
voucher: room.roomRate.voucher,
|
|
},
|
|
}
|
|
}
|
|
|
|
const isMemberRate = !!(room.guest.join || room.guest.membershipNo)
|
|
if ((isMember && isMainRoom) || isMemberRate) {
|
|
if ("member" in room.roomRate && room.roomRate.member) {
|
|
if (pkgsSum.price) {
|
|
return {
|
|
...roomWithoutPrice,
|
|
rateDefinition: {
|
|
isMemberRate: true,
|
|
},
|
|
price: {
|
|
regular: {
|
|
...room.roomRate.member.localPrice,
|
|
pricePerNight: room.roomRate.member.localPrice.pricePerNight,
|
|
pricePerStay: room.roomRate.member.localPrice.pricePerStay,
|
|
regularPricePerStay:
|
|
(room.roomRate.public?.localPrice.pricePerStay ||
|
|
room.roomRate.member.localPrice.pricePerStay) +
|
|
pkgsSum.price,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
return {
|
|
...roomWithoutPrice,
|
|
rateDefinition: {
|
|
isMemberRate: true,
|
|
},
|
|
price: {
|
|
regular: {
|
|
...room.roomRate.member.localPrice,
|
|
regularPricePerStay:
|
|
room.roomRate.public?.localPrice.pricePerStay ||
|
|
room.roomRate.member.localPrice.pricePerStay,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
}
|
|
|
|
if ("public" in room.roomRate && room.roomRate.public) {
|
|
if (pkgsSum.price) {
|
|
return {
|
|
...roomWithoutPrice,
|
|
price: {
|
|
regular: room.roomRate.public.localPrice,
|
|
},
|
|
}
|
|
}
|
|
return {
|
|
...roomWithoutPrice,
|
|
price: {
|
|
regular: room.roomRate.public.localPrice,
|
|
},
|
|
}
|
|
}
|
|
|
|
logger.error("Unknown roomRate", room.roomRate)
|
|
throw new Error(`Unknown roomRate`)
|
|
})
|
|
}
|