Files
web/apps/scandic-web/components/HotelReservation/MyStay/PriceDetails/mapToPrice.ts
Anton Gunnarsson 002d093af4 Merged in feat/sw-2863-move-contentstack-router-to-trpc-package (pull request #2389)
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
2025-06-26 07:53:01 +00:00

127 lines
3.7 KiB
TypeScript

import { CurrencyEnum } from "@scandic-hotels/common/constants/currency"
import { dt } from "@scandic-hotels/common/dt"
import { sumPackages } from "../../utils"
import { PriceTypeEnum } from "@/types/components/hotelReservation/myStay/myStay"
import type { Price } from "@/types/components/hotelReservation/price"
import type { Room } from "@/types/stores/my-stay"
export function mapToPrice(room: Room) {
switch (room.priceType) {
case PriceTypeEnum.cheque:
return {
corporateCheque: {
additionalPricePerStay: room.roomPrice.perStay.local.price,
currency: room.roomPrice.perStay.local.currency,
numberOfCheques: room.cheques,
},
}
case PriceTypeEnum.money:
return {
regular: {
currency: room.currencyCode,
pricePerNight: room.roomPrice.perNight.local.price,
pricePerStay: room.roomPrice.perStay.local.price,
regularPricePerStay:
room.roomPrice.perStay.local.regularPrice ||
room.roomPrice.perStay.local.price,
},
}
case PriceTypeEnum.points:
const nights = dt(room.checkOutDate)
.startOf("day")
.diff(dt(room.checkInDate).startOf("day"), "days")
return {
redemption: {
additionalPricePerStay: room.roomPrice.perStay.local.price,
currency: room.currencyCode,
pointsPerNight: room.roomPoints / nights,
pointsPerStay: room.roomPoints,
},
}
case PriceTypeEnum.voucher:
return {
voucher: {
numberOfVouchers: room.vouchers,
},
}
default:
throw new Error(`Unknown payment method!`)
}
}
export function calculateTotalPrice(rooms: Room[], currency: CurrencyEnum) {
return rooms.reduce<Price>(
(total, room) => {
switch (room.priceType) {
case PriceTypeEnum.cheque:
{
total.local.currency = CurrencyEnum.CC
total.local.price = total.local.price + room.cheques
}
break
case PriceTypeEnum.money:
{
total.local.price = total.local.price + room.totalPrice
if (!total.local.currency) {
total.local.currency = room.currencyCode
}
}
break
case PriceTypeEnum.points:
{
total.local.currency = CurrencyEnum.POINTS
total.local.price = total.local.price + room.totalPoints
}
break
case PriceTypeEnum.voucher:
total.local.currency = CurrencyEnum.Voucher
total.local.price = total.local.price + room.vouchers
break
}
switch (room.priceType) {
case PriceTypeEnum.cheque:
case PriceTypeEnum.points:
{
if (room.totalPrice) {
total.local.additionalPrice =
(total.local.additionalPrice || 0) + room.totalPrice
}
if (!total.local.additionalPriceCurrency) {
total.local.additionalPriceCurrency = currency
}
}
break
case PriceTypeEnum.voucher:
{
if (room.packages) {
const pkgsSum = sumPackages(room.packages)
total.local.additionalPrice =
(total.local.additionalPrice || 0) + pkgsSum.price
if (pkgsSum.currency) {
total.local.additionalPriceCurrency = pkgsSum.currency
} else {
total.local.additionalPriceCurrency = currency
}
}
}
break
}
return total
},
{
local: {
currency,
price: 0,
},
requested: undefined,
}
)
}