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
60 lines
1.6 KiB
TypeScript
60 lines
1.6 KiB
TypeScript
import { createCounter } from "@scandic-hotels/common/telemetry"
|
|
|
|
import * as api from "../../../api"
|
|
import {
|
|
badRequestError,
|
|
extractResponseDetails,
|
|
serverErrorByStatus,
|
|
} from "../../../errors"
|
|
import { toApiLang } from "../../../utils"
|
|
import { bookingConfirmationSchema } from "./schema"
|
|
|
|
import type { Lang } from "@scandic-hotels/common/constants/language"
|
|
|
|
export async function getBooking(
|
|
{ confirmationNumber, lang }: { confirmationNumber: string; lang: Lang },
|
|
token: string
|
|
) {
|
|
const getBookingCounter = createCounter("booking.get")
|
|
const metricsGetBooking = getBookingCounter.init({ confirmationNumber })
|
|
|
|
metricsGetBooking.start()
|
|
|
|
const apiResponse = await api.get(
|
|
api.endpoints.v1.Booking.booking(confirmationNumber),
|
|
{
|
|
headers: {
|
|
Authorization: `Bearer ${token}`,
|
|
},
|
|
},
|
|
{ language: toApiLang(lang) }
|
|
)
|
|
|
|
if (!apiResponse.ok) {
|
|
await metricsGetBooking.httpError(apiResponse)
|
|
|
|
// If the booking is not found, return null.
|
|
// This scenario is expected to happen when a logged in user trying to access a booking that doesn't belong to them.
|
|
if (apiResponse.status === 404) {
|
|
return null
|
|
}
|
|
|
|
throw serverErrorByStatus(
|
|
apiResponse.status,
|
|
await extractResponseDetails(apiResponse),
|
|
"getBooking failed"
|
|
)
|
|
}
|
|
|
|
const apiJson = await apiResponse.json()
|
|
const booking = bookingConfirmationSchema.safeParse(apiJson)
|
|
if (!booking.success) {
|
|
metricsGetBooking.validationError(booking.error)
|
|
throw badRequestError()
|
|
}
|
|
|
|
metricsGetBooking.success()
|
|
|
|
return booking.data
|
|
}
|