Skip to content

Scheduling infrastructure for absolutely everyone.

Same-day schedules created invalid workingHours (#3742)

Alex van Andel87d4afea130a
diff --git a/packages/trpc/server/routers/viewer/slots.tsx b/packages/trpc/server/routers/viewer/slots.tsx
index 0a18e96b9e2..ddbbe6bc267 100644
--- a/packages/trpc/server/routers/viewer/slots.tsx
+++ b/packages/trpc/server/routers/viewer/slots.tsx
@@ -9,6 +9,7 @@ import logger from "@calcom/lib/logger";
import getSlots from "@calcom/lib/slots";
import prisma, { availabilityUserSelect } from "@calcom/prisma";
import { TimeRange } from "@calcom/types/schedule";
+import { ValuesType } from "@calcom/types/utils";
import { TRPCError } from "@trpc/server";
@@ -203,7 +204,37 @@ export async function getSchedule(
})
);
- const workingHours = userSchedules?.flatMap((s) => s.workingHours);
+ // flatMap does not work for COLLECTIVE events
+ const workingHours = userSchedules?.reduce(
+ (currentValue: ValuesType<typeof userSchedules>["workingHours"], s) => {
+ // Collective needs to be exclusive of overlap throughout - others inclusive.
+ if (eventType.schedulingType === SchedulingType.COLLECTIVE) {
+ // taking the first item as a base
+ if (!currentValue.length) {
+ currentValue.push(...s.workingHours);
+ return currentValue;
+ }
+ // the remaining logic subtracts
+ return s.workingHours.reduce((compare, workingHour) => {
+ return compare.map((c) => {
+ const intersect = workingHour.days.filter((day) => c.days.includes(day));
+ return intersect.length
+ ? {
+ days: intersect,
+ startTime: Math.max(workingHour.startTime, c.startTime),
+ endTime: Math.min(workingHour.endTime, c.endTime),
+ }
+ : c;
+ });
+ }, currentValue);
+ } else {
+ // flatMap for ROUND_ROBIN and individuals
+ currentValue.push(...s.workingHours);
+ }
+ return currentValue;
+ },
+ []
+ );
const slots: Record<string, Slot[]> = {};
const availabilityCheckProps = {
@@ -225,6 +256,7 @@ export async function getSchedule(
let checkForAvailabilityTime = 0;
let getSlotsCount = 0;
let checkForAvailabilityCount = 0;
+
do {
const startGetSlots = performance.now();
// get slots retrieves the available times for a given day
@@ -235,6 +267,7 @@ export async function getSchedule(
minimumBookingNotice: eventType.minimumBookingNotice,
frequency: eventType.slotInterval || eventType.length,
});
+
const endGetSlots = performance.now();
getSlotsTime += endGetSlots - startGetSlots;
getSlotsCount++;
diff --git a/packages/types/utils.d.ts b/packages/types/utils.d.ts
index a55cb60d6d7..f599b1b5b25 100644
--- a/packages/types/utils.d.ts
+++ b/packages/types/utils.d.ts
@@ -5,3 +5,13 @@ export type Ensure<T, K extends keyof T> = Omit<T, K> & {
/** Makes selected props from a record optional */
export type Optional<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>;
+
+/** Get the union type of all the values in an object, array or array-like type `T` */
+export type ValuesType<T extends ReadonlyArray<unknown> | ArrayLike<unknown> | Record<unknown, unknown>> =
+ T extends ReadonlyArray<unknown>
+ ? T[number]
+ : T extends ArrayLike<unknown>
+ ? T[number]
+ : T extends object
+ ? T[keyof T]
+ : never;