From e4abc8538224563daf2385e8f83f025fa14627c0 Mon Sep 17 00:00:00 2001 From: Hariom Balhara Date: Sat, 11 Nov 2023 01:36:52 +0530 Subject: [PATCH] fix: Fix recurring booking failure due to rate limiting (#12322) Co-authored-by: Keith Williams --- apps/api/pages/api/bookings/_post.ts | 4 ++-- apps/web/pages/api/book/event.ts | 9 +++++++++ apps/web/pages/api/book/recurring-event.ts | 8 ++++++++ packages/features/bookings/lib/handleNewBooking.ts | 8 -------- 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/apps/api/pages/api/bookings/_post.ts b/apps/api/pages/api/bookings/_post.ts index aae99a031b..062d3683d5 100644 --- a/apps/api/pages/api/bookings/_post.ts +++ b/apps/api/pages/api/bookings/_post.ts @@ -205,8 +205,8 @@ import { defaultResponder } from "@calcom/lib/server"; async function handler(req: NextApiRequest) { const { userId, isAdmin } = req; if (isAdmin) req.userId = req.body.userId || userId; - const booking = await handleNewBooking(req); - return booking; + + return await handleNewBooking(req); } export default defaultResponder(handler); diff --git a/apps/web/pages/api/book/event.ts b/apps/web/pages/api/book/event.ts index 79ac39a20c..1ca14e3a6b 100644 --- a/apps/web/pages/api/book/event.ts +++ b/apps/web/pages/api/book/event.ts @@ -2,9 +2,18 @@ import type { NextApiRequest, NextApiResponse } from "next"; import { getServerSession } from "@calcom/features/auth/lib/getServerSession"; import handleNewBooking from "@calcom/features/bookings/lib/handleNewBooking"; +import { checkRateLimitAndThrowError } from "@calcom/lib/checkRateLimitAndThrowError"; +import getIP from "@calcom/lib/getIP"; import { defaultResponder } from "@calcom/lib/server"; async function handler(req: NextApiRequest & { userId?: number }, res: NextApiResponse) { + const userIp = getIP(req); + + await checkRateLimitAndThrowError({ + rateLimitingType: "core", + identifier: userIp, + }); + const session = await getServerSession({ req, res }); /* To mimic API behavior and comply with types */ req.userId = session?.user?.id || -1; diff --git a/apps/web/pages/api/book/recurring-event.ts b/apps/web/pages/api/book/recurring-event.ts index b1242cce1d..3d1566411a 100644 --- a/apps/web/pages/api/book/recurring-event.ts +++ b/apps/web/pages/api/book/recurring-event.ts @@ -3,12 +3,20 @@ import type { NextApiRequest, NextApiResponse } from "next"; import { getServerSession } from "@calcom/features/auth/lib/getServerSession"; import handleNewBooking from "@calcom/features/bookings/lib/handleNewBooking"; import type { BookingResponse, RecurringBookingCreateBody } from "@calcom/features/bookings/types"; +import { checkRateLimitAndThrowError } from "@calcom/lib/checkRateLimitAndThrowError"; +import getIP from "@calcom/lib/getIP"; import { defaultResponder } from "@calcom/lib/server"; import type { AppsStatus } from "@calcom/types/Calendar"; // @TODO: Didn't look at the contents of this function in order to not break old booking page. async function handler(req: NextApiRequest & { userId?: number }, res: NextApiResponse) { + const userIp = getIP(req); + + await checkRateLimitAndThrowError({ + rateLimitingType: "core", + identifier: userIp, + }); const data: RecurringBookingCreateBody[] = req.body; const session = await getServerSession({ req, res }); const createdBookings: BookingResponse[] = []; diff --git a/packages/features/bookings/lib/handleNewBooking.ts b/packages/features/bookings/lib/handleNewBooking.ts index 2537730da8..1513acd4f7 100644 --- a/packages/features/bookings/lib/handleNewBooking.ts +++ b/packages/features/bookings/lib/handleNewBooking.ts @@ -51,10 +51,8 @@ import getWebhooks from "@calcom/features/webhooks/lib/getWebhooks"; import { cancelScheduledJobs, scheduleTrigger } from "@calcom/features/webhooks/lib/scheduleTrigger"; import { isPrismaObjOrUndefined, parseRecurringEvent } from "@calcom/lib"; import { getVideoCallUrlFromCalEvent } from "@calcom/lib/CalEventParser"; -import { checkRateLimitAndThrowError } from "@calcom/lib/checkRateLimitAndThrowError"; import { getDefaultEvent, getUsernameList } from "@calcom/lib/defaultEvents"; import { getErrorFromUnknown } from "@calcom/lib/errors"; -import getIP from "@calcom/lib/getIP"; import getPaymentAppData from "@calcom/lib/getPaymentAppData"; import { getTeamIdFromEventType } from "@calcom/lib/getTeamIdFromEventType"; import { HttpError } from "@calcom/lib/http-error"; @@ -629,12 +627,6 @@ async function handler( } ) { const { userId } = req; - const userIp = getIP(req); - - await checkRateLimitAndThrowError({ - rateLimitingType: "core", - identifier: userIp, - }); // handle dynamic user let eventType =