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:
63
packages/trpc/lib/services/booking/guaranteeBooking/index.ts
Normal file
63
packages/trpc/lib/services/booking/guaranteeBooking/index.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import { createCounter } from "@scandic-hotels/common/telemetry"
|
||||
|
||||
import * as api from "../../../api"
|
||||
import { langToApiLang } from "../../../constants/apiLang"
|
||||
import { slimBookingSchema } from "../schema"
|
||||
|
||||
import type { Lang } from "@scandic-hotels/common/constants/language"
|
||||
|
||||
export async function guaranteeBooking(
|
||||
{
|
||||
confirmationNumber,
|
||||
language,
|
||||
success,
|
||||
error,
|
||||
cancel,
|
||||
card,
|
||||
}: {
|
||||
confirmationNumber: string
|
||||
language: Lang
|
||||
success: string | null
|
||||
error: string | null
|
||||
cancel: string | null
|
||||
card?: { alias: string; expiryDate: string; cardType: string }
|
||||
},
|
||||
token: string
|
||||
) {
|
||||
const guaranteeBookingCounter = createCounter("trpc.booking.guarantee")
|
||||
const metricsGuaranteeBooking = guaranteeBookingCounter.init({
|
||||
confirmationNumber,
|
||||
language,
|
||||
})
|
||||
|
||||
metricsGuaranteeBooking.start()
|
||||
|
||||
const headers = {
|
||||
Authorization: `Bearer ${token}`,
|
||||
}
|
||||
|
||||
const apiResponse = await api.put(
|
||||
api.endpoints.v1.Booking.guarantee(confirmationNumber),
|
||||
{
|
||||
headers,
|
||||
body: { success, error, cancel, card },
|
||||
},
|
||||
{ language: langToApiLang[language] }
|
||||
)
|
||||
|
||||
if (!apiResponse.ok) {
|
||||
await metricsGuaranteeBooking.httpError(apiResponse)
|
||||
return null
|
||||
}
|
||||
|
||||
const apiJson = await apiResponse.json()
|
||||
const verifiedData = slimBookingSchema.safeParse(apiJson)
|
||||
if (!verifiedData.success) {
|
||||
metricsGuaranteeBooking.validationError(verifiedData.error)
|
||||
return null
|
||||
}
|
||||
|
||||
metricsGuaranteeBooking.success()
|
||||
|
||||
return verifiedData.data
|
||||
}
|
||||
Reference in New Issue
Block a user