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
85 lines
2.3 KiB
TypeScript
85 lines
2.3 KiB
TypeScript
import { createCounter } from "@scandic-hotels/common/telemetry"
|
|
|
|
import { notFoundError } from "../../../errors"
|
|
import { createRefIdPlugin } from "../../../plugins/refIdToConfirmationNumber"
|
|
import { safeProtectedServiceProcedure } from "../../../procedures"
|
|
import { getBooking } from "../../../services/booking/getBooking"
|
|
import { getHotelPageUrls } from "../../contentstack/hotelPage/utils"
|
|
import { getHotel } from "../../hotels/services/getHotel"
|
|
import { getHotelRoom } from "../helpers"
|
|
import { getBookingInput } from "../input"
|
|
|
|
const refIdPlugin = createRefIdPlugin()
|
|
export const getBookingRoute = safeProtectedServiceProcedure
|
|
.input(getBookingInput)
|
|
.concat(refIdPlugin.toConfirmationNumber)
|
|
.use(async ({ ctx, input, next }) => {
|
|
const lang = input.lang ?? ctx.lang
|
|
const token = await ctx.getScandicUserToken()
|
|
|
|
return next({
|
|
ctx: {
|
|
lang,
|
|
token,
|
|
},
|
|
})
|
|
})
|
|
.query(async function ({ ctx }) {
|
|
const { confirmationNumber, lang, token, serviceToken } = ctx
|
|
|
|
const getBookingCounter = createCounter("trpc.booking.get")
|
|
const metricsGetBooking = getBookingCounter.init({ confirmationNumber })
|
|
|
|
metricsGetBooking.start()
|
|
|
|
const booking = await getBooking(
|
|
{ confirmationNumber, lang },
|
|
token ?? serviceToken
|
|
)
|
|
|
|
if (!booking) {
|
|
metricsGetBooking.dataError(
|
|
`Fail to get 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) {
|
|
metricsGetBooking.dataError(
|
|
`Failed to get hotel data for ${booking.hotelId}`,
|
|
{
|
|
hotelId: booking.hotelId,
|
|
}
|
|
)
|
|
throw notFoundError({
|
|
message: "Hotel data not found",
|
|
errorDetails: { hotelId: booking.hotelId },
|
|
})
|
|
}
|
|
|
|
metricsGetBooking.success()
|
|
|
|
return {
|
|
...hotelData,
|
|
url: hotelPage?.url || null,
|
|
booking,
|
|
room: getHotelRoom(hotelData.roomCategories, booking.roomTypeCode),
|
|
}
|
|
})
|