feat(SW-1717): rewrite select-rate to show all variants of rate-cards
This commit is contained in:
committed by
Michael Zetterberg
parent
adde77eaa9
commit
ebaea78fb3
80
apps/scandic-web/stores/select-rate/helpers.ts
Normal file
80
apps/scandic-web/stores/select-rate/helpers.ts
Normal file
@@ -0,0 +1,80 @@
|
||||
import type { AvailabilityError } from "@/types/stores/rates"
|
||||
import type {
|
||||
Product,
|
||||
RoomConfiguration,
|
||||
} from "@/types/trpc/routers/hotel/roomAvailability"
|
||||
|
||||
export function findProduct(rateCode: string, product: Product) {
|
||||
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
|
||||
}
|
||||
|
||||
if ("public" in product && product.public) {
|
||||
return product.public.rateCode === rateCode
|
||||
}
|
||||
|
||||
if ("member" in product && product.member) {
|
||||
return product.member.rateCode === rateCode
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
export function findProductInRoom(rateCode: string, room: RoomConfiguration) {
|
||||
if (room.campaign.length) {
|
||||
const campaignProduct = room.campaign.find((product) =>
|
||||
findProduct(rateCode, product)
|
||||
)
|
||||
if (campaignProduct) {
|
||||
return campaignProduct
|
||||
}
|
||||
}
|
||||
if (room.code.length) {
|
||||
const codeProduct = room.code.find((product) =>
|
||||
findProduct(rateCode, product)
|
||||
)
|
||||
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)
|
||||
)
|
||||
if (regularProduct) {
|
||||
return regularProduct
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function findSelectedRate(
|
||||
rateCode: 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)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user