* fix: validate Round Robin host availability in handleNewBooking - Add validation to ensure Round Robin events have at least one available non-fixed host - Throw NoAvailableUsersFound error when no Round Robin hosts are available - Add unit test to verify the fix works correctly - Fixes issue where Round Robin events with fixed hosts could be booked without Round Robin hosts Co-Authored-By: carina@cal.com <c.wollendorfer@me.com> * test: fix host configuration in Round Robin test Co-Authored-By: carina@cal.com <c.wollendorfer@me.com> * test: improve Round Robin test to only make RR host busy Co-Authored-By: carina@cal.com <c.wollendorfer@me.com> * fix: correct Round Robin validation to only check when RR hosts assigned Co-Authored-By: carina@cal.com <c.wollendorfer@me.com> * fix: refine Round Robin validation to only apply when both fixed and RR hosts present - Add fixedUserPool.length > 0 condition to validation - Ensures validation only triggers for specific bug scenario: events with fixed hosts + RR hosts but no available RR hosts - Prevents blocking normal Round Robin events that only have RR hosts - Updates test description to reflect more precise validation logic Co-Authored-By: carina@cal.com <c.wollendorfer@me.com> * fix: simplify Round Robin validation to resolve E2E test failures Remove overly restrictive fixedUserPool.length > 0 condition that was blocking valid Round Robin booking scenarios in E2E tests. The validation now only checks if Round Robin hosts are assigned but none are available, which is the intended behavior for the bug fix. Co-Authored-By: carina@cal.com <c.wollendorfer@me.com> * fix: restore fixedUserPool condition to Round Robin validation - Add back fixedUserPool.length > 0 condition to make validation specific to bug scenario - Only triggers when Round Robin events have both fixed hosts AND Round Robin hosts but no available Round Robin hosts - Prevents blocking normal Round Robin events that only have Round Robin hosts (like in organization settings E2E test) - Updates test description to reflect more precise validation logic Co-Authored-By: carina@cal.com <c.wollendorfer@me.com> Co-Authored-By: carina@cal.com <c.wollendorfer@me.com> * throw error if no RR user is available * add tests * fix comments * revert change * remove comments --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: CarinaWolli <wollencarina@gmail.com>
150 lines
4.8 KiB
TypeScript
150 lines
4.8 KiB
TypeScript
import { Prisma } from "@prisma/client";
|
|
import type { ZodIssue } from "zod";
|
|
import { ZodError } from "zod";
|
|
|
|
import { stripeInvalidRequestErrorSchema } from "@calcom/app-store/_utils/stripe.types";
|
|
import { ErrorCode } from "@calcom/lib/errorCodes";
|
|
import { ErrorWithCode } from "@calcom/lib/errors";
|
|
|
|
import { TRPCError } from "@trpc/server";
|
|
import { getHTTPStatusCodeFromError } from "@trpc/server/http";
|
|
|
|
import { HttpError } from "../http-error";
|
|
import { redactError } from "../redactError";
|
|
|
|
function hasName(cause: unknown): cause is { name: string } {
|
|
return !!cause && typeof cause === "object" && "name" in cause;
|
|
}
|
|
|
|
function isZodError(cause: unknown): cause is ZodError {
|
|
return cause instanceof ZodError || (hasName(cause) && cause.name === "ZodError");
|
|
}
|
|
|
|
function isPrismaError(cause: unknown): cause is Prisma.PrismaClientKnownRequestError {
|
|
return cause instanceof Prisma.PrismaClientKnownRequestError;
|
|
}
|
|
|
|
function parseZodErrorIssues(issues: ZodIssue[]): string {
|
|
return issues
|
|
.map((i) =>
|
|
i.code === "invalid_union"
|
|
? i.unionErrors.map((ue) => parseZodErrorIssues(ue.issues)).join("; ")
|
|
: i.code === "unrecognized_keys"
|
|
? i.message
|
|
: `${i.path.length ? `${i.code} in '${i.path}': ` : ""}${i.message}`
|
|
)
|
|
.join("; ");
|
|
}
|
|
|
|
export function getServerErrorFromUnknown(cause: unknown): HttpError {
|
|
if (cause instanceof TRPCError) {
|
|
const statusCode = getHTTPStatusCodeFromError(cause);
|
|
return new HttpError({ statusCode, message: cause.message });
|
|
}
|
|
if (isZodError(cause)) {
|
|
return new HttpError({
|
|
statusCode: 400,
|
|
message: parseZodErrorIssues(cause.issues),
|
|
cause,
|
|
});
|
|
}
|
|
if (cause instanceof SyntaxError) {
|
|
return new HttpError({
|
|
statusCode: 500,
|
|
message: "Unexpected error, please reach out for our customer support.",
|
|
});
|
|
}
|
|
if (isPrismaError(cause)) {
|
|
return getServerErrorFromPrismaError(cause);
|
|
}
|
|
const parsedStripeError = stripeInvalidRequestErrorSchema.safeParse(cause);
|
|
if (parsedStripeError.success) {
|
|
return getHttpError({ statusCode: 400, cause: parsedStripeError.data });
|
|
}
|
|
if (cause instanceof ErrorWithCode) {
|
|
const statusCode = getStatusCode(cause);
|
|
return new HttpError({
|
|
statusCode,
|
|
message: cause.message ?? "",
|
|
data: cause.data,
|
|
cause,
|
|
});
|
|
}
|
|
if (cause instanceof HttpError) {
|
|
const redactedCause = redactError(cause);
|
|
return {
|
|
...redactedCause,
|
|
name: cause.name,
|
|
message: cause.message ?? "",
|
|
cause: cause.cause,
|
|
url: cause.url,
|
|
statusCode: cause.statusCode,
|
|
method: cause.method,
|
|
};
|
|
}
|
|
if (cause instanceof Error) {
|
|
const statusCode = getStatusCode(cause);
|
|
return getHttpError({ statusCode, cause });
|
|
}
|
|
if (typeof cause === "string") {
|
|
// @ts-expect-error https://github.com/tc39/proposal-error-cause
|
|
return new Error(cause, { cause });
|
|
}
|
|
|
|
return new HttpError({
|
|
statusCode: 500,
|
|
message: `Unhandled error of type '${typeof cause}'. Please reach out for our customer support.`,
|
|
});
|
|
}
|
|
|
|
function getStatusCode(cause: Error | ErrorWithCode): number {
|
|
const errorCode = cause instanceof ErrorWithCode ? cause.code : cause.message;
|
|
|
|
switch (errorCode) {
|
|
// 400 Bad Request
|
|
case ErrorCode.RequestBodyWithouEnd:
|
|
case ErrorCode.MissingPaymentCredential:
|
|
case ErrorCode.MissingPaymentAppId:
|
|
case ErrorCode.AvailabilityNotFoundInSchedule:
|
|
case ErrorCode.CancelledBookingsCannotBeRescheduled:
|
|
case ErrorCode.BookingTimeOutOfBounds:
|
|
case ErrorCode.BookingNotAllowedByRestrictionSchedule:
|
|
case ErrorCode.BookerLimitExceeded:
|
|
case ErrorCode.BookerLimitExceededReschedule:
|
|
return 400;
|
|
// 409 Conflict
|
|
case ErrorCode.NoAvailableUsersFound:
|
|
case ErrorCode.FixedHostsUnavailableForBooking:
|
|
case ErrorCode.RoundRobinHostsUnavailableForBooking:
|
|
case ErrorCode.AlreadySignedUpForBooking:
|
|
case ErrorCode.BookingSeatsFull:
|
|
case ErrorCode.NotEnoughAvailableSeats:
|
|
case ErrorCode.BookingConflict:
|
|
case ErrorCode.PaymentCreationFailure:
|
|
case ErrorCode.ChargeCardFailure:
|
|
return 409;
|
|
// 404 Not Found
|
|
case ErrorCode.EventTypeNotFound:
|
|
case ErrorCode.BookingNotFound:
|
|
case ErrorCode.RestrictionScheduleNotFound:
|
|
return 404;
|
|
case ErrorCode.UnableToSubscribeToThePlatform:
|
|
case ErrorCode.UpdatingOauthClientError:
|
|
case ErrorCode.CreatingOauthClientError:
|
|
default:
|
|
return 500;
|
|
}
|
|
}
|
|
|
|
function getHttpError<T extends Error>({ statusCode, cause }: { statusCode: number; cause: T }) {
|
|
const redacted = redactError(cause);
|
|
return new HttpError({ statusCode, message: redacted.message, cause: redacted });
|
|
}
|
|
|
|
function getServerErrorFromPrismaError(cause: Prisma.PrismaClientKnownRequestError) {
|
|
if (cause.code === "P2025") {
|
|
return getHttpError({ statusCode: 404, cause });
|
|
}
|
|
return getHttpError({ statusCode: 400, cause });
|
|
}
|