From ecb3264b5261eceda62ad95d9de1fe6654790027 Mon Sep 17 00:00:00 2001 From: zomars Date: Wed, 11 May 2022 19:49:21 -0600 Subject: [PATCH] getBusyTimes consolidation --- apps/web/lib/getBusyTimes.ts | 58 ++++++++++++++++++++++ apps/web/lib/queries/availability/index.ts | 17 ++++--- apps/web/pages/api/availability/[user].ts | 20 ++++---- apps/web/pages/api/book/event.ts | 56 +++++---------------- 4 files changed, 88 insertions(+), 63 deletions(-) create mode 100644 apps/web/lib/getBusyTimes.ts diff --git a/apps/web/lib/getBusyTimes.ts b/apps/web/lib/getBusyTimes.ts new file mode 100644 index 00000000..b6e81d07 --- /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.ts index 19a8eeab..a3c88c0d 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].ts index 96ce33f5..6e45678c 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.ts index 52bf93f9..1c5b3d41 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 busyTimes = await getBusyTimes({ + credentials: currentUser.credentials, + startTime: reqBody.start, + endTime: reqBody.end, + eventTypeId, + userId: currentUser.id, + selectedCalendars, + }); - 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 }))); + console.log("calendarBusyTimes==>>>", busyTimes); - 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); - } - - console.log("calendarBusyTimes==>>>", calendarBusyTimes); - - 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(), }));