Refactored buildSlots (#6389)
* Refactored buildSlots * Added typing for computedAvailableSlots * Adds very simple boundary logic to make sure slots start at the right times * Logic was cutting off too early * Woops, should be eventLength, not frequency * Expect 12:15 slots as availability is till 12:30 for booking * Deliberately skip test for testing purposes * slotInterval overrides intervals, but not duration * Fix calendar mocking * Another 45m length * Unskip :( * Fix test Co-authored-by: Hariom Balhara <[email protected]> Co-authored-by: Omar López <[email protected]>
This commit is contained in:
7 files changed
+179
-209
No files matched your search
@@ -265,7 +265,6 @@ export async function getSchedule(input: z.infer<typeof getScheduleSchema>, ctx:
|
||||
);
|
||||
const workingHours = getAggregateWorkingHours(userAvailability, eventType.schedulingType);
|
||||
|
||||
const computedAvailableSlots: Record<string, Slot[]> = {};
|
||||
const availabilityCheckProps = {
|
||||
eventLength: eventType.length,
|
||||
currentSeats,
|
||||
@@ -280,67 +279,80 @@ export async function getSchedule(input: z.infer<typeof getScheduleSchema>, ctx:
|
||||
periodDays: eventType.periodDays,
|
||||
});
|
||||
|
||||
let currentCheckedTime = startTime;
|
||||
let getSlotsTime = 0;
|
||||
const getSlotsTime = 0;
|
||||
let checkForAvailabilityTime = 0;
|
||||
let getSlotsCount = 0;
|
||||
const getSlotsCount = 0;
|
||||
let checkForAvailabilityCount = 0;
|
||||
do {
|
||||
const startGetSlots = performance.now();
|
||||
|
||||
const timeSlots: ReturnType<typeof getTimeSlots> = [];
|
||||
|
||||
for (
|
||||
let currentCheckedTime = startTime;
|
||||
currentCheckedTime.isBefore(endTime);
|
||||
currentCheckedTime = currentCheckedTime.add(1, "day")
|
||||
) {
|
||||
// get slots retrieves the available times for a given day
|
||||
const timeSlots = getTimeSlots({
|
||||
inviteeDate: currentCheckedTime,
|
||||
eventLength: input.duration || eventType.length,
|
||||
workingHours,
|
||||
dateOverrides,
|
||||
minimumBookingNotice: eventType.minimumBookingNotice,
|
||||
frequency: eventType.slotInterval || input.duration || eventType.length,
|
||||
});
|
||||
timeSlots.push(
|
||||
...getTimeSlots({
|
||||
inviteeDate: currentCheckedTime,
|
||||
eventLength: input.duration || eventType.length,
|
||||
workingHours,
|
||||
dateOverrides,
|
||||
minimumBookingNotice: eventType.minimumBookingNotice,
|
||||
frequency: eventType.slotInterval || input.duration || eventType.length,
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
const endGetSlots = performance.now();
|
||||
getSlotsTime += endGetSlots - startGetSlots;
|
||||
getSlotsCount++;
|
||||
const isCollective = !eventType.schedulingType || eventType.schedulingType === SchedulingType.COLLECTIVE;
|
||||
|
||||
const isCollective = !eventType.schedulingType || eventType.schedulingType === SchedulingType.COLLECTIVE;
|
||||
let availableTimeSlots: typeof timeSlots = [];
|
||||
if (isCollective) {
|
||||
availableTimeSlots = timeSlots.filter((slot) =>
|
||||
userAvailability.every((schedule) => {
|
||||
const startCheckForAvailability = performance.now();
|
||||
const isAvailable = checkIfIsAvailable({
|
||||
time: slot.time,
|
||||
...schedule,
|
||||
...availabilityCheckProps,
|
||||
});
|
||||
const endCheckForAvailability = performance.now();
|
||||
checkForAvailabilityCount++;
|
||||
checkForAvailabilityTime += endCheckForAvailability - startCheckForAvailability;
|
||||
return isAvailable;
|
||||
})
|
||||
);
|
||||
} else {
|
||||
availableTimeSlots = timeSlots
|
||||
.map((slot) => {
|
||||
slot.userIds = slot.userIds?.filter((slotUserId) => {
|
||||
const userSchedule = userAvailability.find(({ userId }) => userId === slotUserId);
|
||||
if (!userSchedule) {
|
||||
throw new TRPCError({
|
||||
message: "Shouldn't happen that we don't have a matching user schedule here",
|
||||
code: "INTERNAL_SERVER_ERROR",
|
||||
});
|
||||
}
|
||||
return checkIfIsAvailable({
|
||||
time: slot.time,
|
||||
...userSchedule,
|
||||
...availabilityCheckProps,
|
||||
});
|
||||
});
|
||||
return slot;
|
||||
})
|
||||
.filter((slot) => slot.userIds && slot.userIds.length > 0);
|
||||
}
|
||||
|
||||
const availableTimeSlots = timeSlots
|
||||
.filter((slot) => isTimeWithinBounds(slot.time))
|
||||
.filter((slot) =>
|
||||
isCollective
|
||||
? // The slot should be available for every user
|
||||
userAvailability.every((schedule) => {
|
||||
const startCheckForAvailability = performance.now();
|
||||
const isAvailable = checkIfIsAvailable({
|
||||
time: slot.time,
|
||||
...schedule,
|
||||
...availabilityCheckProps,
|
||||
});
|
||||
const endCheckForAvailability = performance.now();
|
||||
checkForAvailabilityCount++;
|
||||
checkForAvailabilityTime += endCheckForAvailability - startCheckForAvailability;
|
||||
return isAvailable;
|
||||
})
|
||||
: (() => {
|
||||
// The slot should be available for the atleast one of the slot owners.
|
||||
return slot.userIds?.some((slotUserId) => {
|
||||
const userSchedule = userAvailability.find(({ userId }) => userId === slotUserId);
|
||||
if (!userSchedule) {
|
||||
throw new TRPCError({
|
||||
message: "Shouldn't happen that we don't have a matching user schedule here",
|
||||
code: "INTERNAL_SERVER_ERROR",
|
||||
});
|
||||
}
|
||||
return checkIfIsAvailable({
|
||||
time: slot.time,
|
||||
...userSchedule,
|
||||
...availabilityCheckProps,
|
||||
});
|
||||
});
|
||||
})()
|
||||
);
|
||||
availableTimeSlots = availableTimeSlots.filter((slot) => isTimeWithinBounds(slot.time));
|
||||
|
||||
computedAvailableSlots[currentCheckedTime.format("YYYY-MM-DD")] = availableTimeSlots.map(
|
||||
({ time: time, ...passThroughProps }) => ({
|
||||
const computedAvailableSlots = availableTimeSlots.reduce(
|
||||
(
|
||||
r: Record<string, { time: string; users: string[]; attendees?: number; bookingUid?: string }[]>,
|
||||
{ time: time, ...passThroughProps }
|
||||
) => {
|
||||
r[time.format("YYYY-MM-DD")] = r[time.format("YYYY-MM-DD")] || [];
|
||||
r[time.format("YYYY-MM-DD")].push({
|
||||
...passThroughProps,
|
||||
time: time.toISOString(),
|
||||
users: eventType.users.map((user) => user.username || ""),
|
||||
@@ -355,10 +367,11 @@ export async function getSchedule(input: z.infer<typeof getScheduleSchema>, ctx:
|
||||
currentSeats.findIndex((booking) => booking.startTime.toISOString() === time.toISOString())
|
||||
].uid,
|
||||
}),
|
||||
})
|
||||
);
|
||||
currentCheckedTime = currentCheckedTime.add(1, "day");
|
||||
} while (currentCheckedTime.isBefore(endTime));
|
||||
});
|
||||
return r;
|
||||
},
|
||||
Object.create(null)
|
||||
);
|
||||
|
||||
logger.debug(`getSlots took ${getSlotsTime}ms and executed ${getSlotsCount} times`);
|
||||
|
||||
|
||||
Reference in new issue
Block a user