import { breakfastPackageSchema, packageSchema, } from "@/server/routers/hotels/schemas/packages" import { ChildBedMapEnum } from "@/types/components/bookingWidget/enums" import { BreakfastPackageEnum } from "@/types/enums/breakfast" import { PackageTypeEnum } from "@/types/enums/packages" import type { Package } from "@/types/requests/packages" import type { Room } from "@/types/stores/booking-confirmation" export function mapToPrice(rooms: (Room | null)[], nights: number) { return rooms .filter((room): room is Room => !!room) .map((room) => { let price if (room.cheques) { price = { corporateCheque: { additionalPricePerStay: room.totalPrice ? room.totalPrice : undefined, currency: room.totalPrice ? room.currencyCode : undefined, numberOfCheques: room.cheques, }, } } else if (room.roomPoints) { price = { redemption: { additionalPricePerStay: room.totalPrice ? room.totalPrice : undefined, currency: room.totalPrice ? room.currencyCode : undefined, pointsPerNight: room.roomPoints / nights, pointsPerStay: room.roomPoints, }, } } else if (room.vouchers) { price = { voucher: { numberOfVouchers: room.vouchers, }, } } else { price = { regular: { currency: room.currencyCode, pricePerNight: room.roomPrice / nights, pricePerStay: room.totalPrice, }, } } const breakfastPackage = breakfastPackageSchema.safeParse({ code: room.breakfast?.code, description: room.breakfast?.description, localPrice: { currency: room.breakfast?.currency, price: room.breakfast?.unitPrice, totalPrice: room.breakfast?.totalPrice, }, packageType: room.breakfast?.code === BreakfastPackageEnum.REGULAR_BREAKFAST ? PackageTypeEnum.BreakfastAdult : "", requestedPrice: { currency: room.breakfast?.currency, price: room.breakfast?.unitPrice, totalPrice: room.breakfast?.totalPrice, }, }) const packages = room.roomFeatures ?.map((featPkg) => { const pkg = packageSchema.safeParse({ code: featPkg.code, description: featPkg.description, inventories: [], localPrice: { currency: featPkg.currency, price: featPkg.unitPrice, totalPrice: featPkg.totalPrice, }, requestedPrice: { currency: featPkg.currency, price: featPkg.unitPrice, totalPrice: featPkg.totalPrice, }, }) if (pkg.success) { return pkg.data } return null }) .filter((pkg): pkg is Package => !!pkg) return { ...room, adults: room.adults, bedType: { description: room.bedDescription, roomTypeCode: room.roomTypeCode || "", }, breakfast: breakfastPackage.success ? breakfastPackage.data : undefined, breakfastIncluded: room.rateDefinition.breakfastIncluded, childrenInRoom: room.childrenAges?.map((age) => ({ age, bed: ChildBedMapEnum.UNKNOWN, })), packages, price, roomType: room.name, } }) }