mirror of
https://github.com/hanzoai/calendar.git
synced 2026-10-30 14:46:43 +00:00
2608e136cb
- Change 'Booking not found' to throw NOT_FOUND (404) instead of plain Error - Preserve ErrorWithCode for ChargeCardFailure to properly map to BAD_REQUEST (400) - Add specific error handling for 'User is not a member of the team' -> FORBIDDEN (403) - Add specific error handling for 'User ID is required' and 'No payment credential found' -> BAD_REQUEST (400) - Keep INTERNAL_SERVER_ERROR (500) only for actual server errors like 'Payment app not implemented' - Update test to expect ErrorWithCode to be re-thrown instead of wrapped in translated Error Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
78 lines
2.1 KiB
TypeScript
78 lines
2.1 KiB
TypeScript
import { handleNoShowFee } from "@calcom/features/bookings/lib/payment/handleNoShowFee";
|
|
import { BookingRepository } from "@calcom/features/bookings/repositories/BookingRepository";
|
|
import { ErrorCode } from "@calcom/lib/errorCodes";
|
|
import { ErrorWithCode } from "@calcom/lib/errors";
|
|
import type { PrismaClient } from "@calcom/prisma";
|
|
|
|
import { TRPCError } from "@trpc/server";
|
|
|
|
import type { TrpcSessionUser } from "../../../types";
|
|
import type { TChargeCardInputSchema } from "./chargeCard.schema";
|
|
|
|
interface ChargeCardHandlerOptions {
|
|
ctx: { user: NonNullable<TrpcSessionUser>; prisma: PrismaClient };
|
|
input: TChargeCardInputSchema;
|
|
}
|
|
export const chargeCardHandler = async ({ ctx, input }: ChargeCardHandlerOptions) => {
|
|
const { prisma } = ctx;
|
|
const bookingRepository = new BookingRepository(prisma);
|
|
|
|
const booking = await bookingRepository.getBookingForPaymentProcessing(input.bookingId);
|
|
|
|
if (!booking) {
|
|
throw new TRPCError({
|
|
code: "NOT_FOUND",
|
|
message: "Booking not found",
|
|
});
|
|
}
|
|
|
|
if (booking.payment[0].success) {
|
|
throw new TRPCError({
|
|
code: "BAD_REQUEST",
|
|
message: `The no show fee for ${booking.id} has already been charged.`,
|
|
});
|
|
}
|
|
|
|
try {
|
|
await handleNoShowFee({
|
|
booking,
|
|
payment: booking.payment[0],
|
|
});
|
|
} catch (error) {
|
|
console.error(error);
|
|
|
|
if (error instanceof ErrorWithCode && error.code === ErrorCode.ChargeCardFailure) {
|
|
throw new TRPCError({
|
|
code: "BAD_REQUEST",
|
|
message: error.message,
|
|
});
|
|
}
|
|
|
|
if (error instanceof Error) {
|
|
const message = error.message;
|
|
|
|
if (message.includes("User is not a member of the team")) {
|
|
throw new TRPCError({
|
|
code: "FORBIDDEN",
|
|
message,
|
|
});
|
|
}
|
|
|
|
if (
|
|
message.includes("User ID is required") ||
|
|
message.includes("No payment credential found")
|
|
) {
|
|
throw new TRPCError({
|
|
code: "BAD_REQUEST",
|
|
message,
|
|
});
|
|
}
|
|
}
|
|
|
|
throw new TRPCError({
|
|
code: "INTERNAL_SERVER_ERROR",
|
|
message: `Failed to charge no show fee for ${booking.id}`,
|
|
});
|
|
}
|
|
};
|