Files
web/apps/scandic-web/stores/select-rate/helpers.ts
Tobias Johansson e6ae6ff650 Merged in feat/SW-2113-allow-feature-combinations (pull request #1719)
Feat/SW-2113 allow feature combinations

* feat(SW-2113): Refactor features data to be fetched on filter room filter change

* feat(SW-2113): added loading state

* fix: now clear room selection when applying filter and room doesnt exists. And added room features to mobile summary

* fix

* fix: add package to price details

* feat(SW-2113): added buttons to room filter

* fix: active room

* fix: remove console log

* fix: added form and close handler to room package filter

* fix: add restriction so you cannot select pet room with allergy room and vice versa

* fix: fixes from review feedback

* fix

* fix: hide modify button if on nextcoming rooms if no selection is made, and adjust filter logic in togglePackage

* fix: forgot to use roomFeatureCodes from input..

* fix: naming


Approved-by: Simon.Emanuelsson
2025-04-07 11:36:34 +00:00

122 lines
2.9 KiB
TypeScript

import { RoomPackageCodeEnum } from "@/types/components/hotelReservation/selectRate/roomFilter"
import type { AvailabilityError } from "@/types/stores/rates"
import type {
Product,
RoomConfiguration,
} from "@/types/trpc/routers/hotel/roomAvailability"
export function findProduct(
rateCode: string,
product: Product,
counterRateCode = ""
) {
if ("corporateCheque" in product) {
return product.corporateCheque.rateCode === rateCode
}
if ("redemption" in product) {
return product.redemption.rateCode === rateCode
}
if ("voucher" in product) {
return product.voucher.rateCode === rateCode
}
const memberExists = "member" in product
const publicExists = "public" in product
const isRegularRate = memberExists && publicExists
if (isRegularRate) {
let isProduct = false
if (product.member) {
isProduct =
product.member.rateCode === rateCode ||
product.member.rateCode === counterRateCode
}
if (product.public) {
isProduct =
product.public.rateCode === rateCode ||
product.public.rateCode === counterRateCode
}
return isProduct
}
return null
}
export function findProductInRoom(
rateCode: string,
room: RoomConfiguration,
counterRateCode = ""
) {
if (room.campaign.length) {
const campaignProduct = room.campaign.find((product) =>
findProduct(rateCode, product, counterRateCode)
)
if (campaignProduct) {
return campaignProduct
}
}
if (room.code.length) {
const codeProduct = room.code.find((product) =>
findProduct(rateCode, product, counterRateCode)
)
if (codeProduct) {
return codeProduct
}
}
if (room.redemptions.length) {
const redemptionProduct = room.redemptions.find((product) =>
findProduct(rateCode, product)
)
if (redemptionProduct) {
return redemptionProduct
}
}
if (room.regular.length) {
const regularProduct = room.regular.find((product) =>
findProduct(rateCode, product, counterRateCode)
)
if (regularProduct) {
return regularProduct
}
}
}
export function findSelectedRate(
rateCode: string,
counterRateCode: string,
roomTypeCode: string,
rooms: RoomConfiguration[] | AvailabilityError
) {
if (!Array.isArray(rooms)) {
return null
}
return rooms.find((room) => {
if (room.roomTypeCode !== roomTypeCode) {
return false
}
return findProductInRoom(rateCode, room, counterRateCode)
})
}
export function isRoomPackageCode(
code: string | null
): code is RoomPackageCodeEnum {
return Object.values(RoomPackageCodeEnum).includes(
code as RoomPackageCodeEnum
)
}
export function filterRoomsBySelectedPackages(
selectedPackages: RoomPackageCodeEnum[],
rooms: RoomConfiguration[]
) {
if (!selectedPackages.length) {
return rooms
}
return rooms.filter((r) =>
selectedPackages.every((pkg) => r.features.find((f) => f.code === pkg))
)
}