Files
web/packages/trpc/lib/routers/booking/mutation/cancelBookingRoute.ts
Joakim Jäderberg 16cc26632e 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
2026-02-02 14:28:14 +00:00

52 lines
1.6 KiB
TypeScript

import { createLogger } from "@scandic-hotels/common/logger/createLogger"
import { createRefIdPlugin } from "../../../plugins/refIdToConfirmationNumber"
import { safeProtectedServiceProcedure } from "../../../procedures"
import { cancelBooking } from "../../../services/booking/cancelBooking"
import { cancelBookingsInput } from "../input"
const bookingLogger = createLogger("trpc.booking.cancelBooking")
const refIdPlugin = createRefIdPlugin()
export const cancelBookingRoute = safeProtectedServiceProcedure
.input(cancelBookingsInput)
.concat(refIdPlugin.toConfirmationNumbers)
.use(async ({ ctx, next }) => {
const token = await ctx.getScandicUserToken()
return next({
ctx: {
token,
},
})
})
.mutation(async function ({ ctx, input }) {
const { confirmationNumbers } = ctx
const { language } = input
const token = ctx.token ?? ctx.serviceToken
const responses = await Promise.allSettled(
confirmationNumbers.map((confirmationNumber) =>
cancelBooking({ confirmationNumber, language }, token)
)
)
const cancelledRoomsSuccessfully: (string | null)[] = []
for (const [idx, response] of responses.entries()) {
if (response.status === "fulfilled") {
if (response.value) {
cancelledRoomsSuccessfully.push(confirmationNumbers[idx])
continue
}
} else {
bookingLogger.error(
`Cancelling booking failed for confirmationNumber: ${confirmationNumbers[idx]}`,
response.reason
)
}
cancelledRoomsSuccessfully.push(null)
}
return cancelledRoomsSuccessfully
})