Files
cal.diy/packages/lib/server/getLuckyUser.integration-test.ts
T
Alex van Andel f76696032e 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)
2024-11-15 15:54:02 +00:00

381 lines
11 KiB
TypeScript

import { describe, it, expect, afterEach, beforeAll, afterAll } from "vitest";
import prisma from "@calcom/prisma";
import { getLuckyUser } from "./getLuckyUser";
describe("getLuckyUser Integration tests", () => {
describe("should not consider no show bookings for round robin: ", () => {
let userIds: number[] = [];
let eventTypeId: number;
beforeAll(async () => {
const event = await prisma.eventType.create({
data: {
title: "Test Event",
slug: "test-event",
length: 15,
},
select: {
id: true,
},
});
eventTypeId = event.id;
});
afterEach(async () => {
await prisma.user.deleteMany({
where: {
id: {
in: userIds,
},
},
});
userIds = [];
});
afterAll(async () => {
await prisma.eventType.delete({
where: {
id: eventTypeId,
},
});
});
it("When a host is no show, that is chosen when competing with another host that showed up for the booking", async () => {
const organizerThatShowedUp = await prisma.user.create({
data: {
email: "[email protected]",
bookings: {
create: [
{
uid: "uuid-test-user1-booking1",
createdAt: new Date("2022-01-25T05:30:00.000Z"),
title: "Test user 1 Booking",
startTime: new Date(),
endTime: new Date(),
eventTypeId,
attendees: {
create: [
{
name: "test-attendee",
email: "[email protected]",
timeZone: "Asia/Calcutta",
},
],
},
},
],
},
},
});
const organizerThatDidntShowUp = await prisma.user.create({
data: {
email: "[email protected]",
bookings: {
create: [
{
uid: "uuid-test-user2-booking1",
title: "Test User 2 Booking",
createdAt: new Date("2022-01-25T06:30:00.000Z"),
noShowHost: true,
startTime: new Date(),
endTime: new Date(),
attendees: {
create: [
{
name: "test-attendee",
email: "[email protected]",
timeZone: "Asia/Calcutta",
},
],
},
eventTypeId,
},
],
},
},
});
userIds.push(organizerThatShowedUp.id, organizerThatDidntShowUp.id);
expect(
getLuckyUser({
availableUsers: [organizerThatShowedUp, organizerThatDidntShowUp],
eventType: {
id: eventTypeId,
isRRWeightsEnabled: false,
},
allRRHosts: [],
})
).resolves.toStrictEqual(organizerThatDidntShowUp);
});
it("When a attendee is a noShow for organizers booking, that organizer is competing with another host whose attendee showed up for the booking", async () => {
const organizerWhoseAttendeeShowedUp = await prisma.user.create({
data: {
email: "[email protected]",
bookings: {
create: [
{
uid: "uuid-test-user1-booking1",
createdAt: new Date("2022-01-25T05:30:00.000Z"),
title: "Test User 1 Booking",
startTime: new Date(),
endTime: new Date(),
eventTypeId,
attendees: {
create: [
{
name: "test-attendee",
email: "[email protected]",
timeZone: "Asia/Calcutta",
},
],
},
},
],
},
},
});
const organizerWhoseAttendeeDidntShowUp = await prisma.user.create({
data: {
email: "[email protected]",
bookings: {
create: [
{
uid: "uuid-test-user2-booking1",
title: "Test User 2 Booking",
createdAt: new Date("2022-01-25T06:30:00.000Z"),
startTime: new Date(),
endTime: new Date(),
attendees: {
create: [
{
noShow: true,
name: "test-attendee",
email: "[email protected]",
timeZone: "Asia/Calcutta",
},
],
},
eventTypeId,
},
],
},
},
});
userIds.push(organizerWhoseAttendeeShowedUp.id, organizerWhoseAttendeeDidntShowUp.id);
expect(
getLuckyUser({
availableUsers: [organizerWhoseAttendeeShowedUp, organizerWhoseAttendeeDidntShowUp],
eventType: {
id: eventTypeId,
isRRWeightsEnabled: false,
},
allRRHosts: [],
})
).resolves.toStrictEqual(organizerWhoseAttendeeDidntShowUp);
});
it("When a organizer is attendee (event types with fixed hosts) and no show, that organizer is competing other hosts", async () => {
const organizerWhoseAttendeeShowedUp = await prisma.user.create({
data: {
email: "[email protected]",
bookings: {
create: [
{
uid: "uuid-test-user1-booking1",
createdAt: new Date("2022-01-25T05:30:00.000Z"),
title: "Test User 1 Booking",
startTime: new Date(),
endTime: new Date(),
eventTypeId,
attendees: {
create: [
{
name: "test-attendee",
email: "[email protected]",
timeZone: "Asia/Calcutta",
},
],
},
},
],
},
},
});
const fixedHostOrganizerWhoseAttendeeDidNotShowUp = await prisma.user.create({
data: {
email: "[email protected]",
bookings: {
create: [
{
uid: "uuid-test-user2-booking1",
title: `Test User 2 Booking`,
createdAt: new Date("2022-01-25T06:30:00.000Z"),
startTime: new Date(),
endTime: new Date(),
attendees: {
create: [
{
name: "test-attendee",
email: "[email protected]",
timeZone: "Asia/Calcutta",
},
],
},
eventTypeId,
},
// User2 is fixed user, User 3 was selected as round robin host but did not show up so this booking should be counted for User 3
{
uid: "uuid-test-user2-booking2",
title: `Test User 2 Booking 2`,
createdAt: new Date("2022-01-25T07:30:00.000Z"),
startTime: new Date(),
endTime: new Date(),
attendees: {
create: [
{
name: "test-attendee",
email: "[email protected]",
timeZone: "Asia/Calcutta",
noShow: true,
},
],
},
eventTypeId,
},
],
},
},
});
const organizerWhoWasAttendeeAndDidntShowUp = await prisma.user.create({
data: {
email: `[email protected]`,
bookings: {
create: [
{
uid: "uuid-test-user3-booking1",
title: `Test User 3 Booking`,
createdAt: new Date("2022-01-25T04:30:00.000Z"),
startTime: new Date(),
endTime: new Date(),
attendees: {
create: [
{
name: "test-attendee",
email: "[email protected]",
timeZone: "Asia/Calcutta",
},
],
},
eventTypeId,
},
],
},
},
});
userIds.push(
organizerWhoseAttendeeShowedUp.id,
fixedHostOrganizerWhoseAttendeeDidNotShowUp.id,
organizerWhoWasAttendeeAndDidntShowUp.id
);
expect(
getLuckyUser({
availableUsers: [
organizerWhoseAttendeeShowedUp,
fixedHostOrganizerWhoseAttendeeDidNotShowUp,
organizerWhoWasAttendeeAndDidntShowUp,
],
eventType: {
id: eventTypeId,
isRRWeightsEnabled: false,
},
allRRHosts: [],
})
).resolves.toStrictEqual(organizerWhoWasAttendeeAndDidntShowUp);
});
it("should consider booking when noShowHost is null", async () => {
const user1 = await prisma.user.create({
data: {
email: "[email protected]",
bookings: {
create: [
{
uid: "uuid-test-user1-booking1",
createdAt: new Date("2022-01-25T07:30:00.000Z"),
title: "Test user 1 Booking",
startTime: new Date(),
noShowHost: null,
endTime: new Date(),
eventTypeId,
attendees: {
create: [
{
name: "test-attendee",
email: "[email protected]",
timeZone: "Asia/Calcutta",
},
],
},
},
],
},
},
});
const user2 = await prisma.user.create({
data: {
email: "[email protected]",
bookings: {
create: [
{
uid: "uuid-test-user2-booking1",
title: "Test User 2 Booking",
createdAt: new Date("2022-01-25T06:30:00.000Z"),
noShowHost: null,
startTime: new Date(),
endTime: new Date(),
attendees: {
create: [
{
name: "test-attendee",
email: "[email protected]",
timeZone: "Asia/Calcutta",
},
],
},
eventTypeId,
},
],
},
},
});
userIds.push(user1.id, user2.id);
expect(
getLuckyUser({
availableUsers: [user1, user2],
eventType: {
id: eventTypeId,
isRRWeightsEnabled: false,
},
allRRHosts: [],
})
).resolves.toStrictEqual(user2);
});
});
});