fix: recurring round robin events (#13471)

* use same lucky user + check availability

* add tests for recurring round robin events

* only use luckyUsers for recurring bookings

* don't check availability for all recurring dates

* code clean up

* pass schedulingType

* fix type error

* add availability check for fixed hosts + test

* fix type error

* implement feedback

---------

Co-authored-by: CarinaWolli <[email protected]>
Co-authored-by: Udit Takkar <[email protected]>
This commit is contained in:
authored and GitHub committed 2024-02-06 18:18:42 +00:00
1 parent 684f4a795c
commit 271f31730d
9 files changed
+1271 -547

No files matched your search

+30 -1
View File
@@ -6,6 +6,7 @@ import type { BookingResponse, RecurringBookingCreateBody } from "@calcom/featur
import { checkRateLimitAndThrowError } from "@calcom/lib/checkRateLimitAndThrowError";
import getIP from "@calcom/lib/getIP";
import { defaultResponder } from "@calcom/lib/server";
import { SchedulingType } from "@calcom/prisma/client";
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.
@@ -30,7 +31,34 @@ async function handler(req: NextApiRequest & { userId?: number }, res: NextApiRe
const numSlotsToCheckForAvailability = 2;
let thirdPartyRecurringEventId = null;
for (let key = 0; key < data.length; key++) {
// for round robin, the first slot needs to be handled first to define the lucky user
const firstBooking = data[0];
const isRoundRobin = firstBooking.schedulingType === SchedulingType.ROUND_ROBIN;
let luckyUsers = undefined;
if (isRoundRobin) {
const recurringEventReq: NextApiRequest & { userId?: number } = req;
recurringEventReq.body = {
...firstBooking,
appsStatus,
allRecurringDates,
isFirstRecurringSlot: true,
thirdPartyRecurringEventId,
numSlotsToCheckForAvailability,
currentRecurringIndex: 0,
noEmail: false,
};
const firstBookingResult = await handleNewBooking(recurringEventReq, {
isNotAnApiCall: true,
});
luckyUsers = firstBookingResult.luckyUsers?.map((user) => user.id);
}
for (let key = isRoundRobin ? 1 : 0; key < data.length; key++) {
const booking = data[key];
// Disable AppStatus in Recurring Booking Email as it requires us to iterate backwards to be able to compute the AppsStatus for all the bookings except the very first slot and then send that slot's email with statuses
// It is also doubtful that how useful is to have the AppsStatus of all the bookings in the email.
@@ -61,6 +89,7 @@ async function handler(req: NextApiRequest & { userId?: number }, res: NextApiRe
numSlotsToCheckForAvailability,
currentRecurringIndex: key,
noEmail: key !== 0,
luckyUsers,
};
const promiseEachRecurringBooking: ReturnType<typeof handleNewBooking> = handleNewBooking(
+2 -2
View File
@@ -798,10 +798,10 @@ describe("getSchedule", () => {
schedulingType: "COLLECTIVE",
hosts: [
{
id: 101,
userId: 101,
},
{
id: 102,
userId: 102,
},
],
},
@@ -49,11 +49,15 @@ type InputWorkflow = {
action: WorkflowActions;
template: WorkflowTemplates;
};
type InputHost = {
userId: number;
isFixed?: boolean;
};
/**
* Data to be mocked
*/
export type ScenarioData = {
// hosts: { id: number; eventTypeId?: number; userId?: number; isFixed?: boolean }[];
/**
* Prisma would return these eventTypes
*/
@@ -119,7 +123,7 @@ export type InputEventType = {
* These user ids are `ScenarioData["users"]["id"]`
*/
users?: { id: number }[];
hosts?: { id: number }[];
hosts?: InputHost[];
schedulingType?: SchedulingType;
beforeEventBuffer?: number;
afterEventBuffer?: number;
@@ -161,6 +165,20 @@ export const Timezones = {
"+6:00": "Asia/Dhaka",
};
async function addHostsToDb(eventTypes: InputEventType[]) {
for (const eventType of eventTypes) {
if (eventType.hosts && eventType.hosts.length > 0) {
await prismock.host.createMany({
data: eventType.hosts.map((host) => ({
userId: host.userId,
eventTypeId: eventType.id,
isFixed: host.isFixed ?? false,
})),
});
}
}
}
async function addEventTypesToDb(
eventTypes: (Omit<
Prisma.EventTypeCreateInput,
@@ -283,6 +301,7 @@ async function addEventTypes(eventTypes: InputEventType[], usersStore: InputUser
}
: eventType.schedule,
owner: eventType.owner ? { connect: { id: eventType.owner } } : undefined,
schedulingType: eventType.schedulingType,
};
});
log.silly("TestData: Creating EventType", JSON.stringify(eventTypesWithUsers));
@@ -586,6 +605,7 @@ export async function createBookingScenario(data: ScenarioData) {
);
}
const eventTypes = await addEventTypes(data.eventTypes, data.users);
await addHostsToDb(data.eventTypes);
data.bookings = data.bookings || [];
// allowSuccessfulBookingCreation();
@@ -963,8 +983,7 @@ export function getScenarioData(
webhooks,
workflows,
bookings,
}: // hosts = [],
{
}: {
organizer: ReturnType<typeof getOrganizer>;
eventTypes: ScenarioData["eventTypes"];
apps?: ScenarioData["apps"];
@@ -972,7 +991,6 @@ export function getScenarioData(
webhooks?: ScenarioData["webhooks"];
workflows?: ScenarioData["workflows"];
bookings?: ScenarioData["bookings"];
// hosts?: ScenarioData["hosts"];
},
org?: { id: number | null } | undefined | null
) {
@@ -1003,7 +1021,6 @@ export function getScenarioData(
}
});
return {
// hosts: [...hosts],
eventTypes: eventTypes.map((eventType, index) => {
return {
...eventType,
@@ -1,3 +1,4 @@
import type { SchedulingType } from "@calcom/prisma/client";
import { getDate } from "@calcom/web/test/utils/bookingScenario/bookingScenario";
export const DEFAULT_TIMEZONE_BOOKER = "Asia/Kolkata";
@@ -24,6 +25,7 @@ export function getMockRequestDataForBooking({
bookingUid?: string;
recurringEventId?: string;
recurringCount?: number;
schedulingType?: SchedulingType;
responses: {
email: string;
name: string;