feat(BOOK-750): refactor booking endpoints * WIP * wip * wip * parse dates in UTC * wip * no more errors * Merge branch 'master' of bitbucket.org:scandic-swap/web into chore/refactor-trpc-booking-routes * . * cleanup * import named z from zod * fix(BOOK-750): updateBooking api endpoint expects dateOnly, we passed ISO date Approved-by: Anton Gunnarsson
88 lines
2.3 KiB
TypeScript
88 lines
2.3 KiB
TypeScript
import { createCounter } from "@scandic-hotels/common/telemetry"
|
|
|
|
import { notFoundError } from "../../../errors"
|
|
import { safeProtectedServiceProcedure } from "../../../procedures"
|
|
import { findBooking } from "../../../services/booking/findBooking"
|
|
import { isValidSession } from "../../../utils/session"
|
|
import { getHotelPageUrls } from "../../contentstack/hotelPage/utils"
|
|
import { getHotel } from "../../hotels/services/getHotel"
|
|
import { getHotelRoom } from "../helpers"
|
|
import { findBookingInput } from "../input"
|
|
|
|
export const findBookingRoute = safeProtectedServiceProcedure
|
|
.input(findBookingInput)
|
|
.use(async ({ ctx, input, next }) => {
|
|
const lang = input.lang ?? ctx.lang
|
|
const token = isValidSession(ctx.session)
|
|
? ctx.session.token.access_token
|
|
: ctx.serviceToken
|
|
|
|
return next({
|
|
ctx: {
|
|
lang,
|
|
token,
|
|
},
|
|
})
|
|
})
|
|
.query(async function ({
|
|
ctx,
|
|
input: { confirmationNumber, lastName, firstName, email },
|
|
}) {
|
|
const { lang, token, serviceToken } = ctx
|
|
const findBookingCounter = createCounter("trpc.booking.findBooking")
|
|
const metricsFindBooking = findBookingCounter.init({ confirmationNumber })
|
|
|
|
metricsFindBooking.start()
|
|
|
|
const booking = await findBooking(
|
|
{ confirmationNumber, lang, lastName, firstName, email },
|
|
token
|
|
)
|
|
|
|
if (!booking) {
|
|
metricsFindBooking.dataError(
|
|
`Fail to find booking data for ${confirmationNumber}`,
|
|
{ confirmationNumber }
|
|
)
|
|
return null
|
|
}
|
|
|
|
const [hotelData, hotelPages] = await Promise.all([
|
|
getHotel(
|
|
{
|
|
hotelId: booking.hotelId,
|
|
isCardOnlyPayment: false,
|
|
language: lang,
|
|
},
|
|
serviceToken
|
|
),
|
|
getHotelPageUrls(lang),
|
|
])
|
|
const hotelPage = hotelPages.find(
|
|
(page) => page.hotelId === booking.hotelId
|
|
)
|
|
|
|
if (!hotelData) {
|
|
metricsFindBooking.dataError(
|
|
`Failed to find hotel data for ${booking.hotelId}`,
|
|
{
|
|
hotelId: booking.hotelId,
|
|
}
|
|
)
|
|
|
|
throw notFoundError({
|
|
message: "Hotel data not found",
|
|
errorDetails: { hotelId: booking.hotelId },
|
|
})
|
|
}
|
|
|
|
metricsFindBooking.success()
|
|
|
|
return {
|
|
...hotelData,
|
|
url: hotelPage?.url || null,
|
|
booking,
|
|
room: getHotelRoom(hotelData.roomCategories, booking.roomTypeCode),
|
|
}
|
|
})
|