Skip to content

Scheduling infrastructure for absolutely everyone.

Fixing count and redundant checks for recurring (#5426)

Leo Giovanetti7d1fb7c6597a
diff --git a/apps/web/lib/mutations/bookings/create-recurring-booking.ts b/apps/web/lib/mutations/bookings/create-recurring-booking.ts
index b8df6c90f7f..2a51db8cd6e 100644
--- a/apps/web/lib/mutations/bookings/create-recurring-booking.ts
+++ b/apps/web/lib/mutations/bookings/create-recurring-booking.ts
@@ -3,10 +3,18 @@ import { AppsStatus } from "@calcom/types/Calendar";
import * as fetch from "@lib/core/http/fetch-wrapper";
import { BookingCreateBody, BookingResponse } from "@lib/types/booking";
-type ExtendedBookingCreateBody = BookingCreateBody & { noEmail?: boolean; recurringCount?: number };
+type ExtendedBookingCreateBody = BookingCreateBody & {
+ noEmail?: boolean;
+ recurringCount?: number;
+ appsStatus?: AppsStatus[] | undefined;
+ allRecurringDates?: string[];
+ currentRecurringIndex?: number;
+};
const createRecurringBooking = async (data: ExtendedBookingCreateBody[]) => {
const createdBookings: BookingResponse[] = [];
+ const allRecurringDates: string[] = data.map((booking) => booking.start);
+ let appsStatus: AppsStatus[] | undefined = undefined;
// Reversing to accumulate results for noEmail instances first, to then lastly, create the
// emailed booking taking into account accumulated results to send app status accurately
for (let key = 0; key < data.length; key++) {
@@ -23,22 +31,16 @@ const createRecurringBooking = async (data: ExtendedBookingCreateBody[]) => {
}
return prev;
}, {} as { [key: string]: AppsStatus });
- const appsStatus = Object.values(calcAppsStatus);
- const response = await fetch.post<
- ExtendedBookingCreateBody & { appsStatus: AppsStatus[] },
- BookingResponse
- >("/api/book/event", {
- ...booking,
- appsStatus,
- });
- createdBookings.push(response);
- } else {
- const response = await fetch.post<ExtendedBookingCreateBody, BookingResponse>("/api/book/event", {
- ...booking,
- noEmail: true,
- });
- createdBookings.push(response);
+ appsStatus = Object.values(calcAppsStatus);
}
+
+ const response = await fetch.post<ExtendedBookingCreateBody, BookingResponse>("/api/book/event", {
+ ...booking,
+ appsStatus,
+ allRecurringDates,
+ currentRecurringIndex: key,
+ });
+ createdBookings.push(response);
}
return createdBookings;
};
diff --git a/packages/features/bookings/lib/handleNewBooking.ts b/packages/features/bookings/lib/handleNewBooking.ts
index 19d4a82941b..e2ee7e1eb9e 100644
--- a/packages/features/bookings/lib/handleNewBooking.ts
+++ b/packages/features/bookings/lib/handleNewBooking.ts
@@ -9,7 +9,6 @@ import {
import async from "async";
import { cloneDeep } from "lodash";
import type { NextApiRequest } from "next";
-import { RRule } from "rrule";
import short from "short-uuid";
import { v5 as uuidv5 } from "uuid";
import z from "zod";
@@ -212,7 +211,11 @@ async function ensureAvailableUsers(
eventType: Awaited<ReturnType<typeof getEventTypesFromDB>> & {
users: User[];
},
- input: { dateFrom: string; dateTo: string }
+ input: { dateFrom: string; dateTo: string },
+ recurringDatesInfo?: {
+ allRecurringDates: string[] | undefined;
+ currentRecurringIndex: number | undefined;
+ }
) {
const availableUsers: typeof eventType.users = [];
/** Let's start checking for availability */
@@ -243,9 +246,12 @@ async function ensureAvailableUsers(
let foundConflict = false;
try {
- if (eventType.recurringEvent) {
- const recurringEvent = parseRecurringEvent(eventType.recurringEvent);
- const allBookingDates = new RRule({ dtstart: new Date(input.dateFrom), ...recurringEvent }).all();
+ if (
+ eventType.recurringEvent &&
+ recurringDatesInfo?.currentRecurringIndex === 0 &&
+ recurringDatesInfo.allRecurringDates
+ ) {
+ const allBookingDates = recurringDatesInfo.allRecurringDates.map((strDate) => new Date(strDate));
// Go through each date for the recurring event and check if each one's availability
// DONE: Decreased computational complexity from O(2^n) to O(n) by refactoring this loop to stop
// running at the first unavailable time.
@@ -277,6 +283,8 @@ async function handler(req: NextApiRequest & { userId?: number | undefined }) {
const {
recurringCount,
+ allRecurringDates,
+ currentRecurringIndex,
noEmail,
eventTypeSlug,
eventTypeId,
@@ -376,10 +384,20 @@ async function handler(req: NextApiRequest & { userId?: number | undefined }) {
{
...eventType,
users,
+ ...(eventType.recurringEvent && {
+ recurringEvent: {
+ ...eventType.recurringEvent,
+ count: recurringCount || eventType.recurringEvent.count,
+ },
+ }),
},
{
dateFrom: reqBody.start,
dateTo: reqBody.end,
+ },
+ {
+ allRecurringDates,
+ currentRecurringIndex,
}
);
diff --git a/packages/lib/CloseCom.ts b/packages/lib/CloseCom.ts
index 2bc7269f27c..1808d43a239 100644
--- a/packages/lib/CloseCom.ts
+++ b/packages/lib/CloseCom.ts
@@ -513,7 +513,6 @@ export default class CloseCom {
description: "Generic Lead for Contacts created by Cal.com",
}
): Promise<string> {
- debugger;
const closeComLeadNames = await this.lead.list({ query: { _fields: ["name", "id"] } });
const searchLeadFromCalCom = closeComLeadNames.data.filter((lead) => lead.name === leadInfo.companyName);
if (searchLeadFromCalCom.length === 0) {
diff --git a/packages/prisma/zod-utils.ts b/packages/prisma/zod-utils.ts
index fe4bd8ccc67..a041f5b0518 100644
--- a/packages/prisma/zod-utils.ts
+++ b/packages/prisma/zod-utils.ts
@@ -150,6 +150,8 @@ export const extendedBookingCreateBody = bookingCreateBodySchema.merge(
z.object({
noEmail: z.boolean().optional(),
recurringCount: z.number().optional(),
+ allRecurringDates: z.string().array().optional(),
+ currentRecurringIndex: z.number().optional(),
rescheduleReason: z.string().optional(),
smsReminderNumber: z.string().optional(),
appsStatus: z