* fix: add missing vi.mock() calls to prevent vitest worker shutdown flakiness
Add vi.mock() calls for modules that trigger background network requests
or database connections during import. These transitive imports can cause
the vitest worker RPC to shut down while pending fetch/network operations
are still in flight, resulting in flaky test failures with:
Error: [vitest-worker]: Closing rpc while "fetch" was pending
The primary modules mocked are:
- @calcom/app-store/delegationCredential (triggers credential lookups)
- @calcom/prisma (triggers database initialization)
- @calcom/features/calendars/lib/CalendarManager (triggers calendar API calls)
- @calcom/features/auth/lib/verifyEmail (triggers email service)
- @calcom/lib/domainManager/organization (triggers domain lookups)
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
* fix: remove conflicting empty prisma mocks from files with prismock/prismaMock setups
- Remove vi.mock('@calcom/prisma', () => ({ default: {}, prisma: {} })) from 28 files
that already have prismock/prismaMock test doubles. Vitest hoists all vi.mock() calls
and the last one wins, so these empty mocks were overriding the functional test doubles.
- Fix CalendarSubscriptionService.test.ts to reuse the shared mock from
__mocks__/delegationCredential instead of creating a new unconfigured vi.fn()
- Remove DelegationCredentialRepository.test.ts empty prisma mock (different pattern)
- Remove vi.mock from inside beforeEach in intentToCreateOrg.handler.test.ts
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
* fix: add comprehensive delegationCredential mock exports to prevent CI test failures
The vi.mock blocks for @calcom/app-store/delegationCredential were missing
exports that the code under test transitively imports (e.g.
enrichUsersWithDelegationCredentials, enrichUserWithDelegationCredentialsIncludeServiceAccountKey,
buildAllCredentials, getFirstDelegationConferencingCredentialAppLocation).
Added all exports with passthrough implementations so the booking flow
works correctly without triggering real network requests.
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
* fix: correct credential mock return shapes to match real module API
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
* fix: revert unintended yarn.lock changes
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
---------
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Booking Audit System
Overview
The Booking Audit System tracks all actions and changes related to bookings in Cal.com. The architecture is built around two core tables (AuditActor and BookingAudit) that work together to maintain a complete, immutable audit trail.
Database Architecture
Core Tables
- AuditActor: Stores information about entities that perform actions on bookings. Maintains historical records even after users are deleted.
- BookingAudit: Stores audit records for all booking-related actions.
Key Design Decisions
AuditActor Table:
- Uses UUID primary keys for distributed system compatibility
- Soft references to
User(viauserUuid) andAttendee(viaattendeeId) without foreign key constraints - Audit trail persists independently even after users/attendees are deleted
- Unique constraints prevent duplicate audit actors
- Supports actor types: USER, GUEST, ATTENDEE, SYSTEM, APP
- Identity fields (email, phone, name) can be anonymized when source records are deleted
BookingAudit Table:
- Uses UUID v7 primary keys for time-sortable IDs
bookingUidstored as plain string (no foreign key) to preserve audit trail after booking deletiononDelete: Restrictprevents actor deletion if audit records exist- Explicit
timestampfield represents business event time (may differ fromcreatedAtif processed asynchronously) operationIdrequired field for correlating audit logs from a single user action across different audit types (BookingAudit, UserAudit, etc.)- JSON
datafield stores action-specific contextual data - Indexed for efficient queries by
bookingUid,actorId,timestamp, andoperationId
Protecting the Audit Trail:
- Database rejects deletion of
AuditActorrecords with associatedBookingAuditrecords - When a
Useris deleted, theirAuditActorrecord persists withuserUuidset to null
Actor Types
- USER: Registered Cal.com users
- GUEST: Non-registered users (typically booking guests)
- ATTENDEE: Guests who have an Attendee record associated with a booking
- SYSTEM: Automated system actions
Source and Actor Design Pattern
The booking audit system uses two complementary fields:
Source: The Channel
source identifies how the action was initiated:
- WEBAPP: Cal.com web application
- API_V1: API v1 endpoint
- API_V2: API v2 endpoint
- WEBHOOK: External webhook (e.g., Stripe)
- SYSTEM: Background job (e.g., Tasker's task, trigger.dev job for automatic no-show detection)
- UNKNOWN: Source cannot be determined
Actor: The Entity
actor identifies who or what performed the action:
- User Actor: Registered Cal.com user
- Guest Actor: Non-registered guest
- Attendee Actor: Attendee associated with a booking
- System Actor: Automated system action (generic or named for specific webhooks/services)
This separation enables clear compliance trails, easier debugging, better analytics, and security by distinguishing user-initiated vs automated actions.
Audit Actions
The system tracks various booking actions including:
- CREATED: Initial booking creation
- RESCHEDULED: Booking time/date changed
- ACCEPTED: Booking request approved
- CANCELLED: Booking cancelled
- REJECTED: Booking request declined
- RESCHEDULE_REQUESTED: Request to reschedule
- ATTENDEE_ADDED: New attendee added
- ATTENDEE_REMOVED: Attendee removed
- REASSIGNMENT: Booking reassigned to different host
- LOCATION_CHANGED: Meeting location updated
- NO_SHOW_UPDATED: Host or attendee no-show status changed
- SEAT_BOOKED: Seat reserved in group booking
- SEAT_RESCHEDULED: Seat rescheduled in group booking
Data Structure Pattern
Most audit actions track changes using a consistent structure:
- Each field tracks both old and new values:
{ old: T | null, new: T } old: Previous value (null if field didn't exist before)new: New value after the change- Complete before/after state captured in every record
Exception: The CREATED action captures initial booking state at creation using a flat object with initial values.
Schema Versioning
The audit system uses per-action versioning. Each action maintains its own schema version independently.
Benefits:
- Update one action's schema without affecting others
- Old records handled via discriminated unions (no migration required)
- Strongly-typed schemas for input and storage
Storage Structure:
Version stored separately from audit data: { version, data: {} }
Table Relationships
AuditActor (1) ──────< (many) BookingAudit
↑
│ (soft reference, no FK)
├──────────── User (via userUuid)
│ (soft reference, no FK)
└──────────── Attendee (via attendeeId)
Relationship Details:
- AuditActor → BookingAudit: One-to-Many with FK constraint and
onDelete: Restrict - AuditActor → User: Soft reference (no FK) - nullable to preserve audits after user deletion
- AuditActor → Attendee: Soft reference (no FK) - nullable to preserve audits after attendee deletion
Indexing Strategy
AuditActor Table:
- Indexed on
email,userUuid,attendeeIdfor fast lookups - Indexed on
pseudonymizedAtfor compliance cleanup jobs
BookingAudit Table:
- Indexed on
bookingUid(primary query pattern) - Indexed on
actorId(secondary query pattern) - Indexed on
timestamp(time-based sorting and filtering) - Indexed on
operationId(correlating multi-booking operations)
Special Actors
SYSTEM Actor:
- Fixed UUID representing automated actions
- Used for automated status changes, system-generated meeting URLs, scheduled operations
- Single instance across the entire system
- No userUuid, attendeeId, email, or phone
Design Principles
1. Immutability
Audit records are append-only. Once created, they are never modified or deleted. This ensures complete historical accuracy and a tamper-proof audit trail.
2. Historical Preservation
Actor information is preserved even after source records are deleted. AuditActor records persist with anonymized identity fields. The audit trail remains complete and queryable even after user/attendee deletion.
3. Flexibility
The JSON data field provides schema flexibility for action-specific context without database schema changes. Backward compatible with versioning.
4. Traceability
Every action is fully traceable with who (actor), what (action), when (timestamp), and contextual data.
5. Integrity
Database constraints ensure data quality through foreign keys, onDelete: Restrict protection, unique constraints, and strategic indexes.
6. Reality Over Enforcement
The audit system records actual state, not expected state. It captures what actually happened without enforcing business rules:
- Store actual values from the database
- Record all actions, including anomalies
- Business logic layer enforces rules; audit layer records faithfully
Benefits:
- Shows real system behavior including anomalies
- Easier debugging
- No silent failures
7. Compliance & Data Privacy
GDPR & HIPAA Compliance:
- AuditActor records persist with PII fields nullified when users are deleted
- Maintains immutable audit trail as required by HIPAA §164.312(b)
- GDPR Article 17 compliance through anonymization:
userUuid,email,phone,nameset to null - Application-level logic handles retention policies
8. Queue Privacy
Zero PII in Queue: The audit system works with third-party queue providers without exposing PII:
- UserActor: Only
userUuidis queued - AttendeeActor: Only
attendeeIdis queued - GuestActor: Actor record created before queueing; only
actorIdis queued
Benefits:
- Safe to use third-party queue providers
- GDPR and HIPAA compliant
- Audit trail remains complete even if queue is compromised
Service Architecture
BookingEventHandlerService is the primary entry point for tracking booking changes. It:
- Receives booking events from various parts of the application
- Queues audit tasks via BookingAuditProducerService
- Handles other side effects such as webhooks and notifications
Queue Payload Structure:
bookingUid: String identifier for the bookingactor: ID-only actor object (userUuid,attendeeId, oractorId)organizationId: Number (for feature flag checks)action: Enum value (e.g., "CREATED", "CANCELLED")operationId: Required string for correlating related audit logsdata: Action-specific datatimestamp: Number (milliseconds since epoch)source: Action source (API_V1, API_V2, WEBAPP, WEBHOOK, SYSTEM, UNKNOWN)
BookingAuditTaskConsumer processes audit records:
- Validates queue payload structure
- Resolves actor IDs to AuditActor records
- Routes to appropriate action service for data validation and formatting
- Creates immutable audit records in BookingAudit table
Operation ID
The operationId field correlates audit logs that result from a single user action affecting multiple bookings or across different audit types (BookingAudit, UserAudit, etc.).
Benefits:
- Easy to identify all audits from a single user action
- Track bulk vs individual operations
- Improved debugging and understanding of action scope
- Indexed field for fast lookups
Summary
The Booking Audit System provides a robust, scalable architecture for tracking all booking-related actions:
- Complete Audit Trail: Every action tracked with full context
- Historical Preservation: Data retained even after deletions through PII anonymization
- Flexible Schema: JSON data supports evolution without migrations
- Strong Integrity: Database constraints ensure data quality
- Performance: Strategic indexes, UUID v7 for time-sortable IDs
- HIPAA & GDPR Compliant: Immutable audit records, anonymized actors
- Reality-Based Recording: Captures actual state for debugging
- Independent Audit Trail: Persists after booking deletion
- Operation Correlation: Links related audit logs across different audit types
This architecture supports compliance requirements, debugging, analytics, and provides transparency for users and administrators.