+9









Hariom Balhara
GitHub
hariom@cal.com <hariombalhara@gmail.com>
hariom@cal.com <hariombalhara@gmail.com>
hariom@cal.com <hariombalhara@gmail.com>
hariom@cal.com <hariombalhara@gmail.com>
hariom@cal.com <hariombalhara@gmail.com>
hariom@cal.com <hariombalhara@gmail.com>
hariom@cal.com <hariombalhara@gmail.com>
hariom@cal.com <hariombalhara@gmail.com>
hariom@cal.com <hariombalhara@gmail.com>
hariom@cal.com <hariombalhara@gmail.com>
hariom@cal.com <hariombalhara@gmail.com>
hariom@cal.com <hariombalhara@gmail.com>
hariom@cal.com <hariombalhara@gmail.com>
hariom@cal.com <hariombalhara@gmail.com>
Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Udit Takkar
Volnei Munhoz
5993889616
* Integrate creation/rescheduling booking audit * fix: add missing hostUserUuid to booking audit test data Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * fix-ci * feat: enhance booking audit with seat reference - Added support for seat reference in booking audit actions. - Updated localization for booking creation to include seat information. - Modified relevant services to pass attendee seat ID during booking creation. * fix: update test data to match schema requirements - Add seatReferenceUid: null to default mock audit log data - Add seatReferenceUid: null to multiple audit logs test case - Convert SEAT_RESCHEDULED test data to use numeric timestamps instead of ISO strings Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * Allow nullish seatReferenceUid * feat: enhance booking audit to support rescheduledBy information - Updated booking audit actions to include rescheduledBy details, allowing tracking of who rescheduled a booking. - Refactored related services to accommodate the new rescheduledBy parameter in booking events. - Adjusted type definitions and function signatures to reflect the changes in the booking audit context. * Avoid possible run time issue * Fix imoport path * fix failing test due to merge from main\ * Pass useruuid * chore: retrigger CI (flaky unit test) Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * feat: add context parameter to bulk audit methods for impersonation support - Add context parameter to queueBulkCreatedAudit and queueBulkRescheduledAudit in BookingAuditProducerService interface - Add context parameter to BookingAuditTaskerProducerService implementation - Add context parameter to onBulkBookingsCreated and onBulkBookingsRescheduled in BookingEventHandlerService - Update RegularBookingService.fireBookingEvents to accept and pass impersonation context - Update RecurringBookingService.fireBookingEvents to accept and pass impersonation context - Update handleSeats to accept and pass impersonation context Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * chore: Integrate mark-no-show booking audit Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * fix: Simplify host no-show audit and add comment for attendee actor Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * feat: make impersonatedByUserUuid required with explicit null for non-impersonation cases Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * feat: integrate impersonatedByUserUuid into booking cancellation audit flow - Add impersonatedByUserUuid to CancelBookingInput and CancelBookingMeta types - Pass audit context with impersonatedBy to onBookingCancelled and onBulkBookingsCancelled - Update all cancel booking call sites to pass impersonatedByUserUuid: - Web app cancel route: uses session impersonatedBy - API v1 and v2: explicitly set to null (no impersonation support) Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * refactor: make impersonatedByUserUuid optional and remove explicit null assignments - Changed impersonatedByUserUuid from required (string | null) to optional (string?) - Removed explicit null assignments from API v1 and v2 endpoints - Keep impersonatedByUserUuid only where impersonation actually occurs (web app) Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * Apply suggestions from code review Rfemove unnecessary comment * fix: restore bookingMeta variable in createBookingForApiV1 Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * fix: Make actionSource required with ValidActionSource type and remove unnecessary comments Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * fix: Remove merge conflict markers from bookings.service.ts Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * feat: implement SYSTEM as a booking audit source for background tasks, including no-show triggers. * refactor: Move prisma query to BookingRepository for audit logging Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * Conflict resolution * refactor: introduce `handleMarkHostNoShow` for public viewer and standardize actor and action source parameters for no-show actions. * refactor: Combine HostNoShowUpdatedAuditActionService and AttendeeNoShowUpdatedAuditActionService into NoShowUpdatedActionService - Create new NoShowUpdatedAuditActionService with combined schema supporting both noShowHost and noShowAttendee as optional fields - Update BookingAuditActionServiceRegistry to use combined service with NO_SHOW_UPDATED action type - Update BookingAuditTaskerProducerService with single queueNoShowUpdatedAudit method - Update BookingAuditProducerService.interface.ts with combined method - Update BookingEventHandlerService with single onNoShowUpdated method - Update handleMarkNoShow.ts to use combined audit service - Update tasker tasks (triggerGuestNoShow, triggerHostNoShow) to use combined service - Add NO_SHOW_UPDATED to Prisma BookingAuditAction enum - Remove old separate HostNoShowUpdatedAuditActionService and AttendeeNoShowUpdatedAuditActionService files This allows a single API action (e.g., API V2 markAbsent) that updates both host and attendee no-show status to be logged as a single audit event. Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * chore: Add migration for NO_SHOW_UPDATED audit action enum value Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * refactor: Update no-show audit schema to use host and attendees array - Rename noShowHost to host and noShowAttendee to attendees (remove redundant prefix) - Change attendees from single value to array to support multiple attendees in single audit entry - Update handleMarkNoShow.ts to create single audit entry for all attendees - Update tasker tasks (triggerGuestNoShow, triggerHostNoShow) to use new schema - Update display data types to reflect new schema structure Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * Update schema to be more clear. Call onNoShowUpdated immediately after DB update as audit only cares about DB update, and this would avoid any accidental Audit update on DB change * chore: accommodate schema changes and fix type errors for no-show audit Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * fix: Update migration to remove old no-show enum values Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * fix: Use updated attendees in webhook payload for guest no-show Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * fix: Remove duplicate imports and fix actor parameter in bookings.service.ts Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * refactor: Move booking query to BookingRepository and remove unused method Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * refactor: centralize no-show audit event firing and attendee fetching within `handleMarkNoShow` for improved clarity. * fix: Build attendeesNoShow as Record with attendee IDs as keys Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * Fix and add tests for handleMarkBoShow * refactor: Move prisma queries to AttendeeRepository and BookingRepository Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * fix: Correct return type for updateNoShow method (noShow can be null) Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * fix: Update handleMarkNoShow tests to mock new repository methods Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * fix triggerGuestNoShow * refactor: Move triggerGuestNoShow queries to AttendeeRepository and add unit tests - Add new methods to AttendeeRepository: - findByBookingId: Get attendees with id, email, noShow - findByBookingIdWithDetails: Get full attendee details - updateManyNoShowByBookingIdAndEmails: Update specific attendees - updateManyNoShowByBookingIdExcludingEmails: Update all except specific emails - Refactor triggerGuestNoShow.ts to use AttendeeRepository instead of direct Prisma calls - Add comprehensive unit tests for triggerGuestNoShow following handleMarkNoShow.test.ts pattern: - Mock repositories and external services - In-memory DB simulation - Test core functionality, audit logging, webhook payload, error handling, and edge cases Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * refactor: Move triggerHostNoShow queries to repositories and delete unit test file Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * refactor: Convert repository methods to use named parameters objects Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * refactor: Enhance no-show handling by consolidating audit logging and updating repository interactions - Refactor `buildResultPayload` to accept a structured argument for better clarity. - Update `updateAttendees` to use a named parameter object for improved readability. - Introduce `fireNoShowUpdatedEvent` to centralize no-show audit logging for both hosts and guests. - Remove redundant methods and streamline attendee retrieval in `AttendeeRepository`. - Add comprehensive unit tests for no-show event handling, ensuring accurate audit logging and webhook triggering. * test: Add tests for handleMarkHostNoShow guest actor and unmarking host no-show Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * fix: update attendeesNoShow validation to handle numeric keys - Changed attendeesNoShow schema to use z.coerce.number() for key validation, ensuring string keys are correctly coerced to numbers. - Updated test to validate attendeesNoShow data using numeric key comparison, improving robustness of the audit data checks. * test: Add integration tests for NoShowUpdatedAuditActionService coerce fix Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * feat: add graceful error handling for audit log enrichment - Add hasError field to EnrichedAuditLog type - Create buildFallbackAuditLog() method for failed enrichments - Wrap enrichAuditLog() in try-catch to handle errors gracefully - Add booking_audit_action.error_processing translation key - Update BookingHistory.tsx to show warning icon for error logs - Hide 'Show details' button for logs with hasError - Add comprehensive test cases for error handling scenarios Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * feat: Enhance booking audit action services with new display fields and improved validation - Added "attendee_no_show_updated" and "no_show_updated" translations to common.json. - Updated IAuditActionService to include new methods for handling display fields and migration. - Enhanced NoShowUpdatedAuditActionService to differentiate between host and attendee no-show updates. - Improved ReassignmentAuditActionService to ensure consistent handling of audit data. - Refactored BookingAuditViewerService for better clarity and maintainability. * refactor: Use attendeeEmail instead of attendeeId as key in audit data and store host's userUuid - Updated schema to use attendee email as key instead of attendee ID because attendee records can be reused with different person's data - Store host's userUuid in audit data with format: host: { userUuid, noShow: { old, new } } - Updated fireNoShowUpdated to accept hostUserUuid parameter - Added uuid field to BookingRepository.findByUidIncludeEventTypeAttendeesAndUser - Updated NoShowUpdatedAuditActionService getDisplayFields to work with email-based keys - Added attendeeRepository to DI modules for BookingAuditTaskConsumer - Updated tests to match new schema format Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * test: Update integration tests to use new schema format with host.userUuid and email keys Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * fix: correct audit schema for SYSTEM source and ensure attendee lookup by bookingId - Fix schema inconsistency: use host.userUuid format instead of hostNoShow - Add user.uuid to getBooking select for audit logging - Log warning when hostUserUuid is undefined but host no-show is being updated - Use email as key for attendeesNoShow (not attendee ID) to match schema - Fetch attendees by bookingId before updating to ensure correct scoping - Remove unused safeStringify import * refactor: Change attendeesNoShow schema from Record to Array format - Update NoShowUpdatedAuditActionService schema to use array format: attendeesNoShow: Array<{attendeeEmail: string, noShow: {old, new}}> - Update handleMarkNoShow.ts to build attendeesNoShow as array - Update common.ts fireNoShowUpdatedEvent to convert Map to array - Update all tests to use new array format with attendeeEmail property Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * refactor: Update attendeesNoShow schema to use array format in triggerNoShow tasks - Refactor `triggerGuestNoShow` and `triggerHostNoShow` to replace Map with array for `attendeesNoShowAudit`. - Update `fireNoShowUpdatedEvent` to handle the new array format for attendees. - Ensure consistent data structure across no-show audit logging for better clarity and maintainability. * fix: Update ReassignmentAuditActionService to use GetDisplayFieldsParams interface Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * fix: Update ReassignmentAuditActionService tests to use GetDisplayFieldsParams interface Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * fix: Use handleMarkAttendeeNoShow for API v2 mark absent endpoint - Export handleMarkAttendeeNoShow from platform-libraries - Update API v2 bookings service to use handleMarkAttendeeNoShow with actionSource - Add validation for userUuid before calling handleMarkAttendeeNoShow Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * refactor: Enhance display fields structure in booking audit components - Updated `BookingHistory.tsx` to allow for more flexible display fields, including optional raw values and arrays of values. - Introduced `DisplayFieldValue` component for rendering display fields, improving clarity and maintainability. - Adjusted `IAuditActionService` and `NoShowUpdatedAuditActionService` to align with the new display fields structure. - Ensured consistent handling of display fields across the booking audit service for better data representation. * fix: Remove debug code that duplicated first attendee in display fields Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * refactor: Update attendee repository methods to use structured parameters - Modified `updateNoShow`, `updateManyNoShowByBookingIdAndEmails`, and `updateManyNoShowByBookingIdExcludingEmails` methods to accept structured `where` and `data` parameters for better clarity and maintainability. - Updated calls to these methods throughout the codebase to reflect the new parameter structure. - Removed unused `findByIdWithNoShow` method and adjusted related logic to streamline attendee data retrieval. * feat: Add infrastructure for no-show audit integration - Add Prisma migrations for SYSTEM source and NO_SHOW_UPDATED audit action - Add NoShowUpdatedAuditActionService with array-based attendeesNoShow schema - Update BookingAuditActionServiceRegistry to include NO_SHOW_UPDATED - Update BookingAuditTaskConsumer and BookingAuditViewerService - Add AttendeeRepository methods for no-show queries - Update IAuditActionService interface with values array support - Update locales with no-show audit translation keys Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * fix: Add NO_SHOW_UPDATED to BookingAuditAction and SYSTEM to ActionSource types Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * fix: Remove HOST_NO_SHOW_UPDATED and ATTENDEE_NO_SHOW_UPDATED from BookingAuditAction type to match Prisma schema Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * fix: Update BookingAuditActionSchema to use NO_SHOW_UPDATED instead of HOST_NO_SHOW_UPDATED and ATTENDEE_NO_SHOW_UPDATED Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * refactor: Remove deprecated no-show audit services and unify to NoShowUpdatedAuditActionService - Delete HostNoShowUpdatedAuditActionService and AttendeeNoShowUpdatedAuditActionService - Update BookingAuditProducerService.interface.ts to use queueNoShowUpdatedAudit - Update BookingAuditTaskerProducerService.ts to use queueNoShowUpdatedAudit - Update BookingEventHandlerService.ts to use onNoShowUpdated - Add integration tests for NoShowUpdatedAuditActionService Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * fix: Update test mock to match new AttendeeRepository.updateNoShow signature Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * refactor: rename handleMarkAttendeeNoShow to handleMarkNoShow and update related types - Renamed `handleMarkAttendeeNoShow` to `handleMarkNoShow` for clarity. - Updated type definitions to reflect the new naming convention. - Modified the `markNoShow` handler to require `userUuid` and adjusted related logic. - Enhanced the `fireNoShowUpdatedEvent` to accommodate changes in event type structure. - Updated references across various files to ensure consistency with the new function name. * handle null value * fix: add explicit parentheses for operator precedence clarity Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * Fix cubic reported bug * feat: add impersonation audit support to no-show flow (#27601) Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * feat: add impersonatedByUserUuid context to all BookingEventHandler calls (#27602) - Add context parameter to confirm.handler.ts (onBulkBookingsRejected, onBookingRejected) - Add context parameter to requestReschedule.handler.ts (onRescheduleRequested) - Add context parameter to editLocation.handler.ts (onLocationChanged) - Add context parameter to addGuests.handler.ts (onAttendeeAdded) - Add context parameter to handleConfirmation.ts (onBulkBookingsAccepted, onBookingAccepted) - Update _router.tsx to pass impersonatedByUserUuid from session to handlers This ensures all BookingEventHandler method calls that have loggedInUser context now properly support impersonation tracking for audit logging. Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix: remove stray merge artifact in RegularBookingService Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * fix: remove duplicate imports in BookingRepository Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * fix: resolve type errors and duplicate declarations from merge Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * fix: remove duplicate test assertions and duplicate test from merge artifacts Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * fix: remove redundant ?? undefined from impersonatedByUserUuid Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * fix: update impersonatedByUserUuid type to string | null for consistency Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * fix: make impersonatedByUserUuid required in booking audit flows Changes impersonatedByUserUuid from optional (?: string | null) to required (: string | null) in all booking-related type definitions. This ensures audit-critical parameters are never accidentally omitted. Fixed all call sites to explicitly pass the value or null. Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * fix: add impersonatedByUserUuid to remaining callers and tests Adds impersonatedByUserUuid: null to API v1, API v2, and test callers of handleCancelBooking that were missing the now-required parameter. Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * fix: require impersonatedByUserUuid in DTOs and pass it across all booking flows (coalesce to null) Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * fix: add impersonatedByUserUuid to remaining call sites (confirm, markNoShow, webhook, tests) Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * fix: add impersonatedByUserUuid to confirm handler test and API v2 confirm/decline calls Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * fix: derive impersonatedByUserUuid from session in reportBooking handler instead of hardcoding null Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * fix: reorder spread to ensure impersonatedByUserUuid null fallback is not overwritten Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * fix: add missing impersonatedByUserUuid to CalendarSyncService cancel and reschedule calls Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * fix: update CalendarSyncService tests to expect impersonatedByUserUuid Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * ci: retrigger failed checks (attempt 1) Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * ci: retrigger failed checks (attempt 2) Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * ci: retrigger failed checks (attempt 3) Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * chore: bust prisma cache with comment change Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * ci: retrigger after cache bust (attempt 2) Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * ci: retrigger after cache bust (attempt 3) Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Udit Takkar <53316345+Udit-takkar@users.noreply.github.com> Co-authored-by: Volnei Munhoz <volnei@cal.com>
1051 lines
36 KiB
TypeScript
1051 lines
36 KiB
TypeScript
import { beforeEach, describe, expect, test, vi } from "vitest";
|
|
|
|
import type { CalendarSubscriptionEventItem } from "@calcom/features/calendar-subscription/lib/CalendarSubscriptionPort.interface";
|
|
import type { BookingRepository } from "@calcom/lib/server/repository/booking";
|
|
import type { SelectedCalendar } from "@calcom/prisma/client";
|
|
|
|
import {
|
|
type BookingWithEventType,
|
|
CalendarSyncService,
|
|
buildMetadataFromCalendarEvent,
|
|
buildRescheduleBookingData,
|
|
extractBookingResponses,
|
|
hasStartTimeChanged,
|
|
mergeBookingResponsesWithEventData,
|
|
} from "../CalendarSyncService";
|
|
|
|
const { mockHandleCancelBooking, mockCreateBooking } = vi.hoisted(() => ({
|
|
mockHandleCancelBooking: vi.fn().mockResolvedValue(undefined),
|
|
mockCreateBooking: vi.fn().mockResolvedValue(undefined),
|
|
}));
|
|
|
|
vi.mock("@calcom/features/bookings/lib/handleCancelBooking", () => ({
|
|
default: mockHandleCancelBooking,
|
|
}));
|
|
|
|
vi.mock("@calcom/features/bookings/di/RegularBookingService.container", () => ({
|
|
getRegularBookingService: () => ({
|
|
createBooking: mockCreateBooking,
|
|
}),
|
|
}));
|
|
|
|
vi.mock("@calcom/lib/idempotencyKey/idempotencyKeyService", () => ({
|
|
IdempotencyKeyService: {
|
|
generate: vi.fn(() => "test-idempotency-key"),
|
|
},
|
|
}));
|
|
|
|
vi.mock("@sentry/nextjs", () => ({
|
|
metrics: {
|
|
count: vi.fn(),
|
|
distribution: vi.fn(),
|
|
},
|
|
}));
|
|
|
|
const makeBooking = (overrides: Partial<BookingWithEventType> = {}): BookingWithEventType =>
|
|
({
|
|
id: 1,
|
|
uid: "booking-uid-123",
|
|
userId: 42,
|
|
userPrimaryEmail: "user@example.com",
|
|
startTime: new Date("2024-01-15T10:00:00Z"),
|
|
endTime: new Date("2024-01-15T11:00:00Z"),
|
|
title: "Team Standup",
|
|
description: "Daily standup meeting",
|
|
location: "https://meet.google.com/abc",
|
|
smsReminderNumber: null,
|
|
responses: {
|
|
name: "John Doe",
|
|
email: "john@example.com",
|
|
notes: "Daily standup meeting",
|
|
location: {
|
|
value: "https://meet.google.com/abc",
|
|
label: "Google Meet",
|
|
optionValue: "https://meet.google.com/abc",
|
|
},
|
|
},
|
|
eventTypeId: 10,
|
|
eventType: { id: 10, title: "Standup", length: 60 },
|
|
...overrides,
|
|
}) as unknown as BookingWithEventType;
|
|
|
|
const makeEvent = (overrides: Partial<CalendarSubscriptionEventItem> = {}): CalendarSubscriptionEventItem => ({
|
|
id: "gcal-event-1",
|
|
iCalUID: "booking-uid-123@cal.com",
|
|
start: new Date("2024-01-15T14:00:00Z"),
|
|
end: new Date("2024-01-15T15:00:00Z"),
|
|
busy: true,
|
|
summary: "Team Standup",
|
|
description: "Daily standup meeting",
|
|
location: "https://meet.google.com/abc",
|
|
status: "confirmed",
|
|
isAllDay: false,
|
|
timeZone: "America/New_York",
|
|
recurringEventId: null,
|
|
originalStartDate: null,
|
|
createdAt: new Date("2024-01-10T00:00:00Z"),
|
|
updatedAt: new Date("2024-01-15T09:00:00Z"),
|
|
etag: '"etag-123"',
|
|
kind: "calendar#event",
|
|
...overrides,
|
|
});
|
|
|
|
const mockSelectedCalendar: SelectedCalendar = {
|
|
id: "sc-1",
|
|
userId: 42,
|
|
credentialId: 1,
|
|
integration: "google_calendar",
|
|
externalId: "user@gmail.com",
|
|
eventTypeId: null,
|
|
delegationCredentialId: null,
|
|
domainWideDelegationCredentialId: null,
|
|
googleChannelId: null,
|
|
googleChannelKind: null,
|
|
googleChannelResourceId: null,
|
|
googleChannelResourceUri: null,
|
|
googleChannelExpiration: null,
|
|
error: null,
|
|
lastErrorAt: null,
|
|
watchAttempts: 0,
|
|
maxAttempts: 3,
|
|
unwatchAttempts: 0,
|
|
createdAt: new Date(),
|
|
updatedAt: new Date(),
|
|
channelId: "channel-1",
|
|
channelKind: "web_hook",
|
|
channelResourceId: "resource-1",
|
|
channelResourceUri: "https://example.com/webhook",
|
|
channelExpiration: new Date(Date.now() + 86400000),
|
|
syncSubscribedAt: new Date(),
|
|
syncToken: "sync-token-1",
|
|
syncedAt: new Date(),
|
|
syncErrorAt: null,
|
|
syncErrorCount: 0,
|
|
};
|
|
|
|
// ─── Helper function tests (pure, no mocks needed) ───────────────────────────
|
|
|
|
describe("buildRescheduleBookingData", () => {
|
|
test("preserves original 60-min duration when event is moved", () => {
|
|
const booking = makeBooking();
|
|
const event = makeEvent({
|
|
start: new Date("2024-01-15T14:00:00Z"),
|
|
end: new Date("2024-01-15T15:00:00Z"),
|
|
});
|
|
|
|
const result = buildRescheduleBookingData(booking, event);
|
|
|
|
expect(result.start).toBe("2024-01-15T14:00:00.000Z");
|
|
expect(result.end).toBe("2024-01-15T15:00:00.000Z");
|
|
});
|
|
|
|
test("preserves original 30-min duration when event is stretched to 90min externally", () => {
|
|
const booking = makeBooking({
|
|
startTime: new Date("2024-01-15T10:00:00Z"),
|
|
endTime: new Date("2024-01-15T10:30:00Z"),
|
|
});
|
|
const event = makeEvent({
|
|
start: new Date("2024-01-15T14:00:00Z"),
|
|
end: new Date("2024-01-15T15:30:00Z"), // 90min in external calendar
|
|
});
|
|
|
|
const result = buildRescheduleBookingData(booking, event);
|
|
|
|
expect(result.start).toBe("2024-01-15T14:00:00.000Z");
|
|
expect(result.end).toBe("2024-01-15T14:30:00.000Z"); // 30min preserved, NOT 15:30
|
|
});
|
|
|
|
test("preserves original 2-hour duration when event is shortened externally", () => {
|
|
const booking = makeBooking({
|
|
startTime: new Date("2024-01-15T10:00:00Z"),
|
|
endTime: new Date("2024-01-15T12:00:00Z"),
|
|
});
|
|
const event = makeEvent({
|
|
start: new Date("2024-01-15T14:00:00Z"),
|
|
end: new Date("2024-01-15T14:30:00Z"), // shortened to 30min
|
|
});
|
|
|
|
const result = buildRescheduleBookingData(booking, event);
|
|
|
|
expect(result.start).toBe("2024-01-15T14:00:00.000Z");
|
|
expect(result.end).toBe("2024-01-15T16:00:00.000Z"); // 2h preserved
|
|
});
|
|
|
|
test("falls back to booking start when event start is null", () => {
|
|
const booking = makeBooking();
|
|
const event = makeEvent({ start: null });
|
|
|
|
const result = buildRescheduleBookingData(booking, event);
|
|
|
|
expect(result.start).toBe("2024-01-15T10:00:00.000Z");
|
|
expect(result.end).toBe("2024-01-15T11:00:00.000Z");
|
|
});
|
|
|
|
test("uses event timezone, falls back to UTC", () => {
|
|
const booking = makeBooking();
|
|
|
|
const withTz = buildRescheduleBookingData(booking, makeEvent({ timeZone: "Europe/London" }));
|
|
expect(withTz.timeZone).toBe("Europe/London");
|
|
|
|
const withoutTz = buildRescheduleBookingData(booking, makeEvent({ timeZone: null }));
|
|
expect(withoutTz.timeZone).toBe("UTC");
|
|
});
|
|
|
|
test("sets rescheduleUid to original booking uid", () => {
|
|
const booking = makeBooking({ uid: "original-uid-abc" });
|
|
const event = makeEvent();
|
|
|
|
const result = buildRescheduleBookingData(booking, event);
|
|
|
|
expect(result.rescheduleUid).toBe("original-uid-abc");
|
|
});
|
|
|
|
test("sets eventTypeId from booking", () => {
|
|
const booking = makeBooking({ eventTypeId: 99 });
|
|
const event = makeEvent();
|
|
|
|
const result = buildRescheduleBookingData(booking, event);
|
|
|
|
expect(result.eventTypeId).toBe(99);
|
|
});
|
|
|
|
test("handles cross-day rescheduling correctly", () => {
|
|
const booking = makeBooking({
|
|
startTime: new Date("2024-01-15T23:00:00Z"),
|
|
endTime: new Date("2024-01-16T00:30:00Z"), // 90min crossing midnight
|
|
});
|
|
const event = makeEvent({
|
|
start: new Date("2024-01-20T22:00:00Z"),
|
|
end: new Date("2024-01-20T23:00:00Z"),
|
|
});
|
|
|
|
const result = buildRescheduleBookingData(booking, event);
|
|
|
|
expect(result.start).toBe("2024-01-20T22:00:00.000Z");
|
|
expect(result.end).toBe("2024-01-20T23:30:00.000Z"); // 90min preserved, crosses midnight
|
|
});
|
|
});
|
|
|
|
describe("extractBookingResponses", () => {
|
|
test("returns existing responses when present as object", () => {
|
|
const booking = makeBooking({
|
|
responses: { name: "Jane", email: "jane@test.com", custom: "value" },
|
|
});
|
|
|
|
const result = extractBookingResponses(booking);
|
|
|
|
expect(result).toEqual({ name: "Jane", email: "jane@test.com", custom: "value" });
|
|
});
|
|
|
|
test("builds fallback responses when responses is null", () => {
|
|
const booking = makeBooking({
|
|
responses: null,
|
|
title: "My Booking",
|
|
userPrimaryEmail: "user@test.com",
|
|
description: "Some notes",
|
|
smsReminderNumber: "+5511999999999",
|
|
location: "Zoom",
|
|
});
|
|
|
|
const result = extractBookingResponses(booking);
|
|
|
|
expect(result).toEqual({
|
|
name: "My Booking",
|
|
email: "user@test.com",
|
|
guests: [],
|
|
notes: "Some notes",
|
|
smsReminderNumber: "+5511999999999",
|
|
location: {
|
|
label: "Zoom",
|
|
value: "Zoom",
|
|
optionValue: "Zoom",
|
|
},
|
|
});
|
|
});
|
|
|
|
test("builds fallback responses when responses is an array (invalid shape)", () => {
|
|
const booking = makeBooking({ responses: ["invalid"] as unknown as null });
|
|
|
|
const result = extractBookingResponses(booking);
|
|
|
|
expect(result).toHaveProperty("name");
|
|
expect(result).toHaveProperty("email");
|
|
expect(result).toHaveProperty("guests");
|
|
});
|
|
|
|
test("omits location from fallback when booking has no location", () => {
|
|
const booking = makeBooking({ responses: null, location: null });
|
|
|
|
const result = extractBookingResponses(booking);
|
|
|
|
expect(result.location).toBeUndefined();
|
|
});
|
|
|
|
test("handles empty string location", () => {
|
|
const booking = makeBooking({ responses: null, location: "" });
|
|
|
|
const result = extractBookingResponses(booking);
|
|
|
|
expect(result.location).toBeUndefined();
|
|
});
|
|
|
|
test("handles null userPrimaryEmail in fallback", () => {
|
|
const booking = makeBooking({ responses: null, userPrimaryEmail: null });
|
|
|
|
const result = extractBookingResponses(booking);
|
|
|
|
expect(result.email).toBe("");
|
|
});
|
|
|
|
test("handles null description in fallback", () => {
|
|
const booking = makeBooking({ responses: null, description: null });
|
|
|
|
const result = extractBookingResponses(booking);
|
|
|
|
expect(result.notes).toBe("");
|
|
});
|
|
});
|
|
|
|
describe("mergeBookingResponsesWithEventData", () => {
|
|
test("overrides title, notes, and location from event", () => {
|
|
const booking = makeBooking({
|
|
responses: { name: "John", email: "john@test.com", notes: "old notes" },
|
|
});
|
|
const event = makeEvent({
|
|
summary: "New Title",
|
|
description: "New notes",
|
|
location: "New Location",
|
|
});
|
|
|
|
const result = mergeBookingResponsesWithEventData(booking, event);
|
|
|
|
expect(result.name).toBe("John"); // preserved from booking
|
|
expect(result.email).toBe("john@test.com"); // preserved from booking
|
|
expect(result.title).toBe("New Title"); // from event
|
|
expect(result.notes).toBe("New notes"); // overridden by event
|
|
expect(result.location).toEqual({
|
|
value: "New Location",
|
|
label: "New Location",
|
|
optionValue: "New Location",
|
|
});
|
|
});
|
|
|
|
test("does not override when event fields are null/empty", () => {
|
|
const booking = makeBooking({
|
|
responses: { name: "John", notes: "Original notes", location: { value: "Original" } },
|
|
});
|
|
const event = makeEvent({
|
|
summary: null,
|
|
description: null,
|
|
location: null,
|
|
});
|
|
|
|
const result = mergeBookingResponsesWithEventData(booking, event);
|
|
|
|
expect(result.title).toBeUndefined();
|
|
expect(result.notes).toBe("Original notes");
|
|
expect(result.location).toEqual({ value: "Original" });
|
|
});
|
|
|
|
test("partially overrides — only summary changed", () => {
|
|
const booking = makeBooking({
|
|
responses: { name: "John", notes: "Keep this" },
|
|
});
|
|
const event = makeEvent({
|
|
summary: "Changed Title",
|
|
description: null,
|
|
location: null,
|
|
});
|
|
|
|
const result = mergeBookingResponsesWithEventData(booking, event);
|
|
|
|
expect(result.title).toBe("Changed Title");
|
|
expect(result.notes).toBe("Keep this"); // not overridden
|
|
});
|
|
});
|
|
|
|
describe("buildMetadataFromCalendarEvent", () => {
|
|
test("serializes all event fields into calendarSubscriptionEvent", () => {
|
|
const event = makeEvent();
|
|
|
|
const result = buildMetadataFromCalendarEvent(event);
|
|
|
|
expect(result).toHaveProperty("calendarSubscriptionEvent");
|
|
const parsed = JSON.parse(result.calendarSubscriptionEvent);
|
|
expect(parsed.summary).toBe("Team Standup");
|
|
expect(parsed.description).toBe("Daily standup meeting");
|
|
expect(parsed.location).toBe("https://meet.google.com/abc");
|
|
expect(parsed.busy).toBe(true);
|
|
expect(parsed.status).toBe("confirmed");
|
|
expect(parsed.isAllDay).toBe(false);
|
|
expect(parsed.timeZone).toBe("America/New_York");
|
|
expect(parsed.etag).toBe('"etag-123"');
|
|
expect(parsed.kind).toBe("calendar#event");
|
|
});
|
|
|
|
test("handles all-null optional fields", () => {
|
|
const event = makeEvent({
|
|
summary: null,
|
|
description: null,
|
|
location: null,
|
|
busy: null,
|
|
status: null,
|
|
isAllDay: null,
|
|
timeZone: null,
|
|
recurringEventId: null,
|
|
originalStartDate: null,
|
|
createdAt: null,
|
|
updatedAt: null,
|
|
etag: null,
|
|
kind: null,
|
|
});
|
|
|
|
const result = buildMetadataFromCalendarEvent(event);
|
|
const parsed = JSON.parse(result.calendarSubscriptionEvent);
|
|
|
|
Object.values(parsed).forEach((value) => {
|
|
expect(value).toBeNull();
|
|
});
|
|
});
|
|
|
|
test("serializes dates as ISO strings", () => {
|
|
const event = makeEvent({
|
|
originalStartDate: new Date("2024-01-15T10:00:00Z"),
|
|
createdAt: new Date("2024-01-10T00:00:00Z"),
|
|
updatedAt: new Date("2024-01-15T09:00:00Z"),
|
|
});
|
|
|
|
const result = buildMetadataFromCalendarEvent(event);
|
|
const parsed = JSON.parse(result.calendarSubscriptionEvent);
|
|
|
|
expect(parsed.originalStartDate).toBe("2024-01-15T10:00:00.000Z");
|
|
expect(parsed.createdAt).toBe("2024-01-10T00:00:00.000Z");
|
|
expect(parsed.updatedAt).toBe("2024-01-15T09:00:00.000Z");
|
|
});
|
|
});
|
|
|
|
// ─── Change detection ─────────────────────────────────────────────────────────
|
|
|
|
describe("hasStartTimeChanged", () => {
|
|
test("returns true when event start differs from booking start", () => {
|
|
const booking = makeBooking({ startTime: new Date("2024-01-15T10:00:00Z") });
|
|
const event = makeEvent({ start: new Date("2024-01-15T14:00:00Z") });
|
|
|
|
expect(hasStartTimeChanged(booking, event)).toBe(true);
|
|
});
|
|
|
|
test("returns false when event start equals booking start", () => {
|
|
const booking = makeBooking({ startTime: new Date("2024-01-15T10:00:00Z") });
|
|
const event = makeEvent({ start: new Date("2024-01-15T10:00:00Z") });
|
|
|
|
expect(hasStartTimeChanged(booking, event)).toBe(false);
|
|
});
|
|
|
|
test("returns false when event start is null", () => {
|
|
const booking = makeBooking({ startTime: new Date("2024-01-15T10:00:00Z") });
|
|
const event = makeEvent({ start: null });
|
|
|
|
expect(hasStartTimeChanged(booking, event)).toBe(false);
|
|
});
|
|
|
|
test("detects 1-second difference", () => {
|
|
const booking = makeBooking({ startTime: new Date("2024-01-15T10:00:00Z") });
|
|
const event = makeEvent({ start: new Date("2024-01-15T10:00:01Z") });
|
|
|
|
expect(hasStartTimeChanged(booking, event)).toBe(true);
|
|
});
|
|
|
|
test("same timestamp in different timezone representations is equal", () => {
|
|
const booking = makeBooking({ startTime: new Date("2024-01-15T10:00:00Z") });
|
|
// Same instant, different representation
|
|
const event = makeEvent({ start: new Date("2024-01-15T07:00:00-03:00") });
|
|
|
|
expect(hasStartTimeChanged(booking, event)).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe("CalendarSyncService - skip reschedule when no time change", () => {
|
|
let service: CalendarSyncService;
|
|
let mockBookingRepository: BookingRepository;
|
|
|
|
beforeEach(() => {
|
|
mockBookingRepository = {
|
|
findBookingByUidWithEventType: vi.fn(),
|
|
} as unknown as BookingRepository;
|
|
|
|
service = new CalendarSyncService({
|
|
bookingRepository: mockBookingRepository,
|
|
});
|
|
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
test("skips reschedule when start time has not changed (e.g. RSVP update)", async () => {
|
|
const booking = makeBooking({
|
|
startTime: new Date("2024-01-15T10:00:00Z"),
|
|
endTime: new Date("2024-01-15T11:00:00Z"),
|
|
});
|
|
const event = makeEvent({
|
|
start: new Date("2024-01-15T10:00:00Z"),
|
|
end: new Date("2024-01-15T11:00:00Z"),
|
|
status: "confirmed",
|
|
});
|
|
|
|
mockBookingRepository.findBookingByUidWithEventType = vi.fn().mockResolvedValue(booking);
|
|
|
|
await service.rescheduleBooking(event, mockSelectedCalendar.userId);
|
|
|
|
expect(mockCreateBooking).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test("proceeds with reschedule when start time has changed", async () => {
|
|
const booking = makeBooking({
|
|
startTime: new Date("2024-01-15T10:00:00Z"),
|
|
endTime: new Date("2024-01-15T11:00:00Z"),
|
|
});
|
|
const event = makeEvent({
|
|
start: new Date("2024-01-15T14:00:00Z"),
|
|
end: new Date("2024-01-15T15:00:00Z"),
|
|
});
|
|
|
|
mockBookingRepository.findBookingByUidWithEventType = vi.fn().mockResolvedValue(booking);
|
|
|
|
await service.rescheduleBooking(event, mockSelectedCalendar.userId);
|
|
|
|
expect(mockCreateBooking).toHaveBeenCalled();
|
|
});
|
|
|
|
test("skips reschedule when event start is null", async () => {
|
|
const booking = makeBooking();
|
|
const event = makeEvent({ start: null });
|
|
|
|
mockBookingRepository.findBookingByUidWithEventType = vi.fn().mockResolvedValue(booking);
|
|
|
|
await service.rescheduleBooking(event, mockSelectedCalendar.userId);
|
|
|
|
expect(mockCreateBooking).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
// ─── iCalUID parsing edge cases ───────────────────────────────────────────────
|
|
|
|
describe("iCalUID parsing", () => {
|
|
let service: CalendarSyncService;
|
|
let mockBookingRepository: BookingRepository;
|
|
|
|
beforeEach(() => {
|
|
mockBookingRepository = {
|
|
findBookingByUidWithEventType: vi.fn(),
|
|
} as unknown as BookingRepository;
|
|
|
|
service = new CalendarSyncService({
|
|
bookingRepository: mockBookingRepository,
|
|
});
|
|
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
test("iCalUID with multiple @ signs: only first part is used as bookingUid", async () => {
|
|
const event = makeEvent({ iCalUID: "uid-part@extra@cal.com", status: "cancelled" });
|
|
const booking = makeBooking({ uid: "uid-part" });
|
|
|
|
mockBookingRepository.findBookingByUidWithEventType = vi.fn().mockResolvedValue(booking);
|
|
|
|
await service.cancelBooking(event, mockSelectedCalendar.userId);
|
|
|
|
// split("@") → ["uid-part", "extra", "cal.com"], first element is "uid-part"
|
|
expect(mockBookingRepository.findBookingByUidWithEventType).toHaveBeenCalledWith({
|
|
bookingUid: "uid-part",
|
|
});
|
|
expect(mockHandleCancelBooking).toHaveBeenCalled();
|
|
});
|
|
|
|
test("iCalUID with UUID format works correctly", async () => {
|
|
const uuid = "550e8400-e29b-41d4-a716-446655440000";
|
|
const event = makeEvent({ iCalUID: `${uuid}@cal.com` });
|
|
const booking = makeBooking({ uid: uuid });
|
|
|
|
mockBookingRepository.findBookingByUidWithEventType = vi.fn().mockResolvedValue(booking);
|
|
|
|
await service.rescheduleBooking(event, mockSelectedCalendar.userId);
|
|
|
|
expect(mockBookingRepository.findBookingByUidWithEventType).toHaveBeenCalledWith({
|
|
bookingUid: uuid,
|
|
});
|
|
});
|
|
|
|
test("iCalUID with no @ sign: entire string becomes bookingUid", async () => {
|
|
const event = makeEvent({ iCalUID: "no-at-sign" });
|
|
const booking = makeBooking({ uid: "no-at-sign" });
|
|
|
|
mockBookingRepository.findBookingByUidWithEventType = vi.fn().mockResolvedValue(booking);
|
|
|
|
await service.rescheduleBooking(event, mockSelectedCalendar.userId);
|
|
|
|
// split("@") → ["no-at-sign"], first element is "no-at-sign"
|
|
expect(mockBookingRepository.findBookingByUidWithEventType).toHaveBeenCalledWith({
|
|
bookingUid: "no-at-sign",
|
|
});
|
|
});
|
|
|
|
test("iCalUID that is just '@' results in empty bookingUid and returns early", async () => {
|
|
const event = makeEvent({ iCalUID: "@" });
|
|
|
|
await service.cancelBooking(event, mockSelectedCalendar.userId);
|
|
|
|
// split("@") → ["", ""], first element is "" which is falsy
|
|
expect(mockBookingRepository.findBookingByUidWithEventType).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test("undefined iCalUID returns early", async () => {
|
|
const event = makeEvent({ iCalUID: undefined as unknown as null });
|
|
|
|
await service.cancelBooking(event, mockSelectedCalendar.userId);
|
|
|
|
expect(mockBookingRepository.findBookingByUidWithEventType).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
// ─── Zero/negative duration edge cases ────────────────────────────────────────
|
|
|
|
describe("buildRescheduleBookingData - duration edge cases", () => {
|
|
test("booking with zero duration: end equals start", () => {
|
|
const booking = makeBooking({
|
|
startTime: new Date("2024-01-15T10:00:00Z"),
|
|
endTime: new Date("2024-01-15T10:00:00Z"), // zero duration
|
|
});
|
|
const event = makeEvent({
|
|
start: new Date("2024-01-20T14:00:00Z"),
|
|
});
|
|
|
|
const result = buildRescheduleBookingData(booking, event);
|
|
|
|
// 0ms duration preserved: end === start
|
|
expect(result.start).toBe("2024-01-20T14:00:00.000Z");
|
|
expect(result.end).toBe("2024-01-20T14:00:00.000Z");
|
|
});
|
|
|
|
test("booking with very short duration (1 minute)", () => {
|
|
const booking = makeBooking({
|
|
startTime: new Date("2024-01-15T10:00:00Z"),
|
|
endTime: new Date("2024-01-15T10:01:00Z"),
|
|
});
|
|
const event = makeEvent({
|
|
start: new Date("2024-01-20T14:00:00Z"),
|
|
});
|
|
|
|
const result = buildRescheduleBookingData(booking, event);
|
|
|
|
expect(result.start).toBe("2024-01-20T14:00:00.000Z");
|
|
expect(result.end).toBe("2024-01-20T14:01:00.000Z");
|
|
});
|
|
|
|
test("booking with very long duration (8 hours)", () => {
|
|
const booking = makeBooking({
|
|
startTime: new Date("2024-01-15T09:00:00Z"),
|
|
endTime: new Date("2024-01-15T17:00:00Z"),
|
|
});
|
|
const event = makeEvent({
|
|
start: new Date("2024-01-20T08:00:00Z"),
|
|
});
|
|
|
|
const result = buildRescheduleBookingData(booking, event);
|
|
|
|
expect(result.start).toBe("2024-01-20T08:00:00.000Z");
|
|
expect(result.end).toBe("2024-01-20T16:00:00.000Z"); // 8h preserved
|
|
});
|
|
});
|
|
|
|
// ─── End-to-end data flow: event fields → createBooking ───────────────────────
|
|
|
|
describe("CalendarSyncService - booking data updated from external event", () => {
|
|
let service: CalendarSyncService;
|
|
let mockBookingRepository: BookingRepository;
|
|
|
|
beforeEach(() => {
|
|
mockBookingRepository = {
|
|
findBookingByUidWithEventType: vi.fn(),
|
|
} as unknown as BookingRepository;
|
|
|
|
service = new CalendarSyncService({
|
|
bookingRepository: mockBookingRepository,
|
|
});
|
|
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
test("summary from external event overrides title in responses", async () => {
|
|
const booking = makeBooking({
|
|
responses: { name: "John Doe", email: "john@test.com", notes: "Original" },
|
|
});
|
|
const event = makeEvent({ summary: "New Meeting Title" });
|
|
|
|
mockBookingRepository.findBookingByUidWithEventType = vi.fn().mockResolvedValue(booking);
|
|
await service.rescheduleBooking(event, mockSelectedCalendar.userId);
|
|
|
|
const call = mockCreateBooking.mock.calls[0][0];
|
|
expect(call.bookingData.responses.title).toBe("New Meeting Title");
|
|
expect(call.bookingData.responses.name).toBe("John Doe"); // preserved
|
|
});
|
|
|
|
test("description from external event overrides notes in responses", async () => {
|
|
const booking = makeBooking({
|
|
responses: { name: "John Doe", email: "john@test.com", notes: "Original notes" },
|
|
});
|
|
const event = makeEvent({ description: "Updated meeting agenda" });
|
|
|
|
mockBookingRepository.findBookingByUidWithEventType = vi.fn().mockResolvedValue(booking);
|
|
await service.rescheduleBooking(event, mockSelectedCalendar.userId);
|
|
|
|
const call = mockCreateBooking.mock.calls[0][0];
|
|
expect(call.bookingData.responses.notes).toBe("Updated meeting agenda");
|
|
});
|
|
|
|
test("location from external event overrides location in responses", async () => {
|
|
const booking = makeBooking({
|
|
responses: {
|
|
name: "John Doe",
|
|
location: { value: "Old Room", label: "Old Room", optionValue: "Old Room" },
|
|
},
|
|
});
|
|
const event = makeEvent({ location: "https://zoom.us/j/123" });
|
|
|
|
mockBookingRepository.findBookingByUidWithEventType = vi.fn().mockResolvedValue(booking);
|
|
await service.rescheduleBooking(event, mockSelectedCalendar.userId);
|
|
|
|
const call = mockCreateBooking.mock.calls[0][0];
|
|
expect(call.bookingData.responses.location).toEqual({
|
|
value: "https://zoom.us/j/123",
|
|
label: "https://zoom.us/j/123",
|
|
optionValue: "https://zoom.us/j/123",
|
|
});
|
|
});
|
|
|
|
test("null fields from external event do NOT override booking responses", async () => {
|
|
const booking = makeBooking({
|
|
responses: {
|
|
name: "John Doe",
|
|
email: "john@test.com",
|
|
notes: "Keep these notes",
|
|
location: { value: "Room A", label: "Room A", optionValue: "Room A" },
|
|
},
|
|
});
|
|
const event = makeEvent({ summary: null, description: null, location: null });
|
|
|
|
mockBookingRepository.findBookingByUidWithEventType = vi.fn().mockResolvedValue(booking);
|
|
await service.rescheduleBooking(event, mockSelectedCalendar.userId);
|
|
|
|
const call = mockCreateBooking.mock.calls[0][0];
|
|
expect(call.bookingData.responses.notes).toBe("Keep these notes");
|
|
expect(call.bookingData.responses.location).toEqual({
|
|
value: "Room A",
|
|
label: "Room A",
|
|
optionValue: "Room A",
|
|
});
|
|
expect(call.bookingData.responses.title).toBeUndefined(); // no override
|
|
});
|
|
|
|
test("all fields updated simultaneously from external event", async () => {
|
|
const booking = makeBooking({
|
|
responses: {
|
|
name: "John Doe",
|
|
email: "john@test.com",
|
|
notes: "Old notes",
|
|
location: { value: "Old", label: "Old", optionValue: "Old" },
|
|
guests: ["guest@test.com"],
|
|
},
|
|
});
|
|
const event = makeEvent({
|
|
summary: "Renamed Meeting",
|
|
description: "New agenda for Q1",
|
|
location: "Conference Room B",
|
|
});
|
|
|
|
mockBookingRepository.findBookingByUidWithEventType = vi.fn().mockResolvedValue(booking);
|
|
await service.rescheduleBooking(event, mockSelectedCalendar.userId);
|
|
|
|
const call = mockCreateBooking.mock.calls[0][0];
|
|
const responses = call.bookingData.responses;
|
|
|
|
// Overridden by event
|
|
expect(responses.title).toBe("Renamed Meeting");
|
|
expect(responses.notes).toBe("New agenda for Q1");
|
|
expect(responses.location).toEqual({
|
|
value: "Conference Room B",
|
|
label: "Conference Room B",
|
|
optionValue: "Conference Room B",
|
|
});
|
|
|
|
// Preserved from booking
|
|
expect(responses.name).toBe("John Doe");
|
|
expect(responses.email).toBe("john@test.com");
|
|
expect(responses.guests).toEqual(["guest@test.com"]);
|
|
});
|
|
|
|
test("external event data is stored in metadata as calendarSubscriptionEvent", async () => {
|
|
const booking = makeBooking();
|
|
const event = makeEvent({
|
|
summary: "Sync Test",
|
|
description: "From Google",
|
|
location: "Virtual",
|
|
status: "confirmed",
|
|
timeZone: "America/Sao_Paulo",
|
|
});
|
|
|
|
mockBookingRepository.findBookingByUidWithEventType = vi.fn().mockResolvedValue(booking);
|
|
await service.rescheduleBooking(event, mockSelectedCalendar.userId);
|
|
|
|
const call = mockCreateBooking.mock.calls[0][0];
|
|
const raw = call.bookingData.metadata.calendarSubscriptionEvent;
|
|
expect(typeof raw).toBe("string");
|
|
|
|
const parsed = JSON.parse(raw);
|
|
expect(parsed.summary).toBe("Sync Test");
|
|
expect(parsed.description).toBe("From Google");
|
|
expect(parsed.location).toBe("Virtual");
|
|
expect(parsed.status).toBe("confirmed");
|
|
expect(parsed.timeZone).toBe("America/Sao_Paulo");
|
|
});
|
|
|
|
test("booking without existing responses uses fallback with event overrides", async () => {
|
|
const booking = makeBooking({
|
|
responses: null,
|
|
title: "Original Title",
|
|
userPrimaryEmail: "user@company.com",
|
|
description: "Original description",
|
|
location: "Original Location",
|
|
});
|
|
const event = makeEvent({
|
|
summary: "Calendar Title",
|
|
description: "Calendar description",
|
|
location: "Calendar Location",
|
|
});
|
|
|
|
mockBookingRepository.findBookingByUidWithEventType = vi.fn().mockResolvedValue(booking);
|
|
await service.rescheduleBooking(event, mockSelectedCalendar.userId);
|
|
|
|
const call = mockCreateBooking.mock.calls[0][0];
|
|
const responses = call.bookingData.responses;
|
|
|
|
// Fallback fields from booking
|
|
expect(responses.name).toBe("Original Title");
|
|
expect(responses.email).toBe("user@company.com");
|
|
expect(responses.guests).toEqual([]);
|
|
|
|
// Overridden by event
|
|
expect(responses.title).toBe("Calendar Title");
|
|
expect(responses.notes).toBe("Calendar description");
|
|
expect(responses.location).toEqual({
|
|
value: "Calendar Location",
|
|
label: "Calendar Location",
|
|
optionValue: "Calendar Location",
|
|
});
|
|
});
|
|
});
|
|
|
|
// ─── Service-level integration tests ──────────────────────────────────────────
|
|
|
|
describe("CalendarSyncService - error isolation", () => {
|
|
let service: CalendarSyncService;
|
|
let mockBookingRepository: BookingRepository;
|
|
|
|
beforeEach(() => {
|
|
mockBookingRepository = {
|
|
findBookingByUidWithEventType: vi.fn(),
|
|
} as unknown as BookingRepository;
|
|
|
|
service = new CalendarSyncService({
|
|
bookingRepository: mockBookingRepository,
|
|
});
|
|
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
test("handleEvents: failure in one event does not block others", async () => {
|
|
const cancelEvent = makeEvent({ iCalUID: "cancel-uid@cal.com", status: "cancelled" });
|
|
const rescheduleEvent = makeEvent({
|
|
iCalUID: "reschedule-uid@cal.com",
|
|
status: "confirmed",
|
|
start: new Date("2024-01-20T14:00:00Z"),
|
|
});
|
|
|
|
const cancelBooking = makeBooking({ uid: "cancel-uid" });
|
|
const rescheduleBooking = makeBooking({ uid: "reschedule-uid" });
|
|
|
|
mockBookingRepository.findBookingByUidWithEventType = vi
|
|
.fn()
|
|
.mockImplementation(({ bookingUid }: { bookingUid: string }) => {
|
|
if (bookingUid === "cancel-uid") return Promise.resolve(cancelBooking);
|
|
if (bookingUid === "reschedule-uid") return Promise.resolve(rescheduleBooking);
|
|
return Promise.resolve(null);
|
|
});
|
|
|
|
// Cancel fails, but reschedule should still work
|
|
mockHandleCancelBooking.mockRejectedValue(new Error("Cancel service down"));
|
|
|
|
await service.handleEvents(mockSelectedCalendar, [cancelEvent, rescheduleEvent]);
|
|
|
|
expect(mockHandleCancelBooking).toHaveBeenCalled();
|
|
expect(mockCreateBooking).toHaveBeenCalled();
|
|
});
|
|
|
|
test("handleEvents: DB error on one event does not block others", async () => {
|
|
const event1 = makeEvent({ iCalUID: "uid-1@cal.com", status: "confirmed" });
|
|
const event2 = makeEvent({ iCalUID: "uid-2@cal.com", status: "confirmed" });
|
|
|
|
let callCount = 0;
|
|
mockBookingRepository.findBookingByUidWithEventType = vi.fn().mockImplementation(() => {
|
|
callCount++;
|
|
if (callCount === 1) return Promise.reject(new Error("DB timeout"));
|
|
return Promise.resolve(makeBooking({ uid: "uid-2" }));
|
|
});
|
|
|
|
await service.handleEvents(mockSelectedCalendar, [event1, event2]);
|
|
|
|
expect(mockBookingRepository.findBookingByUidWithEventType).toHaveBeenCalledTimes(2);
|
|
expect(mockCreateBooking).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
test("cancelBooking: passes correct data to handleCancelBooking", async () => {
|
|
const booking = makeBooking({
|
|
uid: "my-booking-uid",
|
|
userId: 99,
|
|
userPrimaryEmail: "admin@company.com",
|
|
});
|
|
const event = makeEvent({ iCalUID: "my-booking-uid@cal.com", status: "cancelled" });
|
|
|
|
mockBookingRepository.findBookingByUidWithEventType = vi.fn().mockResolvedValue(booking);
|
|
|
|
await service.cancelBooking(event, 99);
|
|
|
|
expect(mockHandleCancelBooking).toHaveBeenCalledWith({
|
|
userId: 99,
|
|
actionSource: "SYSTEM",
|
|
impersonatedByUserUuid: null,
|
|
bookingData: {
|
|
uid: "my-booking-uid",
|
|
cancellationReason: "Cancelled on user's calendar",
|
|
cancelledBy: "admin@company.com",
|
|
skipCalendarSyncTaskCancellation: true,
|
|
},
|
|
});
|
|
});
|
|
|
|
test("rescheduleBooking: passes correct bookingMeta flags", async () => {
|
|
const booking = makeBooking();
|
|
const event = makeEvent({
|
|
start: new Date("2024-01-20T14:00:00Z"),
|
|
end: new Date("2024-01-20T15:00:00Z"),
|
|
});
|
|
|
|
mockBookingRepository.findBookingByUidWithEventType = vi.fn().mockResolvedValue(booking);
|
|
|
|
await service.rescheduleBooking(event, mockSelectedCalendar.userId);
|
|
|
|
expect(mockCreateBooking).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
bookingMeta: {
|
|
skipCalendarSyncTaskCreation: true,
|
|
skipAvailabilityCheck: true,
|
|
skipEventLimitsCheck: true,
|
|
impersonatedByUserUuid: null,
|
|
},
|
|
})
|
|
);
|
|
});
|
|
|
|
test("rescheduleBooking: skips booking without eventTypeId", async () => {
|
|
const booking = makeBooking({ eventTypeId: null });
|
|
const event = makeEvent();
|
|
|
|
mockBookingRepository.findBookingByUidWithEventType = vi.fn().mockResolvedValue(booking);
|
|
|
|
await service.rescheduleBooking(event, mockSelectedCalendar.userId);
|
|
|
|
expect(mockCreateBooking).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test("cancelBooking: skips when calendar owner is not the booking host", async () => {
|
|
const booking = makeBooking({ uid: "booking-uid-123", userId: 99 });
|
|
const event = makeEvent({ iCalUID: "booking-uid-123@cal.com", status: "cancelled" });
|
|
|
|
mockBookingRepository.findBookingByUidWithEventType = vi.fn().mockResolvedValue(booking);
|
|
|
|
// calendarUserId (42) !== booking.userId (99) → skip
|
|
await service.cancelBooking(event, mockSelectedCalendar.userId);
|
|
|
|
expect(mockHandleCancelBooking).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test("rescheduleBooking: skips when calendar owner is not the booking host", async () => {
|
|
const booking = makeBooking({ uid: "booking-uid-123", userId: 99 });
|
|
const event = makeEvent({
|
|
iCalUID: "booking-uid-123@cal.com",
|
|
start: new Date("2024-01-20T14:00:00Z"),
|
|
});
|
|
|
|
mockBookingRepository.findBookingByUidWithEventType = vi.fn().mockResolvedValue(booking);
|
|
|
|
// calendarUserId (42) !== booking.userId (99) → skip
|
|
await service.rescheduleBooking(event, mockSelectedCalendar.userId);
|
|
|
|
expect(mockCreateBooking).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test("cancelBooking: skips booking without userId", async () => {
|
|
const booking = makeBooking({ userId: null });
|
|
const event = makeEvent({ iCalUID: "booking-uid-123@cal.com", status: "cancelled" });
|
|
|
|
mockBookingRepository.findBookingByUidWithEventType = vi.fn().mockResolvedValue(booking);
|
|
|
|
await service.cancelBooking(event, mockSelectedCalendar.userId);
|
|
|
|
expect(mockHandleCancelBooking).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test("cancelBooking: skips booking without userPrimaryEmail", async () => {
|
|
const booking = makeBooking({ userPrimaryEmail: null });
|
|
const event = makeEvent({ iCalUID: "booking-uid-123@cal.com", status: "cancelled" });
|
|
|
|
mockBookingRepository.findBookingByUidWithEventType = vi.fn().mockResolvedValue(booking);
|
|
|
|
await service.cancelBooking(event, mockSelectedCalendar.userId);
|
|
|
|
expect(mockHandleCancelBooking).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test("handleEvents: correctly routes cancelled vs non-cancelled events", async () => {
|
|
const cancelledEvent = makeEvent({ iCalUID: "uid-a@cal.com", status: "cancelled" });
|
|
const confirmedEvent = makeEvent({ iCalUID: "uid-b@cal.com", status: "confirmed" });
|
|
|
|
const bookingA = makeBooking({ uid: "uid-a" });
|
|
const bookingB = makeBooking({ uid: "uid-b" });
|
|
|
|
mockBookingRepository.findBookingByUidWithEventType = vi
|
|
.fn()
|
|
.mockImplementation(({ bookingUid }: { bookingUid: string }) => {
|
|
if (bookingUid === "uid-a") return Promise.resolve(bookingA);
|
|
if (bookingUid === "uid-b") return Promise.resolve(bookingB);
|
|
return Promise.resolve(null);
|
|
});
|
|
|
|
await service.handleEvents(mockSelectedCalendar, [cancelledEvent, confirmedEvent]);
|
|
|
|
// "cancelled" → cancelBooking, "confirmed" → rescheduleBooking
|
|
expect(mockHandleCancelBooking).toHaveBeenCalledTimes(1);
|
|
expect(mockCreateBooking).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
test("handleEvents: non-cancelled statuses (tentative, confirmed) all route to reschedule", async () => {
|
|
const tentativeEvent = makeEvent({ iCalUID: "uid-t@cal.com", status: "tentative" });
|
|
const bookingT = makeBooking({ uid: "uid-t" });
|
|
|
|
mockBookingRepository.findBookingByUidWithEventType = vi.fn().mockResolvedValue(bookingT);
|
|
|
|
await service.handleEvents(mockSelectedCalendar, [tentativeEvent]);
|
|
|
|
expect(mockHandleCancelBooking).not.toHaveBeenCalled();
|
|
expect(mockCreateBooking).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|