feat: reset RR every month (#17472)
* only fetch bookings of this month for weighted rr * test set up * only get bookings of current month * reverts test setup * first changes for weight adjustments * reset booking count + adjust calibration * depreciate weightAdjustment * get only bookings created this month * fix typo * make sure createdAt for hosts is correct * use earliest possibel date as fallback * add missing createdAt date to tests * fix typo * clean up changes in tests * fix typo * change end date to current date * fix: Fall back to empty host array when no hosts are found * fix: Restructure code a little * fixed test, incorrectly used now outdated var * perf: remove Dayjs from getLuckyUser * Refactor getHostsWithCalibration for optimised performance, intentionally break test as findMany is always an array * Better mock for host.findMany * Remove team-event-types.test.ts, move to appropriate package * TypeScript cannot auto-infer that an array is non-empty when assigning to a var * fix: Type Fixes and DistributionMethod enum add * Optimise tests * Added test to show that bookings made before a newHost was added affect the lucky user result * Throw error when the usersWithHighestPriority is empty, which should never happen --------- Co-authored-by: CarinaWolli <[email protected]> Co-authored-by: Alex van Andel <[email protected]>
This commit is contained in:
26 files changed
+808
-876
No files matched your search
@@ -21,7 +21,6 @@ import type { TrpcSessionUser } from "../../../trpc";
|
||||
import { setDestinationCalendarHandler } from "../../loggedInViewer/setDestinationCalendar.handler";
|
||||
import type { TUpdateInputSchema } from "./update.schema";
|
||||
import {
|
||||
addWeightAdjustmentToNewHosts,
|
||||
ensureUniqueBookingFields,
|
||||
ensureEmailOrPhoneNumberIsPresent,
|
||||
handleCustomInputs,
|
||||
@@ -83,6 +82,14 @@ export const updateHandler = async ({ ctx, input }: UpdateOptions) => {
|
||||
select: {
|
||||
title: true,
|
||||
isRRWeightsEnabled: true,
|
||||
hosts: {
|
||||
select: {
|
||||
userId: true,
|
||||
priority: true,
|
||||
weight: true,
|
||||
isFixed: true,
|
||||
},
|
||||
},
|
||||
aiPhoneCallConfig: {
|
||||
select: {
|
||||
generalPrompt: true,
|
||||
@@ -276,25 +283,41 @@ export const updateHandler = async ({ ctx, input }: UpdateOptions) => {
|
||||
const isWeightsEnabled =
|
||||
isRRWeightsEnabled || (typeof isRRWeightsEnabled === "undefined" && eventType.isRRWeightsEnabled);
|
||||
|
||||
const hostsWithWeightAdjustment = await addWeightAdjustmentToNewHosts({
|
||||
hosts,
|
||||
isWeightsEnabled,
|
||||
eventTypeId: id,
|
||||
prisma: ctx.prisma,
|
||||
});
|
||||
const oldHostsSet = new Set(eventType.hosts.map((oldHost) => oldHost.userId));
|
||||
const newHostsSet = new Set(hosts.map((oldHost) => oldHost.userId));
|
||||
|
||||
const existingHosts = hosts.filter((newHost) => oldHostsSet.has(newHost.userId));
|
||||
const newHosts = hosts.filter((newHost) => !oldHostsSet.has(newHost.userId));
|
||||
const removedHosts = eventType.hosts.filter((oldHost) => !newHostsSet.has(oldHost.userId));
|
||||
|
||||
data.hosts = {
|
||||
deleteMany: {},
|
||||
create: hostsWithWeightAdjustment.map((host) => {
|
||||
const { ...rest } = host;
|
||||
deleteMany: {
|
||||
OR: removedHosts.map((host) => ({
|
||||
userId: host.userId,
|
||||
eventTypeId: id,
|
||||
})),
|
||||
},
|
||||
create: newHosts.map((host) => {
|
||||
return {
|
||||
...rest,
|
||||
...host,
|
||||
isFixed: data.schedulingType === SchedulingType.COLLECTIVE || host.isFixed,
|
||||
priority: host.priority ?? 2, // default to medium priority
|
||||
priority: host.priority ?? 2,
|
||||
weight: host.weight ?? 100,
|
||||
weightAdjustment: host.weightAdjustment,
|
||||
};
|
||||
}),
|
||||
update: existingHosts.map((host) => ({
|
||||
where: {
|
||||
userId_eventTypeId: {
|
||||
userId: host.userId,
|
||||
eventTypeId: id,
|
||||
},
|
||||
},
|
||||
data: {
|
||||
isFixed: data.schedulingType === SchedulingType.COLLECTIVE || host.isFixed,
|
||||
priority: host.priority ?? 2,
|
||||
weight: host.weight ?? 100,
|
||||
},
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
import { z } from "zod";
|
||||
|
||||
import { markdownToSafeHTML } from "@calcom/lib/markdownToSafeHTML";
|
||||
import { BookingRepository } from "@calcom/lib/server/repository/booking";
|
||||
import type { EventTypeRepository } from "@calcom/lib/server/repository/eventType";
|
||||
import { UserRepository } from "@calcom/lib/server/repository/user";
|
||||
import type { PrismaClient } from "@calcom/prisma";
|
||||
import { MembershipRole, PeriodType } from "@calcom/prisma/enums";
|
||||
import type { CustomInputSchema } from "@calcom/prisma/zod-utils";
|
||||
import { EventTypeMetaDataSchema } from "@calcom/prisma/zod-utils";
|
||||
@@ -197,122 +195,6 @@ type User = {
|
||||
email: string;
|
||||
};
|
||||
|
||||
export async function addWeightAdjustmentToNewHosts({
|
||||
hosts,
|
||||
isWeightsEnabled,
|
||||
eventTypeId,
|
||||
prisma,
|
||||
}: {
|
||||
hosts: Host[];
|
||||
isWeightsEnabled: boolean;
|
||||
eventTypeId: number;
|
||||
prisma: PrismaClient;
|
||||
}): Promise<(Host & { weightAdjustment?: number })[]> {
|
||||
if (!isWeightsEnabled) return hosts;
|
||||
|
||||
// to also have the user email to check for attendees
|
||||
const usersWithHostData = await prisma.user.findMany({
|
||||
where: {
|
||||
id: {
|
||||
in: hosts.map((host) => host.userId),
|
||||
},
|
||||
},
|
||||
select: {
|
||||
email: true,
|
||||
id: true,
|
||||
hosts: {
|
||||
where: {
|
||||
eventTypeId,
|
||||
},
|
||||
select: {
|
||||
isFixed: true,
|
||||
weightAdjustment: true,
|
||||
priority: true,
|
||||
weight: true,
|
||||
scheduleId: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const hostsWithUserData = usersWithHostData.map((user) => {
|
||||
// user.hosts[0] is the previous host data from the db
|
||||
// hostData is the new host data
|
||||
const hostData = hosts.find((host) => host.userId === user.id);
|
||||
return {
|
||||
isNewRRHost: !hostData?.isFixed && (!user.hosts.length || user.hosts[0].isFixed),
|
||||
isFixed: hostData?.isFixed ?? false,
|
||||
weightAdjustment: hostData?.isFixed ? 0 : user.hosts[0]?.weightAdjustment ?? 0,
|
||||
priority: hostData?.priority ?? 2,
|
||||
weight: hostData?.weight ?? 100,
|
||||
user: {
|
||||
id: user.id,
|
||||
email: user.email,
|
||||
},
|
||||
scheduleId: hostData?.scheduleId ?? null,
|
||||
};
|
||||
});
|
||||
|
||||
const ongoingRRHosts = hostsWithUserData.filter((host) => !host.isFixed && !host.isNewRRHost);
|
||||
const allRRHosts = hosts.filter((host) => !host.isFixed);
|
||||
|
||||
if (ongoingRRHosts.length === allRRHosts.length) {
|
||||
//no new RR host was added
|
||||
return hostsWithUserData.map((host) => ({
|
||||
userId: host.user.id,
|
||||
isFixed: host.isFixed,
|
||||
priority: host.priority,
|
||||
weight: host.weight,
|
||||
weightAdjustment: host.weightAdjustment,
|
||||
scheduleId: host.scheduleId,
|
||||
}));
|
||||
}
|
||||
|
||||
const ongoingHostBookings = await BookingRepository.getAllBookingsForRoundRobin({
|
||||
eventTypeId,
|
||||
users: ongoingRRHosts.map((host) => {
|
||||
return { id: host.user.id, email: host.user.email };
|
||||
}),
|
||||
});
|
||||
|
||||
const { ongoingHostsWeightAdjustment, ongoingHostsWeights } = ongoingRRHosts.reduce(
|
||||
(acc, host) => {
|
||||
acc.ongoingHostsWeightAdjustment += host.weightAdjustment ?? 0;
|
||||
acc.ongoingHostsWeights += host.weight ?? 0;
|
||||
return acc;
|
||||
},
|
||||
{ ongoingHostsWeightAdjustment: 0, ongoingHostsWeights: 0 }
|
||||
);
|
||||
|
||||
const hostsWithWeightAdjustments = await Promise.all(
|
||||
hostsWithUserData.map(async (host) => {
|
||||
let weightAdjustment = !host.isFixed ? host.weightAdjustment : 0;
|
||||
if (host.isNewRRHost) {
|
||||
// host can already have bookings, if they ever was assigned before
|
||||
const existingBookings = await BookingRepository.getAllBookingsForRoundRobin({
|
||||
eventTypeId,
|
||||
users: [{ id: host.user.id, email: host.user.email }],
|
||||
});
|
||||
|
||||
const proportionalNrOfBookings =
|
||||
((ongoingHostBookings.length + ongoingHostsWeightAdjustment) / ongoingHostsWeights) * host.weight;
|
||||
weightAdjustment = proportionalNrOfBookings - existingBookings.length;
|
||||
}
|
||||
|
||||
return {
|
||||
userId: host.user.id,
|
||||
isFixed: host.isFixed,
|
||||
priority: host.priority,
|
||||
weight: host.weight,
|
||||
weightAdjustment: weightAdjustment > 0 ? Math.floor(weightAdjustment) : 0,
|
||||
scheduleId: host.scheduleId,
|
||||
};
|
||||
})
|
||||
);
|
||||
|
||||
return hostsWithWeightAdjustments;
|
||||
}
|
||||
|
||||
export const mapEventType = async (eventType: EventType) => ({
|
||||
...eventType,
|
||||
safeDescription: eventType?.description ? markdownToSafeHTML(eventType.description) : undefined,
|
||||
|
||||
Reference in new issue
Block a user