Same-day schedules created invalid workingHours (#3742)

* Same-day schedules created invalid workingHours

* Uncomment logger

* Previous version did not properly substract the days

Co-authored-by: Leo Giovanetti <hello@leog.me>
pull/3740/head^2
Alex van Andel 2022-08-08 21:17:33 +01:00 committed by GitHub
parent 7776bbbf52
commit 87d4afea13
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 1 deletions

View File

@ -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++;

View File

@ -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;