Scheduling infrastructure for absolutely everyone.
getBusyTimes consolidation
zomarsecb3264b5261
diff --git a/apps/web/lib/getBusyTimes.ts b/apps/web/lib/getBusyTimes.tsnew file mode 100644index 00000000000..b6e81d07ce1--- /dev/null+++ b/apps/web/lib/getBusyTimes.ts@@ -0,0 +1,58 @@+import { Credential, SelectedCalendar } from "@prisma/client";++import { getBusyCalendarTimes } from "@calcom/core/CalendarManager";+import { getBusyVideoTimes } from "@calcom/core/videoClient";+import notEmpty from "@calcom/lib/notEmpty";+import type { EventBusyDate } from "@calcom/types/Calendar";++import prisma from "@lib/prisma";++async function getBusyTimes(params: {+ credentials: Credential[];+ userId: number;+ eventTypeId?: number;+ startTime: string;+ endTime: string;+ selectedCalendars: SelectedCalendar[];+}) {+ const { credentials, userId, eventTypeId, startTime, endTime, selectedCalendars } = params;+ const busyTimes: EventBusyDate[] = await prisma.booking+ .findMany({+ where: {+ AND: [+ {+ userId,+ eventTypeId,+ startTime: { gte: new Date(startTime) },+ endTime: { lte: new Date(endTime) },+ },+ {+ OR: [+ {+ status: "ACCEPTED",+ },+ {+ status: "PENDING",+ },+ ],+ },+ ],+ },+ select: {+ startTime: true,+ endTime: true,+ },+ })+ .then((bookings) => bookings.map((booking) => ({ end: booking.endTime, start: booking.startTime })));++ if (credentials) {+ const calendarBusyTimes = await getBusyCalendarTimes(credentials, startTime, endTime, selectedCalendars);+ busyTimes.push(...calendarBusyTimes);+ const videoBusyTimes = (await getBusyVideoTimes(credentials)).filter(notEmpty);+ busyTimes.push(...videoBusyTimes);+ }++ return busyTimes;+}++export default getBusyTimes;diff --git a/apps/web/lib/queries/availability/index.ts b/apps/web/lib/queries/availability/index.tsindex 19a8eeabaaa..a3c88c0deca 100644--- a/apps/web/lib/queries/availability/index.ts+++ b/apps/web/lib/queries/availability/index.ts@@ -1,10 +1,9 @@import { Prisma } from "@prisma/client";import dayjs from "dayjs";-import { getBusyCalendarTimes } from "@calcom/core/CalendarManager";-import { asStringOrNull } from "@lib/asStringOrNull";import { getWorkingHours } from "@lib/availability";+import getBusyTimes from "@lib/getBusyTimes";import prisma from "@lib/prisma";export async function getUserAvailability(query: {@@ -60,12 +59,14 @@ export async function getUserAvailability(query: {const { selectedCalendars, ...currentUser } = rawUser;- const busyTimes = await getBusyCalendarTimes(- currentUser.credentials,- dateFrom.format(),- dateTo.format(),- selectedCalendars- );+ const busyTimes = await getBusyTimes({+ credentials: currentUser.credentials,+ startTime: dateFrom.format(),+ endTime: dateTo.format(),+ eventTypeId: query.eventTypeId,+ userId: currentUser.id,+ selectedCalendars,+ });const bufferedBusyTimes = busyTimes.map((a) => ({start: dayjs(a.start).subtract(currentUser.bufferTime, "minute").toString(),diff --git a/apps/web/pages/api/availability/[user].ts b/apps/web/pages/api/availability/[user].tsindex 96ce33f5efe..6e45678cceb 100644--- a/apps/web/pages/api/availability/[user].ts+++ b/apps/web/pages/api/availability/[user].ts@@ -1,14 +1,12 @@-// import { getBusyVideoTimes } from "@calcom/core/videoClient";import { Prisma } from "@prisma/client";import dayjs from "dayjs";import timezone from "dayjs/plugin/timezone";import utc from "dayjs/plugin/utc";import type { NextApiRequest, NextApiResponse } from "next";-import { getBusyCalendarTimes } from "@calcom/core/CalendarManager";-import { asStringOrNull } from "@lib/asStringOrNull";import { getWorkingHours } from "@lib/availability";+import getBusyTimes from "@lib/getBusyTimes";import prisma from "@lib/prisma";dayjs.extend(utc);@@ -77,14 +75,14 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)const { selectedCalendars, ...currentUser } = rawUser;- const busyTimes = await getBusyCalendarTimes(- currentUser.credentials,- dateFrom.format(),- dateTo.format(),- selectedCalendars- );-- // busyTimes.push(...await getBusyVideoTimes(currentUser.credentials, dateFrom.format(), dateTo.format()));+ const busyTimes = await getBusyTimes({+ credentials: currentUser.credentials,+ startTime: dateFrom.format(),+ endTime: dateTo.format(),+ eventTypeId,+ userId: currentUser.id,+ selectedCalendars,+ });const bufferedBusyTimes = busyTimes.map((a) => ({start: dayjs(a.start).subtract(currentUser.bufferTime, "minute"),diff --git a/apps/web/pages/api/book/event.ts b/apps/web/pages/api/book/event.tsindex 52bf93f912a..1c5b3d41d40 100644--- a/apps/web/pages/api/book/event.ts+++ b/apps/web/pages/api/book/event.ts@@ -10,20 +10,12 @@ import rrule from "rrule";import short from "short-uuid";import { v5 as uuidv5 } from "uuid";-import { getBusyCalendarTimes } from "@calcom/core/CalendarManager";import EventManager from "@calcom/core/EventManager";-import { getBusyVideoTimes } from "@calcom/core/videoClient";import { getDefaultEvent, getGroupName, getUsernameList } from "@calcom/lib/defaultEvents";import { getErrorFromUnknown } from "@calcom/lib/errors";import logger from "@calcom/lib/logger";-import notEmpty from "@calcom/lib/notEmpty";import type { BufferedBusyTime } from "@calcom/types/BufferedBusyTime";-import type {- AdditionInformation,- CalendarEvent,- EventBusyDate,- RecurringEvent,-} from "@calcom/types/Calendar";+import type { AdditionInformation, CalendarEvent, RecurringEvent } from "@calcom/types/Calendar";import type { EventResult, PartialReference } from "@calcom/types/EventManager";import { handlePayment } from "@ee/lib/stripe/server";@@ -35,6 +27,7 @@ import {} from "@lib/emails/email-manager";import { ensureArray } from "@lib/ensureArray";import { getEventName } from "@lib/event";+import getBusyTimes from "@lib/getBusyTimes";import prisma from "@lib/prisma";import { BookingCreateBody } from "@lib/types/booking";import sendPayload from "@lib/webhooks/sendPayload";@@ -546,43 +539,18 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)},});- const credentials = currentUser.credentials;-- const calendarBusyTimes: EventBusyDate[] = await prisma.booking- .findMany({- where: {- AND: [- {- userId: currentUser.id,- eventTypeId: eventTypeId,- },- {- OR: [- {- status: "ACCEPTED",- },- {- status: "PENDING",- },- ],- },- ],- },- })- .then((bookings) => bookings.map((booking) => ({ end: booking.endTime, start: booking.startTime })));-- if (credentials) {- await getBusyCalendarTimes(credentials, reqBody.start, reqBody.end, selectedCalendars).then(- (busyTimes) => calendarBusyTimes.push(...busyTimes)- );-- const videoBusyTimes = (await getBusyVideoTimes(credentials)).filter(notEmpty);- calendarBusyTimes.push(...videoBusyTimes);- }+ const busyTimes = await getBusyTimes({+ credentials: currentUser.credentials,+ startTime: reqBody.start,+ endTime: reqBody.end,+ eventTypeId,+ userId: currentUser.id,+ selectedCalendars,+ });- console.log("calendarBusyTimes==>>>", calendarBusyTimes);+ console.log("calendarBusyTimes==>>>", busyTimes);- const bufferedBusyTimes: BufferedBusyTimes = calendarBusyTimes.map((a) => ({+ const bufferedBusyTimes = busyTimes.map((a) => ({start: dayjs(a.start).subtract(currentUser.bufferTime, "minute").toString(),end: dayjs(a.end).add(currentUser.bufferTime, "minute").toString(),}));