Merged in fix/refactor-currency-display (pull request #3434)
fix(SW-3616): Handle EuroBonus point type everywhere * Add tests to formatPrice * formatPrice * More work replacing config with api points type * More work replacing config with api points type * More fixing with currency * maybe actually fixed it * Fix MyStay * Clean up * Fix comments * Merge branch 'master' into fix/refactor-currency-display * Fix calculateTotalPrice for EB points + SF points + cash Approved-by: Joakim Jäderberg
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { CurrencyEnum } from "@scandic-hotels/common/constants/currency"
|
||||
import { PointType } from "@scandic-hotels/common/constants/pointType"
|
||||
import { formatPrice } from "@scandic-hotels/common/utils/numberFormatting"
|
||||
|
||||
import type { IntlShape } from "react-intl"
|
||||
@@ -10,8 +11,8 @@ export function calculateTotalPrice(
|
||||
Room,
|
||||
| "cheques"
|
||||
| "vouchers"
|
||||
| "roomPoints"
|
||||
| "roomPointType"
|
||||
| "roomPoints"
|
||||
| "totalPoints"
|
||||
| "totalPrice"
|
||||
| "isCancelled"
|
||||
@@ -34,18 +35,19 @@ export function calculateTotalPrice(
|
||||
total.vouchers = total.vouchers + room.vouchers
|
||||
}
|
||||
|
||||
// If roomPointType isn't Scandic, these should be counted as partnerPoints.
|
||||
// If they're not Scandic points, we can ignore them on roomPoints as they
|
||||
// are included in totalPoints, which in turn never contain partner points.
|
||||
if (
|
||||
room.roomPoints &&
|
||||
room.roomPointType &&
|
||||
room.roomPointType !== "Scandic"
|
||||
) {
|
||||
total.partnerPoints = total.partnerPoints + room.roomPoints
|
||||
total.partnerPointsCurrency = room.roomPointType ?? null
|
||||
}
|
||||
|
||||
if (room.totalPoints) {
|
||||
total.scandicFriendsPoints =
|
||||
total.scandicFriendsPoints + room.totalPoints
|
||||
total.scandicPoints = total.scandicPoints + room.totalPoints
|
||||
}
|
||||
|
||||
// room.totalPrice is a negative value when
|
||||
@@ -59,9 +61,8 @@ export function calculateTotalPrice(
|
||||
{
|
||||
cash: 0,
|
||||
cheques: 0,
|
||||
scandicFriendsPoints: 0,
|
||||
scandicPoints: 0,
|
||||
partnerPoints: 0,
|
||||
partnerPointsCurrency: null as Room["roomPointType"],
|
||||
vouchers: 0,
|
||||
}
|
||||
)
|
||||
@@ -86,12 +87,24 @@ export function calculateTotalPrice(
|
||||
priceParts.push(`${totals.cheques} ${CurrencyEnum.CC}`)
|
||||
}
|
||||
|
||||
if (totals.scandicFriendsPoints) {
|
||||
priceParts.push(`${totals.scandicFriendsPoints} ${CurrencyEnum.POINTS}`)
|
||||
if (totals.partnerPoints) {
|
||||
// We can assume that all rooms has the same point type
|
||||
const roomPointType = rooms[0]?.roomPointType || PointType.SCANDIC
|
||||
const currencyText = getPointsCurrencyText(
|
||||
totals.partnerPoints,
|
||||
roomPointType,
|
||||
intl
|
||||
)
|
||||
priceParts.push(`${totals.partnerPoints} ${currencyText}`)
|
||||
}
|
||||
|
||||
if (totals.partnerPoints) {
|
||||
priceParts.push(`${totals.partnerPoints} ${totals.partnerPointsCurrency}`)
|
||||
if (totals.scandicPoints) {
|
||||
const currencyText = getPointsCurrencyText(
|
||||
totals.scandicPoints,
|
||||
PointType.SCANDIC,
|
||||
intl
|
||||
)
|
||||
priceParts.push(`${totals.scandicPoints} ${currencyText}`)
|
||||
}
|
||||
|
||||
if (totals.cash) {
|
||||
@@ -102,18 +115,43 @@ export function calculateTotalPrice(
|
||||
return priceParts.join(" + ")
|
||||
}
|
||||
|
||||
export function calculateTotalPoints(
|
||||
rooms: Room[],
|
||||
allRoomsAreCancelled: boolean
|
||||
) {
|
||||
return rooms.reduce((total, room) => {
|
||||
if (!allRoomsAreCancelled && room.isCancelled) {
|
||||
return total
|
||||
}
|
||||
return total + room.totalPoints
|
||||
}, 0)
|
||||
}
|
||||
|
||||
export function isAllRoomsCancelled(rooms: Room[]) {
|
||||
return !rooms.some((room) => room.isCancelled === false)
|
||||
}
|
||||
|
||||
function getPointsCurrencyText(
|
||||
points: number,
|
||||
pointsType: PointType,
|
||||
intl: IntlShape
|
||||
) {
|
||||
switch (pointsType) {
|
||||
case PointType.SCANDIC: {
|
||||
return intl.formatMessage(
|
||||
{
|
||||
id: "price.numberOfScandicPoints",
|
||||
defaultMessage:
|
||||
"{numberOfScandicPoints, plural, one {Point} other {Points}}",
|
||||
},
|
||||
{
|
||||
numberOfScandicPoints: points,
|
||||
}
|
||||
)
|
||||
}
|
||||
case PointType.EUROBONUS: {
|
||||
return intl.formatMessage(
|
||||
{
|
||||
id: "price.numberOfEuroBonusPoints",
|
||||
defaultMessage:
|
||||
"{numberOfEuroBonusPoints, plural, one {EB Point} other {EB Points}}",
|
||||
},
|
||||
{
|
||||
numberOfEuroBonusPoints: points,
|
||||
}
|
||||
)
|
||||
}
|
||||
default: {
|
||||
const _exhaustiveCheck: never = pointsType
|
||||
return "Points"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user