Merged in chore/refactor-trpc-booking-routes (pull request #3510)
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
This commit is contained in:
81
packages/trpc/lib/services/booking/findBooking/index.ts
Normal file
81
packages/trpc/lib/services/booking/findBooking/index.ts
Normal file
@@ -0,0 +1,81 @@
|
||||
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 "../getBooking/schema"
|
||||
|
||||
import type { Lang } from "@scandic-hotels/common/constants/language"
|
||||
|
||||
export async function findBooking(
|
||||
{
|
||||
confirmationNumber,
|
||||
lang,
|
||||
lastName,
|
||||
firstName,
|
||||
email,
|
||||
}: {
|
||||
confirmationNumber: string
|
||||
lang: Lang
|
||||
lastName?: string
|
||||
firstName?: string
|
||||
email?: string
|
||||
},
|
||||
token: string
|
||||
) {
|
||||
const findBookingCounter = createCounter("booking.find")
|
||||
const metricsGetBooking = findBookingCounter.init({
|
||||
confirmationNumber,
|
||||
lastName,
|
||||
firstName,
|
||||
email,
|
||||
})
|
||||
|
||||
metricsGetBooking.start()
|
||||
|
||||
const apiResponse = await api.post(
|
||||
api.endpoints.v1.Booking.find(confirmationNumber),
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
body: {
|
||||
lastName,
|
||||
firstName,
|
||||
email,
|
||||
},
|
||||
},
|
||||
{ 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 === 400 || apiResponse.status === 404) {
|
||||
return null
|
||||
}
|
||||
|
||||
throw serverErrorByStatus(
|
||||
apiResponse.status,
|
||||
await extractResponseDetails(apiResponse),
|
||||
"findBooking 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
|
||||
}
|
||||
Reference in New Issue
Block a user