From e3861ad22a4879b32361b4af71a4a047f3c521f0 Mon Sep 17 00:00:00 2001 From: Amit Sharma <74371312+Amit91848@users.noreply.github.com> Date: Mon, 29 Jul 2024 18:35:03 +0530 Subject: [PATCH] feat: No-show bookings - do not count towards a member's round robin assignment (#15323) * feat: No-show bookings - do not count towards a member's round robin assignment * filter no show organizer * remove logs * fix: filter bookings where host attendee is no show * fix: use not condition for noShow * chore: test * add default false for noShowHost * Revert "chore: test" This reverts commit c914a8fc4a14ca9c7d8c4a08ff8b0f463789171c. * chore: integration test * fix: noShowHost not working for null * Update packages/lib/server/getLuckyUser.ts * chore: update test description --------- Co-authored-by: Joe Au-Yeung <65426560+joeauyeung@users.noreply.github.com> Co-authored-by: Udit Takkar <53316345+Udit-takkar@users.noreply.github.com> Co-authored-by: gh0st91848 Co-authored-by: Hariom Balhara --- .../server/getLuckyUser.integration-test.ts | 364 ++++++++++++++++++ packages/lib/server/getLuckyUser.ts | 16 + .../migration.sql | 2 + packages/prisma/schema.prisma | 2 +- 4 files changed, 383 insertions(+), 1 deletion(-) create mode 100644 packages/lib/server/getLuckyUser.integration-test.ts create mode 100644 packages/prisma/migrations/20240712162735_default_no_show_host_to_false/migration.sql diff --git a/packages/lib/server/getLuckyUser.integration-test.ts b/packages/lib/server/getLuckyUser.integration-test.ts new file mode 100644 index 0000000000..b563dcb1db --- /dev/null +++ b/packages/lib/server/getLuckyUser.integration-test.ts @@ -0,0 +1,364 @@ +import { describe, it, expect, afterEach, beforeAll, afterAll } from "vitest"; + +import prisma from "@calcom/prisma"; + +import { getLuckyUser } from "./getLuckyUser"; + +describe("getLuckyUser 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: "test-user1@example.com", + 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: "test-attendee@example.com", + timeZone: "Asia/Calcutta", + }, + ], + }, + }, + ], + }, + }, + }); + + const organizerThatDidntShowUp = await prisma.user.create({ + data: { + email: "test-user2@example.com", + 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: "test-attendee@example.com", + timeZone: "Asia/Calcutta", + }, + ], + }, + eventTypeId, + }, + ], + }, + }, + }); + + userIds.push(organizerThatShowedUp.id, organizerThatDidntShowUp.id); + + expect( + getLuckyUser("MAXIMIZE_AVAILABILITY", { + availableUsers: [organizerThatShowedUp, organizerThatDidntShowUp], + eventTypeId, + }) + ).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: "test-user1@example.com", + 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: "test-attendee@example.com", + timeZone: "Asia/Calcutta", + }, + ], + }, + }, + ], + }, + }, + }); + + const organizerWhoseAttendeeDidntShowUp = await prisma.user.create({ + data: { + email: "test-user2@example.com", + 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: "test-attendee@example.com", + timeZone: "Asia/Calcutta", + }, + ], + }, + eventTypeId, + }, + ], + }, + }, + }); + + userIds.push(organizerWhoseAttendeeShowedUp.id, organizerWhoseAttendeeDidntShowUp.id); + + expect( + getLuckyUser("MAXIMIZE_AVAILABILITY", { + availableUsers: [organizerWhoseAttendeeShowedUp, organizerWhoseAttendeeDidntShowUp], + eventTypeId, + }) + ).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: "test-user1@example.com", + 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: "test-attendee@example.com", + timeZone: "Asia/Calcutta", + }, + ], + }, + }, + ], + }, + }, + }); + + const fixedHostOrganizerWhoseAttendeeDidNotShowUp = await prisma.user.create({ + data: { + email: "test-user2@example.com", + 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: "test-attendee@example.com", + 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: "test-user3@example.com", + timeZone: "Asia/Calcutta", + noShow: true, + }, + ], + }, + eventTypeId, + }, + ], + }, + }, + }); + + const organizerWhoWasAttendeeAndDidntShowUp = await prisma.user.create({ + data: { + email: `test-user3@example.com`, + 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: "test-attendee@example.com", + timeZone: "Asia/Calcutta", + }, + ], + }, + eventTypeId, + }, + ], + }, + }, + }); + + userIds.push( + organizerWhoseAttendeeShowedUp.id, + fixedHostOrganizerWhoseAttendeeDidNotShowUp.id, + organizerWhoWasAttendeeAndDidntShowUp.id + ); + + expect( + getLuckyUser("MAXIMIZE_AVAILABILITY", { + availableUsers: [ + organizerWhoseAttendeeShowedUp, + fixedHostOrganizerWhoseAttendeeDidNotShowUp, + organizerWhoWasAttendeeAndDidntShowUp, + ], + eventTypeId, + }) + ).resolves.toStrictEqual(organizerWhoWasAttendeeAndDidntShowUp); + }); + + it("should consider booking when noShowHost is null", async () => { + const user1 = await prisma.user.create({ + data: { + email: "test-user1@example.com", + 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: "test-attendee@example.com", + timeZone: "Asia/Calcutta", + }, + ], + }, + }, + ], + }, + }, + }); + + const user2 = await prisma.user.create({ + data: { + email: "test-user2@example.com", + 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: "test-attendee@example.com", + timeZone: "Asia/Calcutta", + }, + ], + }, + eventTypeId, + }, + ], + }, + }, + }); + + userIds.push(user1.id, user2.id); + + expect( + getLuckyUser("MAXIMIZE_AVAILABILITY", { + availableUsers: [user1, user2], + eventTypeId, + }) + ).resolves.toStrictEqual(user2); + }); + }); +}); diff --git a/packages/lib/server/getLuckyUser.ts b/packages/lib/server/getLuckyUser.ts index af73bec4ad..f679f1f4d4 100644 --- a/packages/lib/server/getLuckyUser.ts +++ b/packages/lib/server/getLuckyUser.ts @@ -24,6 +24,21 @@ async function leastRecentlyBookedUser>({ }, where: { eventTypeId, + attendees: { + some: { + noShow: false, + }, + }, + // not:true won't match null, thus we need to do an OR with null case separately(for bookings that might have null value for `noShowHost` as earlier it didn't have default false) + // https://github.com/calcom/cal.com/pull/15323#discussion_r1687728207 + OR: [ + { + noShowHost: false, + }, + { + noShowHost: null, + }, + ], }, orderBy: { createdAt: "desc", @@ -53,6 +68,7 @@ async function leastRecentlyBookedUser>({ email: { in: availableUsers.map((user) => user.email), }, + noShow: false, }, }, }, diff --git a/packages/prisma/migrations/20240712162735_default_no_show_host_to_false/migration.sql b/packages/prisma/migrations/20240712162735_default_no_show_host_to_false/migration.sql new file mode 100644 index 0000000000..402fffd73e --- /dev/null +++ b/packages/prisma/migrations/20240712162735_default_no_show_host_to_false/migration.sql @@ -0,0 +1,2 @@ +-- AlterTable +ALTER TABLE "Booking" ALTER COLUMN "noShowHost" SET DEFAULT false; diff --git a/packages/prisma/schema.prisma b/packages/prisma/schema.prisma index 7332c9b438..51db6dabe0 100644 --- a/packages/prisma/schema.prisma +++ b/packages/prisma/schema.prisma @@ -577,7 +577,7 @@ model Booking { instantMeetingToken InstantMeetingToken? rating Int? ratingFeedback String? - noShowHost Boolean? + noShowHost Boolean? @default(false) scheduledTriggers WebhookScheduledTriggers[] @@index([eventTypeId])