2022-06-10 18:38:46 +00:00
|
|
|
import { Prisma } from "@prisma/client";
|
|
|
|
import { z } from "zod";
|
|
|
|
|
2022-07-07 15:26:22 +00:00
|
|
|
import dayjs, { Dayjs } from "@calcom/dayjs";
|
2022-06-10 18:38:46 +00:00
|
|
|
import { getWorkingHours } from "@calcom/lib/availability";
|
|
|
|
import { HttpError } from "@calcom/lib/http-error";
|
2022-07-07 15:26:22 +00:00
|
|
|
import logger from "@calcom/lib/logger";
|
2022-06-10 18:38:46 +00:00
|
|
|
import prisma, { availabilityUserSelect } from "@calcom/prisma";
|
|
|
|
import { stringToDayjs } from "@calcom/prisma/zod-utils";
|
|
|
|
|
|
|
|
import { getBusyTimes } from "./getBusyTimes";
|
|
|
|
|
|
|
|
const availabilitySchema = z
|
|
|
|
.object({
|
|
|
|
dateFrom: stringToDayjs,
|
|
|
|
dateTo: stringToDayjs,
|
|
|
|
eventTypeId: z.number().optional(),
|
|
|
|
timezone: z.string().optional(),
|
|
|
|
username: z.string().optional(),
|
|
|
|
userId: z.number().optional(),
|
2022-07-11 11:00:08 +00:00
|
|
|
afterEventBuffer: z.number().optional(),
|
2022-06-10 18:38:46 +00:00
|
|
|
})
|
|
|
|
.refine((data) => !!data.username || !!data.userId, "Either username or userId should be filled in.");
|
|
|
|
|
|
|
|
const getEventType = (id: number) =>
|
|
|
|
prisma.eventType.findUnique({
|
|
|
|
where: { id },
|
|
|
|
select: {
|
2022-06-15 20:54:31 +00:00
|
|
|
id: true,
|
2022-06-10 18:38:46 +00:00
|
|
|
seatsPerTimeSlot: true,
|
|
|
|
timeZone: true,
|
|
|
|
schedule: {
|
|
|
|
select: {
|
|
|
|
availability: true,
|
|
|
|
timeZone: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
availability: {
|
|
|
|
select: {
|
|
|
|
startTime: true,
|
|
|
|
endTime: true,
|
|
|
|
days: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
type EventType = Awaited<ReturnType<typeof getEventType>>;
|
|
|
|
|
|
|
|
const getUser = (where: Prisma.UserWhereUniqueInput) =>
|
|
|
|
prisma.user.findUnique({
|
|
|
|
where,
|
|
|
|
select: availabilityUserSelect,
|
|
|
|
});
|
|
|
|
|
|
|
|
type User = Awaited<ReturnType<typeof getUser>>;
|
|
|
|
|
2022-06-15 20:54:31 +00:00
|
|
|
export const getCurrentSeats = (eventTypeId: number, dateFrom: Dayjs, dateTo: Dayjs) =>
|
|
|
|
prisma.booking.findMany({
|
|
|
|
where: {
|
|
|
|
eventTypeId,
|
|
|
|
startTime: {
|
|
|
|
gte: dateFrom.format(),
|
|
|
|
lte: dateTo.format(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
uid: true,
|
|
|
|
startTime: true,
|
|
|
|
_count: {
|
|
|
|
select: {
|
|
|
|
attendees: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
export type CurrentSeats = Awaited<ReturnType<typeof getCurrentSeats>>;
|
|
|
|
|
2022-06-10 18:38:46 +00:00
|
|
|
export async function getUserAvailability(
|
2022-06-14 21:23:18 +00:00
|
|
|
query: {
|
|
|
|
username?: string;
|
|
|
|
userId?: number;
|
2022-06-10 18:38:46 +00:00
|
|
|
dateFrom: string;
|
|
|
|
dateTo: string;
|
|
|
|
eventTypeId?: number;
|
|
|
|
timezone?: string;
|
2022-07-11 11:00:08 +00:00
|
|
|
afterEventBuffer?: number;
|
2022-06-10 18:38:46 +00:00
|
|
|
},
|
|
|
|
initialData?: {
|
|
|
|
user?: User;
|
|
|
|
eventType?: EventType;
|
2022-06-15 20:54:31 +00:00
|
|
|
currentSeats?: CurrentSeats;
|
2022-06-10 18:38:46 +00:00
|
|
|
}
|
|
|
|
) {
|
2022-07-11 11:00:08 +00:00
|
|
|
const { username, userId, dateFrom, dateTo, eventTypeId, timezone, afterEventBuffer } =
|
|
|
|
availabilitySchema.parse(query);
|
2022-06-10 18:38:46 +00:00
|
|
|
|
|
|
|
if (!dateFrom.isValid() || !dateTo.isValid())
|
|
|
|
throw new HttpError({ statusCode: 400, message: "Invalid time range given." });
|
|
|
|
|
|
|
|
const where: Prisma.UserWhereUniqueInput = {};
|
|
|
|
if (username) where.username = username;
|
|
|
|
if (userId) where.id = userId;
|
|
|
|
|
|
|
|
let user: User | null = initialData?.user || null;
|
|
|
|
if (!user) user = await getUser(where);
|
|
|
|
if (!user) throw new HttpError({ statusCode: 404, message: "No user found" });
|
|
|
|
|
|
|
|
let eventType: EventType | null = initialData?.eventType || null;
|
|
|
|
if (!eventType && eventTypeId) eventType = await getEventType(eventTypeId);
|
|
|
|
|
2022-06-15 20:54:31 +00:00
|
|
|
/* Current logic is if a booking is in a time slot mark it as busy, but seats can have more than one attendee so grab
|
|
|
|
current bookings with a seats event type and display them on the calendar, even if they are full */
|
|
|
|
let currentSeats: CurrentSeats | null = initialData?.currentSeats || null;
|
|
|
|
if (!currentSeats && eventType?.seatsPerTimeSlot)
|
|
|
|
currentSeats = await getCurrentSeats(eventType.id, dateFrom, dateTo);
|
|
|
|
|
2022-06-10 18:38:46 +00:00
|
|
|
const { selectedCalendars, ...currentUser } = user;
|
|
|
|
|
|
|
|
const busyTimes = await getBusyTimes({
|
|
|
|
credentials: currentUser.credentials,
|
|
|
|
startTime: dateFrom.toISOString(),
|
|
|
|
endTime: dateTo.toISOString(),
|
|
|
|
eventTypeId,
|
|
|
|
userId: currentUser.id,
|
|
|
|
selectedCalendars,
|
|
|
|
});
|
|
|
|
|
|
|
|
const bufferedBusyTimes = busyTimes.map((a) => ({
|
|
|
|
start: dayjs(a.start).subtract(currentUser.bufferTime, "minute").toISOString(),
|
2022-07-11 11:00:08 +00:00
|
|
|
end: dayjs(a.end)
|
|
|
|
.add(currentUser.bufferTime + (afterEventBuffer || 0), "minute")
|
|
|
|
.toISOString(),
|
2022-06-10 18:38:46 +00:00
|
|
|
}));
|
|
|
|
|
|
|
|
const schedule = eventType?.schedule
|
|
|
|
? { ...eventType?.schedule }
|
|
|
|
: {
|
|
|
|
...currentUser.schedules.filter(
|
|
|
|
(schedule) => !currentUser.defaultScheduleId || schedule.id === currentUser.defaultScheduleId
|
|
|
|
)[0],
|
|
|
|
};
|
|
|
|
|
2022-06-15 20:54:31 +00:00
|
|
|
const timeZone = timezone || schedule?.timeZone || eventType?.timeZone || currentUser.timeZone;
|
2022-07-07 15:26:22 +00:00
|
|
|
const startGetWorkingHours = performance.now();
|
2022-06-10 18:38:46 +00:00
|
|
|
const workingHours = getWorkingHours(
|
|
|
|
{ timeZone },
|
|
|
|
schedule.availability ||
|
|
|
|
(eventType?.availability.length ? eventType.availability : currentUser.availability)
|
|
|
|
);
|
2022-07-07 15:26:22 +00:00
|
|
|
const endGetWorkingHours = performance.now();
|
|
|
|
logger.debug(`getWorkingHours took ${endGetWorkingHours - startGetWorkingHours}ms for userId ${userId}`);
|
2022-06-10 18:38:46 +00:00
|
|
|
return {
|
|
|
|
busy: bufferedBusyTimes,
|
|
|
|
timeZone,
|
|
|
|
workingHours,
|
|
|
|
currentSeats,
|
|
|
|
};
|
|
|
|
}
|