Merged in chore/add-tests-to-getAdditionalPrice (pull request #3065)

chore: Refactor types and add tests to parts of price calcuations

* Add tests to sumPackages

* Refactor types and add tests to getAdditionalPrice

* Don't always generate coverage

* Add tests and refactor types of getRedemptionPrice


Approved-by: Joakim Jäderberg
This commit is contained in:
Anton Gunnarsson
2025-11-04 12:09:04 +00:00
parent fa10abbe78
commit dc42a22513
6 changed files with 566 additions and 15 deletions

View File

@@ -14,7 +14,6 @@ import type {
CorporateChequeProduct,
PriceProduct,
Product,
RedemptionProduct,
VoucherProduct,
} from "@scandic-hotels/trpc/types/roomAvailability"
import type { User } from "@scandic-hotels/trpc/types/user"
@@ -276,12 +275,23 @@ export function clearSessionStorage() {
sessionStorage.removeItem(detailsStorageName)
}
function getAdditionalPrice(
total: Price,
export function getAdditionalPrice(
total: {
local: {
additionalPrice?: number
additionalPriceCurrency?: CurrencyEnum
}
},
adults: number,
breakfast: BreakfastPackage | false | undefined,
breakfast:
| { localPrice: { price: number; currency?: CurrencyEnum } }
| false
| undefined,
nights: number,
packages: Packages | null,
packages:
| { localPrice: { totalPrice: number; currency?: CurrencyEnum } }[]
| null
| undefined,
additionalPrice = 0,
additionalPriceCurrency?: CurrencyEnum | null | undefined
) {
@@ -440,17 +450,40 @@ function getVoucherPrice(rooms: TRoom[], nights: number) {
)
}
interface TRoomRedemption extends TRoom {
roomRate: RedemptionProduct
type GetRedemptionPriceRoom = {
adults: number
breakfast:
| { localPrice: { price: number; currency?: CurrencyEnum } }
| false
| undefined
roomFeatures:
| {
localPrice: { totalPrice: number; currency?: CurrencyEnum }
}[]
| null
| undefined
// We don't care about roomRate unless it's RedemptionProduct
roomRate: object
}
type RedemptionRoom = GetRedemptionPriceRoom & {
roomRate: {
redemption: {
localPrice: {
pointsPerStay: number
additionalPricePerStay: number
currency?: CurrencyEnum
}
}
}
}
function getRedemptionPrice(
rooms: TRoom[],
export function getRedemptionPrice(
rooms: GetRedemptionPriceRoom[],
nights: number,
pointsCurrency?: CurrencyEnum
) {
return rooms
.filter((room): room is TRoomRedemption => "redemption" in room.roomRate)
.filter((room): room is RedemptionRoom => "redemption" in room.roomRate)
.reduce<Price>(
(total, room) => {
const redemption = room.roomRate.redemption