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" import { RateTypeEnum } from "@/types/enums/rateType" 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 } // Return rate even if single when booking code rate which can be any one of other rate types if (product.productType.public.rateType !== RateTypeEnum.Regular) { 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. * This rule applies to regular rates (Save, Change and Flex) * Exception Booking code rate * * TODO: (Maybe) notify somewhere that this happened */ const allProductsMissAtLeastOneRateCode = data.products.every( ({ productType }) => (!productType.public.rateCode || !productType.member?.rateCode) && productType.public.rateType === RateTypeEnum.Regular ) if (allProductsMissAtLeastOneRateCode) { data.status = AvailabilityEnum.NotAvailable } } return data })