fix: taking care of missing rateDefinitions * fix: taking care of missing rateDefinitions Approved-by: Linus Flood
84 lines
2.6 KiB
TypeScript
84 lines
2.6 KiB
TypeScript
import deepmerge from "deepmerge"
|
|
import { z } from "zod"
|
|
|
|
import { productSchema } from "./product"
|
|
|
|
import { AvailabilityEnum } from "@/types/components/hotelReservation/selectHotel/selectHotel"
|
|
import { RoomPackageCodeEnum } from "@/types/components/hotelReservation/selectRate/roomFilter"
|
|
|
|
export const roomConfigurationSchema = z
|
|
.object({
|
|
breakfastIncludedInAllRatesMember: z.boolean().default(false),
|
|
breakfastIncludedInAllRatesPublic: z.boolean().default(false),
|
|
features: z
|
|
.array(
|
|
z.object({
|
|
inventory: z.number(),
|
|
code: z.enum([
|
|
RoomPackageCodeEnum.PET_ROOM,
|
|
RoomPackageCodeEnum.ALLERGY_ROOM,
|
|
RoomPackageCodeEnum.ACCESSIBILITY_ROOM,
|
|
]),
|
|
})
|
|
)
|
|
.default([]),
|
|
products: z.array(productSchema).default([]),
|
|
roomsLeft: z.number(),
|
|
roomType: z.string(),
|
|
roomTypeCode: z.string(),
|
|
status: z.string(),
|
|
})
|
|
.transform((data) => {
|
|
if (data.products.length) {
|
|
const someProductsMissAtLeastOneRateCode = data.products.some(
|
|
({ productType }) =>
|
|
!productType.public.rateCode || !productType.member?.rateCode
|
|
)
|
|
if (someProductsMissAtLeastOneRateCode) {
|
|
data.products = data.products.map((product) => {
|
|
if (
|
|
product.productType.public.rateCode &&
|
|
product.productType.member?.rateCode
|
|
) {
|
|
return product
|
|
}
|
|
|
|
/**
|
|
* Reset both rateCodes if one is missing to show `No prices available` for the same reason as
|
|
* mentioned above.
|
|
*
|
|
* TODO: (Maybe) notify somewhere that this happened
|
|
*/
|
|
return deepmerge(product, {
|
|
productType: {
|
|
member: {
|
|
rateCode: "",
|
|
oldRateCode: product.productType.member?.rateCode,
|
|
},
|
|
public: {
|
|
rateCode: "",
|
|
oldRateCode: product.productType.public.rateCode,
|
|
},
|
|
},
|
|
})
|
|
})
|
|
}
|
|
|
|
/**
|
|
* When all products miss at least one rateCode (member or public), we change the status to NotAvailable
|
|
* since we cannot as of now (31 january) guarantee the flow with missing rateCodes.
|
|
*
|
|
* TODO: (Maybe) notify somewhere that this happened
|
|
*/
|
|
const allProductsMissAtLeastOneRateCode = data.products.every(
|
|
({ productType }) =>
|
|
!productType.public.rateCode || !productType.member?.rateCode
|
|
)
|
|
if (allProductsMissAtLeastOneRateCode) {
|
|
data.status = AvailabilityEnum.NotAvailable
|
|
}
|
|
}
|
|
|
|
return data
|
|
})
|