From 43bdd80dff00bb5702307710911c759c7bc3e752 Mon Sep 17 00:00:00 2001 From: Christian Andolf Date: Fri, 2 May 2025 07:37:23 +0000 Subject: [PATCH] Merged in fix/SW-2508-new-api-cancel-booking-contract (pull request #1906) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .../Steps/FinalConfirmation/index.tsx | 2 +- apps/scandic-web/lib/api/index.ts | 9 ++++++-- .../server/routers/booking/input.ts | 9 ++------ .../server/routers/booking/mutation.ts | 14 +++--------- .../server/routers/booking/utils.ts | 22 +++++++++---------- 5 files changed, 24 insertions(+), 32 deletions(-) diff --git a/apps/scandic-web/components/HotelReservation/MyStay/ReferenceCard/Actions/NotCancelled/ManageStay/Actions/CancelStay/Steps/FinalConfirmation/index.tsx b/apps/scandic-web/components/HotelReservation/MyStay/ReferenceCard/Actions/NotCancelled/ManageStay/Actions/CancelStay/Steps/FinalConfirmation/index.tsx index 3c5cbca8c..8bd45aadf 100644 --- a/apps/scandic-web/components/HotelReservation/MyStay/ReferenceCard/Actions/NotCancelled/ManageStay/Actions/CancelStay/Steps/FinalConfirmation/index.tsx +++ b/apps/scandic-web/components/HotelReservation/MyStay/ReferenceCard/Actions/NotCancelled/ManageStay/Actions/CancelStay/Steps/FinalConfirmation/index.tsx @@ -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) { diff --git a/apps/scandic-web/lib/api/index.ts b/apps/scandic-web/lib/api/index.ts index 5fc1173c1..532750f9e 100644 --- a/apps/scandic-web/lib/api/index.ts +++ b/apps/scandic-web/lib/api/index.ts @@ -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, + ]) ) } diff --git a/apps/scandic-web/server/routers/booking/input.ts b/apps/scandic-web/server/routers/booking/input.ts index 8a87965a1..bbf444181 100644 --- a/apps/scandic-web/server/routers/booking/input.ts +++ b/apps/scandic-web/server/routers/booking/input.ts @@ -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({ diff --git a/apps/scandic-web/server/routers/booking/mutation.ts b/apps/scandic-web/server/routers/booking/mutation.ts index f0d15af5a..ce648e7ec 100644 --- a/apps/scandic-web/server/routers/booking/mutation.ts +++ b/apps/scandic-web/server/routers/booking/mutation.ts @@ -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])] ) diff --git a/apps/scandic-web/server/routers/booking/utils.ts b/apps/scandic-web/server/routers/booking/utils.ts index 5149a618c..c983407b4 100644 --- a/apps/scandic-web/server/routers/booking/utils.ts +++ b/apps/scandic-web/server/routers/booking/utils.ts @@ -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()