feat: Round Robin Weights (#15558)
* WIP * add frontend * backend to update weight * UI improvements * WIP weight algorithm * enable weights switch + algorithm improvements * fix weightDescription * clean up code * remove OOOEntryHost from schema.prisma * implement logic (not yet tested) * add tests for weight algorithm * add test with weight adjustment * finish unit tests * fix type error * fix type error * add migration * fix type error * fix event type update handler * fix failing test * UI fixes for saving hosts * make sure weightAdjustment is not lost on host changes * fix weightadjustment for new hosts * add weightAdjustment to availableUsers * fix type errors * fix default value for weight * make weight and weightAdjustment optional * fix type errors from schema changes * type fix * clean up code * improve comments * remove comment * clean up code * add tests & weight adjustment improvments * better variable naming * fixes for weight adjustments * make weightAdjustments proportional to weights * fix previous host weight adjustments * improved tests for weight adjustments * save weight and priority + sort hosts correctly * fix type error * code clean up * remove console.log * use BookingRepository to fetch bookings of users * use BookingRepository to fetch bookings in getLuckyUser * fix type errors * fix weightAdjustment if changed from fixed to rr host * disable weights when 'assign all' is enabled * typo * allow 0 weight * set min (and max) for weight and priority * use useWatch * code clean up * fix type error * only count accepted bookings for RR * fix type error * improve data fetching of bookings * only filter bookings of availableUsers * code clean up form feedback * fix tests * don't count no show bookings * code clean up * choose user with highest weight * use one reduce instead of two * use reduce instead of filter and map * don't show weights toggle when 'assign all' is enabled * design fixes * fix type errors * fix: type check * Update packages/features/eventtypes/components/AddMembersWithSwitch.tsx --------- Co-authored-by: CarinaWolli <[email protected]> Co-authored-by: Alex van Andel <[email protected]>
This commit is contained in:
29 files changed
+1249
-218
No files matched your search
@@ -11,6 +11,7 @@ import AddMembersWithSwitch, {
|
||||
} from "@calcom/features/eventtypes/components/AddMembersWithSwitch";
|
||||
import AssignAllTeamMembers from "@calcom/features/eventtypes/components/AssignAllTeamMembers";
|
||||
import ChildrenEventTypeSelect from "@calcom/features/eventtypes/components/ChildrenEventTypeSelect";
|
||||
import { sortHosts, weightDescription } from "@calcom/features/eventtypes/components/HostEditDialogs";
|
||||
import type { FormValues, TeamMember } from "@calcom/features/eventtypes/lib/types";
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
import { SchedulingType } from "@calcom/prisma/enums";
|
||||
@@ -134,6 +135,8 @@ const FixedHosts = ({
|
||||
isFixed: true,
|
||||
userId: parseInt(teamMember.value, 10),
|
||||
priority: 2,
|
||||
weight: 100,
|
||||
weightAdjustment: 0,
|
||||
})),
|
||||
{ shouldDirty: true }
|
||||
)
|
||||
@@ -175,6 +178,8 @@ const FixedHosts = ({
|
||||
isFixed: true,
|
||||
userId: parseInt(teamMember.value, 10),
|
||||
priority: 2,
|
||||
weight: 100,
|
||||
weightAdjustment: 0,
|
||||
})),
|
||||
{ shouldDirty: true }
|
||||
)
|
||||
@@ -202,7 +207,12 @@ const RoundRobinHosts = ({
|
||||
}) => {
|
||||
const { t } = useLocale();
|
||||
|
||||
const { setValue } = useFormContext<FormValues>();
|
||||
const { setValue, getValues, control } = useFormContext<FormValues>();
|
||||
|
||||
const isRRWeightsEnabled = useWatch({
|
||||
control,
|
||||
name: "isRRWeightsEnabled",
|
||||
});
|
||||
|
||||
return (
|
||||
<div className="rounded-lg ">
|
||||
@@ -210,7 +220,26 @@ const RoundRobinHosts = ({
|
||||
<Label className="mb-1 text-sm font-semibold">{t("round_robin_hosts")}</Label>
|
||||
<p className="text-subtle max-w-full break-words text-sm leading-tight">{t("round_robin_helper")}</p>
|
||||
</div>
|
||||
<div className="border-subtle rounded-b-md border border-t-0">
|
||||
<div className="border-subtle rounded-b-md border border-t-0 px-6 pt-4">
|
||||
{!assignAllTeamMembers && (
|
||||
<Controller<FormValues>
|
||||
name="isRRWeightsEnabled"
|
||||
render={({ field: { value, onChange } }) => (
|
||||
<SettingsToggle
|
||||
title={t("enable_weights")}
|
||||
description={weightDescription}
|
||||
checked={value}
|
||||
onCheckedChange={(active) => {
|
||||
onChange(active);
|
||||
|
||||
const rrHosts = getValues("hosts").filter((host) => !host.isFixed);
|
||||
const sortedRRHosts = rrHosts.sort((a, b) => sortHosts(a, b, active));
|
||||
setValue("hosts", sortedRRHosts);
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
)}
|
||||
<AddMembersWithSwitch
|
||||
teamMembers={teamMembers}
|
||||
value={value}
|
||||
@@ -218,20 +247,23 @@ const RoundRobinHosts = ({
|
||||
assignAllTeamMembers={assignAllTeamMembers}
|
||||
setAssignAllTeamMembers={setAssignAllTeamMembers}
|
||||
automaticAddAllEnabled={true}
|
||||
isRRWeightsEnabled={isRRWeightsEnabled}
|
||||
isFixed={false}
|
||||
onActive={() =>
|
||||
containerClassName={assignAllTeamMembers ? "-mt-4" : ""}
|
||||
onActive={() => {
|
||||
setValue(
|
||||
"hosts",
|
||||
teamMembers
|
||||
.map((teamMember) => ({
|
||||
isFixed: false,
|
||||
userId: parseInt(teamMember.value, 10),
|
||||
priority: 2,
|
||||
}))
|
||||
.sort((a, b) => b.priority - a.priority),
|
||||
teamMembers.map((teamMember) => ({
|
||||
isFixed: false,
|
||||
userId: parseInt(teamMember.value, 10),
|
||||
priority: 2,
|
||||
weight: 100,
|
||||
weightAdjustment: 0,
|
||||
})),
|
||||
{ shouldDirty: true }
|
||||
)
|
||||
}
|
||||
);
|
||||
setValue("isRRWeightsEnabled", false);
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
@@ -340,11 +372,8 @@ const Hosts = ({
|
||||
teamMembers={teamMembers}
|
||||
value={value}
|
||||
onChange={(changeValue) => {
|
||||
onChange(
|
||||
[...value.filter((host: Host) => host.isFixed), ...changeValue].sort(
|
||||
(a, b) => b.priority - a.priority
|
||||
)
|
||||
);
|
||||
const hosts = [...value.filter((host: Host) => host.isFixed), ...changeValue];
|
||||
onChange(hosts);
|
||||
}}
|
||||
assignAllTeamMembers={assignAllTeamMembers}
|
||||
setAssignAllTeamMembers={setAssignAllTeamMembers}
|
||||
|
||||
@@ -21,6 +21,7 @@ import {
|
||||
} from "@calcom/features/ee/cal-ai-phone/promptTemplates";
|
||||
import type { Workflow } from "@calcom/features/ee/workflows/lib/types";
|
||||
import type { ChildrenEventType } from "@calcom/features/eventtypes/components/ChildrenEventTypeSelect";
|
||||
import { sortHosts } from "@calcom/features/eventtypes/components/HostEditDialogs";
|
||||
import type { FormValues } from "@calcom/features/eventtypes/lib/types";
|
||||
import { validateIntervalLimitOrder } from "@calcom/lib";
|
||||
import { WEBSITE_URL } from "@calcom/lib/constants";
|
||||
@@ -89,7 +90,13 @@ const ManagedEventTypeDialog = dynamic(() => import("@components/eventtype/Manag
|
||||
|
||||
const AssignmentWarningDialog = dynamic(() => import("@components/eventtype/AssignmentWarningDialog"));
|
||||
|
||||
export type Host = { isFixed: boolean; userId: number; priority: number };
|
||||
export type Host = {
|
||||
isFixed: boolean;
|
||||
userId: number;
|
||||
priority: number;
|
||||
weight: number;
|
||||
weightAdjustment: number;
|
||||
};
|
||||
|
||||
export type CustomInputParsed = typeof customInputSchema._output;
|
||||
|
||||
@@ -297,7 +304,7 @@ const EventTypePage = (props: EventTypeSetupProps & { allActiveWorkflows?: Workf
|
||||
slotInterval: eventType.slotInterval,
|
||||
minimumBookingNotice: eventType.minimumBookingNotice,
|
||||
metadata: eventType.metadata,
|
||||
hosts: eventType.hosts,
|
||||
hosts: eventType.hosts.sort((a, b) => sortHosts(a, b, eventType.isRRWeightsEnabled)),
|
||||
successRedirectUrl: eventType.successRedirectUrl || "",
|
||||
forwardParamsSuccessRedirect: eventType.forwardParamsSuccessRedirect,
|
||||
users: eventType.users,
|
||||
@@ -330,6 +337,7 @@ const EventTypePage = (props: EventTypeSetupProps & { allActiveWorkflows?: Workf
|
||||
templateType: eventType.aiPhoneCallConfig?.templateType ?? "CUSTOM_TEMPLATE",
|
||||
schedulerName: eventType.aiPhoneCallConfig?.schedulerName,
|
||||
},
|
||||
isRRWeightsEnabled: eventType.isRRWeightsEnabled,
|
||||
};
|
||||
}, [eventType, periodDates]);
|
||||
const formMethods = useForm<FormValues>({
|
||||
@@ -399,6 +407,7 @@ const EventTypePage = (props: EventTypeSetupProps & { allActiveWorkflows?: Workf
|
||||
return () => {
|
||||
router.events.off("routeChangeStart", handleRouteChange);
|
||||
};
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [router, eventType.hosts, eventType.children, eventType.assignAllTeamMembers]);
|
||||
|
||||
const appsMetadata = formMethods.getValues("metadata")?.apps;
|
||||
|
||||
@@ -2363,7 +2363,11 @@
|
||||
"Highest": "highest",
|
||||
"send_booker_to": "Send Booker to",
|
||||
"set_priority": "Set Priority",
|
||||
"set_weight": "Set Weight",
|
||||
"enable_weights": "Enable Weights",
|
||||
"priority_for_user": "Priority for {{userName}}",
|
||||
"weights_description": "Weights determine how meetings are distributed among hosts. <1>Learn more</1>",
|
||||
"weight_for_user": "Weight for {{userName}}",
|
||||
"change_priority": "change priority",
|
||||
"field_identifiers_as_variables": "Use field identifiers as variables for your custom event redirect",
|
||||
"field_identifiers_as_variables_with_example": "Use field identifiers as variables for your custom event redirect (e.g. {{variable}})",
|
||||
|
||||
@@ -8,7 +8,7 @@ describe("Tests to Check if Event Types have empty Assignment", () => {
|
||||
checkForEmptyAssignment({
|
||||
assignedUsers: [],
|
||||
assignAllTeamMembers: false,
|
||||
hosts: [{ userId: 101, isFixed: false, priority: 2 }],
|
||||
hosts: [{ userId: 101, isFixed: false, priority: 2, weight: 100, weightAdjustment: 0 }],
|
||||
isManagedEventType: true,
|
||||
})
|
||||
).toBe(true);
|
||||
@@ -61,7 +61,7 @@ describe("Tests to Check if Event Types have empty Assignment", () => {
|
||||
checkForEmptyAssignment({
|
||||
assignedUsers: [],
|
||||
assignAllTeamMembers: false,
|
||||
hosts: [{ userId: 101, isFixed: false, priority: 2 }],
|
||||
hosts: [{ userId: 101, isFixed: false, priority: 2, weight: 100, weightAdjustment: 0 }],
|
||||
isManagedEventType: false,
|
||||
})
|
||||
).toBe(false);
|
||||
|
||||
@@ -1,16 +1,17 @@
|
||||
import prismaMock from "../../../../tests/libs/__mocks__/prismaMock";
|
||||
|
||||
import { expect, it } from "vitest";
|
||||
import { expect, it, describe } from "vitest";
|
||||
|
||||
import { getLuckyUser } from "@calcom/lib/server";
|
||||
import { buildUser } from "@calcom/lib/test/builder";
|
||||
import { buildUser, buildBooking } from "@calcom/lib/test/builder";
|
||||
import { addWeightAdjustmentToNewHosts } from "@calcom/trpc/server/routers/viewer/eventTypes/util";
|
||||
|
||||
it("can find lucky user with maximize availability", async () => {
|
||||
const user1 = buildUser({
|
||||
id: 1,
|
||||
username: "test1",
|
||||
name: "Test User 1",
|
||||
email: "[email protected]",
|
||||
email: "test1@example.com",
|
||||
bookings: [
|
||||
{
|
||||
createdAt: new Date("2022-01-25T05:30:00.000Z"),
|
||||
@@ -24,7 +25,7 @@ it("can find lucky user with maximize availability", async () => {
|
||||
id: 2,
|
||||
username: "test2",
|
||||
name: "Test User 2",
|
||||
email: "tes2[email protected]",
|
||||
email: "test2@example.com",
|
||||
bookings: [
|
||||
{
|
||||
createdAt: new Date("2022-01-25T04:30:00.000Z"),
|
||||
@@ -39,7 +40,11 @@ it("can find lucky user with maximize availability", async () => {
|
||||
await expect(
|
||||
getLuckyUser("MAXIMIZE_AVAILABILITY", {
|
||||
availableUsers: users,
|
||||
eventTypeId: 1,
|
||||
eventType: {
|
||||
id: 1,
|
||||
isRRWeightsEnabled: false,
|
||||
},
|
||||
allRRHosts: [],
|
||||
})
|
||||
).resolves.toStrictEqual(users[1]);
|
||||
});
|
||||
@@ -49,7 +54,7 @@ it("can find lucky user with maximize availability and priority ranking", async
|
||||
id: 1,
|
||||
username: "test1",
|
||||
name: "Test User 1",
|
||||
email: "[email protected]",
|
||||
email: "test1@example.com",
|
||||
priority: 2,
|
||||
bookings: [
|
||||
{
|
||||
@@ -64,7 +69,7 @@ it("can find lucky user with maximize availability and priority ranking", async
|
||||
id: 2,
|
||||
username: "test2",
|
||||
name: "Test User 2",
|
||||
email: "tes2[email protected]",
|
||||
email: "test2@example.com",
|
||||
bookings: [
|
||||
{
|
||||
createdAt: new Date("2022-01-25T04:30:00.000Z"),
|
||||
@@ -76,16 +81,16 @@ it("can find lucky user with maximize availability and priority ranking", async
|
||||
// TODO: we may be able to use native prisma generics somehow?
|
||||
prismaMock.user.findMany.mockResolvedValue(users);
|
||||
prismaMock.booking.findMany.mockResolvedValue([]);
|
||||
const test = await getLuckyUser("MAXIMIZE_AVAILABILITY", {
|
||||
availableUsers: users,
|
||||
eventTypeId: 1,
|
||||
});
|
||||
|
||||
// both users have medium priority (one user has no priority set, default to medium) so pick least recently booked
|
||||
await expect(
|
||||
getLuckyUser("MAXIMIZE_AVAILABILITY", {
|
||||
availableUsers: users,
|
||||
eventTypeId: 1,
|
||||
eventType: {
|
||||
id: 1,
|
||||
isRRWeightsEnabled: false,
|
||||
},
|
||||
allRRHosts: [],
|
||||
})
|
||||
).resolves.toStrictEqual(users[1]);
|
||||
|
||||
@@ -93,7 +98,7 @@ it("can find lucky user with maximize availability and priority ranking", async
|
||||
id: 1,
|
||||
username: "test1",
|
||||
name: "Test User 1",
|
||||
email: "[email protected]",
|
||||
email: "test1@example.com",
|
||||
priority: 0,
|
||||
bookings: [
|
||||
{
|
||||
@@ -105,7 +110,7 @@ it("can find lucky user with maximize availability and priority ranking", async
|
||||
id: 2,
|
||||
username: "test2",
|
||||
name: "Test User 2",
|
||||
email: "tes2[email protected]",
|
||||
email: "test2@example.com",
|
||||
priority: 2,
|
||||
bookings: [
|
||||
{
|
||||
@@ -118,7 +123,7 @@ it("can find lucky user with maximize availability and priority ranking", async
|
||||
id: 2,
|
||||
username: "test2",
|
||||
name: "Test User 2",
|
||||
email: "tes2[email protected]",
|
||||
email: "test2@example.com",
|
||||
priority: 4,
|
||||
bookings: [
|
||||
{
|
||||
@@ -136,7 +141,11 @@ it("can find lucky user with maximize availability and priority ranking", async
|
||||
await expect(
|
||||
getLuckyUser("MAXIMIZE_AVAILABILITY", {
|
||||
availableUsers: usersWithPriorities,
|
||||
eventTypeId: 1,
|
||||
eventType: {
|
||||
id: 1,
|
||||
isRRWeightsEnabled: false,
|
||||
},
|
||||
allRRHosts: [],
|
||||
})
|
||||
).resolves.toStrictEqual(usersWithPriorities[2]);
|
||||
|
||||
@@ -144,7 +153,7 @@ it("can find lucky user with maximize availability and priority ranking", async
|
||||
id: 1,
|
||||
username: "test1",
|
||||
name: "Test User 1",
|
||||
email: "[email protected]",
|
||||
email: "test1@example.com",
|
||||
priority: 0,
|
||||
bookings: [
|
||||
{
|
||||
@@ -169,7 +178,7 @@ it("can find lucky user with maximize availability and priority ranking", async
|
||||
id: 3,
|
||||
username: "test3",
|
||||
name: "Test User 3",
|
||||
email: "test3t@example.com",
|
||||
email: "[email protected]",
|
||||
priority: 3,
|
||||
bookings: [
|
||||
{
|
||||
@@ -187,7 +196,475 @@ it("can find lucky user with maximize availability and priority ranking", async
|
||||
await expect(
|
||||
getLuckyUser("MAXIMIZE_AVAILABILITY", {
|
||||
availableUsers: usersWithSamePriorities,
|
||||
eventTypeId: 1,
|
||||
eventType: {
|
||||
id: 1,
|
||||
isRRWeightsEnabled: false,
|
||||
},
|
||||
allRRHosts: [],
|
||||
})
|
||||
).resolves.toStrictEqual(usersWithSamePriorities[1]);
|
||||
});
|
||||
|
||||
describe("maximize availability and weights", () => {
|
||||
it("can find lucky user if hosts have same weights", async () => {
|
||||
const user1 = buildUser({
|
||||
id: 1,
|
||||
username: "test1",
|
||||
name: "Test User 1",
|
||||
email: "[email protected]",
|
||||
priority: 3,
|
||||
weight: 100,
|
||||
bookings: [
|
||||
{
|
||||
createdAt: new Date("2022-01-25T06:30:00.000Z"),
|
||||
},
|
||||
{
|
||||
createdAt: new Date("2022-01-25T03:30:00.000Z"),
|
||||
},
|
||||
],
|
||||
});
|
||||
const user2 = buildUser({
|
||||
id: 2,
|
||||
username: "test2",
|
||||
name: "Test User 2",
|
||||
email: "[email protected]",
|
||||
priority: 3,
|
||||
weight: 100,
|
||||
bookings: [
|
||||
{
|
||||
createdAt: new Date("2022-01-25T05:30:00.000Z"),
|
||||
},
|
||||
{
|
||||
createdAt: new Date("2022-01-25T04:30:00.000Z"),
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
prismaMock.user.findMany.mockResolvedValue([user1, user2]);
|
||||
|
||||
prismaMock.booking.findMany.mockResolvedValue([
|
||||
buildBooking({
|
||||
id: 1,
|
||||
userId: 1,
|
||||
createdAt: new Date("2022-01-25T06:30:00.000Z"),
|
||||
}),
|
||||
buildBooking({
|
||||
id: 2,
|
||||
userId: 1,
|
||||
createdAt: new Date("2022-01-25T03:30:00.000Z"),
|
||||
}),
|
||||
buildBooking({
|
||||
id: 3,
|
||||
userId: 2,
|
||||
createdAt: new Date("2022-01-25T05:30:00.000Z"),
|
||||
}),
|
||||
buildBooking({
|
||||
id: 4,
|
||||
userId: 2,
|
||||
createdAt: new Date("2022-01-25T04:30:00.000Z"),
|
||||
}),
|
||||
]);
|
||||
|
||||
const allRRHosts = [
|
||||
{
|
||||
user: { id: user1.id, email: user1.email },
|
||||
weight: user1.weight,
|
||||
weightAdjustment: user1.weightAdjustment,
|
||||
},
|
||||
{
|
||||
user: { id: user2.id, email: user2.email },
|
||||
weight: user2.weight,
|
||||
weightAdjustment: user2.weightAdjustment,
|
||||
},
|
||||
];
|
||||
|
||||
await expect(
|
||||
getLuckyUser("MAXIMIZE_AVAILABILITY", {
|
||||
availableUsers: [user1, user2],
|
||||
eventType: {
|
||||
id: 1,
|
||||
isRRWeightsEnabled: true,
|
||||
},
|
||||
allRRHosts,
|
||||
})
|
||||
).resolves.toStrictEqual(user2);
|
||||
});
|
||||
|
||||
it("can find lucky user if hosts have different weights", async () => {
|
||||
const user1 = buildUser({
|
||||
id: 1,
|
||||
username: "test1",
|
||||
name: "Test User 1",
|
||||
email: "[email protected]",
|
||||
priority: 3,
|
||||
weight: 200,
|
||||
bookings: [
|
||||
{
|
||||
createdAt: new Date("2022-01-25T08:30:00.000Z"),
|
||||
},
|
||||
{
|
||||
createdAt: new Date("2022-01-25T07:30:00.000Z"),
|
||||
},
|
||||
{
|
||||
createdAt: new Date("2022-01-25T05:30:00.000Z"),
|
||||
},
|
||||
],
|
||||
});
|
||||
const user2 = buildUser({
|
||||
id: 2,
|
||||
username: "test2",
|
||||
name: "Test User 2",
|
||||
email: "[email protected]",
|
||||
priority: 3,
|
||||
weight: 100,
|
||||
bookings: [
|
||||
{
|
||||
createdAt: new Date("2022-01-25T06:30:00.000Z"),
|
||||
},
|
||||
{
|
||||
createdAt: new Date("2022-01-25T03:30:00.000Z"),
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
prismaMock.user.findMany.mockResolvedValue([user1, user2]);
|
||||
|
||||
prismaMock.booking.findMany.mockResolvedValue([
|
||||
buildBooking({
|
||||
id: 1,
|
||||
userId: 1,
|
||||
createdAt: new Date("2022-01-25T08:30:00.000Z"),
|
||||
}),
|
||||
buildBooking({
|
||||
id: 2,
|
||||
userId: 1,
|
||||
createdAt: new Date("2022-01-25T07:30:00.000Z"),
|
||||
}),
|
||||
buildBooking({
|
||||
id: 3,
|
||||
userId: 1,
|
||||
createdAt: new Date("2022-01-25T05:30:00.000Z"),
|
||||
}),
|
||||
buildBooking({
|
||||
id: 4,
|
||||
userId: 2,
|
||||
createdAt: new Date("2022-01-25T06:30:00.000Z"),
|
||||
}),
|
||||
buildBooking({
|
||||
id: 4,
|
||||
userId: 2,
|
||||
createdAt: new Date("2022-01-25T03:30:00.000Z"),
|
||||
}),
|
||||
]);
|
||||
|
||||
const allRRHosts = [
|
||||
{
|
||||
user: { id: user1.id, email: user1.email },
|
||||
weight: user1.weight,
|
||||
weightAdjustment: user1.weightAdjustment,
|
||||
},
|
||||
{
|
||||
user: { id: user2.id, email: user2.email },
|
||||
weight: user2.weight,
|
||||
weightAdjustment: user2.weightAdjustment,
|
||||
},
|
||||
];
|
||||
|
||||
await expect(
|
||||
getLuckyUser("MAXIMIZE_AVAILABILITY", {
|
||||
availableUsers: [user1, user2],
|
||||
eventType: {
|
||||
id: 1,
|
||||
isRRWeightsEnabled: true,
|
||||
},
|
||||
allRRHosts,
|
||||
})
|
||||
).resolves.toStrictEqual(user1);
|
||||
});
|
||||
|
||||
it("can find lucky user with weights and adjusted weights", async () => {
|
||||
const user1 = buildUser({
|
||||
id: 1,
|
||||
username: "test1",
|
||||
name: "Test User 1",
|
||||
email: "[email protected]",
|
||||
priority: 3,
|
||||
weight: 150,
|
||||
weightAdjustment: 4, // + 3 = 7 bookings
|
||||
bookings: [
|
||||
{
|
||||
createdAt: new Date("2022-01-25T07:30:00.000Z"),
|
||||
},
|
||||
{
|
||||
createdAt: new Date("2022-01-25T05:30:00.000Z"),
|
||||
},
|
||||
{
|
||||
createdAt: new Date("2022-01-25T03:30:00.000Z"),
|
||||
},
|
||||
],
|
||||
});
|
||||
const user2 = buildUser({
|
||||
id: 2,
|
||||
username: "test2",
|
||||
name: "Test User 2",
|
||||
email: "[email protected]",
|
||||
priority: 3,
|
||||
weight: 100,
|
||||
weightAdjustment: 3, // + 2 = 5 bookings
|
||||
bookings: [
|
||||
{
|
||||
createdAt: new Date("2022-01-25T06:30:00.000Z"),
|
||||
},
|
||||
{
|
||||
createdAt: new Date("2022-01-25T03:30:00.000Z"),
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
prismaMock.user.findMany.mockResolvedValue([user1, user2]);
|
||||
|
||||
prismaMock.booking.findMany.mockResolvedValue([
|
||||
buildBooking({
|
||||
id: 1,
|
||||
userId: 1,
|
||||
createdAt: new Date("2022-01-25T05:30:00.000Z"),
|
||||
}),
|
||||
buildBooking({
|
||||
id: 2,
|
||||
userId: 1,
|
||||
createdAt: new Date("2022-01-25T03:30:00.000Z"),
|
||||
}),
|
||||
buildBooking({
|
||||
id: 3,
|
||||
userId: 1,
|
||||
createdAt: new Date("2022-01-25T07:30:00.000Z"),
|
||||
}),
|
||||
buildBooking({
|
||||
id: 4,
|
||||
userId: 2,
|
||||
createdAt: new Date("2022-01-25T06:30:00.000Z"),
|
||||
}),
|
||||
buildBooking({
|
||||
id: 4,
|
||||
userId: 2,
|
||||
createdAt: new Date("2022-01-25T03:30:00.000Z"),
|
||||
}),
|
||||
]);
|
||||
|
||||
const allRRHosts = [
|
||||
{
|
||||
user: { id: user1.id, email: user1.email },
|
||||
weight: user1.weight,
|
||||
weightAdjustment: user1.weightAdjustment,
|
||||
},
|
||||
{
|
||||
user: { id: user2.id, email: user2.email },
|
||||
weight: user2.weight,
|
||||
weightAdjustment: user2.weightAdjustment,
|
||||
},
|
||||
];
|
||||
|
||||
await expect(
|
||||
getLuckyUser("MAXIMIZE_AVAILABILITY", {
|
||||
availableUsers: [user1, user2],
|
||||
eventType: {
|
||||
id: 1,
|
||||
isRRWeightsEnabled: true,
|
||||
},
|
||||
allRRHosts,
|
||||
})
|
||||
).resolves.toStrictEqual(user1);
|
||||
});
|
||||
});
|
||||
|
||||
function convertHostsToUsers(
|
||||
hosts: {
|
||||
userId: number;
|
||||
isFixed: boolean;
|
||||
priority: number;
|
||||
weight: number;
|
||||
weightAdjustment?: number;
|
||||
newHost?: boolean;
|
||||
}[]
|
||||
) {
|
||||
return hosts.map((host) => {
|
||||
return {
|
||||
id: host.userId,
|
||||
email: `test${host.userId}@example.com`,
|
||||
hosts: host.newHost
|
||||
? []
|
||||
: [
|
||||
{
|
||||
isFixed: host.isFixed,
|
||||
priority: host.priority,
|
||||
weightAdjustment: host.weightAdjustment,
|
||||
weight: host.weight,
|
||||
},
|
||||
],
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
describe("addWeightAdjustmentToNewHosts", () => {
|
||||
it("weight adjustment is correctly added to host with two hosts that have the same weight", async () => {
|
||||
const hosts = [
|
||||
{
|
||||
userId: 1,
|
||||
isFixed: false,
|
||||
priority: 2,
|
||||
weight: 100,
|
||||
},
|
||||
{
|
||||
userId: 2,
|
||||
isFixed: false,
|
||||
priority: 2,
|
||||
weight: 100,
|
||||
newHost: true,
|
||||
},
|
||||
];
|
||||
|
||||
const users = convertHostsToUsers(hosts);
|
||||
|
||||
prismaMock.user.findMany.mockResolvedValue(users);
|
||||
|
||||
// mock for allBookings (for ongoing RR hosts)
|
||||
prismaMock.booking.findMany
|
||||
.mockResolvedValueOnce([
|
||||
buildBooking({
|
||||
id: 1,
|
||||
userId: 1,
|
||||
}),
|
||||
buildBooking({
|
||||
id: 2,
|
||||
userId: 1,
|
||||
}),
|
||||
buildBooking({
|
||||
id: 3,
|
||||
userId: 1,
|
||||
}),
|
||||
buildBooking({
|
||||
id: 4,
|
||||
userId: 1,
|
||||
}),
|
||||
])
|
||||
// mock for bookings of new RR host
|
||||
.mockResolvedValueOnce([
|
||||
buildBooking({
|
||||
id: 5,
|
||||
userId: 2,
|
||||
}),
|
||||
]);
|
||||
|
||||
const hostsWithAdjustedWeight = await addWeightAdjustmentToNewHosts({
|
||||
hosts,
|
||||
isWeightsEnabled: true,
|
||||
eventTypeId: 1,
|
||||
prisma: prismaMock,
|
||||
});
|
||||
/*
|
||||
both users have weight 100, user1 has 4 bookings user 2 has 1 bookings already
|
||||
*/
|
||||
expect(hostsWithAdjustedWeight.find((host) => host.userId === 2)?.weightAdjustment).toBe(3);
|
||||
});
|
||||
|
||||
it("weight adjustment is correctly added to host with several hosts that have different weights", async () => {
|
||||
// make different weights
|
||||
const hosts = [
|
||||
{
|
||||
userId: 1,
|
||||
isFixed: false,
|
||||
priority: 2,
|
||||
weight: 100,
|
||||
},
|
||||
{
|
||||
userId: 2,
|
||||
isFixed: false,
|
||||
priority: 2,
|
||||
weight: 200,
|
||||
newHost: true,
|
||||
},
|
||||
{
|
||||
userId: 3,
|
||||
isFixed: false,
|
||||
priority: 2,
|
||||
weight: 200,
|
||||
},
|
||||
{
|
||||
userId: 4,
|
||||
isFixed: false,
|
||||
priority: 2,
|
||||
weight: 100,
|
||||
},
|
||||
{
|
||||
userId: 5,
|
||||
isFixed: false,
|
||||
priority: 2,
|
||||
weight: 50,
|
||||
newHost: true,
|
||||
},
|
||||
];
|
||||
|
||||
const users = convertHostsToUsers(hosts);
|
||||
|
||||
prismaMock.user.findMany.mockResolvedValue(users);
|
||||
|
||||
// mock for allBookings (for ongoing RR hosts)
|
||||
prismaMock.booking.findMany
|
||||
.mockResolvedValueOnce([
|
||||
// 8 bookings for ongoing hosts (hosts that already existed before)
|
||||
buildBooking({
|
||||
id: 1,
|
||||
userId: 1,
|
||||
}),
|
||||
buildBooking({
|
||||
id: 2,
|
||||
userId: 1,
|
||||
}),
|
||||
buildBooking({
|
||||
id: 3,
|
||||
userId: 3,
|
||||
}),
|
||||
buildBooking({
|
||||
id: 4,
|
||||
userId: 3,
|
||||
}),
|
||||
buildBooking({
|
||||
id: 5,
|
||||
userId: 4,
|
||||
}),
|
||||
buildBooking({
|
||||
id: 6,
|
||||
userId: 4,
|
||||
}),
|
||||
buildBooking({
|
||||
id: 7,
|
||||
userId: 4,
|
||||
}),
|
||||
buildBooking({
|
||||
id: 8,
|
||||
userId: 4,
|
||||
}),
|
||||
])
|
||||
// mock for bookings of new RR host
|
||||
.mockResolvedValueOnce([
|
||||
buildBooking({
|
||||
id: 5,
|
||||
userId: 2,
|
||||
}),
|
||||
])
|
||||
.mockResolvedValue([]);
|
||||
|
||||
const hostsWithAdjustedWeight = await addWeightAdjustmentToNewHosts({
|
||||
hosts,
|
||||
isWeightsEnabled: true,
|
||||
eventTypeId: 1,
|
||||
prisma: prismaMock,
|
||||
});
|
||||
|
||||
// 8 bookings from ongoing hosts, 400 total weight --> average 0.02 bookings per weight unit --> 0.02 * 200 = 4 - 1 prev bookings = 3
|
||||
expect(hostsWithAdjustedWeight.find((host) => host.userId === 2)?.weightAdjustment).toBe(3);
|
||||
|
||||
// 8 bookings from ongoing hosts, 400 total weight --> average 0.02 bookings per weight unit --> 0.02 * 50 = 1 (no prev bookings)
|
||||
expect(hostsWithAdjustedWeight.find((host) => host.userId === 5)?.weightAdjustment).toBe(1);
|
||||
});
|
||||
});
|
||||
@@ -185,6 +185,7 @@ type WhiteListedBookingProps = {
|
||||
credentialId?: number | null;
|
||||
})[];
|
||||
bookingSeat?: Prisma.BookingSeatCreateInput[];
|
||||
createdAt?: string;
|
||||
};
|
||||
|
||||
type InputBooking = Partial<Omit<Booking, keyof WhiteListedBookingProps>> & WhiteListedBookingProps;
|
||||
|
||||
Reference in new issue
Block a user