Files
calendar/apps/web/lib/getBusyTimes.ts
T
Omar LópezGitHubkodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
12d66cb9df Booking confirm endpoint refactoring (#2949)
* Adds new default handler and responder

* Moved confirm endpoint

* Fixes availability for unconfirmed bookings

* Cleanup

* Update _patch.ts

* Prevent too much diffs

* Adds missing BookingStatus

* Migrates confirmed & rejected to status

* Adds requiresConfirmation icon to listing

* Adds booking status migration

* Adds migrations to remove confirmed/rejected

* Undo refactor

* Sets the organizer as "accepted" in gCal

* Update getBusyTimes.ts

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
2022-06-06 10:54:47 -06:00

48 lines
1.4 KiB
TypeScript

import { BookingStatus, 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: {
userId,
eventTypeId,
startTime: { gte: new Date(startTime) },
endTime: { lte: new Date(endTime) },
status: {
in: [BookingStatus.ACCEPTED],
},
},
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;