import { serviceProcedure } from "../../../procedures" import { getHotelsAvailabilityByCity } from "../services/getHotelsAvailabilityByCity" import { getHotelsAvailabilityByHotelIds } from "../services/getHotelsAvailabilityByHotelIds" import { hotelsAvailabilityInputSchema } from "./hotelsByCity" export const hotelsByCityWithBookingCode = serviceProcedure .input(hotelsAvailabilityInputSchema) .query(async ({ input, ctx }) => { const bookingCodeAvailabilityResponse = await getHotelsAvailabilityByCity({ input, lang: ctx.lang, userToken: undefined, serviceToken: ctx.serviceToken, userPoints: undefined, }) // Get regular availability of hotels which don't have availability with booking code. const unavailableHotelIds = bookingCodeAvailabilityResponse.availability .filter((hotel) => { return hotel.status === "NotAvailable" }) .flatMap((hotel) => { return hotel.hotelId }) // All hotels have availability with booking code no need to fetch regular prices. // return response as is without any filtering as below. if (!unavailableHotelIds || !unavailableHotelIds.length) { return bookingCodeAvailabilityResponse } const unavailableHotelsInput = { ...input, bookingCode: "", hotelIds: unavailableHotelIds, } const unavailableHotels = await getHotelsAvailabilityByHotelIds({ input: unavailableHotelsInput, lang: ctx.lang, serviceToken: ctx.serviceToken, userToken: undefined, userPoints: undefined, }) // No regular rates available due to network or API failure (no need to filter & merge). if (!unavailableHotels) { return bookingCodeAvailabilityResponse } // Filtering the response hotels to merge bookingCode rates and regular rates in single response. return { availability: bookingCodeAvailabilityResponse.availability .filter((hotel) => { return hotel.status === "Available" }) .concat(unavailableHotels.availability), } })