* Improve error handling in ratelimit * Update route.ts * Update route.ts * Update route.ts * address feedback
61 lines
2.1 KiB
TypeScript
61 lines
2.1 KiB
TypeScript
import { defaultResponderForAppDir } from "app/api/defaultResponderForAppDir";
|
|
import { cookies, headers } from "next/headers";
|
|
import { NextResponse } from "next/server";
|
|
import type { NextRequest } from "next/server";
|
|
|
|
import { getServerSession } from "@calcom/features/auth/lib/getServerSession";
|
|
import handleCancelBooking from "@calcom/features/bookings/lib/handleCancelBooking";
|
|
import { checkRateLimitAndThrowError } from "@calcom/lib/checkRateLimitAndThrowError";
|
|
import getIP from "@calcom/lib/getIP";
|
|
import { piiHasher } from "@calcom/lib/server/PiiHasher";
|
|
import { bookingCancelWithCsrfSchema } from "@calcom/prisma/zod-utils";
|
|
import { validateCsrfToken } from "@calcom/web/lib/validateCsrfToken";
|
|
|
|
import { buildLegacyRequest } from "@lib/buildLegacyCtx";
|
|
|
|
async function handler(req: NextRequest) {
|
|
let appDirRequestBody;
|
|
try {
|
|
appDirRequestBody = await req.json();
|
|
} catch {
|
|
return NextResponse.json({ success: false, message: "Invalid JSON" }, { status: 400 });
|
|
}
|
|
const bookingData = bookingCancelWithCsrfSchema.parse(appDirRequestBody);
|
|
|
|
const csrfError = await validateCsrfToken(bookingData.csrfToken);
|
|
if (csrfError) {
|
|
return csrfError;
|
|
}
|
|
|
|
const session = await getServerSession({ req: buildLegacyRequest(await headers(), await cookies()) });
|
|
|
|
// Rate limit: 10 booking cancellations per 60 seconds per user (or IP if not authenticated)
|
|
const identifier = session?.user?.id
|
|
? `api:cancel-user:${session.user.id}`
|
|
: `api:cancel-ip:${piiHasher.hash(getIP(req))}`;
|
|
await checkRateLimitAndThrowError({
|
|
rateLimitingType: "core",
|
|
identifier,
|
|
});
|
|
|
|
const result = await handleCancelBooking({
|
|
bookingData,
|
|
userId: session?.user?.id || -1,
|
|
});
|
|
|
|
// const bookingCancelService = getBookingCancelService();
|
|
// const result = await bookingCancelService.cancelBooking({
|
|
// bookingData: bookingData,
|
|
// bookingMeta: {
|
|
// userId: session?.user?.id || -1,
|
|
// },
|
|
// });
|
|
|
|
const statusCode = result.success ? 200 : 400;
|
|
|
|
return NextResponse.json(result, { status: statusCode });
|
|
}
|
|
|
|
export const DELETE = defaultResponderForAppDir(handler);
|
|
export const POST = defaultResponderForAppDir(handler);
|