import { createCounter } from "@scandic-hotels/common/telemetry" import { notFoundError } from "../../../errors" import { safeProtectedServiceProcedure } from "../../../procedures" import { findBooking } from "../../../services/booking/findBooking" import { getHotel } from "../../hotels/services/getHotel" import { findBookingInput } from "../input" export const findBookingForCurrentUserRoute = safeProtectedServiceProcedure .input( findBookingInput.omit({ lastName: true, firstName: true, email: true }) ) .query(async function ({ ctx, input }) { const lang = input.lang ?? ctx.lang const { confirmationNumber } = input const user = await ctx.getScandicUser() const token = await ctx.getScandicUserToken() const findBookingCounter = createCounter( "trpc.booking.findBookingForCurrentUser" ) const metricsFindBooking = findBookingCounter.init({ confirmationNumber, }) metricsFindBooking.start() if (!user || !token) { metricsFindBooking.dataError( `Fail to find user when finding booking for ${confirmationNumber}`, { confirmationNumber } ) return null } const booking = await findBooking( { confirmationNumber, lang, lastName: user.lastName, firstName: user.firstName, email: user.email, }, token ) if (!booking) { metricsFindBooking.dataError( `Fail to find booking data for ${confirmationNumber}`, { confirmationNumber } ) return null } const hotelData = await getHotel( { hotelId: booking.hotelId, isCardOnlyPayment: false, language: lang, }, ctx.serviceToken ) 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 { hotel: { name: hotelData.hotel.name, cityName: hotelData.hotel.cityName, }, booking, } })