Chore/refactor hotel trpc routes * chore(SW-3519): refactor trpc hotel routers * chore(SW-3519): refactor trpc hotel routers * refactor * merge * Merge branch 'master' of bitbucket.org:scandic-swap/web into chore/refactor-hotel-trpc-routes Approved-by: Linus Flood
59 lines
2.0 KiB
TypeScript
59 lines
2.0 KiB
TypeScript
import { serviceProcedure } from "../../../procedures"
|
|
import { toApiLang } from "../../../utils"
|
|
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 { lang } = ctx
|
|
const apiLang = toApiLang(lang)
|
|
|
|
const bookingCodeAvailabilityResponse = await getHotelsAvailabilityByCity(
|
|
input,
|
|
apiLang,
|
|
ctx.serviceToken
|
|
)
|
|
|
|
// 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(
|
|
unavailableHotelsInput,
|
|
apiLang,
|
|
ctx.serviceToken
|
|
)
|
|
|
|
// 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),
|
|
}
|
|
})
|