feat: Implements maxLeadThreshold (#17643)

* feat: Implements maxLeadThreshold which puts a maximum to the amount of lead a given host is allowed to be ahead

* Refactor getLuckyUser + implement in handleNewBooking + move logic to repo

* Decoupled @prisma from findQualifiedHosts

* Fix deep type error

* Add maxLeadThreshold to builder.ts

* Removed a console.log :O + fix handleChildrenEventTypes input

* Add distribution UI to enable balanced distribution/maximize availability

* 0-1-2 instead of 0-1-2-3 (3 bookings instead of 4)
This commit is contained in:
Alex van Andel authored and GitHub committed 2024-11-15 15:54:02 +00:00
1 parent c6aa57177a
commit f76696032e
26 files changed
+626 -123

No files matched your search

@@ -13,6 +13,7 @@ import dayjs from "@calcom/dayjs";
import { getSlugOrRequestedSlug, orgDomainConfig } from "@calcom/ee/organizations/lib/orgDomains";
import { isEventTypeLoggingEnabled } from "@calcom/features/bookings/lib/isEventTypeLoggingEnabled";
import { parseBookingLimit, parseDurationLimit } from "@calcom/lib";
import { findQualifiedHosts } from "@calcom/lib/bookings/findQualifiedHosts";
import { getRoutedHostsWithContactOwnerAndFixedHosts } from "@calcom/lib/bookings/getRoutedUsers";
import { RESERVED_SUBDOMAINS } from "@calcom/lib/constants";
import { getUTCOffsetByTimezone } from "@calcom/lib/date-fns";
@@ -180,6 +181,7 @@ export async function getEventType(
rescheduleWithSameRoundRobinHost: true,
periodDays: true,
metadata: true,
maxLeadThreshold: true,
team: {
select: {
id: true,
@@ -223,6 +225,7 @@ export async function getEventType(
hosts: {
select: {
isFixed: true,
createdAt: true,
user: {
select: {
credentials: { select: credentialForCalendarServiceSelect },
@@ -390,12 +393,15 @@ export function getUsersWithCredentialsConsideringContactOwner({
return allHosts;
}
const contactOwnerAndFixedHosts = hosts.reduce((usersArray, host) => {
if (host.isFixed || host.user.email === contactOwnerEmail)
usersArray.push({ ...host.user, isFixed: host.isFixed });
const contactOwnerAndFixedHosts = hosts.reduce(
(usersArray: (GetAvailabilityUser & { isFixed?: boolean })[], host) => {
if (host.isFixed || host.user.email === contactOwnerEmail)
usersArray.push({ ...host.user, isFixed: host.isFixed });
return usersArray;
}, [] as (GetAvailabilityUser & { isFixed?: boolean })[]);
return usersArray;
},
[]
);
return contactOwnerAndFixedHosts;
}
@@ -467,24 +473,7 @@ async function _getAvailableSlots({ input, ctx }: GetScheduleOptions): Promise<I
throw new TRPCError({ message: "Invalid time range given.", code: "BAD_REQUEST" });
}
const eventHosts: {
isFixed: boolean;
email: string;
user: (typeof eventType.hosts)[number]["user"];
}[] =
eventType.hosts?.length && eventType.schedulingType
? eventType.hosts.map((host) => ({
isFixed: host.isFixed,
email: host.user.email,
user: host.user,
}))
: eventType.users.map((user) => {
return {
isFixed: !eventType.schedulingType || eventType.schedulingType === SchedulingType.COLLECTIVE,
email: user.email,
user: user,
};
});
const eventHosts = await monitorCallbackAsync(findQualifiedHosts<GetAvailabilityUser>, eventType);
const contactOwnerEmailFromInput = input.teamMemberEmail ?? null;
const skipContactOwner = input.skipContactOwner;