Skip to content

Commit

Permalink
Moved afterBufferTime to busyTimes & heavily simplify busy check (cal…
Browse files Browse the repository at this point in the history
  • Loading branch information
emrysal authored Jul 11, 2022
1 parent adffb00 commit c9d5af6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 26 deletions.
38 changes: 14 additions & 24 deletions apps/web/server/routers/viewer/slots.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { SchedulingType } from "@prisma/client";
import dayjs, { Dayjs } from "dayjs";
import { z } from "zod";

import type { CurrentSeats } from "@calcom/core/getUserAvailability";
import { getUserAvailability } from "@calcom/core/getUserAvailability";
import dayjs, { Dayjs } from "@calcom/dayjs";
import logger from "@calcom/lib/logger";
import { availabilityUserSelect } from "@calcom/prisma";
import { TimeRange } from "@calcom/types/schedule";
Expand Down Expand Up @@ -45,28 +45,35 @@ const checkForAvailability = ({
busy,
eventLength,
beforeBufferTime,
afterBufferTime,
currentSeats,
}: {
time: Dayjs;
busy: (TimeRange | { start: string; end: string })[];
eventLength: number;
beforeBufferTime: number;
afterBufferTime: number;
currentSeats?: CurrentSeats;
}) => {
if (currentSeats?.some((booking) => booking.startTime.toISOString() === time.toISOString())) {
return true;
}

const slotEndTime = time.add(eventLength, "minutes").utc();
const slotStartTimeWithBeforeBuffer = time.subtract(beforeBufferTime, "minutes").utc();
const slotEndTimeWithAfterBuffer = time.add(eventLength + afterBufferTime, "minutes").utc();
const slotStartTime = time.subtract(beforeBufferTime, "minutes").utc();

return busy.every((busyTime) => {
const startTime = dayjs.utc(busyTime.start);
const endTime = dayjs.utc(busyTime.end);

if (endTime.isBefore(slotStartTime) || startTime.isAfter(slotEndTime)) {
return true;
}

if (slotStartTime.isBetween(startTime, endTime, null, "[)")) {
return false;
} else if (slotEndTime.isBetween(startTime, endTime, null, "(]")) {
return false;
}

// Check if start times are the same
if (time.utc().isBetween(startTime, endTime, null, "[)")) {
return false;
Expand All @@ -79,24 +86,6 @@ const checkForAvailability = ({
else if (startTime.isBetween(time, slotEndTime)) {
return false;
}
// Check if timeslot has before buffer time space free
else if (
slotStartTimeWithBeforeBuffer.isBetween(
startTime.subtract(beforeBufferTime, "minutes"),
endTime.add(afterBufferTime, "minutes")
)
) {
return false;
}
// Check if timeslot has after buffer time space free
else if (
slotEndTimeWithAfterBuffer.isBetween(
startTime.subtract(beforeBufferTime, "minutes"),
endTime.add(afterBufferTime, "minutes")
)
) {
return false;
}

return true;
});
Expand Down Expand Up @@ -179,6 +168,7 @@ export const slotsRouter = createRouter().query("getSchedule", {
dateFrom: startTime.format(),
dateTo: endTime.format(),
eventTypeId: input.eventTypeId,
afterEventBuffer: eventType.afterEventBuffer,
},
{ user: currentUser, eventType, currentSeats }
);
Expand All @@ -197,7 +187,6 @@ export const slotsRouter = createRouter().query("getSchedule", {
const availabilityCheckProps = {
eventLength: eventType.length,
beforeBufferTime: eventType.beforeEventBuffer,
afterBufferTime: eventType.afterEventBuffer,
currentSeats,
};
const isWithinBounds = (_time: Parameters<typeof isOutOfBounds>[0]) =>
Expand Down Expand Up @@ -232,6 +221,7 @@ export const slotsRouter = createRouter().query("getSchedule", {
!eventType.schedulingType || eventType.schedulingType === SchedulingType.COLLECTIVE
? ("every" as const)
: ("some" as const);

const filteredTimes = times.filter(isWithinBounds).filter((time) =>
userSchedules[filterStrategy]((schedule) => {
const startCheckForAvailability = performance.now();
Expand Down
9 changes: 7 additions & 2 deletions packages/core/getUserAvailability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const availabilitySchema = z
timezone: z.string().optional(),
username: z.string().optional(),
userId: z.number().optional(),
afterEventBuffer: z.number().optional(),
})
.refine((data) => !!data.username || !!data.userId, "Either username or userId should be filled in.");

Expand Down Expand Up @@ -84,14 +85,16 @@ export async function getUserAvailability(
dateTo: string;
eventTypeId?: number;
timezone?: string;
afterEventBuffer?: number;
},
initialData?: {
user?: User;
eventType?: EventType;
currentSeats?: CurrentSeats;
}
) {
const { username, userId, dateFrom, dateTo, eventTypeId, timezone } = availabilitySchema.parse(query);
const { username, userId, dateFrom, dateTo, eventTypeId, timezone, afterEventBuffer } =
availabilitySchema.parse(query);

if (!dateFrom.isValid() || !dateTo.isValid())
throw new HttpError({ statusCode: 400, message: "Invalid time range given." });
Expand Down Expand Up @@ -126,7 +129,9 @@ export async function getUserAvailability(

const bufferedBusyTimes = busyTimes.map((a) => ({
start: dayjs(a.start).subtract(currentUser.bufferTime, "minute").toISOString(),
end: dayjs(a.end).add(currentUser.bufferTime, "minute").toISOString(),
end: dayjs(a.end)
.add(currentUser.bufferTime + (afterEventBuffer || 0), "minute")
.toISOString(),
}));

const schedule = eventType?.schedule
Expand Down

0 comments on commit c9d5af6

Please sign in to comment.