feat(SW-2863): Move contentstack router to trpc package * Add exports to packages and lint rule to prevent relative imports * Add env to trpc package * Add eslint to trpc package * Apply lint rules * Use direct imports from trpc package * Add lint-staged config to trpc * Move lang enum to common * Restructure trpc package folder structure * WIP first step * update internal imports in trpc * Fix most errors in scandic-web Just 100 left... * Move Props type out of trpc * Fix CategorizedFilters types * Move more schemas in hotel router * Fix deps * fix getNonContentstackUrls * Fix import error * Fix entry error handling * Fix generateMetadata metrics * Fix alertType enum * Fix duplicated types * lint:fix * Merge branch 'master' into feat/sw-2863-move-contentstack-router-to-trpc-package * Fix broken imports * Merge branch 'master' into feat/sw-2863-move-contentstack-router-to-trpc-package Approved-by: Linus Flood
81 lines
2.1 KiB
TypeScript
81 lines
2.1 KiB
TypeScript
import { CurrencyEnum } from "@scandic-hotels/common/constants/currency"
|
|
|
|
import { formatPrice } from "@/utils/numberFormatting"
|
|
|
|
import type { IntlShape } from "react-intl"
|
|
|
|
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
|
|
}
|
|
// room.totalPrice is a negative value when
|
|
// its a vouchers booking (╯°□°)╯︵ ┻━┻
|
|
if (room.totalPrice && !room.vouchers) {
|
|
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)
|
|
}
|