Files
cal-diy-oidc/packages/features/bookings/lib/handleSeats/test/handleSeats.test.ts
T
Volnei Munhoz bbf9274d37 chore: upgrade Vitest to 4.0.16 and Vite to 6.4.1 (#26351)
* chore: upgrade Vitest to 4.0.16 and Vite to 6.4.1

- Update vitest from 2.1.9 to 4.0.16
- Update @vitest/ui from 2.1.9 to 4.0.16
- Update vitest-fetch-mock from 0.3.0 to 0.4.5
- Update vitest-mock-extended from 2.0.2 to 3.1.0
- Update vite from 4.5.14/5.4.21 to 6.4.1 across all packages
- Update @vitejs/plugin-react to 5.1.2
- Update @vitejs/plugin-react-swc to 4.2.2
- Update @vitejs/plugin-basic-ssl to 2.1.0
- Update vite-plugin-dts to 4.5.4
- Rename vitest.config.ts to vitest.config.mts for ESM compatibility
- Add globals: true to vitest config

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

* fix: address Vitest 4.0 and Vite 6 breaking changes

- Convert arrow function mockImplementation patterns to regular functions
  (Vitest 4.0 breaking change: arrow functions can't be constructor mocks)
- Fix CSS imports with ?inline suffix for Vite 6 compatibility
- Add biome override to disable useArrowFunction rule for test files
- Fix syntax errors in test files introduced by regex replacements

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

* fix: fix remaining Vitest 4.0 constructor mock patterns

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

* fix: fix more Vitest 4.0 constructor mock patterns and exclude API v2 spec files

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

* fix: convert more arrow function mocks to regular functions for Vitest 4.0

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

* fix: convert more arrow function mocks to regular functions for Vitest 4.0

- Fix CrmService.integration.test.ts jsforce.Connection mock
- Fix RetellSDKClient.test.ts Retell mock
- Fix RetellAIService.test.ts CreditService mocks
- Fix GoogleCalendarSubscriptionAdapter.test.ts CalendarAuth mock

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

* fix: convert Google Calendar and OAuthManager arrow function mocks for Vitest 4.0

- Fix googleapis.ts Calendar, OAuth2Client, and JWT mocks
- Fix utils.ts JWT mock
- Fix OAuthManager.ts defaultMockOAuthManager mock

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

* fix: add React plugin, jsdom environment, and fix more constructor mocks for Vitest 4.0

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

* fix: convert HostRepository PrismaClient mock to regular function for Vitest 4.0

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

* fix: add useOrgBranding mock to React component tests for Vitest 4.0

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

* fix: update TestFunction type for Vitest 4.0 compatibility

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

* fix: convert listBookingReports constructor mocks to regular functions for Vitest 4.0

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

* fix: convert UserRepository constructor mock to regular function for Vitest 4.0

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

* fix: convert OrganizationPaymentService constructor mock to regular function for Vitest 4.0

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

* fix: convert more constructor mocks to regular functions for Vitest 4.0

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

* fix: add apps/web path aliases to vitest config

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

* fix: fix test issues for Vitest 4.0 compatibility

- Fix Response constructor 204 status code issue in testUtils.ts
- Fix FeaturesRepository mock persistence in handleNotificationWhenNoSlots.test.ts
- Add @vitest-environment node directive to formSubmissionUtils.test.ts
- Fix document.querySelector mock in embed.test.ts

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

* fix: clear EventManager spy between tests for Vitest 4.0 compatibility

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

* fix: update TeamRepository mock pattern for Vitest 4.0 compatibility

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

* fix: convert RoutingFormResponseRepository mock to regular function for Vitest 4.0

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

* fix: convert more constructor mocks to regular functions for Vitest 4.0

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

* fix: fix mock reset and spy clear issues for Vitest 4.0

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

* fix: fix remaining test failures for Vitest 4.0 upgrade

- Fix booking-validations.test.ts: convert UserRepository mock to regular function
- Fix route.test.ts: update 500 error test to mock ImageResponse instead of fetch
- Fix users-public-view.test.tsx: add missing mocks for getOrgFullOrigin and useRouterQuery
- Add @calcom/web path alias to vitest config

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

* fix: add vitest-mocks for generated files that don't exist in CI

- Add svg-hashes.json mock for route.test.ts
- Add tailwind.generated.css mock for embed.test.ts
- Update vitest config to use mock files

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

* fix: update vitest config aliases for CI compatibility

- Use array format for aliases to ensure proper ordering
- Add @calcom/platform-constants alias to resolve from source
- Add @calcom/embed-react alias to resolve from source
- Ensure svg-hashes.json mock alias is matched before @calcom/web

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

* fix: add @calcom/embed-snippet alias for CI compatibility

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

* Fix wrong test

* fix: migrate from CLI flags to VITEST_MODE env var for Vitest 4.0

Vitest 4.0 no longer allows custom CLI flags like --packaged-embed-tests-only.
This change migrates to using VITEST_MODE environment variable instead:
- VITEST_MODE=packaged-embed for packaged embed tests
- VITEST_MODE=integration for integration tests
- VITEST_MODE=timezone for timezone-dependent tests

Updated vitest.config.mts to handle mode-based include/exclude patterns.
Updated CI workflows and package scripts to use the new env var approach.

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

* fix: return default include pattern instead of undefined in vitest config

The getTestInclude() function was returning undefined for the default case,
but Vitest 4.0 expects an array. This caused 'resolved.include is not iterable'
error in CI.

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

* fix: always set INTEGRATION_TEST_MODE for jsdom environment

The getBookingFields.ts file checks for INTEGRATION_TEST_MODE to allow
server-side imports in the jsdom environment. Without this, tests fail
with 'getBookingFields must not be imported on the client side' error.

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

* fix: support legacy CLI flags for backwards compatibility with main workflow

The CI runs workflows from main branch, which uses the old CLI flag approach
(yarn test -- --integrationTestsOnly). This commit adds backwards compatibility
by checking both VITEST_MODE env var and process.argv for the legacy flags.

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-01-01 18:16:10 -03:00

2676 lines
84 KiB
TypeScript

import prismaMock from "../../../../../../tests/libs/__mocks__/prisma";
import {
getBooker,
TestData,
getOrganizer,
createBookingScenario,
getGoogleCalendarCredential,
Timezones,
getScenarioData,
mockSuccessfulVideoMeetingCreation,
BookingLocations,
getDate,
getMockBookingAttendee,
} from "@calcom/web/test/utils/bookingScenario/bookingScenario";
import { createMockNextJsRequest } from "@calcom/web/test/utils/bookingScenario/createMockNextJsRequest";
import { getMockRequestDataForBooking } from "@calcom/web/test/utils/bookingScenario/getMockRequestDataForBooking";
import { getMockRequestDataForCancelBooking } from "@calcom/web/test/utils/bookingScenario/getMockRequestDataForCancelBooking";
import { setupAndTeardown } from "@calcom/web/test/utils/bookingScenario/setupAndTeardown";
import { describe, test, vi, expect } from "vitest";
import { appStoreMetadata } from "@calcom/app-store/apps.metadata.generated";
import { ErrorCode } from "@calcom/lib/errorCodes";
import { SchedulingType } from "@calcom/prisma/enums";
import { BookingStatus } from "@calcom/prisma/enums";
import { getNewBookingHandler } from "../../handleNewBooking/test/getNewBookingHandler";
import * as handleSeatsModule from "../handleSeats";
describe("handleSeats", () => {
setupAndTeardown();
describe("Correct parameters being passed into handleSeats from handleNewBooking", () => {
vi.mock("./handleSeats");
test("On new booking handleSeats is not called", async () => {
const handleNewBooking = getNewBookingHandler();
const spy = vi.spyOn(handleSeatsModule, "default");
spy.mockClear();
const booker = getBooker({
email: "booker@example.com",
name: "Booker",
});
const organizer = getOrganizer({
name: "Organizer",
email: "organizer@example.com",
id: 101,
schedules: [TestData.schedules.IstWorkHours],
});
await createBookingScenario(
getScenarioData({
eventTypes: [
{
id: 1,
slotInterval: 30,
length: 30,
users: [
{
id: 101,
},
],
seatsPerTimeSlot: 3,
},
],
organizer,
})
);
mockSuccessfulVideoMeetingCreation({
metadataLookupKey: "dailyvideo",
videoMeetingData: {
id: "MOCK_ID",
password: "MOCK_PASS",
url: `http://mock-dailyvideo.example.com/meeting-1`,
},
});
const mockBookingData = getMockRequestDataForBooking({
data: {
eventTypeId: 1,
responses: {
email: booker.email,
name: booker.name,
location: { optionValue: "", value: BookingLocations.CalVideo },
},
},
});
await handleNewBooking({
bookingData: mockBookingData,
});
expect(spy).toHaveBeenCalledTimes(1);
});
test("handleSeats is called when a new attendee is added", async () => {
const spy = vi.spyOn(handleSeatsModule, "default");
spy.mockClear();
const handleNewBooking = getNewBookingHandler();
const booker = getBooker({
email: "booker@example.com",
name: "Booker",
});
const organizer = getOrganizer({
name: "Organizer",
email: "organizer@example.com",
id: 101,
schedules: [TestData.schedules.IstWorkHours],
});
const bookingId = 1;
const bookingUid = "abc123";
const { dateString: plus1DateString } = getDate({ dateIncrement: 1 });
const bookingStartTime = `${plus1DateString}T04:00:00Z`;
const bookingEndTime = `${plus1DateString}T04:30:00Z`;
const bookingScenario = await createBookingScenario(
getScenarioData({
eventTypes: [
{
id: 1,
slug: "seated-event",
slotInterval: 30,
length: 30,
users: [
{
id: 101,
},
],
seatsPerTimeSlot: 3,
seatsShowAttendees: false,
},
],
bookings: [
{
id: bookingId,
uid: bookingUid,
eventTypeId: 1,
status: BookingStatus.ACCEPTED,
startTime: bookingStartTime,
endTime: bookingEndTime,
metadata: {
videoCallUrl: "https://existing-daily-video-call-url.example.com",
},
references: [
{
type: appStoreMetadata.dailyvideo.type,
uid: "MOCK_ID",
meetingId: "MOCK_ID",
meetingPassword: "MOCK_PASS",
meetingUrl: "http://mock-dailyvideo.example.com",
credentialId: null,
},
],
},
],
organizer,
})
);
mockSuccessfulVideoMeetingCreation({
metadataLookupKey: "dailyvideo",
videoMeetingData: {
id: "MOCK_ID",
password: "MOCK_PASS",
url: `http://mock-dailyvideo.example.com/meeting-1`,
},
});
const reqBookingUser = "seatedAttendee";
const mockBookingData = getMockRequestDataForBooking({
data: {
eventTypeId: 1,
responses: {
email: booker.email,
name: booker.name,
location: { optionValue: "", value: BookingLocations.CalVideo },
},
bookingUid: bookingUid,
user: reqBookingUser,
},
});
const { req } = createMockNextJsRequest({
method: "POST",
body: mockBookingData,
});
const createdBooking = await handleNewBooking({
bookingData: mockBookingData,
});
expect(createdBooking.metadata).toHaveProperty("videoCallUrl");
const handleSeatsCall = spy.mock.calls[0][0];
expect(handleSeatsCall).toEqual(
expect.objectContaining({
bookerEmail: booker.email,
reqBodyUser: reqBookingUser,
tAttendees: expect.any(Function),
additionalNotes: expect.anything(),
noEmail: undefined,
})
);
const bookingScenarioEventType = bookingScenario.eventTypes[0];
expect(handleSeatsCall.eventTypeInfo).toEqual(
expect.objectContaining({
eventTitle: bookingScenarioEventType.title,
eventDescription: bookingScenarioEventType.description,
length: bookingScenarioEventType.length,
})
);
expect(handleSeatsCall.eventType).toEqual(
expect.objectContaining({
id: bookingScenarioEventType.id,
slug: bookingScenarioEventType.slug,
workflows: bookingScenarioEventType.workflows,
seatsPerTimeSlot: bookingScenarioEventType.seatsPerTimeSlot,
seatsShowAttendees: bookingScenarioEventType.seatsShowAttendees,
})
);
expect(handleSeatsCall.evt).toEqual(
expect.objectContaining({
startTime: bookingStartTime,
})
);
expect(handleSeatsCall.invitee).toEqual([
expect.objectContaining({
email: booker.email,
name: booker.name,
}),
]);
});
test("handleSeats is called on rescheduling a seated event", async () => {
const spy = vi.spyOn(handleSeatsModule, "default");
spy.mockClear();
const handleNewBooking = getNewBookingHandler();
const booker = getBooker({
email: "booker@example.com",
name: "Booker",
});
const organizer = getOrganizer({
name: "Organizer",
email: "organizer@example.com",
id: 101,
schedules: [TestData.schedules.IstWorkHours],
});
const bookingId = 1;
const bookingUid = "abc123";
const { dateString: plus1DateString } = getDate({ dateIncrement: 1 });
const bookingStartTime = `${plus1DateString}T04:00:00Z`;
const bookingEndTime = `${plus1DateString}T04:30:00Z`;
const bookingScenario = await createBookingScenario(
getScenarioData({
eventTypes: [
{
id: 1,
slug: "seated-event",
slotInterval: 30,
length: 30,
users: [
{
id: 101,
},
],
seatsPerTimeSlot: 3,
seatsShowAttendees: false,
},
],
bookings: [
{
id: bookingId,
uid: bookingUid,
eventTypeId: 1,
status: BookingStatus.ACCEPTED,
startTime: bookingStartTime,
endTime: bookingEndTime,
metadata: {
videoCallUrl: "https://existing-daily-video-call-url.example.com",
},
references: [
{
type: appStoreMetadata.dailyvideo.type,
uid: "MOCK_ID",
meetingId: "MOCK_ID",
meetingPassword: "MOCK_PASS",
meetingUrl: "http://mock-dailyvideo.example.com",
credentialId: null,
},
],
},
],
organizer,
})
);
mockSuccessfulVideoMeetingCreation({
metadataLookupKey: "dailyvideo",
videoMeetingData: {
id: "MOCK_ID",
password: "MOCK_PASS",
url: `http://mock-dailyvideo.example.com/meeting-1`,
},
});
const reqBookingUser = "seatedAttendee";
const mockBookingData = getMockRequestDataForBooking({
data: {
rescheduleUid: bookingUid,
eventTypeId: 1,
responses: {
email: booker.email,
name: booker.name,
location: { optionValue: "", value: BookingLocations.CalVideo },
},
bookingUid: bookingUid,
user: reqBookingUser,
},
});
await handleNewBooking({
bookingData: mockBookingData,
});
const handleSeatsCall = spy.mock.calls[0][0];
expect(handleSeatsCall).toEqual(
expect.objectContaining({
rescheduleUid: bookingUid,
bookerEmail: booker.email,
reqBodyUser: reqBookingUser,
tAttendees: expect.any(Function),
additionalNotes: expect.anything(),
noEmail: undefined,
})
);
const bookingScenarioEventType = bookingScenario.eventTypes[0];
expect(handleSeatsCall.eventTypeInfo).toEqual(
expect.objectContaining({
eventTitle: bookingScenarioEventType.title,
eventDescription: bookingScenarioEventType.description,
length: bookingScenarioEventType.length,
})
);
expect(handleSeatsCall.eventType).toEqual(
expect.objectContaining({
id: bookingScenarioEventType.id,
slug: bookingScenarioEventType.slug,
workflows: bookingScenarioEventType.workflows,
seatsPerTimeSlot: bookingScenarioEventType.seatsPerTimeSlot,
seatsShowAttendees: bookingScenarioEventType.seatsShowAttendees,
})
);
expect(handleSeatsCall.evt).toEqual(
expect.objectContaining({
startTime: bookingStartTime,
})
);
expect(handleSeatsCall.invitee).toEqual([
expect.objectContaining({
email: booker.email,
name: booker.name,
}),
]);
});
});
describe("As an attendee", () => {
describe("Creating a new booking", () => {
test("Attendee should be added to existing seated event", async () => {
const handleNewBooking = getNewBookingHandler();
const booker = getBooker({
email: "seat2@example.com",
name: "Seat 2",
});
const organizer = getOrganizer({
name: "Organizer",
email: "organizer@example.com",
id: 101,
schedules: [TestData.schedules.IstWorkHours],
});
const bookingId = 1;
const bookingUid = "abc123";
const { dateString: plus1DateString } = getDate({ dateIncrement: 1 });
const bookingStartTime = `${plus1DateString}T04:00:00Z`;
const bookingEndTime = `${plus1DateString}T04:30:00Z`;
await createBookingScenario(
getScenarioData({
eventTypes: [
{
id: 1,
slug: "seated-event",
slotInterval: 30,
length: 30,
users: [
{
id: 101,
},
],
seatsPerTimeSlot: 3,
seatsShowAttendees: false,
},
],
bookings: [
{
id: bookingId,
uid: bookingUid,
eventTypeId: 1,
status: BookingStatus.ACCEPTED,
startTime: bookingStartTime,
endTime: bookingEndTime,
metadata: {
videoCallUrl: "https://existing-daily-video-call-url.example.com",
},
references: [
{
type: appStoreMetadata.dailyvideo.type,
uid: "MOCK_ID",
meetingId: "MOCK_ID",
meetingPassword: "MOCK_PASS",
meetingUrl: "http://mock-dailyvideo.example.com",
credentialId: null,
},
],
attendees: [
getMockBookingAttendee({
id: 1,
name: "Seat 1",
email: "seat1@test.com",
locale: "en",
timeZone: "America/Toronto",
bookingSeat: {
referenceUid: "booking-seat-1",
data: {},
},
}),
],
},
],
organizer,
})
);
mockSuccessfulVideoMeetingCreation({
metadataLookupKey: "dailyvideo",
videoMeetingData: {
id: "MOCK_ID",
password: "MOCK_PASS",
url: `http://mock-dailyvideo.example.com/meeting-1`,
},
});
const reqBookingUser = "seatedAttendee";
const mockBookingData = getMockRequestDataForBooking({
data: {
eventTypeId: 1,
responses: {
email: booker.email,
name: booker.name,
location: { optionValue: "", value: BookingLocations.CalVideo },
},
bookingUid: bookingUid,
user: reqBookingUser,
},
});
await handleNewBooking({
bookingData: mockBookingData,
});
const newAttendee = await prismaMock.attendee.findFirst({
where: {
email: booker.email,
bookingId: bookingId,
},
include: {
bookingSeat: true,
},
});
// Check for the existence of the new attendee w/ booking seat
expect(newAttendee?.bookingSeat).toEqual(
expect.objectContaining({
referenceUid: expect.any(String),
data: expect.any(Object),
bookingId: bookingId,
})
);
});
// Testing in case of a wave of people book a time slot at the same time
test("Attendee should be added to existing seated event when bookingUid is not present", async () => {
const handleNewBooking = getNewBookingHandler();
const booker = getBooker({
email: "seat2@example.com",
name: "Seat 2",
});
const organizer = getOrganizer({
name: "Organizer",
email: "organizer@example.com",
id: 101,
schedules: [TestData.schedules.IstWorkHours],
});
const bookingId = 1;
const bookingUid = "abc123";
const { dateString: plus1DateString } = getDate({ dateIncrement: 1 });
const bookingStartTime = `${plus1DateString}T04:00:00Z`;
const bookingEndTime = `${plus1DateString}T04:30:00Z`;
await createBookingScenario(
getScenarioData({
eventTypes: [
{
id: 1,
slug: "seated-event",
slotInterval: 30,
length: 30,
users: [
{
id: 101,
},
],
seatsPerTimeSlot: 3,
seatsShowAttendees: false,
},
],
bookings: [
{
id: bookingId,
uid: bookingUid,
eventTypeId: 1,
status: BookingStatus.ACCEPTED,
startTime: bookingStartTime,
endTime: bookingEndTime,
metadata: {
videoCallUrl: "https://existing-daily-video-call-url.example.com",
},
references: [
{
type: appStoreMetadata.dailyvideo.type,
uid: "MOCK_ID",
meetingId: "MOCK_ID",
meetingPassword: "MOCK_PASS",
meetingUrl: "http://mock-dailyvideo.example.com",
credentialId: null,
},
],
attendees: [
getMockBookingAttendee({
id: 1,
name: "Seat 1",
email: "seat1@test.com",
locale: "en",
timeZone: "America/Toronto",
bookingSeat: {
referenceUid: "booking-seat-1",
data: {},
},
}),
],
},
],
organizer,
})
);
mockSuccessfulVideoMeetingCreation({
metadataLookupKey: "dailyvideo",
videoMeetingData: {
id: "MOCK_ID",
password: "MOCK_PASS",
url: `http://mock-dailyvideo.example.com/meeting-1`,
},
});
const reqBookingUser = "seatedAttendee";
const mockBookingData = getMockRequestDataForBooking({
data: {
eventTypeId: 1,
responses: {
email: booker.email,
name: booker.name,
location: { optionValue: "", value: BookingLocations.CalVideo },
},
user: reqBookingUser,
},
});
await handleNewBooking({
bookingData: mockBookingData,
});
const newAttendee = await prismaMock.attendee.findFirst({
where: {
email: booker.email,
bookingId: bookingId,
},
include: {
bookingSeat: true,
},
});
// Check for the existence of the new attendee w/ booking seat
expect(newAttendee?.bookingSeat).toEqual(
expect.objectContaining({
referenceUid: expect.any(String),
data: expect.any(Object),
bookingId: bookingId,
})
);
});
test("If attendee is already a part of the booking then throw an error", async () => {
const handleNewBooking = getNewBookingHandler();
const booker = getBooker({
email: "seat1@example.com",
name: "Seat 1",
});
const organizer = getOrganizer({
name: "Organizer",
email: "organizer@example.com",
id: 101,
schedules: [TestData.schedules.IstWorkHours],
});
const bookingId = 1;
const bookingUid = "abc123";
const { dateString: plus1DateString } = getDate({ dateIncrement: 1 });
const bookingStartTime = `${plus1DateString}T04:00:00Z`;
const bookingEndTime = `${plus1DateString}T04:30:00Z`;
await createBookingScenario(
getScenarioData({
eventTypes: [
{
id: 1,
slug: "seated-event",
slotInterval: 30,
length: 30,
users: [
{
id: 101,
},
],
seatsPerTimeSlot: 3,
seatsShowAttendees: false,
},
],
bookings: [
{
id: bookingId,
uid: bookingUid,
eventTypeId: 1,
status: BookingStatus.ACCEPTED,
startTime: bookingStartTime,
endTime: bookingEndTime,
metadata: {
videoCallUrl: "https://existing-daily-video-call-url.example.com",
},
references: [
{
type: appStoreMetadata.dailyvideo.type,
uid: "MOCK_ID",
meetingId: "MOCK_ID",
meetingPassword: "MOCK_PASS",
meetingUrl: "http://mock-dailyvideo.example.com",
credentialId: null,
},
],
attendees: [
getMockBookingAttendee({
id: 1,
name: "Seat 1",
email: "seat1@example.com",
locale: "en",
timeZone: "America/Toronto",
bookingSeat: {
referenceUid: "booking-seat-1",
data: {},
},
}),
],
},
],
organizer,
})
);
mockSuccessfulVideoMeetingCreation({
metadataLookupKey: "dailyvideo",
videoMeetingData: {
id: "MOCK_ID",
password: "MOCK_PASS",
url: `http://mock-dailyvideo.example.com/meeting-1`,
},
});
const reqBookingUser = "seatedAttendee";
const mockBookingData = getMockRequestDataForBooking({
data: {
eventTypeId: 1,
responses: {
email: booker.email,
name: booker.name,
location: { optionValue: "", value: BookingLocations.CalVideo },
},
bookingUid: bookingUid,
user: reqBookingUser,
},
});
await expect(() =>
handleNewBooking({
bookingData: mockBookingData,
})
).rejects.toThrowError(ErrorCode.AlreadySignedUpForBooking);
});
test("If event is already full, fail", async () => {
const handleNewBooking = getNewBookingHandler();
const booker = getBooker({
email: "seat3@example.com",
name: "Seat 3",
});
const organizer = getOrganizer({
name: "Organizer",
email: "organizer@example.com",
id: 101,
schedules: [TestData.schedules.IstWorkHours],
});
const bookingId = 1;
const bookingUid = "abc123";
const { dateString: plus1DateString } = getDate({ dateIncrement: 1 });
const bookingStartTime = `${plus1DateString}T04:00:00Z`;
const bookingEndTime = `${plus1DateString}T04:30:00Z`;
await createBookingScenario(
getScenarioData({
eventTypes: [
{
id: 1,
slug: "seated-event",
slotInterval: 30,
length: 30,
users: [
{
id: 101,
},
],
seatsPerTimeSlot: 2,
seatsShowAttendees: false,
},
],
bookings: [
{
id: bookingId,
uid: bookingUid,
eventTypeId: 1,
status: BookingStatus.ACCEPTED,
startTime: bookingStartTime,
endTime: bookingEndTime,
metadata: {
videoCallUrl: "https://existing-daily-video-call-url.example.com",
},
references: [
{
type: appStoreMetadata.dailyvideo.type,
uid: "MOCK_ID",
meetingId: "MOCK_ID",
meetingPassword: "MOCK_PASS",
meetingUrl: "http://mock-dailyvideo.example.com",
credentialId: null,
},
],
attendees: [
getMockBookingAttendee({
id: 1,
name: "Seat 1",
email: "seat1@test.com",
locale: "en",
timeZone: "America/Toronto",
bookingSeat: {
referenceUid: "booking-seat-1",
data: {},
},
}),
getMockBookingAttendee({
id: 2,
name: "Seat 2",
email: "seat2@test.com",
locale: "en",
timeZone: "America/Toronto",
bookingSeat: {
referenceUid: "booking-seat-2",
data: {},
},
}),
],
},
],
organizer,
})
);
mockSuccessfulVideoMeetingCreation({
metadataLookupKey: "dailyvideo",
videoMeetingData: {
id: "MOCK_ID",
password: "MOCK_PASS",
url: `http://mock-dailyvideo.example.com/meeting-1`,
},
});
const reqBookingUser = "seatedAttendee";
const mockBookingData = getMockRequestDataForBooking({
data: {
eventTypeId: 1,
responses: {
email: booker.email,
name: booker.name,
location: { optionValue: "", value: BookingLocations.CalVideo },
},
bookingUid: bookingUid,
user: reqBookingUser,
},
});
await expect(() =>
handleNewBooking({
bookingData: mockBookingData,
})
).rejects.toThrowError(ErrorCode.BookingSeatsFull);
});
test("Verify Seat Availability Calculation Based on Booked Seats, Not Total Attendees", async () => {
const handleNewBooking = getNewBookingHandler();
const booker = getBooker({
email: "seat2@example.com",
name: "Seat 2",
});
const organizer = getOrganizer({
name: "Organizer",
email: "organizer@example.com",
id: 101,
defaultScheduleId: null,
schedules: [TestData.schedules.IstMorningShift],
credentials: [getGoogleCalendarCredential()],
selectedCalendars: [TestData.selectedCalendars.google],
destinationCalendar: {
integration: TestData.apps["google-calendar"].type,
externalId: "organizer@google-calendar.com",
},
});
const otherTeamMembers = [
{
name: "Other Team Member 1",
username: "other-team-member-1",
timeZone: Timezones["+5:30"],
defaultScheduleId: null,
email: "other-team-member-1@example.com",
id: 102,
schedules: [TestData.schedules.IstEveningShift],
credentials: [getGoogleCalendarCredential()],
selectedCalendars: [TestData.selectedCalendars.google],
destinationCalendar: {
integration: TestData.apps["google-calendar"].type,
externalId: "other-team-member-1@google-calendar.com",
},
},
];
const bookingId = 1;
const bookingUid = "abc123";
const { dateString: plus1DateString } = getDate({ dateIncrement: 1 });
const bookingStartTime = `${plus1DateString}T04:00:00Z`;
const bookingEndTime = `${plus1DateString}T04:30:00Z`;
await createBookingScenario(
getScenarioData({
eventTypes: [
{
id: 1,
slug: "collective-team-seated-event",
slotInterval: 30,
length: 30,
schedulingType: SchedulingType.COLLECTIVE,
users: [
{
id: 101,
},
{
id: 102,
},
],
destinationCalendar: {
integration: TestData.apps["google-calendar"].type,
externalId: "event-type-1@google-calendar.com",
},
seatsPerTimeSlot: 2,
seatsShowAttendees: false,
},
],
bookings: [
{
id: bookingId,
uid: bookingUid,
eventTypeId: 1,
status: BookingStatus.ACCEPTED,
startTime: bookingStartTime,
endTime: bookingEndTime,
metadata: {
videoCallUrl: "https://existing-daily-video-call-url.example.com",
},
references: [
{
type: appStoreMetadata.dailyvideo.type,
uid: "MOCK_ID",
meetingId: "MOCK_ID",
meetingPassword: "MOCK_PASS",
meetingUrl: "http://mock-dailyvideo.example.com",
credentialId: null,
},
],
attendees: [
getMockBookingAttendee({
id: 1,
name: "Other Team Member 1",
email: "other-team-member-1@example.com",
locale: "en",
timeZone: "America/Toronto",
}),
getMockBookingAttendee({
id: 2,
name: "Seat 1",
email: "seat1@test.com",
locale: "en",
timeZone: "America/Toronto",
bookingSeat: {
referenceUid: "booking-seat-1",
data: {},
},
}),
],
},
],
organizer,
usersApartFromOrganizer: otherTeamMembers,
apps: [TestData.apps["google-calendar"], TestData.apps["daily-video"]],
})
);
mockSuccessfulVideoMeetingCreation({
metadataLookupKey: "dailyvideo",
videoMeetingData: {
id: "MOCK_ID",
password: "MOCK_PASS",
url: `http://mock-dailyvideo.example.com/meeting-1`,
},
});
const reqBookingUser = "seatedAttendee";
const mockBookingData = getMockRequestDataForBooking({
data: {
eventTypeId: 1,
responses: {
email: booker.email,
name: booker.name,
location: { optionValue: "", value: BookingLocations.CalVideo },
},
bookingUid: bookingUid,
user: reqBookingUser,
},
});
await handleNewBooking({
bookingData: mockBookingData,
});
const newAttendee = await prismaMock.attendee.findFirst({
where: {
email: booker.email,
bookingId: bookingId,
},
include: {
bookingSeat: true,
},
});
// Check for the existence of the new attendee with booking seat
expect(newAttendee?.bookingSeat).toEqual(
expect.objectContaining({
referenceUid: expect.any(String),
data: expect.any(Object),
bookingId: bookingId,
})
);
// Verify that the booking seat count is now 2 out of 2
const bookingSeatCount = await prismaMock.bookingSeat.count({
where: {
bookingId: bookingId,
},
});
expect(bookingSeatCount).toBe(2);
});
});
describe("Rescheduling a booking", () => {
test("When rescheduling to an existing booking, move attendee", async () => {
const handleNewBooking = getNewBookingHandler();
const attendeeToReschedule = getMockBookingAttendee({
id: 2,
name: "Seat 2",
email: "seat2@test.com",
locale: "en",
timeZone: "America/Toronto",
bookingSeat: {
referenceUid: "booking-seat-2",
data: {},
},
});
const booker = getBooker({
email: attendeeToReschedule.email,
name: attendeeToReschedule.name,
});
const organizer = getOrganizer({
name: "Organizer",
email: "organizer@example.com",
id: 101,
schedules: [TestData.schedules.IstWorkHours],
});
const firstBookingId = 1;
const firstBookingUid = "abc123";
const { dateString: plus1DateString } = getDate({ dateIncrement: 1 });
const firstBookingStartTime = `${plus1DateString}T04:00:00Z`;
const firstBookingEndTime = `${plus1DateString}T04:30:00Z`;
const secondBookingId = 2;
const secondBookingUid = "def456";
const { dateString: plus2DateString } = getDate({ dateIncrement: 2 });
const secondBookingStartTime = `${plus2DateString}T04:00:00Z`;
const secondBookingEndTime = `${plus2DateString}T04:30:00Z`;
await createBookingScenario(
getScenarioData({
eventTypes: [
{
id: 1,
slug: "seated-event",
slotInterval: 30,
length: 30,
users: [
{
id: 101,
},
],
seatsPerTimeSlot: 3,
seatsShowAttendees: false,
},
],
bookings: [
{
id: firstBookingId,
uid: firstBookingUid,
eventTypeId: 1,
status: BookingStatus.ACCEPTED,
startTime: firstBookingStartTime,
endTime: firstBookingEndTime,
metadata: {
videoCallUrl: "https://existing-daily-video-call-url.example.com",
},
references: [
{
type: appStoreMetadata.dailyvideo.type,
uid: "MOCK_ID",
meetingId: "MOCK_ID",
meetingPassword: "MOCK_PASS",
meetingUrl: "http://mock-dailyvideo.example.com",
credentialId: null,
},
],
attendees: [
getMockBookingAttendee({
id: 1,
name: "Seat 1",
email: "seat1@test.com",
locale: "en",
timeZone: "America/Toronto",
bookingSeat: {
referenceUid: "booking-seat-1",
data: {},
},
}),
attendeeToReschedule,
],
},
{
id: secondBookingId,
uid: secondBookingUid,
eventTypeId: 1,
status: BookingStatus.ACCEPTED,
startTime: secondBookingStartTime,
endTime: secondBookingEndTime,
metadata: {
videoCallUrl: "https://existing-daily-video-call-url.example.com",
},
references: [
{
type: appStoreMetadata.dailyvideo.type,
uid: "MOCK_ID",
meetingId: "MOCK_ID",
meetingPassword: "MOCK_PASS",
meetingUrl: "http://mock-dailyvideo.example.com",
credentialId: null,
},
],
attendees: [
getMockBookingAttendee({
id: 3,
name: "Seat 3",
email: "seat3@test.com",
locale: "en",
timeZone: "America/Toronto",
bookingSeat: {
referenceUid: "booking-seat-3",
data: {},
},
}),
],
},
],
organizer,
})
);
mockSuccessfulVideoMeetingCreation({
metadataLookupKey: "dailyvideo",
videoMeetingData: {
id: "MOCK_ID",
password: "MOCK_PASS",
url: `http://mock-dailyvideo.example.com/meeting-1`,
},
});
const reqBookingUser = "seatedAttendee";
const mockBookingData = getMockRequestDataForBooking({
data: {
eventTypeId: 1,
responses: {
email: booker.email,
name: booker.name,
location: { optionValue: "", value: BookingLocations.CalVideo },
},
rescheduleUid: "booking-seat-2",
start: secondBookingStartTime,
end: secondBookingEndTime,
user: reqBookingUser,
},
});
await handleNewBooking({
bookingData: mockBookingData,
});
// Ensure that the attendee is no longer a part of the old booking
const oldBookingAttendees = await prismaMock.attendee.findMany({
where: {
bookingId: firstBookingId,
},
select: {
id: true,
},
});
expect(oldBookingAttendees).not.toContain({ id: attendeeToReschedule.id });
expect(oldBookingAttendees).toHaveLength(1);
// Ensure that the attendee is a part of the new booking
const newBookingAttendees = await prismaMock.attendee.findMany({
where: {
bookingId: secondBookingId,
},
select: {
email: true,
},
});
expect(newBookingAttendees).toContainEqual({ email: attendeeToReschedule.email });
expect(newBookingAttendees).toHaveLength(2);
// Ensure that the attendeeSeat is also updated to the new booking
const attendeeSeat = await prismaMock.bookingSeat.findFirst({
where: {
attendeeId: attendeeToReschedule.id,
},
select: {
bookingId: true,
},
});
expect(attendeeSeat?.bookingId).toEqual(secondBookingId);
});
test("When rescheduling to an empty timeslot, create a new booking", async () => {
const handleNewBooking = getNewBookingHandler();
const attendeeToReschedule = getMockBookingAttendee({
id: 2,
name: "Seat 2",
email: "seat2@test.com",
locale: "en",
timeZone: "America/Toronto",
bookingSeat: {
referenceUid: "booking-seat-2",
data: {},
},
});
const booker = getBooker({
email: attendeeToReschedule.email,
name: attendeeToReschedule.name,
});
const organizer = getOrganizer({
name: "Organizer",
email: "organizer@example.com",
id: 101,
schedules: [TestData.schedules.IstWorkHours],
});
const firstBookingId = 1;
const firstBookingUid = "abc123";
const { dateString: plus1DateString } = getDate({ dateIncrement: 1 });
const firstBookingStartTime = `${plus1DateString}T04:00:00Z`;
const firstBookingEndTime = `${plus1DateString}T04:30:00Z`;
const { dateString: plus2DateString } = getDate({ dateIncrement: 2 });
const secondBookingStartTime = `${plus2DateString}T04:00:00Z`;
const secondBookingEndTime = `${plus2DateString}T04:30:00Z`;
await createBookingScenario(
getScenarioData({
eventTypes: [
{
id: 1,
slug: "seated-event",
slotInterval: 30,
length: 30,
users: [
{
id: 101,
},
],
seatsPerTimeSlot: 3,
seatsShowAttendees: false,
},
],
bookings: [
{
id: firstBookingId,
uid: firstBookingUid,
eventTypeId: 1,
status: BookingStatus.ACCEPTED,
startTime: firstBookingStartTime,
endTime: firstBookingEndTime,
metadata: {
videoCallUrl: "https://existing-daily-video-call-url.example.com",
},
references: [
{
type: appStoreMetadata.dailyvideo.type,
uid: "MOCK_ID",
meetingId: "MOCK_ID",
meetingPassword: "MOCK_PASS",
meetingUrl: "http://mock-dailyvideo.example.com",
credentialId: null,
},
],
attendees: [
getMockBookingAttendee({
id: 1,
name: "Seat 1",
email: "seat1@test.com",
locale: "en",
timeZone: "America/Toronto",
bookingSeat: {
referenceUid: "booking-seat-1",
data: {},
},
}),
attendeeToReschedule,
],
},
],
organizer,
})
);
mockSuccessfulVideoMeetingCreation({
metadataLookupKey: "dailyvideo",
videoMeetingData: {
id: "MOCK_ID",
password: "MOCK_PASS",
url: `http://mock-dailyvideo.example.com/meeting-1`,
},
});
const reqBookingUser = "seatedAttendee";
const mockBookingData = getMockRequestDataForBooking({
data: {
eventTypeId: 1,
responses: {
email: booker.email,
name: booker.name,
location: { optionValue: "", value: BookingLocations.CalVideo },
},
rescheduleUid: "booking-seat-2",
start: secondBookingStartTime,
end: secondBookingEndTime,
user: reqBookingUser,
},
});
const { req } = createMockNextJsRequest({
method: "POST",
body: mockBookingData,
});
const createdBooking = await handleNewBooking({
bookingData: mockBookingData,
});
// Ensure that the attendee is no longer a part of the old booking
const oldBookingAttendees = await prismaMock.attendee.findMany({
where: {
bookingId: firstBookingId,
},
select: {
id: true,
},
});
expect(oldBookingAttendees).not.toContain({ id: attendeeToReschedule.id });
expect(oldBookingAttendees).toHaveLength(1);
expect(createdBooking.id).not.toEqual(firstBookingId);
// Ensure that the attendee and bookingSeat is also updated to the new booking
const attendee = await prismaMock.attendee.findFirst({
where: {
bookingId: createdBooking.id,
},
include: {
bookingSeat: true,
},
});
expect(attendee?.bookingSeat?.bookingId).toEqual(createdBooking.id);
});
test("When last attendee is rescheduled, delete old booking", async () => {
const handleNewBooking = getNewBookingHandler();
const attendeeToReschedule = getMockBookingAttendee({
id: 2,
name: "Seat 2",
email: "seat2@test.com",
locale: "en",
timeZone: "America/Toronto",
bookingSeat: {
referenceUid: "booking-seat-2",
data: {},
},
});
const booker = getBooker({
email: attendeeToReschedule.email,
name: attendeeToReschedule.name,
});
const organizer = getOrganizer({
name: "Organizer",
email: "organizer@example.com",
id: 101,
schedules: [TestData.schedules.IstWorkHours],
});
const firstBookingId = 1;
const firstBookingUid = "abc123";
const { dateString: plus1DateString } = getDate({ dateIncrement: 1 });
const firstBookingStartTime = `${plus1DateString}T04:00:00Z`;
const firstBookingEndTime = `${plus1DateString}T04:30:00Z`;
const { dateString: plus2DateString } = getDate({ dateIncrement: 2 });
const secondBookingStartTime = `${plus2DateString}T04:00:00Z`;
const secondBookingEndTime = `${plus2DateString}T04:30:00Z`;
await createBookingScenario(
getScenarioData({
eventTypes: [
{
id: 1,
slug: "seated-event",
slotInterval: 30,
length: 30,
users: [
{
id: 101,
},
],
seatsPerTimeSlot: 3,
seatsShowAttendees: false,
},
],
bookings: [
{
id: firstBookingId,
uid: firstBookingUid,
eventTypeId: 1,
status: BookingStatus.ACCEPTED,
startTime: firstBookingStartTime,
endTime: firstBookingEndTime,
metadata: {
videoCallUrl: "https://existing-daily-video-call-url.example.com",
},
references: [
{
type: appStoreMetadata.dailyvideo.type,
uid: "MOCK_ID",
meetingId: "MOCK_ID",
meetingPassword: "MOCK_PASS",
meetingUrl: "http://mock-dailyvideo.example.com",
credentialId: null,
},
],
attendees: [attendeeToReschedule],
},
],
organizer,
})
);
mockSuccessfulVideoMeetingCreation({
metadataLookupKey: "dailyvideo",
videoMeetingData: {
id: "MOCK_ID",
password: "MOCK_PASS",
url: `http://mock-dailyvideo.example.com/meeting-1`,
},
});
const reqBookingUser = "seatedAttendee";
const mockBookingData = getMockRequestDataForBooking({
data: {
eventTypeId: 1,
responses: {
email: booker.email,
name: booker.name,
location: { optionValue: "", value: BookingLocations.CalVideo },
},
rescheduleUid: "booking-seat-2",
start: secondBookingStartTime,
end: secondBookingEndTime,
user: reqBookingUser,
},
});
const createdBooking = await handleNewBooking({
bookingData: mockBookingData,
});
// Ensure that the old booking is cancelled
const oldBooking = await prismaMock.booking.findFirst({
where: {
id: firstBookingId,
},
select: {
status: true,
},
});
expect(oldBooking?.status).toEqual(BookingStatus.CANCELLED);
// Ensure that the attendee and attendeeSeat is also updated to the new booking
const attendeeSeat = await prismaMock.attendee.findFirst({
where: {
bookingId: createdBooking.id,
},
include: {
bookingSeat: true,
},
});
expect(attendeeSeat?.bookingSeat?.bookingId).toEqual(createdBooking.id);
});
});
describe("Canceling a booking", async () => {
test("When canceling a booking, only remove that single attendee", async () => {
const handleCancelBooking = (await import("@calcom/features/bookings/lib/handleCancelBooking"))
.default;
const organizer = getOrganizer({
name: "Organizer",
email: "organizer@example.com",
id: 101,
schedules: [TestData.schedules.IstWorkHours],
});
const bookingId = 1;
const bookingUid = "abc123";
const { dateString: plus1DateString } = getDate({ dateIncrement: 1 });
const bookingStartTime = `${plus1DateString}T04:00:00Z`;
const bookingEndTime = `${plus1DateString}T04:30:00Z`;
const attendeeIdToBeCancelled = 2;
const bookingSeatToBeCancelledUid = "booking-seat-2";
await createBookingScenario(
getScenarioData({
eventTypes: [
{
id: 1,
slug: "seated-event",
slotInterval: 30,
length: 30,
users: [
{
id: 101,
},
],
seatsPerTimeSlot: 4,
seatsShowAttendees: false,
owner: organizer.id,
},
],
bookings: [
{
id: bookingId,
uid: bookingUid,
eventTypeId: 1,
userId: organizer.id,
status: BookingStatus.ACCEPTED,
startTime: bookingStartTime,
endTime: bookingEndTime,
metadata: {
videoCallUrl: "https://existing-daily-video-call-url.example.com",
},
references: [
{
type: appStoreMetadata.dailyvideo.type,
uid: "MOCK_ID",
meetingId: "MOCK_ID",
meetingPassword: "MOCK_PASS",
meetingUrl: "http://mock-dailyvideo.example.com",
credentialId: null,
},
],
attendees: [
getMockBookingAttendee({
id: 1,
name: "Seat 1",
email: "seat1@test.com",
locale: "en",
timeZone: "America/Toronto",
bookingSeat: {
referenceUid: "booking-seat-1",
data: {},
},
}),
getMockBookingAttendee({
id: attendeeIdToBeCancelled,
name: "Seat 2",
email: "seat2@test.com",
locale: "en",
timeZone: "America/Toronto",
bookingSeat: {
referenceUid: bookingSeatToBeCancelledUid,
data: {},
},
}),
],
},
],
organizer,
})
);
mockSuccessfulVideoMeetingCreation({
metadataLookupKey: "dailyvideo",
videoMeetingData: {
id: "MOCK_ID",
password: "MOCK_PASS",
url: `http://mock-dailyvideo.example.com/meeting-1`,
},
});
const mockCancelBookingData = getMockRequestDataForCancelBooking({
id: bookingId,
uid: bookingUid,
seatReferenceUid: bookingSeatToBeCancelledUid,
});
await handleCancelBooking({
bookingData: {
...mockCancelBookingData,
cancellationReason: "test cancellation reason",
},
userId: organizer.id,
});
// Ensure that the booking has been cancelled
const cancelledBooking = await prismaMock.booking.findFirst({
where: {
id: bookingId,
},
select: {
status: true,
},
});
// Check that the booking is still accepted
expect(cancelledBooking?.status).toEqual(BookingStatus.ACCEPTED);
// Check that the booking does not contain the cancelled attendee
const attendees = await prismaMock.attendee.findMany({
where: {
bookingId,
},
select: {
id: true,
},
});
expect(attendees).not.toContain({ id: attendeeIdToBeCancelled });
const bookingSeats = await prismaMock.bookingSeat.findMany({
where: {
bookingId,
},
select: {
id: true,
},
});
expect(bookingSeats).not.toContain({ referenceUid: bookingSeatToBeCancelledUid });
});
test("When last attendee cancels a booking, delete event", async () => {
const handleCancelBooking = (await import("@calcom/features/bookings/lib/handleCancelBooking"))
.default;
const organizer = getOrganizer({
name: "Organizer",
email: "organizer@example.com",
id: 101,
schedules: [TestData.schedules.IstWorkHours],
});
const bookingId = 1;
const bookingUid = "abc123";
const { dateString: plus1DateString } = getDate({ dateIncrement: 1 });
const bookingStartTime = `${plus1DateString}T04:00:00Z`;
const bookingEndTime = `${plus1DateString}T04:30:00Z`;
const attendeeIdToBeCancelled = 1;
const bookingSeatToBeCancelledUid = "booking-seat-1";
await createBookingScenario(
getScenarioData({
eventTypes: [
{
id: 1,
slug: "seated-event",
slotInterval: 30,
length: 30,
users: [
{
id: 101,
},
],
seatsPerTimeSlot: 4,
seatsShowAttendees: false,
owner: organizer.id,
},
],
bookings: [
{
id: bookingId,
uid: bookingUid,
eventTypeId: 1,
userId: organizer.id,
status: BookingStatus.ACCEPTED,
startTime: bookingStartTime,
endTime: bookingEndTime,
metadata: {
videoCallUrl: "https://existing-daily-video-call-url.example.com",
},
references: [
{
type: appStoreMetadata.dailyvideo.type,
uid: "MOCK_ID",
meetingId: "MOCK_ID",
meetingPassword: "MOCK_PASS",
meetingUrl: "http://mock-dailyvideo.example.com",
credentialId: null,
},
],
attendees: [
getMockBookingAttendee({
id: attendeeIdToBeCancelled,
name: "Seat 1",
email: "seat1@test.com",
locale: "en",
timeZone: "America/Toronto",
bookingSeat: {
referenceUid: bookingSeatToBeCancelledUid,
data: {},
},
}),
],
},
],
organizer,
})
);
mockSuccessfulVideoMeetingCreation({
metadataLookupKey: "dailyvideo",
videoMeetingData: {
id: "MOCK_ID",
password: "MOCK_PASS",
url: `http://mock-dailyvideo.example.com/meeting-1`,
},
});
const mockCancelBookingData = getMockRequestDataForCancelBooking({
id: bookingId,
uid: bookingUid,
seatReferenceUid: bookingSeatToBeCancelledUid,
});
await handleCancelBooking({
bookingData: {
...mockCancelBookingData,
cancellationReason: "test cancellation reason",
},
userId: organizer.id,
});
// Ensure that the booking has been cancelled
const cancelledBooking = await prismaMock.booking.findFirst({
where: {
id: bookingId,
},
select: {
status: true,
},
});
// Check that the booking is still accepted
expect(cancelledBooking?.status).toEqual(BookingStatus.CANCELLED);
// Check that the booking does not contain the cancelled attendee
const attendees = await prismaMock.attendee.findMany({
where: {
bookingId,
},
select: {
id: true,
},
});
expect(attendees).not.toContain({ id: attendeeIdToBeCancelled });
const bookingSeats = await prismaMock.bookingSeat.findMany({
where: {
bookingId,
},
select: {
id: true,
},
});
expect(bookingSeats).not.toContain({ referenceUid: bookingSeatToBeCancelledUid });
});
});
});
describe("As an owner", () => {
describe("Rescheduling a booking", () => {
test("When rescheduling to new timeslot, ensure all attendees are moved", async () => {
const handleNewBooking = getNewBookingHandler();
const booker = getBooker({
email: "booker@example.com",
name: "Booker",
});
const organizer = getOrganizer({
name: "Organizer",
email: "organizer@example.com",
id: 101,
schedules: [TestData.schedules.IstWorkHours],
});
const firstBookingId = 1;
const firstBookingUid = "abc123";
const { dateString: plus1DateString } = getDate({ dateIncrement: 1 });
const firstBookingStartTime = `${plus1DateString}T04:00:00Z`;
const firstBookingEndTime = `${plus1DateString}T04:30:00Z`;
const { dateString: plus2DateString } = getDate({ dateIncrement: 2 });
const secondBookingStartTime = `${plus2DateString}T04:00:00Z`;
const secondBookingEndTime = `${plus2DateString}T04:30:00Z`;
await createBookingScenario(
getScenarioData({
eventTypes: [
{
id: 1,
slug: "seated-event",
slotInterval: 30,
length: 30,
users: [
{
id: 101,
},
],
seatsPerTimeSlot: 3,
seatsShowAttendees: false,
},
],
bookings: [
{
id: firstBookingId,
uid: firstBookingUid,
eventTypeId: 1,
userId: organizer.id,
status: BookingStatus.ACCEPTED,
startTime: firstBookingStartTime,
endTime: firstBookingEndTime,
metadata: {
videoCallUrl: "https://existing-daily-video-call-url.example.com",
},
references: [
{
type: appStoreMetadata.dailyvideo.type,
uid: "MOCK_ID",
meetingId: "MOCK_ID",
meetingPassword: "MOCK_PASS",
meetingUrl: "http://mock-dailyvideo.example.com",
credentialId: null,
},
],
attendees: [
getMockBookingAttendee({
id: 1,
name: "Seat 1",
email: "seat1@test.com",
locale: "en",
timeZone: "America/Toronto",
bookingSeat: {
referenceUid: "booking-seat-1",
data: {},
},
}),
getMockBookingAttendee({
id: 2,
name: "Seat 2",
email: "seat2@test.com",
locale: "en",
timeZone: "America/Toronto",
bookingSeat: {
referenceUid: "booking-seat-2",
data: {},
},
}),
getMockBookingAttendee({
id: 3,
name: "Seat 3",
email: "seat3@test.com",
locale: "en",
timeZone: "America/Toronto",
bookingSeat: {
referenceUid: "booking-seat-3",
data: {},
},
}),
],
},
],
organizer,
})
);
mockSuccessfulVideoMeetingCreation({
metadataLookupKey: "dailyvideo",
videoMeetingData: {
id: "MOCK_ID",
password: "MOCK_PASS",
url: `http://mock-dailyvideo.example.com/meeting-1`,
},
});
const reqBookingUser = "seatedAttendee";
const mockBookingData = getMockRequestDataForBooking({
data: {
eventTypeId: 1,
responses: {
email: booker.email,
name: booker.name,
location: { optionValue: "", value: BookingLocations.CalVideo },
},
rescheduleUid: firstBookingUid,
start: secondBookingStartTime,
end: secondBookingEndTime,
user: reqBookingUser,
},
});
const rescheduledBooking = await handleNewBooking({
bookingData: mockBookingData,
userId: organizer.id,
});
// Ensure that the booking has been moved
expect(rescheduledBooking?.startTime).toEqual(secondBookingStartTime);
expect(rescheduledBooking?.endTime).toEqual(secondBookingEndTime);
// Ensure that the attendees are still a part of the event
const attendees = await prismaMock.attendee.findMany({
where: {
bookingId: rescheduledBooking?.id,
},
});
expect(attendees).toHaveLength(3);
// Ensure that the bookingSeats are still a part of the event
const bookingSeats = await prismaMock.bookingSeat.findMany({
where: {
bookingId: rescheduledBooking?.id,
},
});
expect(bookingSeats).toHaveLength(3);
});
test("When rescheduling to existing booking, merge attendees ", async () => {
const handleNewBooking = getNewBookingHandler();
const booker = getBooker({
email: "booker@example.com",
name: "Booker",
});
const organizer = getOrganizer({
name: "Organizer",
email: "organizer@example.com",
id: 101,
schedules: [TestData.schedules.IstWorkHours],
});
const firstBookingId = 1;
const firstBookingUid = "abc123";
const { dateString: plus1DateString } = getDate({ dateIncrement: 1 });
const firstBookingStartTime = `${plus1DateString}T04:00:00Z`;
const firstBookingEndTime = `${plus1DateString}T04:30:00Z`;
const secondBookingId = 2;
const secondBookingUid = "def456";
const { dateString: plus2DateString } = getDate({ dateIncrement: 2 });
const secondBookingStartTime = `${plus2DateString}T04:00:00Z`;
const secondBookingEndTime = `${plus2DateString}T04:30:00Z`;
await createBookingScenario(
getScenarioData({
eventTypes: [
{
id: 1,
slug: "seated-event",
slotInterval: 30,
length: 30,
users: [
{
id: 101,
},
],
seatsPerTimeSlot: 4,
seatsShowAttendees: false,
},
],
bookings: [
{
id: firstBookingId,
uid: firstBookingUid,
eventTypeId: 1,
userId: organizer.id,
status: BookingStatus.ACCEPTED,
startTime: firstBookingStartTime,
endTime: firstBookingEndTime,
metadata: {
videoCallUrl: "https://existing-daily-video-call-url.example.com",
},
references: [
{
type: appStoreMetadata.dailyvideo.type,
uid: "MOCK_ID",
meetingId: "MOCK_ID",
meetingPassword: "MOCK_PASS",
meetingUrl: "http://mock-dailyvideo.example.com",
credentialId: null,
},
],
attendees: [
getMockBookingAttendee({
id: 1,
name: "Seat 1",
email: "seat1@test.com",
locale: "en",
timeZone: "America/Toronto",
bookingSeat: {
referenceUid: "booking-seat-1",
data: {},
},
}),
getMockBookingAttendee({
id: 2,
name: "Seat 2",
email: "seat2@test.com",
locale: "en",
timeZone: "America/Toronto",
bookingSeat: {
referenceUid: "booking-seat-2",
data: {},
},
}),
],
},
{
id: secondBookingId,
uid: secondBookingUid,
eventTypeId: 1,
status: BookingStatus.ACCEPTED,
startTime: secondBookingStartTime,
endTime: secondBookingEndTime,
metadata: {
videoCallUrl: "https://existing-daily-video-call-url.example.com",
},
references: [
{
type: appStoreMetadata.dailyvideo.type,
uid: "MOCK_ID",
meetingId: "MOCK_ID",
meetingPassword: "MOCK_PASS",
meetingUrl: "http://mock-dailyvideo.example.com",
credentialId: null,
},
],
attendees: [
getMockBookingAttendee({
id: 3,
name: "Seat 3",
email: "seat3@test.com",
locale: "en",
timeZone: "America/Toronto",
bookingSeat: {
referenceUid: "booking-seat-3",
data: {},
},
}),
getMockBookingAttendee({
id: 4,
name: "Seat 4",
email: "seat4@test.com",
locale: "en",
timeZone: "America/Toronto",
bookingSeat: {
referenceUid: "booking-seat-4",
data: {},
},
}),
],
},
],
organizer,
})
);
mockSuccessfulVideoMeetingCreation({
metadataLookupKey: "dailyvideo",
videoMeetingData: {
id: "MOCK_ID",
password: "MOCK_PASS",
url: `http://mock-dailyvideo.example.com/meeting-1`,
},
});
const reqBookingUser = "seatedAttendee";
const mockBookingData = getMockRequestDataForBooking({
data: {
eventTypeId: 1,
responses: {
email: booker.email,
name: booker.name,
location: { optionValue: "", value: BookingLocations.CalVideo },
},
rescheduleUid: firstBookingUid,
start: secondBookingStartTime,
end: secondBookingEndTime,
user: reqBookingUser,
},
});
const rescheduledBooking = await handleNewBooking({
bookingData: mockBookingData,
userId: organizer.id,
});
// Ensure that the booking has been moved
expect(rescheduledBooking?.startTime).toEqual(new Date(secondBookingStartTime));
expect(rescheduledBooking?.endTime).toEqual(new Date(secondBookingEndTime));
// Ensure that the attendees are still a part of the event
const attendees = await prismaMock.attendee.findMany({
where: {
bookingId: rescheduledBooking?.id,
},
});
expect(attendees).toHaveLength(4);
// Ensure that the bookingSeats are still a part of the event
const bookingSeats = await prismaMock.bookingSeat.findMany({
where: {
bookingId: rescheduledBooking?.id,
},
});
expect(bookingSeats).toHaveLength(4);
// Ensure that the previous booking has been canceled
const originalBooking = await prismaMock.booking.findFirst({
where: {
id: firstBookingId,
},
select: {
status: true,
},
});
expect(originalBooking?.status).toEqual(BookingStatus.CANCELLED);
});
test("When merging more attendees than seats, fail ", async () => {
const handleNewBooking = getNewBookingHandler();
const booker = getBooker({
email: "booker@example.com",
name: "Booker",
});
const organizer = getOrganizer({
name: "Organizer",
email: "organizer@example.com",
id: 101,
schedules: [TestData.schedules.IstWorkHours],
});
const firstBookingId = 1;
const firstBookingUid = "abc123";
const { dateString: plus1DateString } = getDate({ dateIncrement: 1 });
const firstBookingStartTime = `${plus1DateString}T04:00:00Z`;
const firstBookingEndTime = `${plus1DateString}T04:30:00Z`;
const secondBookingId = 2;
const secondBookingUid = "def456";
const { dateString: plus2DateString } = getDate({ dateIncrement: 2 });
const secondBookingStartTime = `${plus2DateString}T04:00:00Z`;
const secondBookingEndTime = `${plus2DateString}T04:30:00Z`;
await createBookingScenario(
getScenarioData({
eventTypes: [
{
id: 1,
slug: "seated-event",
slotInterval: 30,
length: 30,
users: [
{
id: 101,
},
],
seatsPerTimeSlot: 3,
seatsShowAttendees: false,
},
],
bookings: [
{
id: firstBookingId,
uid: firstBookingUid,
eventTypeId: 1,
userId: organizer.id,
status: BookingStatus.ACCEPTED,
startTime: firstBookingStartTime,
endTime: firstBookingEndTime,
metadata: {
videoCallUrl: "https://existing-daily-video-call-url.example.com",
},
references: [
{
type: appStoreMetadata.dailyvideo.type,
uid: "MOCK_ID",
meetingId: "MOCK_ID",
meetingPassword: "MOCK_PASS",
meetingUrl: "http://mock-dailyvideo.example.com",
credentialId: null,
},
],
attendees: [
getMockBookingAttendee({
id: 1,
name: "Seat 1",
email: "seat1@test.com",
locale: "en",
timeZone: "America/Toronto",
bookingSeat: {
referenceUid: "booking-seat-1",
data: {},
},
}),
getMockBookingAttendee({
id: 2,
name: "Seat 2",
email: "seat2@test.com",
locale: "en",
timeZone: "America/Toronto",
bookingSeat: {
referenceUid: "booking-seat-2",
data: {},
},
}),
],
},
{
id: secondBookingId,
uid: secondBookingUid,
eventTypeId: 1,
status: BookingStatus.ACCEPTED,
startTime: secondBookingStartTime,
endTime: secondBookingEndTime,
metadata: {
videoCallUrl: "https://existing-daily-video-call-url.example.com",
},
references: [
{
type: appStoreMetadata.dailyvideo.type,
uid: "MOCK_ID",
meetingId: "MOCK_ID",
meetingPassword: "MOCK_PASS",
meetingUrl: "http://mock-dailyvideo.example.com",
credentialId: null,
},
],
attendees: [
getMockBookingAttendee({
id: 3,
name: "Seat 3",
email: "seat3@test.com",
locale: "en",
timeZone: "America/Toronto",
bookingSeat: {
referenceUid: "booking-seat-3",
data: {},
},
}),
getMockBookingAttendee({
id: 4,
name: "Seat 4",
email: "seat4@test.com",
locale: "en",
timeZone: "America/Toronto",
bookingSeat: {
referenceUid: "booking-seat-4",
data: {},
},
}),
],
},
],
organizer,
})
);
mockSuccessfulVideoMeetingCreation({
metadataLookupKey: "dailyvideo",
videoMeetingData: {
id: "MOCK_ID",
password: "MOCK_PASS",
url: `http://mock-dailyvideo.example.com/meeting-1`,
},
});
const reqBookingUser = "seatedAttendee";
const mockBookingData = getMockRequestDataForBooking({
data: {
eventTypeId: 1,
responses: {
email: booker.email,
name: booker.name,
location: { optionValue: "", value: BookingLocations.CalVideo },
},
rescheduleUid: firstBookingUid,
start: secondBookingStartTime,
end: secondBookingEndTime,
user: reqBookingUser,
},
});
// const rescheduledBooking = await handleNewBooking(req);
await expect(() =>
handleNewBooking({
bookingData: mockBookingData,
userId: organizer.id,
})
).rejects.toThrowError(ErrorCode.NotEnoughAvailableSeats);
});
test("When trying to reschedule in a non-available slot, throw an error", async () => {
const handleNewBooking = getNewBookingHandler();
const booker = getBooker({
email: "booker@example.com",
name: "Booker",
});
const organizer = getOrganizer({
name: "Organizer",
email: "organizer@example.com",
id: 101,
schedules: [TestData.schedules.IstWorkHours],
});
const firstBookingId = 1;
const firstBookingUid = "abc123";
const { dateString: plus1DateString } = getDate({ dateIncrement: 1 });
const firstBookingStartTime = `${plus1DateString}T04:00:00Z`;
const firstBookingEndTime = `${plus1DateString}T04:30:00Z`;
const { dateString: plus2DateString } = getDate({ dateIncrement: 2 });
// Non-available time slot chosen (7:30PM - 8:00PM IST) while rescheduling
const secondBookingStartTime = `${plus2DateString}T14:00:00Z`;
const secondBookingEndTime = `${plus2DateString}T14:30:00Z`;
await createBookingScenario(
getScenarioData({
eventTypes: [
{
id: 1,
slug: "seated-event",
slotInterval: 30,
length: 30,
users: [
{
id: 101,
},
],
seatsPerTimeSlot: 3,
seatsShowAttendees: false,
},
],
bookings: [
{
id: firstBookingId,
uid: firstBookingUid,
eventTypeId: 1,
userId: organizer.id,
status: BookingStatus.ACCEPTED,
startTime: firstBookingStartTime,
endTime: firstBookingEndTime,
metadata: {
videoCallUrl: "https://existing-daily-video-call-url.example.com",
},
references: [
{
type: appStoreMetadata.dailyvideo.type,
uid: "MOCK_ID",
meetingId: "MOCK_ID",
meetingPassword: "MOCK_PASS",
meetingUrl: "http://mock-dailyvideo.example.com",
credentialId: null,
},
],
attendees: [
getMockBookingAttendee({
id: 1,
name: "Seat 1",
email: "seat1@test.com",
locale: "en",
timeZone: "America/Toronto",
bookingSeat: {
referenceUid: "booking-seat-1",
data: {},
},
noShow: false,
}),
getMockBookingAttendee({
id: 2,
name: "Seat 2",
email: "seat2@test.com",
locale: "en",
timeZone: "America/Toronto",
bookingSeat: {
referenceUid: "booking-seat-2",
data: {},
},
noShow: false,
}),
getMockBookingAttendee({
id: 3,
name: "Seat 3",
email: "seat3@test.com",
locale: "en",
timeZone: "America/Toronto",
bookingSeat: {
referenceUid: "booking-seat-3",
data: {},
},
noShow: false,
}),
],
},
],
organizer,
})
);
mockSuccessfulVideoMeetingCreation({
metadataLookupKey: "dailyvideo",
videoMeetingData: {
id: "MOCK_ID",
password: "MOCK_PASS",
url: `http://mock-dailyvideo.example.com/meeting-1`,
},
});
const reqBookingUser = "seatedAttendee";
const mockBookingData = getMockRequestDataForBooking({
data: {
eventTypeId: 1,
responses: {
email: booker.email,
name: booker.name,
location: { optionValue: "", value: BookingLocations.CalVideo },
},
rescheduleUid: firstBookingUid,
start: secondBookingStartTime,
end: secondBookingEndTime,
user: reqBookingUser,
},
});
await expect(() =>
handleNewBooking({
bookingData: mockBookingData,
userId: organizer.id,
})
).rejects.toThrowError(ErrorCode.NoAvailableUsersFound);
});
});
describe("Cancelling a booking", () => {
test("When owner cancels booking, cancel booking for all attendees", async () => {
const handleCancelBooking = (await import("@calcom/features/bookings/lib/handleCancelBooking"))
.default;
const booker = getBooker({
email: "booker@example.com",
name: "Booker",
});
const organizer = getOrganizer({
name: "Organizer",
email: "organizer@example.com",
id: 101,
schedules: [TestData.schedules.IstWorkHours],
});
const firstBookingId = 1;
const firstBookingUid = "abc123";
const { dateString: plus1DateString } = getDate({ dateIncrement: 1 });
const firstBookingStartTime = `${plus1DateString}T04:00:00Z`;
const firstBookingEndTime = `${plus1DateString}T04:30:00Z`;
await createBookingScenario(
getScenarioData({
eventTypes: [
{
id: 1,
slug: "seated-event",
slotInterval: 30,
length: 30,
users: [
{
id: 101,
},
],
seatsPerTimeSlot: 4,
seatsShowAttendees: false,
owner: organizer.id,
},
],
bookings: [
{
id: firstBookingId,
uid: firstBookingUid,
eventTypeId: 1,
userId: organizer.id,
status: BookingStatus.ACCEPTED,
startTime: firstBookingStartTime,
endTime: firstBookingEndTime,
metadata: {
videoCallUrl: "https://existing-daily-video-call-url.example.com",
},
references: [
{
type: appStoreMetadata.dailyvideo.type,
uid: "MOCK_ID",
meetingId: "MOCK_ID",
meetingPassword: "MOCK_PASS",
meetingUrl: "http://mock-dailyvideo.example.com",
credentialId: null,
},
],
attendees: [
getMockBookingAttendee({
id: 1,
name: "Seat 1",
email: "seat1@test.com",
locale: "en",
timeZone: "America/Toronto",
bookingSeat: {
referenceUid: "booking-seat-1",
data: {},
},
}),
getMockBookingAttendee({
id: 2,
name: "Seat 2",
email: "seat2@test.com",
locale: "en",
timeZone: "America/Toronto",
bookingSeat: {
referenceUid: "booking-seat-2",
data: {},
},
}),
],
},
],
organizer,
})
);
mockSuccessfulVideoMeetingCreation({
metadataLookupKey: "dailyvideo",
videoMeetingData: {
id: "MOCK_ID",
password: "MOCK_PASS",
url: `http://mock-dailyvideo.example.com/meeting-1`,
},
});
const mockBookingData = getMockRequestDataForBooking({
data: {
id: firstBookingId,
eventTypeId: 1,
responses: {
email: booker.email,
name: booker.name,
location: { optionValue: "", value: BookingLocations.CalVideo },
},
rescheduleUid: firstBookingUid,
cancelledBy: organizer.email,
},
});
await handleCancelBooking({
bookingData: {
...mockBookingData,
cancellationReason: "test cancellation reason",
},
userId: organizer.id,
});
// Ensure that the booking has been cancelled
const cancelledBooking = await prismaMock.booking.findFirst({
where: {
id: firstBookingId,
},
select: {
status: true,
cancelledBy: true,
},
});
expect(cancelledBooking?.status).toEqual(BookingStatus.CANCELLED);
expect(cancelledBooking?.cancelledBy).toEqual(organizer.email);
});
});
});
});