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
77 lines
2.0 KiB
TypeScript
77 lines
2.0 KiB
TypeScript
import { createCounter } from "@scandic-hotels/common/telemetry"
|
|
|
|
import * as api from "../../../api"
|
|
import {
|
|
badGatewayError,
|
|
extractResponseDetails,
|
|
serverErrorByStatus,
|
|
} from "../../../errors"
|
|
import { toApiLang } from "../../../utils"
|
|
import { getBooking } from "../getBooking"
|
|
import { cancelBookingSchema } from "./schema"
|
|
|
|
import type { Lang } from "@scandic-hotels/common/constants/language"
|
|
|
|
import type { CancelBooking } from "./schema"
|
|
|
|
export async function cancelBooking(
|
|
{
|
|
confirmationNumber,
|
|
language,
|
|
}: { confirmationNumber: string; language: Lang },
|
|
token: string
|
|
): Promise<CancelBooking | null> {
|
|
const cancelBookingCounter = createCounter("booking.cancel")
|
|
const metricsCancelBooking = cancelBookingCounter.init({
|
|
confirmationNumber,
|
|
language,
|
|
})
|
|
|
|
metricsCancelBooking.start()
|
|
|
|
const headers = {
|
|
Authorization: `Bearer ${token}`,
|
|
}
|
|
|
|
const booking = await getBooking(
|
|
{ confirmationNumber, lang: language },
|
|
token
|
|
)
|
|
if (!booking) {
|
|
metricsCancelBooking.noDataError({ confirmationNumber })
|
|
return null
|
|
}
|
|
const { firstName, lastName, email } = booking.guest
|
|
const apiResponse = await api.remove(
|
|
api.endpoints.v1.Booking.cancel(confirmationNumber),
|
|
{
|
|
headers,
|
|
body: { firstName, lastName, email },
|
|
},
|
|
{ language: toApiLang(language) }
|
|
)
|
|
|
|
if (!apiResponse.ok) {
|
|
await metricsCancelBooking.httpError(apiResponse)
|
|
throw serverErrorByStatus(
|
|
apiResponse.status,
|
|
await extractResponseDetails(apiResponse),
|
|
`cancelBooking failed for ${confirmationNumber}`
|
|
)
|
|
}
|
|
|
|
const apiJson = await apiResponse.json()
|
|
const verifiedData = cancelBookingSchema.safeParse(apiJson)
|
|
if (!verifiedData.success) {
|
|
metricsCancelBooking.validationError(verifiedData.error)
|
|
throw badGatewayError({
|
|
message: "Invalid response from cancelBooking",
|
|
errorDetails: { validationError: verifiedData.error },
|
|
})
|
|
}
|
|
|
|
metricsCancelBooking.success()
|
|
|
|
return verifiedData.data
|
|
}
|