-
Notifications
You must be signed in to change notification settings - Fork 12k
feat: implement comprehensive booking audit system #22854
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6f62d1c
feat: implement comprehensive booking audit system
Vansh5632 99d7cc4
added the changes in the file according to the suggestions
Vansh5632 0f00de5
changes
Vansh5632 e2c6903
fix: handle undefined cancelledBy in handleCancelBooking
Vansh5632 57d450b
chore: rename services to CamelCase
volnei 09607b6
fix booking cancellation
volnei d1b5dab
chore: add fields to select
volnei 92b725a
Merge branch 'main' into feat/booking-audit-system
volnei File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
225 changes: 225 additions & 0 deletions
225
packages/features/bookings/lib/audit/BookingAuditService.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,225 @@ | ||
| import { describe, it, expect, vi, beforeEach } from "vitest"; | ||
|
|
||
| import { BookingAuditAction, getBookingAuditSchema } from "./bookingAudit.schemas"; | ||
|
|
||
| // Mock Prisma client | ||
| const mockPrismaCreate = vi.fn(); | ||
| const mockPrismaFindMany = vi.fn(); | ||
|
|
||
| vi.mock("@calcom/prisma", () => ({ | ||
| default: { | ||
| bookingAudit: { | ||
| create: mockPrismaCreate, | ||
| findMany: mockPrismaFindMany, | ||
| }, | ||
| }, | ||
| })); | ||
|
|
||
| // Mock logger | ||
| vi.mock("@calcom/lib/logger", () => ({ | ||
| default: { | ||
| getSubLogger: () => ({ | ||
| error: vi.fn(), | ||
| info: vi.fn(), | ||
| }), | ||
| }, | ||
| })); | ||
|
|
||
| describe("BookingAuditService", () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| // Set up default mock behaviors | ||
| mockPrismaCreate.mockResolvedValue({}); | ||
| mockPrismaFindMany.mockResolvedValue([]); | ||
| }); | ||
|
|
||
| describe("Audit System Integration", () => { | ||
| it("should create audit log for booking creation with valid data", async () => { | ||
| const { BookingAuditService } = await import("./BookingAuditService"); | ||
|
|
||
| const mockResult = { | ||
| id: "audit-123", | ||
| bookingId: 1, | ||
| action: BookingAuditAction.CREATED, | ||
| data: {}, | ||
| createdAt: new Date(), | ||
| }; | ||
|
|
||
| mockPrismaCreate.mockResolvedValue(mockResult); | ||
|
|
||
| await BookingAuditService.logBookingCreated({ | ||
| bookingId: 1, | ||
| eventTypeId: 1, | ||
| eventTypeName: "Test Meeting", | ||
| organizerId: 123, | ||
| startTime: "2025-08-01T10:00:00.000Z", | ||
| endTime: "2025-08-01T11:00:00.000Z", | ||
| isConfirmed: true, | ||
| }); | ||
|
|
||
| expect(mockPrismaCreate).toHaveBeenCalledWith({ | ||
| data: expect.objectContaining({ | ||
| bookingId: 1, | ||
| action: BookingAuditAction.CREATED, | ||
| data: expect.any(Object), | ||
| }), | ||
| }); | ||
| }); | ||
|
|
||
| it("should reject invalid booking cancellation data", () => { | ||
| const invalidData = { | ||
| bookingId: "not-a-number", // Invalid type | ||
| action: BookingAuditAction.CANCELLED, | ||
| data: { | ||
| eventTypeId: "invalid", // Invalid type | ||
| startTime: "not-a-date", // Invalid date format | ||
| endTime: "not-a-date", // Invalid date format | ||
| }, | ||
| }; | ||
|
|
||
| const schema = getBookingAuditSchema(BookingAuditAction.CANCELLED); | ||
| const result = schema.safeParse(invalidData); | ||
|
|
||
| expect(result.success).toBe(false); | ||
| }); | ||
|
|
||
| it("should reject invalid booking reschedule data", () => { | ||
| const invalidData = { | ||
| bookingId: "not-a-number", // Invalid type | ||
| action: BookingAuditAction.RESCHEDULED, | ||
| data: { | ||
| eventTypeId: "invalid", // Invalid type | ||
| startTime: "not-a-date", // Invalid date format | ||
| endTime: "not-a-date", // Invalid date format | ||
| oldStartTime: "not-a-date", // Invalid date format | ||
| oldEndTime: "not-a-date", // Invalid date format | ||
| organizerChanged: "yes", // Invalid type | ||
| }, | ||
| }; | ||
|
|
||
| const schema = getBookingAuditSchema(BookingAuditAction.RESCHEDULED); | ||
| const result = schema.safeParse(invalidData); | ||
|
|
||
| expect(result.success).toBe(false); | ||
| }); | ||
|
|
||
| it("should create audit log for booking cancellation", async () => { | ||
| const { BookingAuditService } = await import("./BookingAuditService"); | ||
|
|
||
| const mockResult = { | ||
| id: "audit-124", | ||
| bookingId: 2, | ||
| action: BookingAuditAction.CANCELLED, | ||
| data: {}, | ||
| createdAt: new Date(), | ||
| }; | ||
|
|
||
| mockPrismaCreate.mockResolvedValue(mockResult); | ||
|
|
||
| await BookingAuditService.logBookingCancelled({ | ||
| bookingId: 2, | ||
| eventTypeId: 1, | ||
| startTime: new Date("2025-08-01T10:00:00.000Z"), | ||
| endTime: new Date("2025-08-01T11:00:00.000Z"), | ||
| cancellationReason: "Meeting cancelled", | ||
| }); | ||
|
|
||
| expect(mockPrismaCreate).toHaveBeenCalledWith({ | ||
| data: expect.objectContaining({ | ||
| bookingId: 2, | ||
| action: BookingAuditAction.CANCELLED, | ||
| }), | ||
| }); | ||
| }); | ||
|
|
||
| it("should create audit log for booking reschedule", async () => { | ||
| const { BookingAuditService } = await import("./BookingAuditService"); | ||
|
|
||
| const mockResult = { | ||
| id: "audit-125", | ||
| bookingId: 3, | ||
| action: BookingAuditAction.RESCHEDULED, | ||
| data: {}, | ||
| createdAt: new Date(), | ||
| }; | ||
|
|
||
| mockPrismaCreate.mockResolvedValue(mockResult); | ||
|
|
||
| await BookingAuditService.logBookingRescheduled({ | ||
| bookingId: 3, | ||
| eventTypeId: 1, | ||
| originalStartTime: new Date("2025-08-01T10:00:00.000Z"), | ||
| originalEndTime: new Date("2025-08-01T11:00:00.000Z"), | ||
| newStartTime: new Date("2025-08-02T10:00:00.000Z"), | ||
| newEndTime: new Date("2025-08-02T11:00:00.000Z"), | ||
| }); | ||
|
|
||
| expect(mockPrismaCreate).toHaveBeenCalledWith({ | ||
| data: expect.objectContaining({ | ||
| bookingId: 3, | ||
| action: BookingAuditAction.RESCHEDULED, | ||
| }), | ||
| }); | ||
| }); | ||
|
|
||
| it("should retrieve audit logs for a booking", async () => { | ||
| const { BookingAuditService } = await import("./BookingAuditService"); | ||
|
|
||
| const mockLogs = [ | ||
| { | ||
| id: "audit-123", | ||
| bookingId: 1, | ||
| action: BookingAuditAction.CREATED, | ||
| data: {}, | ||
| createdAt: new Date(), | ||
| }, | ||
| { | ||
| id: "audit-124", | ||
| bookingId: 1, | ||
| action: BookingAuditAction.CANCELLED, | ||
| data: {}, | ||
| createdAt: new Date(), | ||
| }, | ||
| ]; | ||
|
|
||
| mockPrismaFindMany.mockResolvedValue(mockLogs); | ||
|
|
||
| const result = await BookingAuditService.getAuditLogs(1); | ||
|
|
||
| expect(result).toEqual(mockLogs); | ||
| expect(mockPrismaFindMany).toHaveBeenCalledWith({ | ||
| select: { | ||
| id: true, | ||
| bookingId: true, | ||
| action: true, | ||
| data: true, | ||
| createdAt: true, | ||
| actor: true, | ||
| version: true, | ||
| }, | ||
| where: { bookingId: 1 }, | ||
| orderBy: { createdAt: "desc" }, | ||
| }); | ||
| }); | ||
|
|
||
| it("should handle errors gracefully and not throw", async () => { | ||
| const { BookingAuditService } = await import("./BookingAuditService"); | ||
|
|
||
| // Mock Prisma to reject with an error | ||
| mockPrismaCreate.mockRejectedValue(new Error("Database error")); | ||
|
|
||
| // The service should not throw errors, it should log them | ||
| await expect( | ||
| BookingAuditService.logBookingCreated({ | ||
| bookingId: 1, | ||
| eventTypeId: 1, | ||
| eventTypeName: "Test Meeting", | ||
| organizerId: 123, | ||
| startTime: "2025-08-01T10:00:00.000Z", | ||
| endTime: "2025-08-01T11:00:00.000Z", | ||
| isConfirmed: true, | ||
| }) | ||
| ).resolves.toBeUndefined(); | ||
| }); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.