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
81 lines
2.1 KiB
TypeScript
81 lines
2.1 KiB
TypeScript
import { type Dayjs, dt } from "@scandic-hotels/common/dt"
|
|
import { createCounter } from "@scandic-hotels/common/telemetry"
|
|
|
|
import * as api from "../../api"
|
|
import { langToApiLang } from "../../constants/apiLang"
|
|
import {
|
|
badGatewayError,
|
|
extractResponseDetails,
|
|
serverErrorByStatus,
|
|
} from "../../errors"
|
|
import { bookingConfirmationSchema } from "./getBooking/schema"
|
|
|
|
import type { Lang } from "@scandic-hotels/common/constants/language"
|
|
|
|
export async function updateBooking(
|
|
input: {
|
|
confirmationNumber: string
|
|
lang: Lang
|
|
checkInDate: Dayjs | Date | undefined
|
|
checkOutDate: Dayjs | Date | undefined
|
|
guest?: {
|
|
email?: string | undefined
|
|
phoneNumber?: string | undefined
|
|
countryCode?: string | undefined
|
|
}
|
|
},
|
|
token: string
|
|
) {
|
|
const updateBookingCounter = createCounter("booking.update")
|
|
const metricsUpdateBooking = updateBookingCounter.init({
|
|
confirmationNumber: input.confirmationNumber,
|
|
language: input.lang,
|
|
})
|
|
|
|
metricsUpdateBooking.start()
|
|
const body = {
|
|
checkInDate: input.checkInDate
|
|
? dt(input.checkInDate).format("YYYY-MM-DD")
|
|
: undefined,
|
|
checkOutDate: input.checkOutDate
|
|
? dt(input.checkOutDate).format("YYYY-MM-DD")
|
|
: undefined,
|
|
guest: input.guest,
|
|
}
|
|
|
|
const apiResponse = await api.put(
|
|
api.endpoints.v1.Booking.booking(input.confirmationNumber),
|
|
{
|
|
body,
|
|
headers: {
|
|
Authorization: `Bearer ${token}`,
|
|
},
|
|
},
|
|
{ language: langToApiLang[input.lang] }
|
|
)
|
|
|
|
if (!apiResponse.ok) {
|
|
await metricsUpdateBooking.httpError(apiResponse)
|
|
throw serverErrorByStatus(
|
|
apiResponse.status,
|
|
await extractResponseDetails(apiResponse),
|
|
"updateBooking failed for " + input.confirmationNumber
|
|
)
|
|
}
|
|
|
|
const apiJson = await apiResponse.json()
|
|
|
|
const verifiedData = bookingConfirmationSchema.safeParse(apiJson)
|
|
if (!verifiedData.success) {
|
|
metricsUpdateBooking.validationError(verifiedData.error)
|
|
throw badGatewayError({
|
|
message: "Invalid response from updateBooking",
|
|
errorDetails: { validationError: verifiedData.error },
|
|
})
|
|
}
|
|
|
|
metricsUpdateBooking.success()
|
|
|
|
return verifiedData.data
|
|
}
|