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:
@@ -0,0 +1,65 @@
|
||||
import { createCounter } from "@scandic-hotels/common/telemetry"
|
||||
|
||||
import * as api from "../../../api"
|
||||
import {
|
||||
badRequestError,
|
||||
extractResponseDetails,
|
||||
serverErrorByStatus,
|
||||
} from "../../../errors"
|
||||
import { toApiLang } from "../../../utils"
|
||||
import { bookingStatusSchema } from "./schema"
|
||||
|
||||
import type { Lang } from "@scandic-hotels/common/constants/language"
|
||||
|
||||
import type { BookingStatus } from "./schema"
|
||||
export type { BookingStatus } from "./schema"
|
||||
|
||||
export async function getBookingStatus(
|
||||
{ confirmationNumber, lang }: { confirmationNumber: string; lang: Lang },
|
||||
serviceToken: string
|
||||
): Promise<BookingStatus> {
|
||||
const language = toApiLang(lang)
|
||||
|
||||
const getBookingStatusCounter = createCounter("trpc.booking.status")
|
||||
const metricsGetBookingStatus = getBookingStatusCounter.init({
|
||||
confirmationNumber,
|
||||
})
|
||||
|
||||
metricsGetBookingStatus.start()
|
||||
|
||||
const apiResponse = await api.get(
|
||||
api.endpoints.v1.Booking.status(confirmationNumber),
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${serviceToken}`,
|
||||
},
|
||||
},
|
||||
{
|
||||
language,
|
||||
}
|
||||
)
|
||||
|
||||
if (!apiResponse.ok) {
|
||||
await metricsGetBookingStatus.httpError(apiResponse)
|
||||
throw serverErrorByStatus(
|
||||
apiResponse.status,
|
||||
await extractResponseDetails(apiResponse),
|
||||
"getBookingStatus failed"
|
||||
)
|
||||
}
|
||||
|
||||
const apiJson = await apiResponse.json()
|
||||
const verifiedData = bookingStatusSchema.safeParse(apiJson)
|
||||
if (!verifiedData.success) {
|
||||
metricsGetBookingStatus.validationError(verifiedData.error)
|
||||
|
||||
throw badRequestError({
|
||||
message: "Invalid booking data",
|
||||
errorDetails: verifiedData.error.formErrors,
|
||||
})
|
||||
}
|
||||
|
||||
metricsGetBookingStatus.success()
|
||||
|
||||
return verifiedData.data
|
||||
}
|
||||
Reference in New Issue
Block a user