## What does this PR do? > **⚠️ Note: This PR does not enable booking audit in production.** The `BookingAuditTaskerProducerService` has an [`IS_PRODUCTION` guard](https://github.com/calcom/cal.com/blob/integrate-booking-creation-reschedule-audit/packages/features/booking-audit/lib/service/BookingAuditTaskerProducerService.ts#L106-L108) that skips audit task queueing in production environments. This allows the integration to be tested in development before enabling it in production. Integrates audit logging for booking cancellations, following the pattern established in PR #26046 for booking creation/rescheduling audit. - Related to #25125 (Booking Audit Infrastructure) ### Changes: - Add audit logging for single booking cancellation via `onBookingCancelled` - Add audit logging for bulk recurring booking cancellation via `onBulkBookingsCancelled` - Pass `userUuid` and `actionSource` from webapp cancel route (WEBAPP) - Pass `userUuid` and `actionSource` from API-v2 bookings service (API_V2) - Create `getAuditActor` helper to derive actor from userUuid or create synthetic guest actor - Add `getUniqueIdentifier` helper for generating unique actor identifiers - Add warning log when `actionSource` is "UNKNOWN" for observability - Add integration tests for booking cancellation audit ### Audit Data Captured: - `cancellationReason` (simple string value) - `cancelledBy` (simple string value) - `status` (old → new, e.g., "ACCEPTED" → "CANCELLED") ### Updates since last revision: - Simplified `CancelledAuditActionService` schema: `cancellationReason` and `cancelledBy` are now stored as simple nullable strings instead of change objects (old/new), since cancellation is a one-time event where tracking previous values doesn't apply - Added integration tests for cancelled booking audit in `booking-audit-cancelled.integration-test.ts` - Added `getUniqueIdentifier` helper function in actor.ts for generating unique identifiers with prefixes ## Mandatory Tasks (DO NOT REMOVE) - [x] I have self-reviewed the code (A decent size PR without self-review might be rejected). - [x] I have updated the developer docs in /docs if this PR makes changes that would require a [documentation change](https://cal.com/docs). N/A - no documentation changes needed. - [ ] I confirm automated tests are in place that prove my fix is effective or that my feature works. ## How should this be tested? 1. Cancel a single booking via the webapp - verify audit record is created with actor and actionSource="WEBAPP" 2. Cancel a single booking via API v2 - verify audit record is created with actionSource="API_V2" 3. Cancel all remaining bookings in a recurring series - verify bulk audit records are created with shared operationId 4. Cancel via unauthenticated cancel link - verify guest actor is created with synthetic email (prefixed with "param-" or "fallback-") 5. Run integration tests: `yarn test packages/features/booking-audit/lib/service/__tests__/booking-audit-cancelled.integration-test.ts` ## Human Review Checklist - [ ] Verify `onBookingCancelled` and `onBulkBookingsCancelled` methods exist in `BookingEventHandlerService` - [ ] Review the `getAuditActor` fallback logic - creates synthetic email with "fallback-" or "param-" prefix when no userUuid available - [ ] Confirm the simplified schema for `cancellationReason`/`cancelledBy` (no longer tracking old→new) is intentional - [ ] Note: Audit logging calls are awaited directly - if audit service fails, the cancellation will fail. Confirm this is the desired behavior. - [ ] Verify `CancelledAuditDisplayData` type no longer includes `previousReason` and `previousCancelledBy` fields --- Link to Devin run: https://app.devin.ai/sessions/42404e76a66946fe9e46fa07fb12e779 Requested by: @hariombalhara ([email protected])
1069 lines
38 KiB
TypeScript
1069 lines
38 KiB
TypeScript
import { describe, it, expect, beforeEach, vi, type Mock } from "vitest";
|
|
|
|
import { PermissionCheckService } from "@calcom/features/pbac/services/permission-check.service";
|
|
import { UserRepository } from "@calcom/features/users/repositories/UserRepository";
|
|
import { BookingRepository } from "@calcom/features/bookings/repositories/BookingRepository";
|
|
import { MembershipRepository } from "@calcom/features/membership/repositories/MembershipRepository";
|
|
import { CredentialRepository } from "@calcom/features/credentials/repositories/CredentialRepository";
|
|
import type { IAttendeeRepository } from "@calcom/features/bookings/repositories/IAttendeeRepository";
|
|
import type { ISimpleLogger } from "@calcom/features/di/shared/services/logger.service";
|
|
|
|
import { BookingAuditViewerService } from "../BookingAuditViewerService";
|
|
import { BookingAuditPermissionError, BookingAuditErrorCode } from "../BookingAuditAccessService";
|
|
import type {
|
|
IBookingAuditRepository,
|
|
BookingAuditWithActor,
|
|
BookingAuditAction,
|
|
BookingAuditType,
|
|
} from "../../repository/IBookingAuditRepository";
|
|
import type { AuditActorType } from "../../repository/IAuditActorRepository";
|
|
import type { BookingAuditContext } from "../../dto/types";
|
|
|
|
vi.mock("@calcom/features/pbac/services/permission-check.service");
|
|
vi.mock("@calcom/features/users/repositories/UserRepository");
|
|
vi.mock("@calcom/features/bookings/repositories/BookingRepository");
|
|
vi.mock("@calcom/features/membership/repositories/MembershipRepository");
|
|
vi.mock("@calcom/features/credentials/repositories/CredentialRepository");
|
|
|
|
const createMockTeamBooking = (overrides?: {
|
|
userId?: number;
|
|
teamId?: number | null;
|
|
parentTeamId?: number;
|
|
}) => ({
|
|
userId: overrides?.userId ?? 456,
|
|
user: {
|
|
id: overrides?.userId ?? 456,
|
|
email: "[email protected]",
|
|
},
|
|
eventType: {
|
|
teamId: (overrides && "teamId" in overrides ? overrides.teamId : (overrides?.teamId ?? 100)) ?? null,
|
|
parent: (overrides?.parentTeamId ? { teamId: overrides.parentTeamId } : undefined) ?? null,
|
|
hosts: [],
|
|
users: [],
|
|
},
|
|
attendees: [],
|
|
});
|
|
|
|
const createMockAuditLog = (
|
|
overrides?: Partial<{
|
|
id: string;
|
|
bookingUid: string;
|
|
action: BookingAuditAction;
|
|
type: BookingAuditType;
|
|
timestamp: Date;
|
|
createdAt: Date;
|
|
updatedAt: Date;
|
|
data: unknown;
|
|
actorId: string;
|
|
actorType: AuditActorType;
|
|
actorUserUuid: string | null;
|
|
actorName: string | null;
|
|
context: BookingAuditContext | null;
|
|
}>
|
|
): BookingAuditWithActor => ({
|
|
id: overrides?.id ?? "audit-log-1",
|
|
bookingUid: overrides?.bookingUid ?? "booking-uid-123",
|
|
action: overrides?.action ?? "CREATED",
|
|
type: overrides?.type ?? "RECORD_CREATED",
|
|
timestamp: overrides?.timestamp ?? new Date("2024-01-15T10:00:00Z"),
|
|
createdAt: overrides?.createdAt ?? new Date("2024-01-15T10:00:00Z"),
|
|
updatedAt: overrides?.updatedAt ?? new Date("2024-01-15T10:00:00Z"),
|
|
actorId: overrides?.actorId ?? "actor-1",
|
|
data: overrides?.data ?? {
|
|
version: 1,
|
|
fields: { startTime: 1705315200000, endTime: 1705318800000, status: "ACCEPTED" },
|
|
},
|
|
source: "WEBAPP" as const,
|
|
operationId: "operation-id-123",
|
|
context: (overrides && "context" in overrides ? overrides.context : null) as BookingAuditContext | null,
|
|
actor: {
|
|
id: overrides?.actorId ?? "actor-1",
|
|
type: overrides?.actorType ?? ("USER" as const),
|
|
userUuid: (overrides && "actorUserUuid" in overrides ? overrides.actorUserUuid : "user-uuid-123") as
|
|
| string
|
|
| null,
|
|
attendeeId: null,
|
|
credentialId: null,
|
|
name: (overrides && "actorName" in overrides ? overrides.actorName : "John Doe") as string | null,
|
|
createdAt: new Date("2024-01-01T00:00:00Z"),
|
|
},
|
|
});
|
|
|
|
const createMockUser = (
|
|
overrides?: Partial<{ id: number; name: string | null; email: string; avatarUrl: string | null }>
|
|
) => ({
|
|
id: overrides?.id ?? 123,
|
|
name: (overrides && "name" in overrides ? overrides.name : "John Doe") as string | null,
|
|
email: overrides?.email ?? "[email protected]",
|
|
avatarUrl: (overrides && "avatarUrl" in overrides ? overrides.avatarUrl : null) as string | null,
|
|
});
|
|
|
|
describe("BookingAuditViewerService - Integration Tests", () => {
|
|
let service: BookingAuditViewerService;
|
|
let mockBookingRepository: {
|
|
findByUidIncludeEventType: Mock<BookingRepository["findByUidIncludeEventType"]>;
|
|
getFromRescheduleUid: Mock<BookingRepository["getFromRescheduleUid"]>;
|
|
};
|
|
let mockUserRepository: {
|
|
getUserOrganizationAndTeams: Mock<UserRepository["getUserOrganizationAndTeams"]>;
|
|
findByUuid: Mock<UserRepository["findByUuid"]>;
|
|
};
|
|
let mockBookingAuditRepository: {
|
|
create: Mock<IBookingAuditRepository["create"]>;
|
|
findAllForBooking: Mock<IBookingAuditRepository["findAllForBooking"]>;
|
|
findRescheduledLogsOfBooking: Mock<IBookingAuditRepository["findRescheduledLogsOfBooking"]>;
|
|
};
|
|
let mockMembershipRepository: {
|
|
hasMembership: Mock<MembershipRepository["hasMembership"]>;
|
|
};
|
|
let mockPermissionCheckService: {
|
|
checkPermission: Mock<PermissionCheckService["checkPermission"]>;
|
|
};
|
|
let mockAttendeeRepository: {
|
|
findById: Mock;
|
|
};
|
|
let mockCredentialRepository: {
|
|
findByCredentialId: Mock<CredentialRepository["findByCredentialId"]>;
|
|
};
|
|
let mockLog: {
|
|
error: Mock;
|
|
};
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
|
|
mockBookingRepository = {
|
|
findByUidIncludeEventType: vi.fn(),
|
|
getFromRescheduleUid: vi.fn(),
|
|
};
|
|
|
|
mockUserRepository = {
|
|
getUserOrganizationAndTeams: vi.fn(),
|
|
findByUuid: vi.fn(),
|
|
};
|
|
|
|
mockBookingAuditRepository = {
|
|
create: vi.fn(),
|
|
findAllForBooking: vi.fn().mockResolvedValue([]),
|
|
findRescheduledLogsOfBooking: vi.fn(),
|
|
};
|
|
|
|
mockMembershipRepository = {
|
|
hasMembership: vi.fn(),
|
|
};
|
|
|
|
mockPermissionCheckService = {
|
|
checkPermission: vi.fn(),
|
|
};
|
|
|
|
mockAttendeeRepository = {
|
|
findById: vi.fn(),
|
|
};
|
|
|
|
mockCredentialRepository = {
|
|
findByCredentialId: vi.fn(),
|
|
};
|
|
|
|
mockLog = {
|
|
error: vi.fn(),
|
|
};
|
|
|
|
vi.mocked(BookingRepository).mockImplementation(function () {
|
|
return mockBookingRepository as unknown as BookingRepository;
|
|
});
|
|
vi.mocked(UserRepository).mockImplementation(function () {
|
|
return mockUserRepository as unknown as UserRepository;
|
|
});
|
|
vi.mocked(MembershipRepository).mockImplementation(function () {
|
|
return mockMembershipRepository as unknown as MembershipRepository;
|
|
});
|
|
vi.mocked(PermissionCheckService).mockImplementation(function () {
|
|
return mockPermissionCheckService as unknown as PermissionCheckService;
|
|
});
|
|
vi.mocked(CredentialRepository).mockImplementation(function () {
|
|
return mockCredentialRepository as unknown as CredentialRepository;
|
|
});
|
|
|
|
service = new BookingAuditViewerService({
|
|
bookingAuditRepository: mockBookingAuditRepository as unknown as IBookingAuditRepository,
|
|
userRepository: mockUserRepository as unknown as UserRepository,
|
|
bookingRepository: mockBookingRepository as unknown as BookingRepository,
|
|
membershipRepository: mockMembershipRepository as unknown as MembershipRepository,
|
|
attendeeRepository: mockAttendeeRepository as unknown as IAttendeeRepository,
|
|
credentialRepository: mockCredentialRepository as unknown as CredentialRepository,
|
|
log: mockLog as unknown as ISimpleLogger,
|
|
});
|
|
});
|
|
|
|
describe("getAuditLogsForBooking", () => {
|
|
describe("when user has permission to view audit logs", () => {
|
|
beforeEach(() => {
|
|
mockBookingRepository.findByUidIncludeEventType.mockResolvedValue(
|
|
createMockTeamBooking({ teamId: 100 })
|
|
);
|
|
mockPermissionCheckService.checkPermission.mockResolvedValue(true);
|
|
});
|
|
|
|
it("should return enriched audit logs with actor information", async () => {
|
|
const mockLog = createMockAuditLog();
|
|
const mockUser = createMockUser();
|
|
|
|
mockBookingAuditRepository.findAllForBooking.mockResolvedValue([mockLog]);
|
|
mockUserRepository.findByUuid.mockResolvedValue(mockUser);
|
|
|
|
const result = await service.getAuditLogsForBooking({
|
|
bookingUid: "booking-uid-123",
|
|
userId: 123,
|
|
userEmail: "[email protected]",
|
|
userTimeZone: "America/New_York",
|
|
organizationId: 200,
|
|
});
|
|
|
|
expect(result.bookingUid).toBe("booking-uid-123");
|
|
expect(result.auditLogs).toHaveLength(1);
|
|
expect(result.auditLogs[0]).toMatchObject({
|
|
id: "audit-log-1",
|
|
bookingUid: "booking-uid-123",
|
|
action: "CREATED",
|
|
type: "RECORD_CREATED",
|
|
actor: expect.objectContaining({
|
|
type: "USER",
|
|
displayName: "John Doe",
|
|
displayEmail: "[email protected]",
|
|
displayAvatar: null,
|
|
}),
|
|
});
|
|
});
|
|
|
|
it("should format timestamps as ISO strings", async () => {
|
|
const mockLog = createMockAuditLog({
|
|
timestamp: new Date("2024-01-15T10:00:00Z"),
|
|
createdAt: new Date("2024-01-15T10:00:00Z"),
|
|
});
|
|
|
|
mockBookingAuditRepository.findAllForBooking.mockResolvedValue([mockLog]);
|
|
mockUserRepository.findByUuid.mockResolvedValue(createMockUser());
|
|
|
|
const result = await service.getAuditLogsForBooking({
|
|
bookingUid: "booking-uid-123",
|
|
userId: 123,
|
|
userEmail: "[email protected]",
|
|
userTimeZone: "America/New_York",
|
|
organizationId: 200,
|
|
});
|
|
|
|
expect(result.auditLogs[0].timestamp).toBe("2024-01-15T10:00:00.000Z");
|
|
expect(result.auditLogs[0].createdAt).toBe("2024-01-15T10:00:00.000Z");
|
|
});
|
|
|
|
it("should include action display title with translation key", async () => {
|
|
const mockLog = createMockAuditLog({ action: "CREATED" });
|
|
|
|
mockBookingAuditRepository.findAllForBooking.mockResolvedValue([mockLog]);
|
|
mockUserRepository.findByUuid.mockResolvedValue(createMockUser());
|
|
|
|
const result = await service.getAuditLogsForBooking({
|
|
bookingUid: "booking-uid-123",
|
|
userId: 123,
|
|
userEmail: "[email protected]",
|
|
userTimeZone: "America/New_York",
|
|
organizationId: 200,
|
|
});
|
|
|
|
expect(result.auditLogs[0].actionDisplayTitle).toEqual({
|
|
key: "booking_audit_action.created",
|
|
});
|
|
});
|
|
|
|
it("should handle multiple audit logs in correct order", async () => {
|
|
const logs = [
|
|
createMockAuditLog({
|
|
id: "log-1",
|
|
action: "CREATED",
|
|
timestamp: new Date("2024-01-15T10:00:00Z"),
|
|
data: {
|
|
version: 1,
|
|
fields: { startTime: 1705315200000, endTime: 1705318800000, status: "ACCEPTED" },
|
|
},
|
|
}),
|
|
createMockAuditLog({
|
|
id: "log-2",
|
|
action: "ACCEPTED",
|
|
timestamp: new Date("2024-01-15T11:00:00Z"),
|
|
data: { version: 1, fields: { status: { old: "PENDING", new: "ACCEPTED" } } },
|
|
}),
|
|
createMockAuditLog({
|
|
id: "log-3",
|
|
action: "CANCELLED",
|
|
timestamp: new Date("2024-01-15T12:00:00Z"),
|
|
data: {
|
|
version: 1,
|
|
fields: {
|
|
status: { old: "ACCEPTED", new: "CANCELLED" },
|
|
cancellationReason: "User requested",
|
|
cancelledBy: "user-123",
|
|
},
|
|
},
|
|
}),
|
|
];
|
|
|
|
mockBookingAuditRepository.findAllForBooking.mockResolvedValue(logs);
|
|
mockUserRepository.findByUuid.mockResolvedValue(createMockUser());
|
|
|
|
const result = await service.getAuditLogsForBooking({
|
|
bookingUid: "booking-uid-123",
|
|
userId: 123,
|
|
userEmail: "[email protected]",
|
|
userTimeZone: "America/New_York",
|
|
organizationId: 200,
|
|
});
|
|
|
|
expect(result.auditLogs).toHaveLength(3);
|
|
expect(result.auditLogs[0].id).toBe("log-1");
|
|
expect(result.auditLogs[1].id).toBe("log-2");
|
|
expect(result.auditLogs[2].id).toBe("log-3");
|
|
});
|
|
|
|
it("should return empty array when no audit logs exist", async () => {
|
|
mockBookingAuditRepository.findAllForBooking.mockResolvedValue([]);
|
|
|
|
const result = await service.getAuditLogsForBooking({
|
|
bookingUid: "booking-uid-123",
|
|
userId: 123,
|
|
userEmail: "[email protected]",
|
|
userTimeZone: "America/New_York",
|
|
organizationId: 200,
|
|
});
|
|
|
|
expect(result.bookingUid).toBe("booking-uid-123");
|
|
expect(result.auditLogs).toEqual([]);
|
|
});
|
|
});
|
|
|
|
describe("when enriching actor information", () => {
|
|
beforeEach(() => {
|
|
mockBookingRepository.findByUidIncludeEventType.mockResolvedValue(
|
|
createMockTeamBooking({ teamId: 100 })
|
|
);
|
|
mockPermissionCheckService.checkPermission.mockResolvedValue(true);
|
|
});
|
|
|
|
it("should enrich USER actor with user details from repository", async () => {
|
|
const mockLog = createMockAuditLog({
|
|
actorType: "USER",
|
|
actorUserUuid: "user-uuid-456",
|
|
});
|
|
const mockUser = createMockUser({
|
|
name: "Jane Smith",
|
|
email: "[email protected]",
|
|
avatarUrl: "https://example.com/avatar.jpg",
|
|
});
|
|
|
|
mockBookingAuditRepository.findAllForBooking.mockResolvedValue([mockLog]);
|
|
mockUserRepository.findByUuid.mockResolvedValue(mockUser);
|
|
|
|
const result = await service.getAuditLogsForBooking({
|
|
bookingUid: "booking-uid-123",
|
|
userId: 123,
|
|
userEmail: "[email protected]",
|
|
userTimeZone: "UTC",
|
|
organizationId: 200,
|
|
});
|
|
|
|
expect(mockUserRepository.findByUuid).toHaveBeenCalledWith({ uuid: "user-uuid-456" });
|
|
expect(result.auditLogs[0].actor).toMatchObject({
|
|
displayName: "Jane Smith",
|
|
displayEmail: "[email protected]",
|
|
displayAvatar: "https://example.com/avatar.jpg",
|
|
});
|
|
});
|
|
|
|
it("should use email as display name when user name is null", async () => {
|
|
const mockLog = createMockAuditLog({
|
|
actorType: "USER",
|
|
actorName: null,
|
|
});
|
|
const mockUser = createMockUser({ name: null, email: "[email protected]" });
|
|
|
|
mockBookingAuditRepository.findAllForBooking.mockResolvedValue([mockLog]);
|
|
mockUserRepository.findByUuid.mockResolvedValue(mockUser);
|
|
|
|
const result = await service.getAuditLogsForBooking({
|
|
bookingUid: "booking-uid-123",
|
|
userId: 123,
|
|
userEmail: "[email protected]",
|
|
userTimeZone: "UTC",
|
|
organizationId: 200,
|
|
});
|
|
|
|
expect(result.auditLogs[0].actor.displayName).toBe("[email protected]");
|
|
});
|
|
|
|
it("should show 'Deleted User' when user not found in repository", async () => {
|
|
const mockLog = createMockAuditLog({ actorType: "USER" });
|
|
|
|
mockBookingAuditRepository.findAllForBooking.mockResolvedValue([mockLog]);
|
|
mockUserRepository.findByUuid.mockResolvedValue(null);
|
|
|
|
const result = await service.getAuditLogsForBooking({
|
|
bookingUid: "booking-uid-123",
|
|
userId: 123,
|
|
userEmail: "[email protected]",
|
|
userTimeZone: "UTC",
|
|
organizationId: 200,
|
|
});
|
|
|
|
expect(result.auditLogs[0].actor).toMatchObject({
|
|
displayName: "Deleted User",
|
|
displayEmail: null,
|
|
displayAvatar: null,
|
|
});
|
|
});
|
|
|
|
it("should show 'Cal.com' for SYSTEM actor", async () => {
|
|
const mockLog = createMockAuditLog({
|
|
actorType: "SYSTEM",
|
|
actorUserUuid: null,
|
|
});
|
|
|
|
mockBookingAuditRepository.findAllForBooking.mockResolvedValue([mockLog]);
|
|
|
|
const result = await service.getAuditLogsForBooking({
|
|
bookingUid: "booking-uid-123",
|
|
userId: 123,
|
|
userEmail: "[email protected]",
|
|
userTimeZone: "UTC",
|
|
organizationId: 200,
|
|
});
|
|
|
|
expect(result.auditLogs[0].actor).toMatchObject({
|
|
type: "SYSTEM",
|
|
displayName: "Cal.com",
|
|
displayEmail: null,
|
|
displayAvatar: null,
|
|
});
|
|
expect(mockUserRepository.findByUuid).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("should show 'Guest' for GUEST actor without name", async () => {
|
|
const mockLog = createMockAuditLog({
|
|
actorType: "GUEST",
|
|
actorUserUuid: null,
|
|
actorName: null,
|
|
});
|
|
|
|
mockBookingAuditRepository.findAllForBooking.mockResolvedValue([mockLog]);
|
|
|
|
const result = await service.getAuditLogsForBooking({
|
|
bookingUid: "booking-uid-123",
|
|
userId: 123,
|
|
userEmail: "[email protected]",
|
|
userTimeZone: "UTC",
|
|
organizationId: 200,
|
|
});
|
|
|
|
expect(result.auditLogs[0].actor).toMatchObject({
|
|
type: "GUEST",
|
|
displayName: "Guest",
|
|
displayEmail: null,
|
|
displayAvatar: null,
|
|
});
|
|
});
|
|
|
|
it("should use provided name for GUEST actor", async () => {
|
|
const mockLog = createMockAuditLog({
|
|
actorType: "GUEST",
|
|
actorUserUuid: null,
|
|
actorName: "External Guest",
|
|
});
|
|
|
|
mockBookingAuditRepository.findAllForBooking.mockResolvedValue([mockLog]);
|
|
|
|
const result = await service.getAuditLogsForBooking({
|
|
bookingUid: "booking-uid-123",
|
|
userId: 123,
|
|
userEmail: "[email protected]",
|
|
userTimeZone: "UTC",
|
|
organizationId: 200,
|
|
});
|
|
|
|
expect(result.auditLogs[0].actor.displayName).toBe("External Guest");
|
|
});
|
|
|
|
it("should show 'Attendee' for ATTENDEE actor without name", async () => {
|
|
const mockLog = createMockAuditLog({
|
|
actorType: "ATTENDEE",
|
|
actorUserUuid: null,
|
|
actorName: null,
|
|
});
|
|
|
|
// Update the mock to include attendeeId in the actor
|
|
const logWithAttendeeId = {
|
|
...mockLog,
|
|
actor: {
|
|
...mockLog.actor,
|
|
attendeeId: 999,
|
|
},
|
|
};
|
|
|
|
mockBookingAuditRepository.findAllForBooking.mockResolvedValue([logWithAttendeeId]);
|
|
mockAttendeeRepository.findById.mockResolvedValue({
|
|
id: 999,
|
|
name: null,
|
|
email: "[email protected]",
|
|
});
|
|
|
|
const result = await service.getAuditLogsForBooking({
|
|
bookingUid: "booking-uid-123",
|
|
userId: 123,
|
|
userEmail: "[email protected]",
|
|
userTimeZone: "UTC",
|
|
organizationId: 200,
|
|
});
|
|
|
|
expect(mockAttendeeRepository.findById).toHaveBeenCalledWith(999);
|
|
expect(result.auditLogs[0].actor).toMatchObject({
|
|
type: "ATTENDEE",
|
|
displayName: "[email protected]",
|
|
displayEmail: "[email protected]",
|
|
displayAvatar: null,
|
|
});
|
|
});
|
|
|
|
it("should use provided name for ATTENDEE actor", async () => {
|
|
const mockLog = createMockAuditLog({
|
|
actorType: "ATTENDEE",
|
|
actorUserUuid: null,
|
|
actorName: "Meeting Participant",
|
|
});
|
|
|
|
// Update the mock to include attendeeId in the actor
|
|
const logWithAttendeeId = {
|
|
...mockLog,
|
|
actor: {
|
|
...mockLog.actor,
|
|
attendeeId: 888,
|
|
},
|
|
};
|
|
|
|
mockBookingAuditRepository.findAllForBooking.mockResolvedValue([logWithAttendeeId]);
|
|
mockAttendeeRepository.findById.mockResolvedValue({
|
|
id: 888,
|
|
name: "Meeting Participant",
|
|
email: "[email protected]",
|
|
});
|
|
|
|
const result = await service.getAuditLogsForBooking({
|
|
bookingUid: "booking-uid-123",
|
|
userId: 123,
|
|
userEmail: "[email protected]",
|
|
userTimeZone: "UTC",
|
|
organizationId: 200,
|
|
});
|
|
|
|
expect(mockAttendeeRepository.findById).toHaveBeenCalledWith(888);
|
|
expect(result.auditLogs[0].actor.displayName).toBe("Meeting Participant");
|
|
expect(result.auditLogs[0].actor.displayEmail).toBe("[email protected]");
|
|
});
|
|
|
|
it("should show 'Deleted Attendee' when attendee not found in repository", async () => {
|
|
const mockLog = createMockAuditLog({
|
|
actorType: "ATTENDEE",
|
|
actorUserUuid: null,
|
|
actorName: null,
|
|
});
|
|
|
|
// Update the mock to include attendeeId in the actor
|
|
const logWithAttendeeId = {
|
|
...mockLog,
|
|
actor: {
|
|
...mockLog.actor,
|
|
attendeeId: 777,
|
|
},
|
|
};
|
|
|
|
mockBookingAuditRepository.findAllForBooking.mockResolvedValue([logWithAttendeeId]);
|
|
mockAttendeeRepository.findById.mockResolvedValue(null);
|
|
|
|
const result = await service.getAuditLogsForBooking({
|
|
bookingUid: "booking-uid-123",
|
|
userId: 123,
|
|
userEmail: "[email protected]",
|
|
userTimeZone: "UTC",
|
|
organizationId: 200,
|
|
});
|
|
|
|
expect(mockAttendeeRepository.findById).toHaveBeenCalledWith(777);
|
|
expect(result.auditLogs[0].actor).toMatchObject({
|
|
type: "ATTENDEE",
|
|
displayName: "Deleted Attendee",
|
|
displayEmail: null,
|
|
displayAvatar: null,
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("when handling rescheduled bookings", () => {
|
|
beforeEach(() => {
|
|
mockBookingRepository.findByUidIncludeEventType.mockResolvedValue(
|
|
createMockTeamBooking({ teamId: 100 })
|
|
);
|
|
mockPermissionCheckService.checkPermission.mockResolvedValue(true);
|
|
});
|
|
|
|
it("should include rescheduled from log when booking was created from reschedule", async () => {
|
|
const currentBookingLog = createMockAuditLog({
|
|
id: "current-log",
|
|
bookingUid: "new-booking-uid",
|
|
action: "CREATED",
|
|
});
|
|
|
|
const rescheduledLog = createMockAuditLog({
|
|
id: "rescheduled-log",
|
|
bookingUid: "old-booking-uid",
|
|
action: "RESCHEDULED",
|
|
data: {
|
|
version: 1,
|
|
fields: {
|
|
startTime: { old: "2024-01-15T10:00:00Z", new: "2024-01-16T10:00:00Z" },
|
|
endTime: { old: "2024-01-15T11:00:00Z", new: "2024-01-16T11:00:00Z" },
|
|
rescheduledToUid: { old: null, new: "new-booking-uid" },
|
|
},
|
|
},
|
|
});
|
|
|
|
mockBookingAuditRepository.findAllForBooking.mockResolvedValue([currentBookingLog]);
|
|
mockBookingRepository.getFromRescheduleUid.mockResolvedValue("old-booking-uid");
|
|
mockBookingAuditRepository.findRescheduledLogsOfBooking.mockResolvedValue([rescheduledLog]);
|
|
mockUserRepository.findByUuid.mockResolvedValue(createMockUser());
|
|
|
|
const result = await service.getAuditLogsForBooking({
|
|
bookingUid: "new-booking-uid",
|
|
userId: 123,
|
|
userEmail: "[email protected]",
|
|
userTimeZone: "UTC",
|
|
organizationId: 200,
|
|
});
|
|
|
|
expect(result.auditLogs).toHaveLength(2);
|
|
expect(result.auditLogs[0].id).toBe("rescheduled-log");
|
|
expect(result.auditLogs[0].bookingUid).toBe("new-booking-uid");
|
|
expect(result.auditLogs[0].actionDisplayTitle.key).toBe("booking_audit_action.rescheduled_from");
|
|
expect(result.auditLogs[0].displayJson).toHaveProperty("rescheduledFromUid", "old-booking-uid");
|
|
expect(result.auditLogs[1].id).toBe("current-log");
|
|
});
|
|
|
|
it("should not include rescheduled from log when booking was not rescheduled", async () => {
|
|
const mockLog = createMockAuditLog();
|
|
|
|
mockBookingAuditRepository.findAllForBooking.mockResolvedValue([mockLog]);
|
|
mockBookingRepository.getFromRescheduleUid.mockResolvedValue(null);
|
|
mockUserRepository.findByUuid.mockResolvedValue(createMockUser());
|
|
|
|
const result = await service.getAuditLogsForBooking({
|
|
bookingUid: "booking-uid-123",
|
|
userId: 123,
|
|
userEmail: "[email protected]",
|
|
userTimeZone: "UTC",
|
|
organizationId: 200,
|
|
});
|
|
|
|
expect(result.auditLogs).toHaveLength(1);
|
|
expect(mockBookingAuditRepository.findRescheduledLogsOfBooking).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("should continue without rescheduled from log when matching log not found", async () => {
|
|
const currentBookingLog = createMockAuditLog({
|
|
bookingUid: "new-booking-uid",
|
|
});
|
|
|
|
const rescheduledLog = createMockAuditLog({
|
|
id: "rescheduled-log",
|
|
bookingUid: "old-booking-uid",
|
|
action: "RESCHEDULED",
|
|
data: {
|
|
version: 1,
|
|
fields: {
|
|
startTime: { old: "2024-01-15T10:00:00Z", new: "2024-01-16T10:00:00Z" },
|
|
endTime: { old: "2024-01-15T11:00:00Z", new: "2024-01-16T11:00:00Z" },
|
|
rescheduledToUid: { old: null, new: "different-booking-uid" },
|
|
},
|
|
},
|
|
});
|
|
|
|
mockBookingAuditRepository.findAllForBooking.mockResolvedValue([currentBookingLog]);
|
|
mockBookingRepository.getFromRescheduleUid.mockResolvedValue("old-booking-uid");
|
|
mockBookingAuditRepository.findRescheduledLogsOfBooking.mockResolvedValue([rescheduledLog]);
|
|
mockUserRepository.findByUuid.mockResolvedValue(createMockUser());
|
|
|
|
const result = await service.getAuditLogsForBooking({
|
|
bookingUid: "new-booking-uid",
|
|
userId: 123,
|
|
userEmail: "[email protected]",
|
|
userTimeZone: "UTC",
|
|
organizationId: 200,
|
|
});
|
|
|
|
expect(result.auditLogs).toHaveLength(1);
|
|
expect(result.auditLogs[0].id).toBe("audit-log-1");
|
|
expect(mockLog.error).toHaveBeenCalledWith(
|
|
"No rescheduled log found for booking old-booking-uid -> new-booking-uid"
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("when permission check fails", () => {
|
|
it("should throw error when user lacks permission", async () => {
|
|
mockBookingRepository.findByUidIncludeEventType.mockResolvedValue(
|
|
createMockTeamBooking({ teamId: 100 })
|
|
);
|
|
mockPermissionCheckService.checkPermission.mockResolvedValue(false);
|
|
mockMembershipRepository.hasMembership.mockResolvedValue(true);
|
|
|
|
await expect(
|
|
service.getAuditLogsForBooking({
|
|
bookingUid: "booking-uid-123",
|
|
userId: 123,
|
|
userEmail: "[email protected]",
|
|
userTimeZone: "UTC",
|
|
organizationId: 200,
|
|
})
|
|
).rejects.toThrow(BookingAuditPermissionError);
|
|
|
|
expect(mockBookingAuditRepository.findAllForBooking).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("should throw error when organization ID is null", async () => {
|
|
await expect(
|
|
service.getAuditLogsForBooking({
|
|
bookingUid: "booking-uid-123",
|
|
userId: 123,
|
|
userEmail: "[email protected]",
|
|
userTimeZone: "UTC",
|
|
organizationId: null,
|
|
})
|
|
).rejects.toThrow(BookingAuditPermissionError);
|
|
|
|
expect(mockBookingAuditRepository.findAllForBooking).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("should throw error when booking not found", async () => {
|
|
mockBookingRepository.findByUidIncludeEventType.mockResolvedValue(null);
|
|
|
|
await expect(
|
|
service.getAuditLogsForBooking({
|
|
bookingUid: "non-existent-booking",
|
|
userId: 123,
|
|
userEmail: "[email protected]",
|
|
userTimeZone: "UTC",
|
|
organizationId: 200,
|
|
})
|
|
).rejects.toThrow(BookingAuditErrorCode.BOOKING_NOT_FOUND_OR_PERMISSION_DENIED);
|
|
|
|
expect(mockBookingAuditRepository.findAllForBooking).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
describe("when handling different timezones", () => {
|
|
beforeEach(() => {
|
|
mockBookingRepository.findByUidIncludeEventType.mockResolvedValue(
|
|
createMockTeamBooking({ teamId: 100 })
|
|
);
|
|
mockPermissionCheckService.checkPermission.mockResolvedValue(true);
|
|
});
|
|
|
|
it("should pass user timezone to action service for display formatting", async () => {
|
|
const mockLog = createMockAuditLog({
|
|
action: "RESCHEDULED",
|
|
data: {
|
|
version: 1,
|
|
fields: {
|
|
startTime: { old: "2024-01-15T10:00:00Z", new: "2024-01-16T10:00:00Z" },
|
|
endTime: { old: "2024-01-15T11:00:00Z", new: "2024-01-16T11:00:00Z" },
|
|
rescheduledToUid: { old: null, new: "new-booking-uid" },
|
|
},
|
|
},
|
|
});
|
|
|
|
mockBookingAuditRepository.findAllForBooking.mockResolvedValue([mockLog]);
|
|
mockUserRepository.findByUuid.mockResolvedValue(createMockUser());
|
|
|
|
const result = await service.getAuditLogsForBooking({
|
|
bookingUid: "booking-uid-123",
|
|
userId: 123,
|
|
userEmail: "[email protected]",
|
|
userTimeZone: "America/Los_Angeles",
|
|
organizationId: 200,
|
|
});
|
|
|
|
expect(result.auditLogs[0].actionDisplayTitle).toHaveProperty("params");
|
|
expect(result.auditLogs[0].displayJson).toBeDefined();
|
|
});
|
|
});
|
|
|
|
describe("when handling seat audit actions", () => {
|
|
beforeEach(() => {
|
|
mockBookingRepository.findByUidIncludeEventType.mockResolvedValue(
|
|
createMockTeamBooking({ teamId: 100 })
|
|
);
|
|
mockPermissionCheckService.checkPermission.mockResolvedValue(true);
|
|
});
|
|
|
|
it("should handle SEAT_BOOKED action with seat information", async () => {
|
|
const mockLog = createMockAuditLog({
|
|
id: "seat-booked-log",
|
|
action: "SEAT_BOOKED",
|
|
data: {
|
|
version: 1,
|
|
fields: {
|
|
seatReferenceUid: "seat-ref-123",
|
|
attendeeEmail: "[email protected]",
|
|
attendeeName: "Jane Attendee",
|
|
startTime: 1705315200000,
|
|
endTime: 1705318800000,
|
|
},
|
|
},
|
|
});
|
|
|
|
mockBookingAuditRepository.findAllForBooking.mockResolvedValue([mockLog]);
|
|
mockUserRepository.findByUuid.mockResolvedValue(createMockUser());
|
|
|
|
const result = await service.getAuditLogsForBooking({
|
|
bookingUid: "booking-uid-123",
|
|
userId: 123,
|
|
userEmail: "[email protected]",
|
|
userTimeZone: "America/New_York",
|
|
organizationId: 200,
|
|
});
|
|
|
|
expect(result.auditLogs).toHaveLength(1);
|
|
expect(result.auditLogs[0].action).toBe("SEAT_BOOKED");
|
|
expect(result.auditLogs[0].actionDisplayTitle.key).toBe("booking_audit_action.seat_booked");
|
|
expect(result.auditLogs[0].displayJson).toBeDefined();
|
|
});
|
|
|
|
it("should handle SEAT_RESCHEDULED action with time changes", async () => {
|
|
const mockLog = createMockAuditLog({
|
|
id: "seat-rescheduled-log",
|
|
action: "SEAT_RESCHEDULED",
|
|
data: {
|
|
version: 1,
|
|
fields: {
|
|
seatReferenceUid: "seat-ref-123",
|
|
attendeeEmail: "[email protected]",
|
|
startTime: {
|
|
old: "2024-01-15T10:00:00Z",
|
|
new: "2024-01-16T10:00:00Z",
|
|
},
|
|
endTime: {
|
|
old: "2024-01-15T11:00:00Z",
|
|
new: "2024-01-16T11:00:00Z",
|
|
},
|
|
rescheduledToBookingUid: {
|
|
old: null,
|
|
new: "new-booking-uid",
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
mockBookingAuditRepository.findAllForBooking.mockResolvedValue([mockLog]);
|
|
mockUserRepository.findByUuid.mockResolvedValue(createMockUser());
|
|
|
|
const result = await service.getAuditLogsForBooking({
|
|
bookingUid: "booking-uid-123",
|
|
userId: 123,
|
|
userEmail: "[email protected]",
|
|
userTimeZone: "America/New_York",
|
|
organizationId: 200,
|
|
});
|
|
|
|
expect(result.auditLogs).toHaveLength(1);
|
|
expect(result.auditLogs[0].action).toBe("SEAT_RESCHEDULED");
|
|
expect(result.auditLogs[0].actionDisplayTitle.key).toBe("booking_audit_action.seat_rescheduled");
|
|
expect(result.auditLogs[0].actionDisplayTitle.params).toBeDefined();
|
|
expect(result.auditLogs[0].actionDisplayTitle.components).toBeDefined();
|
|
expect(result.auditLogs[0].displayJson).toBeDefined();
|
|
});
|
|
|
|
it("should handle SEAT_RESCHEDULED action without moving to different booking", async () => {
|
|
const mockLog = createMockAuditLog({
|
|
id: "seat-rescheduled-same-booking",
|
|
action: "SEAT_RESCHEDULED",
|
|
data: {
|
|
version: 1,
|
|
fields: {
|
|
seatReferenceUid: "seat-ref-123",
|
|
attendeeEmail: "[email protected]",
|
|
startTime: {
|
|
old: "2024-01-15T10:00:00Z",
|
|
new: "2024-01-15T11:00:00Z",
|
|
},
|
|
endTime: {
|
|
old: "2024-01-15T11:00:00Z",
|
|
new: "2024-01-15T12:00:00Z",
|
|
},
|
|
rescheduledToBookingUid: {
|
|
old: null,
|
|
new: null,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
mockBookingAuditRepository.findAllForBooking.mockResolvedValue([mockLog]);
|
|
mockUserRepository.findByUuid.mockResolvedValue(createMockUser());
|
|
|
|
const result = await service.getAuditLogsForBooking({
|
|
bookingUid: "booking-uid-123",
|
|
userId: 123,
|
|
userEmail: "[email protected]",
|
|
userTimeZone: "UTC",
|
|
organizationId: 200,
|
|
});
|
|
|
|
expect(result.auditLogs).toHaveLength(1);
|
|
expect(result.auditLogs[0].action).toBe("SEAT_RESCHEDULED");
|
|
expect(result.auditLogs[0].actionDisplayTitle.components).toBeUndefined();
|
|
});
|
|
});
|
|
|
|
describe("when handling impersonatedBy context", () => {
|
|
beforeEach(() => {
|
|
mockBookingRepository.findByUidIncludeEventType.mockResolvedValue(
|
|
createMockTeamBooking({ teamId: 100 })
|
|
);
|
|
mockPermissionCheckService.checkPermission.mockResolvedValue(true);
|
|
});
|
|
|
|
it("should enrich impersonatedBy when user exists", async () => {
|
|
const mockLog = createMockAuditLog({
|
|
context: { impersonatedBy: "impersonator-uuid-456" },
|
|
});
|
|
const impersonatorUser = createMockUser({
|
|
id: 456,
|
|
name: "Admin User",
|
|
email: "[email protected]",
|
|
avatarUrl: "https://example.com/admin-avatar.jpg",
|
|
});
|
|
|
|
mockBookingAuditRepository.findAllForBooking.mockResolvedValue([mockLog]);
|
|
mockUserRepository.findByUuid.mockResolvedValueOnce(createMockUser()); // For actor
|
|
mockUserRepository.findByUuid.mockResolvedValueOnce(impersonatorUser); // For impersonator
|
|
|
|
const result = await service.getAuditLogsForBooking({
|
|
bookingUid: "booking-uid-123",
|
|
userId: 123,
|
|
userEmail: "[email protected]",
|
|
userTimeZone: "UTC",
|
|
organizationId: 200,
|
|
});
|
|
|
|
expect(mockUserRepository.findByUuid).toHaveBeenCalledWith({ uuid: "impersonator-uuid-456" });
|
|
expect(result.auditLogs[0].impersonatedBy).toMatchObject({
|
|
displayName: "Admin User",
|
|
displayEmail: "[email protected]",
|
|
displayAvatar: "https://example.com/admin-avatar.jpg",
|
|
});
|
|
});
|
|
|
|
it("should use email as displayName when impersonator name is null", async () => {
|
|
const mockLog = createMockAuditLog({
|
|
context: { impersonatedBy: "impersonator-uuid-456" },
|
|
});
|
|
const impersonatorUser = createMockUser({
|
|
id: 456,
|
|
name: null,
|
|
email: "[email protected]",
|
|
avatarUrl: null,
|
|
});
|
|
|
|
mockBookingAuditRepository.findAllForBooking.mockResolvedValue([mockLog]);
|
|
mockUserRepository.findByUuid.mockResolvedValueOnce(createMockUser()); // For actor
|
|
mockUserRepository.findByUuid.mockResolvedValueOnce(impersonatorUser); // For impersonator
|
|
|
|
const result = await service.getAuditLogsForBooking({
|
|
bookingUid: "booking-uid-123",
|
|
userId: 123,
|
|
userEmail: "[email protected]",
|
|
userTimeZone: "UTC",
|
|
organizationId: 200,
|
|
});
|
|
|
|
expect(result.auditLogs[0].impersonatedBy).toMatchObject({
|
|
displayName: "[email protected]",
|
|
displayEmail: "[email protected]",
|
|
displayAvatar: null,
|
|
});
|
|
});
|
|
|
|
it("should show 'Deleted User' when impersonator user not found", async () => {
|
|
const mockLog = createMockAuditLog({
|
|
context: { impersonatedBy: "impersonator-uuid-456" },
|
|
});
|
|
|
|
mockBookingAuditRepository.findAllForBooking.mockResolvedValue([mockLog]);
|
|
mockUserRepository.findByUuid.mockResolvedValueOnce(createMockUser()); // For actor
|
|
mockUserRepository.findByUuid.mockResolvedValueOnce(null); // For impersonator (not found)
|
|
|
|
const result = await service.getAuditLogsForBooking({
|
|
bookingUid: "booking-uid-123",
|
|
userId: 123,
|
|
userEmail: "[email protected]",
|
|
userTimeZone: "UTC",
|
|
organizationId: 200,
|
|
});
|
|
|
|
expect(mockUserRepository.findByUuid).toHaveBeenCalledWith({ uuid: "impersonator-uuid-456" });
|
|
expect(result.auditLogs[0].impersonatedBy).toMatchObject({
|
|
displayName: "Deleted User",
|
|
displayEmail: null,
|
|
displayAvatar: null,
|
|
});
|
|
});
|
|
|
|
it("should return null when context is null", async () => {
|
|
const mockLog = createMockAuditLog({
|
|
context: null,
|
|
});
|
|
|
|
mockBookingAuditRepository.findAllForBooking.mockResolvedValue([mockLog]);
|
|
mockUserRepository.findByUuid.mockResolvedValue(createMockUser());
|
|
|
|
const result = await service.getAuditLogsForBooking({
|
|
bookingUid: "booking-uid-123",
|
|
userId: 123,
|
|
userEmail: "[email protected]",
|
|
userTimeZone: "UTC",
|
|
organizationId: 200,
|
|
});
|
|
|
|
expect(result.auditLogs[0].impersonatedBy).toBeNull();
|
|
// Should not call findByUuid for impersonator when context is null
|
|
expect(mockUserRepository.findByUuid).toHaveBeenCalledTimes(1); // Only for actor
|
|
});
|
|
|
|
it("should return null when impersonatedBy is not in context", async () => {
|
|
const mockLog = createMockAuditLog({
|
|
context: {},
|
|
});
|
|
|
|
mockBookingAuditRepository.findAllForBooking.mockResolvedValue([mockLog]);
|
|
mockUserRepository.findByUuid.mockResolvedValue(createMockUser());
|
|
|
|
const result = await service.getAuditLogsForBooking({
|
|
bookingUid: "booking-uid-123",
|
|
userId: 123,
|
|
userEmail: "[email protected]",
|
|
userTimeZone: "UTC",
|
|
organizationId: 200,
|
|
});
|
|
|
|
expect(result.auditLogs[0].impersonatedBy).toBeNull();
|
|
// Should not call findByUuid for impersonator when impersonatedBy is not in context
|
|
expect(mockUserRepository.findByUuid).toHaveBeenCalledTimes(1); // Only for actor
|
|
});
|
|
});
|
|
});
|
|
});
|