feat: refactor of my stay
This commit is contained in:
committed by
Simon.Emanuelsson
parent
b5deb84b33
commit
ec087a3d15
77
apps/scandic-web/stores/my-stay/helpers.ts
Normal file
77
apps/scandic-web/stores/my-stay/helpers.ts
Normal file
@@ -0,0 +1,77 @@
|
||||
import { formatPrice } from "@/utils/numberFormatting"
|
||||
|
||||
import type { IntlShape } from "react-intl"
|
||||
|
||||
import { CurrencyEnum } from "@/types/enums/currency"
|
||||
import type { Room } from "@/types/stores/my-stay"
|
||||
|
||||
export function calculateTotalPrice(
|
||||
rooms: Room[],
|
||||
currency: CurrencyEnum,
|
||||
intl: IntlShape,
|
||||
allRoomsAreCancelled: boolean
|
||||
) {
|
||||
const totals = rooms.reduce(
|
||||
(total, room) => {
|
||||
if (!allRoomsAreCancelled && room.isCancelled) {
|
||||
return total
|
||||
}
|
||||
|
||||
if (room.cheques) {
|
||||
total.cheques = total.cheques + room.cheques
|
||||
}
|
||||
if (room.vouchers) {
|
||||
total.vouchers = total.vouchers + room.vouchers
|
||||
}
|
||||
if (room.totalPoints) {
|
||||
total.points = total.points + room.totalPoints
|
||||
}
|
||||
if (room.totalPrice) {
|
||||
total.cash = total.cash + room.totalPrice
|
||||
}
|
||||
return total
|
||||
},
|
||||
{
|
||||
cash: 0,
|
||||
cheques: 0,
|
||||
points: 0,
|
||||
vouchers: 0,
|
||||
}
|
||||
)
|
||||
|
||||
let totalPrice = ""
|
||||
if (totals.cheques) {
|
||||
totalPrice = `${totals.cheques} ${CurrencyEnum.CC}`
|
||||
}
|
||||
if (totals.points) {
|
||||
const appendTotalPrice = totalPrice ? `${totalPrice} + ` : ""
|
||||
totalPrice = `${appendTotalPrice}${totals.points} ${CurrencyEnum.POINTS}`
|
||||
}
|
||||
if (totals.vouchers) {
|
||||
const appendTotalPrice = totalPrice ? `${totalPrice} + ` : ""
|
||||
totalPrice = `${appendTotalPrice}${totals.vouchers} ${CurrencyEnum.Voucher}`
|
||||
}
|
||||
if (totals.cash) {
|
||||
const appendTotalPrice = totalPrice ? `${totalPrice} + ` : ""
|
||||
const cashPrice = formatPrice(intl, totals.cash, currency)
|
||||
totalPrice = `${appendTotalPrice}${cashPrice}`
|
||||
}
|
||||
|
||||
return totalPrice
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user