Files
calendar/apps/web/test/utils/bookingScenario/bookingScenario.ts
T
Keith WilliamsGitHubkeith@cal.com <keithwillcode@gmail.com>Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>cal.comMorgancubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>
6923b97cd2 feat: upgrade Prisma to 6.16.0 with no-rust engine (#23816)
* feat: upgrade Prisma to 6.16.0 with no-rust engine

- Update Prisma packages to 6.16.0
- Add PostgreSQL adapter dependency
- Configure engineType: 'client' and provider: 'prisma-client' in schema
- Update Prisma client instantiation with PostgreSQL adapter
- Remove binaryTargets from generators (not needed with library engine)
- Fix schema view issue by removing @id decorator from BookingTimeStatusDenormalized
- Fix ESLint warning by removing non-null assertion

Co-Authored-By: keith@cal.com <keithwillcode@gmail.com>

* Web app running but types wrecked

* web app running but build and type issues

* Removed the connection pool

* Fixed zod type issue

* Fixed types in booking reference extension

* Fixed test issues

* Type checks passing it seems

* Using cjs as moduleFormat

* Fixing Prisma undefined

* fix: update prismock initialization for Prisma 6.16 compatibility

- Add @prisma/internals dependency for getDMMF()
- Restructure prismock initialization to use createPrismock() with DMMF
- Create Proxy that's returned from mock factory for proper spy support
- Fixes 89 failing unit tests with 'Cannot read properties of undefined (reading datamodel)' error
- Based on workaround from prismock issue #1482

All unit tests now pass (375 test files, 3323 tests passed)

Co-Authored-By: keith@cal.com <keithwillcode@gmail.com>

* fix: cast serviceAccountKey to Prisma.InputJsonValue in bookingScenario.ts

- Apply type cast at lines 2493 and 2535
- Fixes type errors from Prisma 6.16 upgrade
- Follows established pattern from delegationCredential.ts
- Add eslint-disable for pre-existing any types
- Rename unused appStoreLookupKey parameter to satisfy lint
- All 3323 tests passing

Co-Authored-By: keith@cal.com <keithwillcode@gmail.com>

* chore: remove whitespace-only lines from bookingScenario.ts

- Remove blank lines where eslint-disable comments were replaced
- Cleanup from pre-commit hook formatting

Co-Authored-By: keith@cal.com <keithwillcode@gmail.com>

* Update is-prisma-available-check.ts

* fix: remove datasources config when using Prisma Driver Adapters

- Update customPrisma to create new adapter when datasources URL is provided
- Remove datasources config from API v2 Prisma services (already in adapter)
- Fixes 'Custom datasource configuration is not compatible with Prisma Driver Adapters' error

Co-Authored-By: keith@cal.com <keithwillcode@gmail.com>

* fix: use Pool instances for PrismaPg adapters in index.ts

- Create Pool instance before passing to PrismaPg adapter
- Update customPrisma to create Pool for custom connection strings
- Matches working pattern from API v2 services
- Fixes 'Invalid `prisma.$queryRawUnsafe()` invocation' error

Co-Authored-By: keith@cal.com <keithwillcode@gmail.com>

* Not using queryRawUnsafe

* Trying anything at this point

* Make sure the DB is ready first

* Don't auto run migrations in CI mode

* Revert "Make sure the DB is ready first"

This reverts commit 2b20bd45c974f3d7e07d8b904bc7fcdae37cce03.

* Dynamic import of prisma

* Commenting where it seems to break

* Backwards compatability for API v2

* fix: add explicit type annotations for map callbacks in API v2

- Add type annotation for map parameter in memberships.repository.ts
- Add type annotation for map parameter in stripe.service.ts
- Fixes implicit 'any' type errors from stricter Prisma 6.16.0 type inference

Co-Authored-By: keith@cal.com <keithwillcode@gmail.com>

* fix: add explicit type annotations for API v2 map callbacks

- users.repository.ts:292: add Profile & { user: User } type
- memberships.service.ts:19-20: add Membership type to filter callbacks

Co-Authored-By: keith@cal.com <keithwillcode@gmail.com>

* fix: add explicit type annotation for attributeToUser in organizations-users.repository.ts

- organizations-users.repository.ts:63: add AttributeToUser with nested relations type

Co-Authored-By: keith@cal.com <keithwillcode@gmail.com>

* fix: add explicit Membership type annotations in teams.repository.ts

Co-Authored-By: keith@cal.com <keithwillcode@gmail.com>

* fix: use API v2 dedicated Prisma client to support adapter in PrismaClientOptions

Co-Authored-By: keith@cal.com <keithwillcode@gmail.com>

* Running API v2 build commands together so they all get the space size var

* Fixing Maximum call depth exceeded error

* fixed type issues

* Trying to make the seed more stable

* Revert "Trying to make the seed more stable"

This reverts commit 1fd4495e6af7acd7981cda7dedec3168979b0e9d.

* Fixed path to prisma client

* Fixed type check

* Fix eslint warnings

* fix: externalize @prisma/adapter-pg and pg in platform-libraries Vite config

- Add @prisma/adapter-pg and pg to external dependencies list
- Add corresponding globals for these packages
- Fix Prisma client aliases to point to packages/prisma/client instead of node_modules
- Add Node.js resolve conditions to prefer Node.js exports
- Keep commonjsOptions.include for proper CommonJS transformation
- Add eslint-disable for __dirname in Vite config file
- Remove problematic prettier/prettier eslint comment

This fixes the 'Extensions.defineExtension is unable to run in this browser environment' error when running yarn generate-swagger in apps/api/v2 after upgrading to Prisma v6.16 with the no-rust engine approach.

Co-Authored-By: keith@cal.com <keithwillcode@gmail.com>

* fix: update Prisma imports in API v2 services to use package path

- Change imports from '../../../generated/prisma/client' to '@calcom/prisma/client'
- Fixes CI error: Cannot find module '../../../../../packages/prisma/generated/prisma/client.ts'
- Aligns with backwards compatibility re-export structure after Prisma v6.16 upgrade

Co-Authored-By: keith@cal.com <keithwillcode@gmail.com>

* fix: remove .ts extension from Prisma client path mapping in tsconfig

- Remove file extension from @calcom/prisma/client path mapping
- Fixes runtime error: Cannot find module '../../../../../packages/prisma/generated/prisma/client.ts'
- TypeScript path mappings should not include file extensions per best practices
- Allows Node.js to correctly resolve to .js files at runtime

Co-Authored-By: keith@cal.com <keithwillcode@gmail.com>

* fix: resolve Prisma 6.16.0 type incompatibilities in bookingScenario tests

- Changed InputPayment.data type from PaymentData to Prisma.InputJsonValue
- Changed createCredentials key parameter from JsonValue to InputJsonValue
- Removed unused PaymentData type definition
- Resolves type errors at lines 709 and 1088 without using 'as any' casts

Co-Authored-By: keith@cal.com <keithwillcode@gmail.com>

* fix: remove non-existent Watchlist fields from test fixtures

- Remove createdById from isLockedOrBlocked.test.ts (lines 15, 20)
- Remove severity and createdById from _post.test.ts (line 110)
- These fields don't exist in Watchlist model schema after Prisma 6.16.0 upgrade
- Resolves TS2353 errors without using 'as any' casts

Relates to PR #23816

Co-Authored-By: keith@cal.com <keithwillcode@gmail.com>

* fix: api v2 imports generated prisma and platform libraries

* fix: resolve type errors from Prisma 6.16 upgrade

- Add missing markdownToSafeHTML import in AppCard.tsx
- Fix organizationId null handling in fresh-booking.test.ts
- Remove non-existent createdById field from Watchlist test utils

Co-Authored-By: keith@cal.com <keithwillcode@gmail.com>

* Put back some external rollups

* Added back the resolve conditions

* Stop using Pool directly

* chore: remove prisma bookingReferenceExtension and update calls

* fix: organizations-admin-not-team-member-event-types.e2e-spec.ts

* chore: bring back POOL in api v2 prisma clients

* chore: remove Pool but await connect

* fixup! chore: remove Pool but await connect

* chore: bring back Pool on all clients

* chore: end pool manually

* chore: test with pool max 1

* chore: e2e test prisma max  pool of 1 connection

* chore: give more control over pool for prisma module with env

* remove pool from base prisma client

* chore: prisma client in libraries use pool

* Fixed types

* chore: log pool events and improve pooling

* Fixing some types and tests

* Changing the parsing of USE_POOL

* fix: ensure Prisma client is connected before seeding to prevent transaction errors

Co-Authored-By: keith@cal.com <keithwillcode@gmail.com>

* chore: adjust pools

* chore: add process.env.USE_POOL to libraries vite config

* fix: v1 _patch reference check bookingRef on the booking find

* fix: v1 get references deleted null for system admin

* test: add integration tests for bookingReference soft-delete behavior

- Add bookingReference.integration-test.ts to test repository methods
- Add handleDeleteCredential.integration-test.ts to test credential deletion cascade
- Add booking-references.integration-test.ts for API v1 integration tests
- All tests verify soft-delete behavior without using mocks
- Tests use real database operations to ensure soft-deleted records persist
- Cover scenarios: replacing references, credential deletion, querying with filters

Co-Authored-By: keith@cal.com <keithwillcode@gmail.com>

* refactor: convert booking-references test to actual API endpoint testing

- Modified _get.ts to export handler function for testing
- Refactored integration test to call API handler instead of directly testing Prisma
- Added timestamps to test data to avoid conflicts
- Tests now verify API layer correctly filters soft-deleted references
- All 4 tests passing

Co-Authored-By: keith@cal.com <keithwillcode@gmail.com>

* fix: add explicit prisma.$connect() call to seed-insights script

With Prisma 6.16 and the PostgreSQL adapter, scripts need to explicitly call $connect() before running database operations to ensure the connection pool is properly initialized. This prevents 'Transaction already closed' errors.

Co-Authored-By: keith@cal.com <keithwillcode@gmail.com>

* fix: add $connect() to main() execution in seed-insights

Both main() and createPerformanceData() entry points need explicit prisma.$connect() calls with the Prisma 6.16 PostgreSQL adapter.

Co-Authored-By: keith@cal.com <keithwillcode@gmail.com>

* fix: always use connection pool for Prisma PostgreSQL adapter

Enable connection pooling by default for the Prisma adapter to prevent
transaction state issues during seed operations. Without a pool, each
operation creates a new connection which can lead to 'Transaction already
closed' errors during heavy database operations like seeding.

Co-Authored-By: keith@cal.com <keithwillcode@gmail.com>

* Revert "fix: always use connection pool for Prisma PostgreSQL adapter"

This reverts commit 6724bb08e42bc0a94846069de83b04db0aeb8e8b.

* fix: enable connection pool for db-seed in cache-db action

Set USE_POOL=true when running yarn db-seed to use connection pooling
with the Prisma PostgreSQL adapter. This prevents 'Transaction already
closed' errors during seeding by maintaining stable database connections.

Co-Authored-By: keith@cal.com <keithwillcode@gmail.com>

* fix: add safety check for undefined ownerForEvent in seed script

Prevent 'Cannot read properties of undefined' error when orgMembersInDBWithProfileId
is empty. This can happen if organization members fail to create or when there's a
duplicate constraint violation causing early return.

Co-Authored-By: keith@cal.com <keithwillcode@gmail.com>

* fix: v1 _patch reference check bookingRef

* fix: increase pool size and add timeout settings to prevent transaction errors

- Increase max connections from 5 to 10
- Add connectionTimeoutMillis: 30000 (30 seconds)
- Add statement_timeout: 60000 (60 seconds)

These settings help prevent 'Unknown transaction status' errors during
heavy database operations like seeding by giving transactions more time
to complete and allowing more concurrent connections.

Co-Authored-By: keith@cal.com <keithwillcode@gmail.com>

* Revert "fix: increase pool size and add timeout settings to prevent transaction errors"

This reverts commit 148264f1f1861dfb09a082937a3e2b49e78fc41a.

* fix: remove standalone execution in seed-app-store to prevent premature disconnect

The seed-app-store.ts file had a standalone main() call at the bottom
that would execute immediately when imported, including a prisma.$disconnect()
in its .finally() block.

This caused issues because:
1. seed.ts imports and calls mainAppStore()
2. The import triggers the standalone main() execution
3. This standalone execution disconnects prisma after completion
4. seed.ts then tries to call mainHugeEventTypesSeed() but prisma is disconnected
5. This leads to 'Unknown transaction status' errors

Fixed by removing the standalone execution since mainAppStore() is already
called programmatically from seed.ts which manages the connection lifecycle.

Co-Authored-By: keith@cal.com <keithwillcode@gmail.com>

* fix: use require.main check to prevent premature disconnect when imported

Added require.main === module check so seed-app-store.ts:
- Runs standalone with proper connection management when executed directly
  via 'yarn seed-app-store' or 'ts-node seed-app-store.ts'
- Does NOT run standalone when imported as a module by seed.ts,
  preventing premature prisma disconnect

This fixes 'Unknown transaction status' errors while maintaining
backward compatibility for direct execution.

Co-Authored-By: keith@cal.com <keithwillcode@gmail.com>

* fix: seed apps before creating users to prevent foreign key constraint violation

Reordered seeding operations to call mainAppStore() before main() because:
- main() creates users with credentials that reference apps via appId foreign key
- mainAppStore() seeds the App table with app records
- Apps must exist before credentials can reference them

This fixes the 'Foreign key constraint violated on Credential_appId_fkey' error
that occurred when creating credentials before the apps they reference existed.

Co-Authored-By: keith@cal.com <keithwillcode@gmail.com>

* Apply suggestion from @cubic-dev-ai[bot]

Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>

* Removing functional changes of deleted: null

* Apply suggestion from @keithwillcode

* refactor: move seedAppData call to bottom of main() in seed.ts

Moved seedAppData() call from seed-app-store.ts to the bottom of main()
in seed.ts to ensure the 'pro' user is created before attempting to
create routing form data for them.

Changes:
- Exported seedAppData function from seed-app-store.ts
- Removed seedAppData() call from the main() export in seed-app-store.ts
- Added seedAppData() call at the bottom of main() in seed.ts
- Updated standalone execution in seed-app-store.ts to still call
  seedAppData() when run directly via 'yarn seed-app-store'

This ensures proper ordering: apps seeded → users created → routing
form data created for existing users.

Co-Authored-By: keith@cal.com <keithwillcode@gmail.com>

* refactor: move routing form seeding from seed-app-store.ts to seed.ts

Moved the routing form seeding logic (previously in seedAppData function)
from seed-app-store.ts to be inline at the bottom of main() in seed.ts.

This ensures the 'pro' user is created before attempting to create routing
form data for them.

Changes:
- Removed seedAppData function and seededForm export from seed-app-store.ts
- Removed import of seedAppData from seed.ts
- Added routing form seeding logic inline at bottom of main() in seed.ts

Seeding order: apps → users (including 'pro') → routing forms

Co-Authored-By: keith@cal.com <keithwillcode@gmail.com>

* fix: add deleted: null filter to bookingReference update operations

- Add deleted: null filter to API v1 PATCH endpoint to prevent updating soft-deleted booking references
- Add deleted: null filter to DailyVideo updateMeetingTokenIfExpired and setEnableRecordingUIAndUserIdForOrganizer
- Add comprehensive test coverage for PATCH endpoint soft-delete behavior
- Tests verify that soft-deleted booking references cannot be updated
- Tests verify that only active booking references can be updated successfully

Co-Authored-By: keith@cal.com <keithwillcode@gmail.com>

* revert: remove deleted: null filters to preserve existing functionality

Per @keithwillcode's feedback, reverting the soft-delete filtering changes to preserve existing functionality in this PR. This PR should focus only on the Prisma upgrade itself.

- Reverted API v1 PATCH endpoint change
- Reverted DailyVideo adapter changes (updateMeetingTokenIfExpired and setEnableRecordingUIAndUserIdForOrganizer)
- Removed test file that was added for soft-delete behavior testing

Addresses comments:
- https://github.com/calcom/cal.com/pull/23816#discussion_r2448854197
- https://github.com/calcom/cal.com/pull/23816#discussion_r2448860594
- https://github.com/calcom/cal.com/pull/23816#discussion_r2448860833

Co-Authored-By: keith@cal.com <keithwillcode@gmail.com>

* test: restore and update booking reference tests to match existing functionality

Updated tests to verify existing behavior where PATCH endpoint can update
booking references regardless of their deleted status. This matches the
current implementation after reverting the deleted: null filters.

Changes:
- Restored test file that was previously deleted
- Updated PATCH tests to expect successful updates of soft-deleted references
- Renamed test suite to 'Existing functionality' to clarify intent
- Tests now verify that the PATCH endpoint preserves existing behavior

Co-Authored-By: keith@cal.com <keithwillcode@gmail.com>

* test: rename booking-references test to integration-test

The test requires a database connection and should run in the integration
test job, not the unit test job. Renamed from .test.ts to .integration-test.ts
to match the repository's testing conventions.

Co-Authored-By: keith@cal.com <keithwillcode@gmail.com>

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: cal.com <morgan@cal.com>
Co-authored-by: Morgan <33722304+ThyMinimalDev@users.noreply.github.com>
Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>
2025-10-21 20:02:03 +01:00

2592 lines
73 KiB
TypeScript

/* eslint-disable @typescript-eslint/no-explicit-any */
import i18nMock from "../../../../../tests/libs/__mocks__/libServerI18n";
import prismock from "../../../../../tests/libs/__mocks__/prisma";
import type Stripe from "stripe";
import { v4 as uuidv4 } from "uuid";
import { vi } from "vitest";
import "vitest-fetch-mock";
import type { z } from "zod";
import { appStoreMetadata } from "@calcom/app-store/appStoreMetaData";
import { handleStripePaymentSuccess } from "@calcom/features/ee/payments/api/webhook";
import { ProfileRepository } from "@calcom/features/profile/repositories/ProfileRepository";
import { weekdayToWeekIndex, type WeekDays } from "@calcom/lib/dayjs";
import type { HttpError } from "@calcom/lib/http-error";
import type { IntervalLimit } from "@calcom/lib/intervalLimits/intervalLimitSchema";
import logger from "@calcom/lib/logger";
import { safeStringify } from "@calcom/lib/safeStringify";
import type { BookingReference, Attendee, Booking, Membership } from "@calcom/prisma/client";
import type { Prisma } from "@calcom/prisma/client";
import type { WebhookTriggerEvents } from "@calcom/prisma/client";
import type {
WorkflowActions,
WorkflowTemplates,
WorkflowTriggerEvents,
WorkflowMethods,
} from "@calcom/prisma/client";
import type { PaymentOption, SchedulingType, SMSLockState, TimeUnit } from "@calcom/prisma/enums";
import type { BookingStatus } from "@calcom/prisma/enums";
import type { teamMetadataSchema } from "@calcom/prisma/zod-utils";
import type { userMetadataType } from "@calcom/prisma/zod-utils";
import type { eventTypeBookingFields } from "@calcom/prisma/zod-utils";
import type { EventTypeMetaDataSchema } from "@calcom/prisma/zod-utils";
import type { AppMeta } from "@calcom/types/App";
import type {
Calendar,
CalendarEvent,
IntegrationCalendar,
NewCalendarEventType,
EventBusyDate,
} from "@calcom/types/Calendar";
import type { CredentialPayload } from "@calcom/types/Credential";
import type { VideoApiAdapter } from "@calcom/types/VideoApiAdapter";
import { getMockPaymentService } from "./MockPaymentService";
import type { getMockRequestDataForBooking } from "./getMockRequestDataForBooking";
type NonNullableVideoApiAdapter = NonNullable<VideoApiAdapter>;
vi.mock("@calcom/app-store/calendar.services.generated", () => ({
CalendarServiceMap: {
googlecalendar: Promise.resolve({ default: vi.fn() }),
office365calendar: Promise.resolve({ default: vi.fn() }),
applecalendar: Promise.resolve({ default: vi.fn() }),
caldavcalendar: Promise.resolve({ default: vi.fn() }),
},
}));
const mockVideoAdapterRegistry: Record<string, unknown> = {};
vi.mock("@calcom/app-store/video.adapters.generated", () => ({
VideoApiAdapterMap: new Proxy(
{},
{
get(target, prop) {
if (typeof prop === "string" && mockVideoAdapterRegistry[prop]) {
return mockVideoAdapterRegistry[prop];
}
return Promise.resolve({ default: vi.fn() });
},
}
),
}));
// We don't need to test it. Also, it causes Formbricks error when imported
vi.mock("@calcom/lib/raqb/findTeamMembersMatchingAttributeLogic", () => ({
default: {},
}));
vi.mock("@calcom/lib/crypto", async (importOriginal) => {
const actual = await importOriginal<typeof import("@calcom/lib/crypto")>();
return {
...actual,
symmetricEncrypt: vi.fn((serviceAccountKey) => serviceAccountKey),
symmetricDecrypt: vi.fn((serviceAccountKey) => serviceAccountKey),
};
});
type Fields = z.infer<typeof eventTypeBookingFields>;
logger.settings.minLevel = 1;
const log = logger.getSubLogger({ prefix: ["[bookingScenario]"] });
type InputWebhook = {
id?: string;
appId: string | null;
userId?: number | null;
teamId?: number | null;
eventTypeId?: number;
active: boolean;
eventTriggers: WebhookTriggerEvents[];
subscriberUrl: string;
time?: number | null;
timeUnit?: TimeUnit | null;
};
type InputWorkflow = {
id?: number;
userId?: number | null;
teamId?: number | null;
name?: string;
activeOn?: number[];
activeOnTeams?: number[];
trigger: WorkflowTriggerEvents;
action: WorkflowActions;
template: WorkflowTemplates;
time?: number | null;
timeUnit?: TimeUnit | null;
sendTo?: string;
verifiedAt?: Date;
};
type InputPayment = {
id?: number;
uid: string;
appId?: string | null;
bookingId: number;
amount: number;
fee: number;
currency: string;
success: boolean;
refunded: boolean;
data: Prisma.InputJsonValue;
externalId: string;
paymentOption?: PaymentOption;
};
type InputWorkflowReminder = {
id?: number;
bookingUid: string;
method: WorkflowMethods;
scheduledDate: Date;
scheduled: boolean;
workflowStepId?: number;
workflowId: number;
};
type InputHost = {
userId: number;
isFixed?: boolean;
scheduleId?: number | null;
groupId?: string | null;
};
type InputSelectedSlot = {
eventTypeId: number;
userId: number;
slotUtcStartDate: Date;
slotUtcEndDate: Date;
uid: string;
releaseAt: Date;
isSeat?: boolean;
};
/**
* Data to be mocked
*/
export type ScenarioData = {
/**
* Prisma would return these eventTypes
*/
eventTypes: InputEventType[];
/**
* Prisma would return these users
*/
users: InputUser[];
/**
* Prisma would return these apps
*/
apps?: Partial<AppMeta>[];
bookings?: InputBooking[];
webhooks?: InputWebhook[];
workflows?: InputWorkflow[];
payment?: InputPayment[];
selectedSlots?: InputSelectedSlot[];
};
type InputCredential = typeof TestData.credentials.google & {
id?: number;
};
type InputSelectedCalendar = (typeof TestData.selectedCalendars)[keyof typeof TestData.selectedCalendars] & {
eventTypeId?: number | null;
};
type InputUser = Omit<typeof TestData.users.example, "defaultScheduleId"> & {
id: number;
defaultScheduleId?: number | null;
credentials?: InputCredential[];
organizationId?: number | null;
selectedCalendars?: InputSelectedCalendar[];
teams?: {
membership: Partial<Membership>;
team: {
id: number;
name: string;
slug: string;
parentId?: number;
isPrivate?: boolean;
};
}[];
schedules: {
// Allows giving id in the input directly so that it can be referenced somewhere else as well
id?: number;
name: string;
availability: {
days: number[];
startTime: Date;
endTime: Date;
date: string | null;
}[];
timeZone: string;
}[];
destinationCalendar?: Prisma.DestinationCalendarCreateInput;
weekStart?: string;
profiles?: Prisma.ProfileUncheckedCreateWithoutUserInput[];
completedOnboarding?: boolean;
outOfOffice?: {
dateRanges: {
start: string;
end: string;
}[];
};
requiresBookerEmailVerification?: boolean;
secondaryEmails?: { email: string; emailVerified: Date | null }[];
};
export type InputEventType = {
id: number;
title?: string;
length?: number;
offsetStart?: number;
slotInterval?: number;
userId?: number;
minimumBookingNotice?: number;
useEventLevelSelectedCalendars?: boolean;
users?: { id: number }[];
hosts?: InputHost[];
hostGroups?: { id: string; name: string }[];
schedulingType?: SchedulingType;
parent?: { id: number };
beforeEventBuffer?: number;
afterEventBuffer?: number;
teamId?: number | null;
team?: {
id?: number | null;
parentId?: number | null;
bookingLimits?: IntervalLimit;
includeManagedEventsInLimits?: boolean;
};
requiresConfirmation?: boolean;
destinationCalendar?: Prisma.DestinationCalendarCreateInput;
schedule?: InputUser["schedules"][number] | null;
bookingLimits?: IntervalLimit;
durationLimits?: IntervalLimit;
owner?: number;
metadata?: z.infer<typeof EventTypeMetaDataSchema>;
rescheduleWithSameRoundRobinHost?: boolean;
restrictionSchedule?: {
create: {
name: string;
timeZone: string;
availability: {
createMany: {
data: {
days: number[];
startTime: Date;
endTime: Date;
date: string | null;
}[];
};
};
user: { connect: { id: number } };
};
};
useBookerTimezone?: boolean;
restrictionScheduleId?: number;
} & Partial<Omit<Prisma.EventTypeCreateInput, "users" | "schedule" | "bookingLimits" | "durationLimits">>;
type AttendeeBookingSeatInput = Pick<Prisma.BookingSeatCreateInput, "referenceUid" | "data">;
type WhiteListedBookingProps = {
id?: number;
uid?: string;
userId?: number;
eventTypeId: number;
startTime: string;
endTime: string;
title?: string;
status: BookingStatus;
attendees?: {
email: string;
phoneNumber?: string;
bookingSeat?: AttendeeBookingSeatInput | null;
timeZone?: string;
}[];
references?: (Omit<ReturnType<typeof getMockBookingReference>, "credentialId"> & {
// TODO: Make sure that all references start providing credentialId and then remove this intersection of optional credentialId
credentialId?: number | null;
})[];
user?: { id: number };
bookingSeat?: Prisma.BookingSeatCreateInput[];
createdAt?: string;
};
type InputBooking = Partial<Omit<Booking, keyof WhiteListedBookingProps>> & WhiteListedBookingProps;
export const Timezones = {
"+5:30": "Asia/Kolkata",
"+6:00": "Asia/Dhaka",
"-11:00": "Pacific/Pago_Pago",
};
async function addHostsToDb(eventTypes: InputEventType[]) {
for (const eventType of eventTypes) {
// Create host groups first if they exist
if (eventType.hostGroups?.length) {
await prismock.hostGroup.createMany({
data: eventType.hostGroups.map((group) => ({
id: group.id, // Preserve the input ID
name: group.name,
eventTypeId: eventType.id,
})),
});
}
if (!eventType.hosts?.length) continue;
for (const host of eventType.hosts) {
const data: Prisma.HostCreateInput = {
eventType: {
connect: {
id: eventType.id,
},
},
isFixed: host.isFixed ?? false,
user: {
connect: {
id: host.userId,
},
},
schedule: host.scheduleId
? {
connect: {
id: host.scheduleId,
},
}
: undefined,
group: host.groupId
? {
connect: {
id: host.groupId,
},
}
: undefined,
};
await prismock.host.create({
data,
});
}
}
}
export async function addEventTypesToDb(
eventTypes: (Omit<
Prisma.EventTypeCreateInput,
"users" | "workflows" | "destinationCalendar" | "schedule"
> & {
id?: number;
users?: ({ id: number } | undefined)[];
userId?: number;
hosts?: {
user: InputUser | undefined;
id: number;
}[];
workflows?: Prisma.WorkflowCreateInput[];
destinationCalendar?: {
create: Prisma.DestinationCalendarCreateInput;
};
schedule?: { create: Prisma.ScheduleCreateInput } | null | undefined;
metadata?: z.infer<typeof EventTypeMetaDataSchema>;
team?: { id?: number | null; bookingLimits?: IntervalLimit; includeManagedEventsInLimits?: boolean };
restrictionSchedule?: {
create: {
name: string;
timeZone: string;
availability: {
createMany: {
data: {
days: number[];
startTime: Date;
endTime: Date;
date: string | null;
}[];
};
};
user: { connect: { id: number } };
};
};
})[]
) {
log.silly("TestData: Add EventTypes to DB", JSON.stringify(eventTypes));
await prismock.eventType.createMany({
data: eventTypes,
});
const allEventTypes = await prismock.eventType.findMany({
include: {
users: true,
workflows: true,
destinationCalendar: true,
schedule: true,
hostGroups: true,
},
});
for (let i = 0; i < eventTypes.length; i++) {
const eventType = eventTypes[i];
const createdEventType = allEventTypes[i];
if (eventType.schedule) {
log.silly("TestData: Creating Schedule for EventType", JSON.stringify(eventType));
await prismock.schedule.create({
data: {
...eventType.schedule.create,
eventType: {
connect: {
id: createdEventType.id,
},
},
},
});
}
// Handle restrictionSchedule creation and connection
if (eventType.restrictionSchedule) {
const createdRestrictionSchedule = await prismock.schedule.create({
data: {
...eventType.restrictionSchedule.create,
eventType: {
connect: {
id: createdEventType.id,
},
},
},
});
await prismock.eventType.update({
where: { id: createdEventType.id },
data: { restrictionScheduleId: createdRestrictionSchedule.id },
});
}
if (eventType.team?.id) {
const createdTeam = await prismock.team.create({
data: {
id: eventType.team?.id,
bookingLimits: eventType.team?.bookingLimits,
includeManagedEventsInLimits: eventType.team?.includeManagedEventsInLimits,
name: "",
},
});
await prismock.eventType.update({
where: { id: eventType.id },
data: { teamId: createdTeam.id },
});
}
}
log.silly(
"TestData: All EventTypes in DB are",
JSON.stringify({
eventTypes: allEventTypes,
})
);
return allEventTypes;
}
export async function addEventTypes(eventTypes: InputEventType[], usersStore: InputUser[]) {
const baseEventType = {
title: "Base EventType Title",
slug: "base-event-type-slug",
timeZone: null,
beforeEventBuffer: 0,
afterEventBuffer: 0,
bookingLimits: {},
includeManagedEventsInLimits: false,
schedulingType: null,
length: 15,
//TODO: What is the purpose of periodStartDate and periodEndDate? Test these?
periodStartDate: new Date("2022-01-21T09:03:48.000Z"),
periodEndDate: new Date("2022-01-21T09:03:48.000Z"),
periodCountCalendarDays: false,
periodDays: 30,
seatsPerTimeSlot: null,
metadata: {},
minimumBookingNotice: 0,
offsetStart: 0,
};
const foundEvents: Record<number, boolean> = {};
const eventTypesWithUsers = eventTypes.map((eventType) => {
if (!eventType.slotInterval && !eventType.length) {
throw new Error("eventTypes[number]: slotInterval or length must be defined");
}
if (foundEvents[eventType.id]) {
throw new Error(`eventTypes[number]: id ${eventType.id} is not unique`);
}
foundEvents[eventType.id] = true;
const users =
eventType.users?.map((userWithJustId) => {
return usersStore.find((user) => user.id === userWithJustId.id);
}) || [];
const hosts =
eventType.users?.map((host) => {
const user = usersStore.find((user) => user.id === host.id);
return { ...host, user };
}) || [];
return {
...baseEventType,
...eventType,
workflows: [],
users,
hosts,
hostGroups: eventType.hostGroups || [],
destinationCalendar: eventType.destinationCalendar
? {
create: eventType.destinationCalendar,
}
: eventType.destinationCalendar,
schedule: eventType.schedule
? {
create: {
...eventType.schedule,
availability: {
createMany: {
data: eventType.schedule.availability,
},
},
},
}
: eventType.schedule,
restrictionScheduleId: eventType.restrictionScheduleId ?? undefined,
useBookerTimezone: eventType.useBookerTimezone ?? false,
restrictionSchedule: eventType.restrictionSchedule
? {
create: {
name: eventType.restrictionSchedule.create.name,
timeZone: eventType.restrictionSchedule.create.timeZone,
availability: {
createMany: {
data: eventType.restrictionSchedule.create.availability.createMany.data,
},
},
user: { connect: { id: eventType.userId || (eventType.users && eventType.users[0]?.id) || 1 } },
},
}
: undefined,
owner: eventType.owner ? { connect: { id: eventType.owner } } : undefined,
schedulingType: eventType.schedulingType,
parent: eventType.parent ? { connect: { id: eventType.parent.id } } : undefined,
rescheduleWithSameRoundRobinHost: eventType.rescheduleWithSameRoundRobinHost,
};
});
log.silly("TestData: Creating EventType", JSON.stringify(eventTypesWithUsers));
return await addEventTypesToDb(eventTypesWithUsers as unknown as any);
}
function addBookingReferencesToDB(bookingReferences: Prisma.BookingReferenceCreateManyInput[]) {
prismock.bookingReference.createMany({
data: bookingReferences,
});
}
async function addBookingsToDb(
bookings: (Prisma.BookingCreateInput & {
references: any[];
user?: { id: number };
})[]
) {
log.silly("TestData: Creating Bookings", JSON.stringify(bookings));
function getDateObj(time: string | Date) {
return time instanceof Date ? time : new Date(time);
}
// Make sure that we store the date in Date object always. This is to ensure consistency which Prisma does but not prismock
log.silly("Handling Prismock bug-3");
const fixedBookings = bookings.map((booking) => {
const startTime = getDateObj(booking.startTime);
const endTime = getDateObj(booking.endTime);
return { ...booking, startTime, endTime };
});
await prismock.booking.createMany({
data: fixedBookings,
});
log.silly(
"TestData: Bookings as in DB",
JSON.stringify({
bookings: await prismock.booking.findMany({
include: {
references: true,
attendees: true,
},
}),
})
);
}
export async function addBookings(bookings: InputBooking[]) {
log.silly("TestData: Creating Bookings", JSON.stringify(bookings));
const allBookings = [...bookings].map((booking) => {
if (booking.references) {
addBookingReferencesToDB(
booking.references.map((reference) => {
return {
...reference,
bookingId: booking.id,
};
})
);
}
return {
uid: booking.uid || uuidv4(),
workflowReminders: [],
references: [],
title: "Test Booking Title",
...booking,
};
});
await addBookingsToDb(
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
allBookings.map((booking) => {
const bookingCreate = booking;
if (booking.references) {
bookingCreate.references = {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
createMany: {
data: booking.references,
},
};
}
if (booking.attendees) {
bookingCreate.attendees = {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
createMany: {
data: booking.attendees.map((attendee) => {
if (attendee.bookingSeat) {
const { bookingSeat, ...attendeeWithoutBookingSeat } = attendee;
return {
...attendeeWithoutBookingSeat,
bookingSeat: {
create: { ...bookingSeat, bookingId: booking.id },
},
};
} else {
return attendee;
}
}),
},
};
}
if (booking?.user?.id) {
bookingCreate.user = {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
connect: {
id: booking.user.id,
},
};
}
return bookingCreate;
})
);
}
async function addWebhooksToDb(webhooks: any[]) {
await prismock.webhook.createMany({
data: webhooks,
});
}
async function addPaymentToDb(payment: InputPayment[]) {
await prismock.payment.createMany({
data: payment,
});
}
async function addWebhooks(webhooks: InputWebhook[]) {
log.silly("TestData: Creating Webhooks", safeStringify(webhooks));
await addWebhooksToDb(webhooks);
}
async function addWorkflowsToDb(workflows: InputWorkflow[]) {
await Promise.all(
workflows.map(async (workflow) => {
const team = await prismock.team.findUnique({
where: {
id: workflow.teamId ?? 0,
},
});
if (workflow.teamId && !team) {
throw new Error(`Team with ID ${workflow.teamId} not found`);
}
const isOrg = team?.isOrganization;
// Create the workflow first
const createdWorkflow = await prismock.workflow.create({
data: {
...(workflow.id && { id: workflow.id }),
userId: workflow.userId,
teamId: workflow.teamId,
trigger: workflow.trigger,
name: workflow.name ? workflow.name : "Test Workflow",
time: workflow.time,
timeUnit: workflow.timeUnit,
},
include: {
steps: true,
},
});
await prismock.workflowStep.create({
data: {
stepNumber: 1,
action: workflow.action,
template: workflow.template,
numberVerificationPending: false,
includeCalendarEvent: false,
sendTo: workflow.sendTo,
workflow: {
connect: {
id: createdWorkflow.id,
},
},
verifiedAt: workflow?.verifiedAt ?? new Date(),
},
});
//activate event types and teams on workflows
if (isOrg && workflow.activeOnTeams) {
await Promise.all(
workflow.activeOnTeams.map((id) =>
prismock.workflowsOnTeams.create({
data: {
workflowId: createdWorkflow.id,
teamId: id,
},
})
)
);
} else if (workflow.activeOn) {
await Promise.all(
workflow.activeOn.map((id) =>
prismock.workflowsOnEventTypes.create({
data: {
workflowId: createdWorkflow.id,
eventTypeId: id,
},
})
)
);
}
})
);
}
async function addWorkflows(workflows: InputWorkflow[]) {
log.silly("TestData: Creating Workflows", safeStringify(workflows));
return await addWorkflowsToDb(workflows);
}
export async function addWorkflowReminders(workflowReminders: InputWorkflowReminder[]) {
log.silly("TestData: Creating Workflow Reminders", safeStringify(workflowReminders));
return await prismock.workflowReminder.createMany({
data: workflowReminders,
});
}
export async function addUsersToDb(users: InputUser[]) {
log.silly("TestData: Creating Users", JSON.stringify(users));
await prismock.user.createMany({
data: users,
});
// Create OutOfOfficeEntry for users with outOfOffice data
for (const user of users) {
if (user.outOfOffice) {
log.debug("Creating OutOfOfficeEntry for user", user.id);
for (const dateRange of user.outOfOffice.dateRanges) {
await prismock.outOfOfficeEntry.create({
data: {
uuid: uuidv4(),
start: new Date(dateRange.start),
end: new Date(dateRange.end),
user: {
connect: {
id: user.id,
},
},
},
});
}
}
}
for (const user of users) {
if (user.secondaryEmails) {
log.debug("Creating SecondaryEmail entries for user", user.id);
for (const secondaryEmail of user.secondaryEmails) {
await prismock.secondaryEmail.create({
data: {
email: secondaryEmail.email,
emailVerified: secondaryEmail.emailVerified,
userId: user.id,
},
});
}
}
}
const allUsers = await prismock.user.findMany({
include: {
credentials: true,
teams: true,
profiles: true,
schedules: {
include: {
availability: true,
},
},
destinationCalendar: true,
secondaryEmails: true,
},
});
log.silly(
"Added users to Db",
safeStringify({
allUsers,
})
);
return allUsers;
}
export async function addTeamsToDb(teams: NonNullable<InputUser["teams"]>[number]["team"][]) {
log.silly("TestData: Creating Teams", JSON.stringify(teams));
for (const team of teams) {
const teamsWithParentId = {
...team,
parentId: team.parentId,
};
await prismock.team.upsert({
where: {
id: teamsWithParentId.id,
},
update: {
...teamsWithParentId,
},
create: {
...teamsWithParentId,
},
});
}
const addedTeams = await prismock.team.findMany({
where: {
id: {
in: teams.map((team) => team.id),
},
},
});
log.silly(
"Added teams to Db",
safeStringify({
addedTeams,
})
);
return addedTeams;
}
export async function addUsers(users: InputUser[]) {
const prismaUsersCreate = [];
for (let i = 0; i < users.length; i++) {
const newUser = users[i];
const user = users[i];
if (user.schedules) {
newUser.schedules = {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
createMany: {
data: user.schedules.map((schedule) => {
return {
...schedule,
availability: {
createMany: {
data: schedule.availability,
},
},
};
}),
},
};
}
if (user.credentials) {
newUser.credentials = {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
createMany: {
data: user.credentials,
},
};
}
if (user.teams) {
const addedTeams = await addTeamsToDb(user.teams.map((team) => team.team));
newUser.teams = {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
createMany: {
data: user.teams.map((team, index) => {
return {
teamId: addedTeams[index].id,
...team.membership,
};
}),
},
};
}
if (user.selectedCalendars) {
newUser.selectedCalendars = {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
createMany: {
data: user.selectedCalendars,
},
};
}
if (user.destinationCalendar) {
newUser.destinationCalendar = {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
create: user.destinationCalendar,
};
}
if (user.profiles) {
newUser.profiles = {
// @ts-expect-error Not sure why this is not working
createMany: {
data: user.profiles,
},
};
}
prismaUsersCreate.push(newUser);
}
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
return await addUsersToDb(prismaUsersCreate);
}
async function addAppsToDb(apps: any[]) {
log.silly("TestData: Creating Apps", JSON.stringify({ apps }));
await prismock.app.createMany({
data: apps,
});
const allApps = await prismock.app.findMany();
log.silly("TestData: Apps as in DB", JSON.stringify({ apps: allApps }));
}
async function addSelectedSlotsToDb(selectedSlots: InputSelectedSlot[]) {
log.silly("TestData: Creating Selected Slots", JSON.stringify(selectedSlots));
await prismock.selectedSlots.createMany({
data: selectedSlots,
});
const allSelectedSlots = await prismock.selectedSlots.findMany();
log.silly("TestData: Selected Slots as in DB", JSON.stringify({ selectedSlots: allSelectedSlots }));
}
export async function createBookingScenario(data: ScenarioData) {
log.silly("TestData: Creating Scenario", JSON.stringify({ data }));
await addUsers(data.users);
if (data.apps) {
await addAppsToDb(
data.apps.map((app) => {
// Enable the app by default
return { enabled: true, ...app };
})
);
}
const eventTypes = await addEventTypes(data.eventTypes, data.users);
await addHostsToDb(data.eventTypes);
data.bookings = data.bookings || [];
// allowSuccessfulBookingCreation();
await addBookings(data.bookings);
// mockBusyCalendarTimes([]);
await addWebhooks(data.webhooks || []);
// addPaymentMock();
const workflows = await addWorkflows(data.workflows || []);
await addPaymentToDb(data.payment || []);
if (data.selectedSlots) {
await addSelectedSlotsToDb(data.selectedSlots);
}
return {
eventTypes,
workflows,
};
}
type TeamCreateReturnType = Awaited<ReturnType<typeof prismock.team.create>>;
function assertNonNullableSlug<T extends { slug: string | null }>(
org: T
): asserts org is T & { slug: string } {
if (org.slug === null) {
throw new Error("Slug cannot be null");
}
}
export async function createOrganization(orgData: {
name: string;
slug: string;
metadata?: z.infer<typeof teamMetadataSchema>;
withTeam?: boolean;
}): Promise<TeamCreateReturnType & { slug: NonNullable<TeamCreateReturnType["slug"]>; children: any }> {
const org = await prismock.team.create({
data: {
name: orgData.name,
slug: orgData.slug,
isOrganization: true,
metadata: {
...(orgData.metadata || {}),
isOrganization: true,
},
},
});
if (orgData.withTeam) {
await prismock.team.create({
data: {
name: "Org Team",
slug: "org-team",
isOrganization: false,
parent: {
connect: {
id: org.id,
},
},
},
});
}
assertNonNullableSlug(org);
const team = await prismock.team.findUnique({ where: { id: org.id }, include: { children: true } });
if (!team) {
throw new Error(`Team with id ${org.id} not found`);
}
assertNonNullableSlug(team);
return team;
}
export async function createCredentials(
credentialData: {
type: string;
key: Prisma.InputJsonValue;
id?: number;
userId?: number | null;
teamId?: number | null;
}[]
) {
const credentials = await prismock.credential.createMany({
data: credentialData,
});
return credentials;
}
// async function addPaymentsToDb(payments: Prisma.PaymentCreateInput[]) {
// await prismaMock.payment.createMany({
// data: payments,
// });
// }
/**
* This fn indents to /ally compute day, month, year for the purpose of testing.
* We are not using DayJS because that's actually being tested by this code.
* - `dateIncrement` adds the increment to current day
* - `monthIncrement` adds the increment to current month
* - `yearIncrement` adds the increment to current year
* - `fromDate` starts incrementing from this date (default: today)
* @deprecated Stop using this function as it is not timezone aware and can return wrong date depending on the time of the day and timezone. Instead
* use vi.setSystemTime to fix the date and time and then use hardcoded days instead of dynamic date calculation.
*/
export const getDate = (
param: {
dateIncrement?: number;
monthIncrement?: number;
yearIncrement?: number;
fromDate?: Date;
} = {}
) => {
let { dateIncrement, monthIncrement, yearIncrement, fromDate } = param;
dateIncrement = dateIncrement || 0;
monthIncrement = monthIncrement || 0;
yearIncrement = yearIncrement || 0;
fromDate = fromDate || new Date();
fromDate.setDate(fromDate.getDate() + dateIncrement);
fromDate.setMonth(fromDate.getMonth() + monthIncrement);
fromDate.setFullYear(fromDate.getFullYear() + yearIncrement);
let _date = fromDate.getDate();
let year = fromDate.getFullYear();
// Make it start with 1 to match with DayJS requiremet
let _month = fromDate.getMonth() + 1;
// If last day of the month(As _month is plus 1 already it is going to be the 0th day of next month which is the last day of current month)
const lastDayOfMonth = new Date(year, _month, 0).getDate();
const numberOfDaysForNextMonth = +_date - +lastDayOfMonth;
if (numberOfDaysForNextMonth > 0) {
_date = numberOfDaysForNextMonth;
_month = _month + 1;
}
if (_month === 13) {
_month = 1;
year = year + 1;
}
const date = _date < 10 ? `0${_date}` : _date;
const month = _month < 10 ? `0${_month}` : _month;
return {
date: String(date),
month: String(month),
year: String(year),
dateString: `${year}-${month}-${date}`,
};
};
const isWeekStart = (date: Date, weekStart: WeekDays) => {
return date.getDay() === weekdayToWeekIndex(weekStart);
};
export const getNextMonthNotStartingOnWeekStart = (weekStart: WeekDays, from?: Date) => {
const date = from ?? new Date();
const incrementMonth = (date: Date) => {
date.setMonth(date.getMonth() + 1);
};
// start searching from the 1st day of next month
incrementMonth(date);
date.setDate(1);
while (isWeekStart(date, weekStart)) {
incrementMonth(date);
}
return getDate({ fromDate: date });
};
export function getMockedCredential({
metadataLookupKey,
key,
}: {
metadataLookupKey: string;
key: {
expiry_date?: number;
token_type?: string;
access_token?: string;
refresh_token?: string;
scope: string;
};
}) {
const app = appStoreMetadata[metadataLookupKey as keyof typeof appStoreMetadata];
return {
type: app.type,
appId: app.slug,
app: app,
key: {
expiry_date: Date.now() + 1000000,
token_type: "Bearer",
access_token: "ACCESS_TOKEN",
refresh_token: "REFRESH_TOKEN",
...key,
},
};
}
export function getGoogleCalendarCredential() {
return getMockedCredential({
metadataLookupKey: "googlecalendar",
key: {
scope:
"https://www.googleapis.com/auth/calendar.events https://www.googleapis.com/auth/calendar.readonly",
},
});
}
export function getGoogleMeetCredential() {
return getMockedCredential({
metadataLookupKey: "googlevideo",
key: {
scope: "",
},
});
}
export function getAppleCalendarCredential() {
return getMockedCredential({
metadataLookupKey: "applecalendar",
key: {
scope:
"https://www.applecalendar.example/auth/calendar.events https://www.applecalendar.example/auth/calendar.readonly",
},
});
}
export function getZoomAppCredential() {
return getMockedCredential({
metadataLookupKey: "zoomvideo",
key: {
scope: "meeting:write",
},
});
}
export function getStripeAppCredential() {
return getMockedCredential({
metadataLookupKey: "stripepayment",
key: {
scope: "read_write",
},
});
}
export const TestData = {
selectedCalendars: {
google: {
integration: "google_calendar",
externalId: "john@example.com",
},
office365: {
integration: "office365_calendar",
externalId: "john@example.com",
},
},
credentials: {
google: getGoogleCalendarCredential(),
},
schedules: {
IstWorkHours: {
id: 1,
name: "9:30AM to 6PM in India - 4:00AM to 12:30PM in GMT",
availability: [
{
// userId: null,
// eventTypeId: null,
days: [0, 1, 2, 3, 4, 5, 6],
startTime: new Date("1970-01-01T09:30:00.000Z"),
endTime: new Date("1970-01-01T18:00:00.000Z"),
date: null,
},
],
timeZone: Timezones["+5:30"],
},
/**
* Has an overlap with IstEveningShift from 5PM to 6PM IST(11:30AM to 12:30PM GMT)
*/
IstMorningShift: {
name: "9:30AM to 6PM in India - 4:00AM to 12:30PM in GMT",
availability: [
{
// userId: null,
// eventTypeId: null,
days: [0, 1, 2, 3, 4, 5, 6],
startTime: new Date("1970-01-01T09:30:00.000Z"),
endTime: new Date("1970-01-01T18:00:00.000Z"),
date: null,
},
],
timeZone: Timezones["+5:30"],
},
/**
* Has an overlap with IstMorningShift and IstEveningShift
*/
IstMidShift: {
name: "12:30AM to 8PM in India - 7:00AM to 14:30PM in GMT",
availability: [
{
// userId: null,
// eventTypeId: null,
days: [0, 1, 2, 3, 4, 5, 6],
startTime: new Date("1970-01-01T12:30:00.000Z"),
endTime: new Date("1970-01-01T20:00:00.000Z"),
date: null,
},
],
timeZone: Timezones["+5:30"],
},
/**
* Has an overlap with IstMorningShift from 5PM to 6PM IST(11:30AM to 12:30PM GMT)
*/
IstEveningShift: {
name: "5:00PM to 10PM in India - 11:30AM to 16:30PM in GMT",
availability: [
{
// userId: null,
// eventTypeId: null,
days: [0, 1, 2, 3, 4, 5, 6],
startTime: new Date("1970-01-01T17:00:00.000Z"),
endTime: new Date("1970-01-01T22:00:00.000Z"),
date: null,
},
],
timeZone: Timezones["+5:30"],
},
EmptyAvailability: {
name: "Empty Availability",
availability: [],
timeZone: Timezones["+5:30"],
},
IstNotAvailableForFullMonth: (monthYear: string) => {
const [year, month] = monthYear.split("-").map(Number); // Expecting format 'YYYY-MM'
const startOfMonth = new Date(year, month - 1, 1);
const endOfMonth = new Date(year, month, 0);
const availability: {
days: number[];
startTime: Date;
endTime: Date;
date: string | null;
}[] = [
{
days: [0, 1, 2, 3, 4, 5, 6],
startTime: new Date("1970-01-01T18:00:00.000Z"),
endTime: new Date("1970-01-01T22:00:00.000Z"),
date: null,
},
];
// Generate unavailable dates for the entire month
const currentDate = new Date(startOfMonth);
while (currentDate <= endOfMonth) {
const dateString = currentDate.toISOString().split("T")[0];
availability.push({
days: [],
startTime: new Date(`${dateString}T00:00:00.000Z`),
endTime: new Date(`${dateString}T00:00:00.000Z`),
date: dateString,
});
currentDate.setDate(currentDate.getDate() + 1);
}
return {
name: "Unavailable for the entire month, then available from 18:00AM to 22:00 IST",
availability,
timeZone: Timezones["+5:30"],
};
},
IstWorkHoursWithFirstTwoWeeksUnavailable: (dateString: string) => {
const date = new Date(dateString);
const availability: {
days: number[];
startTime: Date;
endTime: Date;
date: string | null;
}[] = [
{
days: [0, 1, 2, 3, 4, 5, 6],
startTime: new Date("1970-01-01T18:00:00.000Z"),
endTime: new Date("1970-01-01T22:00:00.000Z"),
date: null,
},
];
// Generate dateoverride for each day in the first two weeks
for (let i = 0; i < 15; i++) {
const dateString = date.toISOString().split("T")[0];
availability.push({
days: [],
startTime: new Date(`${dateString}T00:00:00.000Z`),
endTime: new Date(`${dateString}T00:00:00.000Z`),
date: dateString,
});
date.setDate(date.getDate() + 1);
}
return {
name: "Unavailable for the first two weeks, then available from 18:00 to 22:00 IST",
availability,
timeZone: Timezones["+5:30"],
};
},
IstWorkHoursWithDateOverride: (dateString: string) => ({
name: "9:30AM to 6PM in India - 4:00AM to 12:30PM in GMT but with a Date Override for 2PM to 6PM IST(in GST time it is 8:30AM to 12:30PM)",
availability: [
{
days: [0, 1, 2, 3, 4, 5, 6],
startTime: new Date("1970-01-01T09:30:00.000Z"),
endTime: new Date("1970-01-01T18:00:00.000Z"),
date: null,
},
{
days: [0, 1, 2, 3, 4, 5, 6],
startTime: new Date(`1970-01-01T14:00:00.000Z`),
endTime: new Date(`1970-01-01T18:00:00.000Z`),
date: dateString,
},
],
timeZone: Timezones["+5:30"],
}),
IstWorkHoursNoWeekends: {
id: 1,
name: "9:30AM to 6PM in India - 4:00AM to 12:30PM in GMT",
availability: [
{
// userId: null,
// eventTypeId: null,
days: [/*0*/ 1, 2, 3, 4, 5 /*6*/],
startTime: new Date("1970-01-01T09:30:00.000Z"),
endTime: new Date("1970-01-01T18:00:00.000Z"),
date: null,
},
],
timeZone: Timezones["+5:30"],
},
},
users: {
example: {
name: "Example",
email: "example@example.com",
username: "example.username",
defaultScheduleId: 1,
timeZone: Timezones["+5:30"],
},
},
apps: {
"google-calendar": {
...appStoreMetadata.googlecalendar,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
keys: {
expiry_date: Infinity,
client_id: "client_id",
client_secret: "client_secret",
redirect_uris: ["http://localhost:3000/auth/callback"],
},
},
"office365-calendar": {
...appStoreMetadata.office365calendar,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
keys: {
expiry_date: Infinity,
client_id: "client_id",
client_secret: "client_secret",
},
},
"google-meet": {
...appStoreMetadata.googlevideo,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
keys: {
expiry_date: Infinity,
client_id: "client_id",
client_secret: "client_secret",
redirect_uris: ["http://localhost:3000/auth/callback"],
},
},
"daily-video": {
...appStoreMetadata.dailyvideo,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
keys: {
expiry_date: Infinity,
api_key: "",
scale_plan: "false",
client_id: "client_id",
client_secret: "client_secret",
redirect_uris: ["http://localhost:3000/auth/callback"],
},
},
zoomvideo: {
...appStoreMetadata.zoomvideo,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
keys: {
expiry_date: Infinity,
api_key: "",
scale_plan: "false",
client_id: "client_id",
client_secret: "client_secret",
redirect_uris: ["http://localhost:3000/auth/callback"],
},
},
"stripe-payment": {
...appStoreMetadata.stripepayment,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
keys: {
expiry_date: Infinity,
api_key: "",
scale_plan: "false",
client_id: "client_id",
client_secret: "client_secret",
redirect_uris: ["http://localhost:3000/auth/callback"],
},
},
},
};
export class MockError extends Error {
constructor(message: string) {
super(message);
this.name = "MockError";
}
}
export function getOrganizer({
name,
email,
id,
schedules,
credentials,
selectedCalendars,
destinationCalendar,
defaultScheduleId,
weekStart = "Sunday",
teams,
organizationId,
metadata,
smsLockState,
completedOnboarding,
username,
locked,
emailVerified,
requiresBookerEmailVerification,
secondaryEmails,
}: {
name: string;
email: string;
id: number;
organizationId?: number | null;
schedules: InputUser["schedules"];
credentials?: InputCredential[];
selectedCalendars?: InputSelectedCalendar[];
defaultScheduleId?: number | null;
destinationCalendar?: Prisma.DestinationCalendarCreateInput;
weekStart?: WeekDays;
teams?: InputUser["teams"];
metadata?: userMetadataType;
smsLockState?: SMSLockState;
completedOnboarding?: boolean;
username?: string;
locked?: boolean;
emailVerified?: Date | null;
requiresBookerEmailVerification?: boolean;
secondaryEmails?: { email: string; emailVerified: Date | null }[];
}) {
username = username ?? TestData.users.example.username;
return {
...TestData.users.example,
name,
username,
email,
id,
schedules,
credentials,
selectedCalendars,
destinationCalendar,
defaultScheduleId,
weekStart,
teams,
organizationId,
profiles: [],
metadata,
smsLockState,
completedOnboarding,
locked,
emailVerified,
requiresBookerEmailVerification,
secondaryEmails,
};
}
export function getScenarioData(
{
/**
* organizer has no special meaning. It is a regular user. It is supposed to be deprecated along with `usersApartFromOrganizer` and we should introduce a new `users` field instead
*/
organizer,
eventTypes,
usersApartFromOrganizer = [],
apps = [],
users: _users,
webhooks,
workflows,
bookings,
payment,
}: {
organizer?: ReturnType<typeof getOrganizer>;
eventTypes: ScenarioData["eventTypes"];
apps?: ScenarioData["apps"];
users?: ScenarioData["users"];
usersApartFromOrganizer?: ScenarioData["users"];
webhooks?: ScenarioData["webhooks"];
workflows?: ScenarioData["workflows"];
bookings?: ScenarioData["bookings"];
payment?: ScenarioData["payment"];
},
org?: { id: number | null; profileUsername?: string } | undefined | null
) {
if (_users && (usersApartFromOrganizer.length || organizer)) {
throw new Error("When users are provided, usersApartFromOrganizer and organizer should not be provided");
}
const users = _users ? _users : organizer ? [organizer, ...usersApartFromOrganizer] : [];
if (!users.length) {
throw new Error("No users are specified in any way");
}
if (org) {
const orgId = org.id;
if (!orgId) {
throw new Error("If org is specified org.id is required");
}
users.forEach((user) => {
const profileUsername = org.profileUsername ?? user.username ?? "";
user.profiles = [
{
organizationId: orgId,
username: profileUsername,
uid: ProfileRepository.generateProfileUid(),
},
];
});
}
eventTypes.forEach((eventType) => {
if (
eventType.users?.filter((eventTypeUser) => {
return !users.find((userToCreate) => userToCreate.id === eventTypeUser.id);
}).length
) {
throw new Error(`EventType ${eventType.id} has users that are not present in ScenarioData["users"]`);
}
});
return {
eventTypes: eventTypes.map((eventType, index) => {
return {
...eventType,
teamId: eventType.teamId || null,
team: {
id: eventType.teamId ?? eventType.team?.id,
parentId: org ? org.id : null,
bookingLimits: eventType?.team?.bookingLimits,
includeManagedEventsInLimits: eventType?.team?.includeManagedEventsInLimits,
},
title: `Test Event Type - ${index + 1}`,
description: `It's a test event type - ${index + 1}`,
};
}),
users: users.map((user) => {
const newUser = {
...user,
organizationId: user.organizationId ?? null,
};
return newUser;
}),
apps: [...apps],
webhooks,
bookings: bookings || [],
workflows,
payment,
} satisfies ScenarioData;
}
export function enableEmailFeature() {
prismock.feature.create({
data: {
slug: "emails",
enabled: false,
type: "KILL_SWITCH",
},
});
}
export function mockNoTranslations() {
log.silly("Mocking i18n.getTranslation to return identity function");
i18nMock.getTranslation.mockImplementation(() => {
return new Promise((resolve) => {
const identityFn = (key: string) => key;
resolve(identityFn);
});
});
}
export const enum BookingLocations {
CalVideo = "integrations:daily",
ZoomVideo = "integrations:zoom",
GoogleMeet = "integrations:google:meet",
}
export type CalendarServiceMethodMockCallBase = {
calendarServiceConstructorArgs: {
credential: CredentialPayload;
};
};
type CreateEventMethodMockCall = CalendarServiceMethodMockCallBase & {
args: {
calEvent: CalendarEvent;
credentialId: number;
externalCalendarId?: string;
};
};
type UpdateEventMethodMockCall = CalendarServiceMethodMockCallBase & {
args: {
uid: string;
event: CalendarEvent;
externalCalendarId?: string | null;
};
};
type DeleteEventMethodMockCall = CalendarServiceMethodMockCallBase & {
args: {
uid: string;
event: CalendarEvent;
externalCalendarId?: string | null;
};
};
type GetAvailabilityMethodMockCall = CalendarServiceMethodMockCallBase & {
args: {
dateFrom: string;
dateTo: string;
selectedCalendars: IntegrationCalendar[];
shouldServeCache?: boolean;
};
};
export type CalendarServiceMethodMock = {
createEventCalls: CreateEventMethodMockCall[];
updateEventCalls: UpdateEventMethodMockCall[];
deleteEventCalls: DeleteEventMethodMockCall[];
getAvailabilityCalls: GetAvailabilityMethodMockCall[];
};
/**
* @param metadataLookupKey
* @param calendarData Specify uids and other data to be faked to be returned by createEvent and updateEvent
*/
export async function mockCalendar(
metadataLookupKey: keyof typeof appStoreMetadata,
calendarData?: {
create?: {
id?: string;
uid?: string;
iCalUID?: string;
appSpecificData?: {
googleCalendar?: {
hangoutLink?: string;
};
office365Calendar?: {
url?: string;
};
};
};
update?: {
id?: string;
uid: string;
iCalUID?: string;
appSpecificData?: {
googleCalendar?: {
hangoutLink?: string;
};
office365Calendar?: {
url?: string;
};
};
};
busySlots?: { start: `${string}Z`; end: `${string}Z` }[];
creationCrash?: boolean;
updationCrash?: boolean;
getAvailabilityCrash?: boolean;
}
): Promise<CalendarServiceMethodMock> {
const appStoreLookupKey = metadataLookupKey;
const normalizedCalendarData = calendarData || {
create: {
uid: "MOCK_ID",
},
update: {
uid: "UPDATED_MOCK_ID",
},
};
log.silly(`Mocking ${appStoreLookupKey} on appStoreMock`);
const createEventCalls: CreateEventMethodMockCall[] = [];
const updateEventCalls: UpdateEventMethodMockCall[] = [];
const deleteEventCalls: DeleteEventMethodMockCall[] = [];
const getAvailabilityCalls: GetAvailabilityMethodMockCall[] = [];
const app = appStoreMetadata[metadataLookupKey as keyof typeof appStoreMetadata];
const { CalendarServiceMap } = await import("@calcom/app-store/calendar.services.generated");
const calendarServiceKey = appStoreLookupKey as keyof typeof CalendarServiceMap;
const calendarServicePromise = CalendarServiceMap[calendarServiceKey];
if (calendarServicePromise) {
const resolvedService = await calendarServicePromise;
vi.mocked(resolvedService.default).mockImplementation(
// @ts-expect-error - Mock implementation satisfies Calendar interface but TypeScript expects specific calendar service types
function MockCalendarService(credential) {
return {
createEvent: async function (
...rest: Parameters<Calendar["createEvent"]>
): Promise<NewCalendarEventType> {
if (calendarData?.creationCrash) {
throw new Error("MockCalendarService.createEvent fake error");
}
const [calEvent, credentialId, externalCalendarId] = rest;
log.debug(
"mockCalendar.createEvent",
JSON.stringify({ calEvent, credentialId, externalCalendarId })
);
createEventCalls.push({
args: {
calEvent,
credentialId,
externalCalendarId,
},
calendarServiceConstructorArgs: {
credential,
},
});
const isGoogleMeetLocation = calEvent?.location === BookingLocations.GoogleMeet;
if (app.type === "google_calendar") {
return Promise.resolve({
type: app.type,
additionalInfo: {
hangoutLink:
normalizedCalendarData.create?.appSpecificData?.googleCalendar?.hangoutLink ||
"https://GOOGLE_MEET_URL_IN_CALENDAR_EVENT",
},
hangoutLink:
normalizedCalendarData.create?.appSpecificData?.googleCalendar?.hangoutLink ||
"https://GOOGLE_MEET_URL_IN_CALENDAR_EVENT",
uid: normalizedCalendarData.create?.uid || "GOOGLE_CALENDAR_EVENT_ID",
id: normalizedCalendarData.create?.id || "GOOGLE_CALENDAR_EVENT_ID",
iCalUID:
normalizedCalendarData.create?.iCalUID || calEvent.iCalUID || "GOOGLE_CALENDAR_EVENT_ID",
password: "MOCK_PASSWORD",
url:
normalizedCalendarData.create?.appSpecificData?.googleCalendar?.hangoutLink ||
"https://GOOGLE_MEET_URL_IN_CALENDAR_EVENT",
});
} else if (app.type === "office365_calendar") {
return Promise.resolve({
type: app.type,
additionalInfo: {},
uid: normalizedCalendarData.create?.uid || "OFFICE_365_CALENDAR_EVENT_ID",
id: normalizedCalendarData.create?.id || "OFFICE_365_CALENDAR_EVENT_ID",
iCalUID:
normalizedCalendarData.create?.iCalUID ||
calEvent.iCalUID ||
"OFFICE_365_CALENDAR_EVENT_ID",
password: "MOCK_PASSWORD",
url:
normalizedCalendarData.create?.appSpecificData?.office365Calendar?.url ||
"https://UNUSED_URL",
});
} else {
return Promise.resolve({
type: app.type,
additionalInfo: {},
uid: "PROBABLY_UNUSED_UID",
hangoutLink:
(isGoogleMeetLocation
? normalizedCalendarData.create?.appSpecificData?.googleCalendar?.hangoutLink
: null) || "https://UNUSED_URL",
// A Calendar is always expected to return an id.
id: normalizedCalendarData.create?.id || "FALLBACK_MOCK_CALENDAR_EVENT_ID",
iCalUID: normalizedCalendarData.create?.iCalUID,
// Password and URL seems useless for CalendarService, plan to remove them if that's the case
password: "MOCK_PASSWORD",
url: "https://UNUSED_URL",
});
}
},
updateEvent: async function (
...rest: Parameters<Calendar["updateEvent"]>
): Promise<NewCalendarEventType> {
if (calendarData?.updationCrash) {
throw new Error("MockCalendarService.updateEvent fake error");
}
const [uid, event, externalCalendarId] = rest;
log.silly("mockCalendar.updateEvent", JSON.stringify({ uid, event, externalCalendarId }));
updateEventCalls.push({
args: {
uid,
event,
externalCalendarId,
},
calendarServiceConstructorArgs: {
credential,
},
});
const isGoogleMeetLocation = event.location === BookingLocations.GoogleMeet;
return Promise.resolve({
type: app.type,
additionalInfo: {},
uid: "PROBABLY_UNUSED_UID",
iCalUID: normalizedCalendarData.update?.iCalUID,
id: normalizedCalendarData.update?.uid || "FALLBACK_MOCK_ID",
// Password and URL seems useless for CalendarService, plan to remove them if that's the case
password: "MOCK_PASSWORD",
url: "https://UNUSED_URL",
location: isGoogleMeetLocation ? "https://UNUSED_URL" : undefined,
hangoutLink:
(isGoogleMeetLocation
? normalizedCalendarData.update?.appSpecificData?.googleCalendar?.hangoutLink
: null) || "https://UNUSED_URL",
conferenceData: isGoogleMeetLocation ? event.conferenceData : undefined,
});
},
deleteEvent: async (...rest: Parameters<Calendar["deleteEvent"]>) => {
log.silly("mockCalendar.deleteEvent", JSON.stringify({ rest }));
deleteEventCalls.push({
args: {
uid: rest[0],
event: rest[1],
externalCalendarId: rest[2],
},
calendarServiceConstructorArgs: {
credential,
},
});
},
getAvailability: async (
dateFrom: string,
dateTo: string,
selectedCalendars: IntegrationCalendar[],
shouldServeCache?: boolean
): Promise<EventBusyDate[]> => {
if (calendarData?.getAvailabilityCrash) {
throw new Error("MockCalendarService.getAvailability fake error");
}
getAvailabilityCalls.push({
args: {
dateFrom,
dateTo,
selectedCalendars,
shouldServeCache,
},
calendarServiceConstructorArgs: {
credential,
},
});
return new Promise((resolve) => {
resolve(calendarData?.busySlots || []);
});
},
} as Calendar;
}
);
}
return {
createEventCalls,
updateEventCalls,
deleteEventCalls,
getAvailabilityCalls,
};
}
export async function mockCalendarToHaveNoBusySlots(
metadataLookupKey: keyof typeof appStoreMetadata,
calendarData?: Parameters<typeof mockCalendar>[1]
) {
calendarData = calendarData || {
create: {
uid: "MOCK_ID",
},
update: {
uid: "UPDATED_MOCK_ID",
},
};
return await mockCalendar(metadataLookupKey, { ...calendarData, busySlots: [] });
}
export async function mockCalendarToCrashOnGetAvailability(metadataLookupKey: keyof typeof appStoreMetadata) {
return await mockCalendar(metadataLookupKey, { getAvailabilityCrash: true });
}
export async function mockCalendarToCrashOnCreateEvent(metadataLookupKey: keyof typeof appStoreMetadata) {
return await mockCalendar(metadataLookupKey, { creationCrash: true });
}
export async function mockCalendarToCrashOnUpdateEvent(metadataLookupKey: keyof typeof appStoreMetadata) {
return await mockCalendar(metadataLookupKey, { updationCrash: true });
}
export function mockVideoApp({
metadataLookupKey,
appStoreLookupKey,
videoMeetingData,
creationCrash,
updationCrash,
}: {
metadataLookupKey: string;
appStoreLookupKey?: string;
videoMeetingData?: {
password: string;
id: string;
url: string;
};
creationCrash?: boolean;
updationCrash?: boolean;
}) {
appStoreLookupKey = appStoreLookupKey || metadataLookupKey;
videoMeetingData = videoMeetingData || {
id: "MOCK_ID",
password: "MOCK_PASS",
url: `http://mock-${metadataLookupKey}.example.com`,
};
log.silly("mockVideoApp", JSON.stringify({ metadataLookupKey, appStoreLookupKey }));
const createMeetingCalls: any[] = [];
const updateMeetingCalls: any[] = [];
const deleteMeetingCalls: any[] = [];
const mockVideoAdapter = (credential: unknown) => {
return {
createMeeting: (...rest: Parameters<NonNullableVideoApiAdapter["createMeeting"]>) => {
if (creationCrash) {
throw new Error("MockVideoApiAdapter.createMeeting fake error");
}
createMeetingCalls.push({
credential,
args: rest,
});
return Promise.resolve({
type: appStoreMetadata[metadataLookupKey as keyof typeof appStoreMetadata].type,
...videoMeetingData,
});
},
updateMeeting: async (...rest: Parameters<NonNullableVideoApiAdapter["updateMeeting"]>) => {
if (updationCrash) {
throw new Error("MockVideoApiAdapter.updateMeeting fake error");
}
const [bookingRef, calEvent] = rest;
updateMeetingCalls.push({
credential,
args: rest,
});
if (!bookingRef.type) {
throw new Error("bookingRef.type is not defined");
}
if (!calEvent.organizer) {
throw new Error("calEvent.organizer is not defined");
}
log.silly("MockVideoApiAdapter.updateMeeting", JSON.stringify({ bookingRef, calEvent }));
return Promise.resolve({
type: appStoreMetadata[metadataLookupKey as keyof typeof appStoreMetadata].type,
...videoMeetingData,
});
},
deleteMeeting: async (...rest: Parameters<NonNullableVideoApiAdapter["deleteMeeting"]>) => {
log.silly("MockVideoApiAdapter.deleteMeeting", JSON.stringify(rest));
deleteMeetingCalls.push({
credential,
args: rest,
});
},
};
};
mockVideoAdapterRegistry[appStoreLookupKey] = Promise.resolve({
default: mockVideoAdapter,
});
return {
createMeetingCalls,
updateMeetingCalls,
deleteMeetingCalls,
};
}
export function mockSuccessfulVideoMeetingCreation({
metadataLookupKey,
appStoreLookupKey,
videoMeetingData,
}: {
metadataLookupKey: string;
appStoreLookupKey?: string;
videoMeetingData?: {
password: string;
id: string;
url: string;
};
}) {
return mockVideoApp({
metadataLookupKey,
appStoreLookupKey,
videoMeetingData,
});
}
export function mockVideoAppToCrashOnCreateMeeting({
metadataLookupKey,
appStoreLookupKey,
}: {
metadataLookupKey: string;
appStoreLookupKey?: string;
}) {
return mockVideoApp({
metadataLookupKey,
appStoreLookupKey,
creationCrash: true,
});
}
export function mockPaymentApp({
metadataLookupKey,
appStoreLookupKey,
}: {
metadataLookupKey: string;
appStoreLookupKey?: string;
}) {
const _appStoreLookupKey = appStoreLookupKey || metadataLookupKey;
const { paymentUid, externalId } = getMockPaymentService();
return {
paymentUid,
externalId,
};
}
export function mockErrorOnVideoMeetingCreation({
metadataLookupKey,
appStoreLookupKey,
}: {
metadataLookupKey: string;
appStoreLookupKey?: string;
}) {
appStoreLookupKey = appStoreLookupKey || metadataLookupKey;
const mockErrorAdapter = () => ({
createMeeting: () => {
throw new MockError("Error creating Video meeting");
},
});
mockVideoAdapterRegistry[appStoreLookupKey] = Promise.resolve({
default: mockErrorAdapter,
});
}
export function mockCrmApp(
metadataLookupKey: string,
crmData?: {
createContacts?: {
id: string;
email: string;
}[];
getContacts?: {
id: string;
email: string;
ownerEmail: string;
}[];
}
) {
let contactsCreated: {
id: string;
email: string;
}[] = [];
let contactsQueried: {
id: string;
email: string;
ownerEmail: string;
}[] = [];
const eventsCreated: boolean[] = [];
// Mock the CrmServiceMap directly instead of using the old app-store index approach
vi.doMock("@calcom/app-store/crm.apps.generated", async (importOriginal) => {
const original = await importOriginal<typeof import("@calcom/app-store/crm.apps.generated")>();
class MockCrmService {
constructor() {
log.debug("Create CrmService");
}
createContact() {
if (crmData?.createContacts) {
contactsCreated = crmData.createContacts;
return Promise.resolve(crmData?.createContacts);
}
return Promise.resolve([]);
}
getContacts(email: string) {
if (crmData?.getContacts) {
contactsQueried = crmData?.getContacts;
const contactsOfEmail = contactsQueried.filter((contact) => contact.email === email);
return Promise.resolve(contactsOfEmail);
}
return Promise.resolve([]);
}
createEvent() {
eventsCreated.push(true);
return Promise.resolve({});
}
}
return {
...original,
CrmServiceMap: {
...original.CrmServiceMap,
[metadataLookupKey]: Promise.resolve({
default: MockCrmService,
}),
},
};
});
return {
contactsCreated,
contactsQueried,
eventsCreated,
};
}
export function getBooker({
name,
email,
attendeePhoneNumber,
}: {
name: string;
email: string;
attendeePhoneNumber?: string;
}) {
return {
name,
email,
attendeePhoneNumber,
};
}
export function getMockedStripePaymentEvent({ paymentIntentId }: { paymentIntentId: string }) {
return {
id: null,
data: {
object: {
id: paymentIntentId,
},
},
} as unknown as Stripe.Event;
}
export async function mockPaymentSuccessWebhookFromStripe({ externalId }: { externalId: string }) {
let webhookResponse = null;
try {
await handleStripePaymentSuccess(getMockedStripePaymentEvent({ paymentIntentId: externalId }));
} catch (e) {
log.silly("mockPaymentSuccessWebhookFromStripe:catch", JSON.stringify(e));
webhookResponse = e as HttpError;
}
return { webhookResponse };
}
export function getExpectedCalEventForBookingRequest({
bookingRequest,
eventType,
}: {
bookingRequest: ReturnType<typeof getMockRequestDataForBooking>;
eventType: any;
}) {
return {
// keep adding more fields as needed, so that they can be verified in all scenarios
type: eventType.slug,
// Not sure why, but milliseconds are missing in cal Event.
startTime: bookingRequest.start.replace(".000Z", "Z"),
endTime: bookingRequest.end.replace(".000Z", "Z"),
};
}
export function getMockBookingReference(
bookingReference: Partial<BookingReference> & Pick<BookingReference, "type" | "uid" | "credentialId">
) {
let credentialId = bookingReference.credentialId;
if (bookingReference.type === appStoreMetadata.dailyvideo.type) {
// Right now we seems to be storing credentialId for `dailyvideo` in BookingReference as null. Another possible value is 0 in there.
credentialId = null;
log.debug("Ensuring null credentialId for dailyvideo");
}
return {
...bookingReference,
credentialId,
};
}
export function getMockBookingAttendee(
attendee: Omit<Attendee, "bookingId" | "phoneNumber" | "email" | "noShow"> & {
bookingSeat?: AttendeeBookingSeatInput;
phoneNumber?: string | null;
email: string;
noShow?: boolean;
}
) {
return {
id: attendee.id,
timeZone: attendee.timeZone,
name: attendee.name,
email: attendee.email,
locale: attendee.locale,
bookingSeat: attendee.bookingSeat || null,
phoneNumber: attendee.phoneNumber ?? undefined,
noShow: attendee.noShow ?? false,
};
}
const getMockAppStatus = ({
slug,
failures,
success,
overrideName,
}: {
slug: string;
failures: number;
success: number;
overrideName?: string;
}) => {
const foundEntry = Object.entries(appStoreMetadata).find(([, app]) => {
return app.slug === slug;
});
if (!foundEntry) {
throw new Error("App not found for the slug");
}
const foundApp = foundEntry[1];
return {
appName: overrideName ?? foundApp.slug,
type: foundApp.type,
failures,
success,
errors: [],
};
};
export const getMockFailingAppStatus = ({ slug }: { slug: string }) => {
return getMockAppStatus({ slug, failures: 1, success: 0 });
};
export const getMockPassingAppStatus = ({ slug, overrideName }: { slug: string; overrideName?: string }) => {
return getMockAppStatus({ slug, overrideName, failures: 0, success: 1 });
};
export const replaceDates = (dates: string[], replacement: Record<string, string>) => {
return dates.map((date) => {
return date.replace(/(.*)T/, (_, group1) => `${replacement[group1]}T`);
});
};
export const getDefaultBookingFields = ({
emailField,
bookingFields = [],
}: {
emailField?: Fields[number];
bookingFields: Fields;
}) => {
return [
{
name: "name",
type: "name",
sources: [{ id: "default", type: "default", label: "Default" }],
editable: "system",
required: true,
defaultLabel: "your_name",
},
emailField
? emailField
: {
name: "email",
type: "email",
label: "",
hidden: false,
sources: [{ id: "default", type: "default", label: "Default" }],
editable: "system-but-optional",
required: true,
placeholder: "",
defaultLabel: "email_address",
},
{
name: "location",
type: "radioInput",
sources: [{ id: "default", type: "default", label: "Default" }],
editable: "system",
required: false,
defaultLabel: "location",
getOptionsAt: "locations",
optionsInputs: {
phone: { type: "phone", required: true, placeholder: "" },
attendeeInPerson: { type: "address", required: true, placeholder: "" },
},
hideWhenJustOneOption: true,
},
{
name: "title",
type: "text",
hidden: true,
sources: [{ id: "default", type: "default", label: "Default" }],
editable: "system-but-optional",
required: true,
defaultLabel: "what_is_this_meeting_about",
defaultPlaceholder: "",
},
{
name: "notes",
type: "textarea",
sources: [{ id: "default", type: "default", label: "Default" }],
editable: "system-but-optional",
required: false,
defaultLabel: "additional_notes",
defaultPlaceholder: "share_additional_notes",
},
{
name: "guests",
type: "multiemail",
hidden: false,
sources: [{ id: "default", type: "default", label: "Default" }],
editable: "system-but-optional",
required: false,
defaultLabel: "additional_guests",
defaultPlaceholder: "email",
},
{
name: "rescheduleReason",
type: "textarea",
views: [{ id: "reschedule", label: "Reschedule View" }],
sources: [{ id: "default", type: "default", label: "Default" }],
editable: "system-but-optional",
required: false,
defaultLabel: "reason_for_reschedule",
defaultPlaceholder: "reschedule_placeholder",
},
...bookingFields,
] as Fields;
};
export const createDelegationCredential = async (orgId: number, type: "google" | "office365" = "google") => {
if (type === "google") {
const encryptedServiceAccountKey = {
type: "service_account",
auth_uri: "https://accounts.google.com/o/oauth2/auth",
client_id: "CLIENT_ID",
token_uri: "https://oauth2.googleapis.com/token",
project_id: "PROJECT_ID",
encrypted_credentials: `{"private_key": "PRIVATE_KEY"}`,
client_email: "CLIENT_EMAIL",
private_key_id: "PRIVATE_KEY_ID",
universe_domain: "googleapis.com",
client_x509_cert_url: "CLIENT_X509_CERT_URL",
auth_provider_x509_cert_url: "AUTH_PROVIDER_X509_CERT_URL",
};
const workspace = await prismock.workspacePlatform.create({
data: {
name: "Test Workspace",
slug: "google",
description: "Test Workspace",
defaultServiceAccountKey: encryptedServiceAccountKey,
enabled: true,
},
});
const decryptedServiceAccountKey = {
type: "service_account",
auth_uri: "https://accounts.google.com/o/oauth2/auth",
client_id: "CLIENT_ID",
token_uri: "https://oauth2.googleapis.com/token",
project_id: "PROJECT_ID",
private_key: "PRIVATE_KEY",
client_email: "CLIENT_EMAIL",
private_key_id: "PRIVATE_KEY_ID",
universe_domain: "googleapis.com",
client_x509_cert_url: "CLIENT_X509_CERT_URL",
auth_provider_x509_cert_url: "AUTH_PROVIDER_X509_CERT_URL",
};
const delegationCredential = await prismock.delegationCredential.create({
data: {
workspacePlatform: {
connect: {
id: workspace.id,
},
},
domain: "example.com",
enabled: true,
organization: {
connect: {
id: orgId,
},
},
serviceAccountKey: workspace.defaultServiceAccountKey as unknown as Prisma.InputJsonValue,
},
});
return { ...delegationCredential, serviceAccountKey: decryptedServiceAccountKey };
} else if (type === "office365") {
const encryptedServiceAccountKey = {
client_id: "CLIENT_ID",
encrypted_credentials: `{"private_key": "PRIVATE_KEY"}`,
tenant_id: "TENANT_ID",
};
const workspace = await prismock.workspacePlatform.create({
data: {
name: "Test Workspace",
slug: "office365",
description: "Test Workspace",
defaultServiceAccountKey: encryptedServiceAccountKey,
enabled: true,
},
});
const decryptedServiceAccountKey = {
client_id: "CLIENT_ID",
private_key: "PRIVATE_KEY",
tenant_id: "TENANT_ID",
};
const delegationCredential = await prismock.delegationCredential.create({
data: {
workspacePlatform: {
connect: {
id: workspace.id,
},
},
domain: "example.com",
enabled: true,
organization: {
connect: {
id: orgId,
},
},
serviceAccountKey: workspace.defaultServiceAccountKey as unknown as Prisma.InputJsonValue,
},
});
return { ...delegationCredential, serviceAccountKey: decryptedServiceAccountKey };
}
throw new Error(`Unsupported type: ${type}`);
};
export const buildDelegationCredential = ({ serviceAccountKey }: { serviceAccountKey: any }) => {
return {
id: -1,
key: {
access_token: "NOOP_UNUSED_DELEGATION_TOKEN",
},
delegatedTo: {
serviceAccountKey,
},
};
};