Merged in fix/SW-2508-new-api-cancel-booking-contract (pull request #1906)
Ending up doing some extra things: Consolidated booking queries: We had both cancel and cancelMany, but functionally they’re the same, only one accepts an array and the other doesn’t. Didn’t see much point in keeping the single cancel as it wasn’t used anywhere. Thus, I could rename cancelMany to be the one stop method. remove method for API now properly supports body so we don’t have to hijack the typing in certain places * fix(SW-2508): now sending additional params to cancel api for new contract Approved-by: Niclas Edenvin
This commit is contained in:
committed by
Niclas Edenvin
parent
f0dbf294d8
commit
43bdd80dff
@@ -40,7 +40,7 @@ export default function FinalConfirmation({
|
||||
defaultMessage: "We’re sorry that things didn’t work out.",
|
||||
})
|
||||
|
||||
const cancelBookingsMutation = trpc.booking.cancelMany.useMutation({
|
||||
const cancelBookingsMutation = trpc.booking.cancel.useMutation({
|
||||
onSuccess(data, variables) {
|
||||
const allCancellationsWentThrough = data.every((cancelled) => cancelled)
|
||||
if (allCancellationsWentThrough) {
|
||||
|
||||
@@ -100,14 +100,19 @@ export async function put(
|
||||
|
||||
export async function remove(
|
||||
endpoint: Endpoint | `${Endpoint}/${string}`,
|
||||
options: RequestOptionsWithOutBody,
|
||||
options: RequestOptionsWithJSONBody,
|
||||
params = {}
|
||||
) {
|
||||
const { body, ...requestOptions } = options
|
||||
const url = new URL(env.API_BASEURL)
|
||||
url.pathname = endpoint
|
||||
url.search = new URLSearchParams(params).toString()
|
||||
return wrappedFetch(
|
||||
url,
|
||||
merge.all([defaultOptions, { method: "DELETE" }, options])
|
||||
merge.all([
|
||||
defaultOptions,
|
||||
{ body: JSON.stringify(body), method: "DELETE" },
|
||||
requestOptions,
|
||||
])
|
||||
)
|
||||
}
|
||||
|
||||
@@ -126,14 +126,9 @@ export const priceChangeInput = z.object({
|
||||
confirmationNumber: z.string(),
|
||||
})
|
||||
|
||||
export const cancelBookingInput = z.object({
|
||||
confirmationNumber: z.string(),
|
||||
language: z.nativeEnum(Lang).transform((val) => langToApiLang[val]),
|
||||
})
|
||||
|
||||
export const cancelManyBookingsInput = z.object({
|
||||
export const cancelBookingsInput = z.object({
|
||||
confirmationNumbers: z.array(z.string()),
|
||||
language: z.nativeEnum(Lang).transform((val) => langToApiLang[val]),
|
||||
language: z.nativeEnum(Lang),
|
||||
})
|
||||
|
||||
export const guaranteeBookingInput = z.object({
|
||||
|
||||
@@ -5,8 +5,7 @@ import { router, safeProtectedServiceProcedure } from "@/server/trpc"
|
||||
|
||||
import {
|
||||
addPackageInput,
|
||||
cancelBookingInput,
|
||||
cancelManyBookingsInput,
|
||||
cancelBookingsInput,
|
||||
createBookingInput,
|
||||
guaranteeBookingInput,
|
||||
priceChangeInput,
|
||||
@@ -113,14 +112,7 @@ export const bookingMutationRouter = router({
|
||||
return verifiedData.data
|
||||
}),
|
||||
cancel: safeProtectedServiceProcedure
|
||||
.input(cancelBookingInput)
|
||||
.mutation(async function ({ ctx, input }) {
|
||||
const token = ctx.session?.token.access_token ?? ctx.serviceToken
|
||||
const { confirmationNumber, language } = input
|
||||
return await cancelBooking(confirmationNumber, language, token)
|
||||
}),
|
||||
cancelMany: safeProtectedServiceProcedure
|
||||
.input(cancelManyBookingsInput)
|
||||
.input(cancelBookingsInput)
|
||||
.mutation(async function ({ ctx, input }) {
|
||||
const token = ctx.session?.token.access_token ?? ctx.serviceToken
|
||||
const { confirmationNumbers, language } = input
|
||||
@@ -297,7 +289,7 @@ export const bookingMutationRouter = router({
|
||||
api.endpoints.v1.Booking.packages(confirmationNumber),
|
||||
{
|
||||
headers,
|
||||
} as RequestInit,
|
||||
},
|
||||
[["language", language], ...codes.map((code) => ["codes", code])]
|
||||
)
|
||||
|
||||
|
||||
@@ -80,17 +80,11 @@ export async function getBooking(
|
||||
|
||||
export async function cancelBooking(
|
||||
confirmationNumber: string,
|
||||
language: string,
|
||||
language: Lang,
|
||||
token: string
|
||||
) {
|
||||
const cancellationReason = {
|
||||
reasonCode: "WEB-CANCEL",
|
||||
reason: "WEB-CANCEL",
|
||||
}
|
||||
|
||||
const cancelBookingCounter = createCounter("booking", "cancel")
|
||||
const metricsCancelBooking = cancelBookingCounter.init({
|
||||
cancellationReason,
|
||||
confirmationNumber,
|
||||
language,
|
||||
})
|
||||
@@ -101,18 +95,24 @@ export async function cancelBooking(
|
||||
Authorization: `Bearer ${token}`,
|
||||
}
|
||||
|
||||
const booking = await getBooking(confirmationNumber, 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: JSON.stringify(cancellationReason),
|
||||
} as RequestInit,
|
||||
{ language }
|
||||
body: { firstName, lastName, email },
|
||||
},
|
||||
{ language: toApiLang(language) }
|
||||
)
|
||||
|
||||
if (!apiResponse.ok) {
|
||||
await metricsCancelBooking.httpError(apiResponse)
|
||||
return false
|
||||
return null
|
||||
}
|
||||
|
||||
const apiJson = await apiResponse.json()
|
||||
|
||||
Reference in New Issue
Block a user