feat: [Booking Audit Stack - 1] Add Booking Audit System foundation (database schema and repositories) (#24838)

* feat: Implement Booking Audit System with database architecture and repository interfaces

- Added `ARCHITECTURE.md` detailing the design and structure of the Booking Audit System, including core tables `AuditActor` and `BookingAudit`.
- Created repository interfaces `IAuditActorRepository` and `IBookingAuditRepository` for managing audit actor and booking audit records.
- Implemented `PrismaAuditActorRepository` and `PrismaBookingAuditRepository` for database interactions.
- Defined enums for `BookingAuditType`, `BookingAuditAction`, and `AuditActorType` in the Prisma schema.
- Added migration scripts to create necessary database tables and enums for the audit system.

This commit establishes a robust framework for tracking booking-related actions, ensuring compliance and data integrity.

* feat(audit): add system actor migration
This commit is contained in:
Hariom Balhara authored and GitHub committed 2025-11-19 08:21:34 -03:00
1 parent 075d209b6f
commit c04402e7ee
8 files changed
+1099

No files matched your search

@@ -0,0 +1,70 @@
-- CreateEnum
CREATE TYPE "public"."BookingAuditType" AS ENUM ('record_created', 'record_updated', 'record_deleted');
-- CreateEnum
CREATE TYPE "public"."BookingAuditAction" AS ENUM ('created', 'cancelled', 'accepted', 'rejected', 'pending', 'awaiting_host', 'rescheduled', 'attendee_added', 'attendee_removed', 'reassignment', 'location_changed', 'host_no_show_updated', 'attendee_no_show_updated', 'reschedule_requested');
-- CreateEnum
CREATE TYPE "public"."AuditActorType" AS ENUM ('user', 'guest', 'attendee', 'system');
-- CreateTable
CREATE TABLE "public"."AuditActor" (
"id" TEXT NOT NULL,
"type" "public"."AuditActorType" NOT NULL,
"userUuid" UUID,
"attendeeId" INTEGER,
"email" TEXT,
"phone" TEXT,
"name" TEXT,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "AuditActor_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "public"."BookingAudit" (
"id" UUID NOT NULL,
"bookingUid" TEXT NOT NULL,
"actorId" TEXT NOT NULL,
"type" "public"."BookingAuditType" NOT NULL,
"action" "public"."BookingAuditAction" NOT NULL,
"timestamp" TIMESTAMP(3) NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
"data" JSONB,
CONSTRAINT "BookingAudit_pkey" PRIMARY KEY ("id")
);
-- CreateIndex
CREATE INDEX "AuditActor_email_idx" ON "public"."AuditActor"("email");
-- CreateIndex
CREATE INDEX "AuditActor_userUuid_idx" ON "public"."AuditActor"("userUuid");
-- CreateIndex
CREATE INDEX "AuditActor_attendeeId_idx" ON "public"."AuditActor"("attendeeId");
-- CreateIndex
CREATE UNIQUE INDEX "AuditActor_userUuid_key" ON "public"."AuditActor"("userUuid");
-- CreateIndex
CREATE UNIQUE INDEX "AuditActor_attendeeId_key" ON "public"."AuditActor"("attendeeId");
-- CreateIndex
CREATE UNIQUE INDEX "AuditActor_email_key" ON "public"."AuditActor"("email");
-- CreateIndex
CREATE UNIQUE INDEX "AuditActor_phone_key" ON "public"."AuditActor"("phone");
-- CreateIndex
CREATE INDEX "BookingAudit_actorId_idx" ON "public"."BookingAudit"("actorId");
-- CreateIndex
CREATE INDEX "BookingAudit_bookingUid_idx" ON "public"."BookingAudit"("bookingUid");
-- CreateIndex
CREATE INDEX "BookingAudit_timestamp_idx" ON "public"."BookingAudit"("timestamp");
-- AddForeignKey
ALTER TABLE "public"."BookingAudit" ADD CONSTRAINT "BookingAudit_actorId_fkey" FOREIGN KEY ("actorId") REFERENCES "public"."AuditActor"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
@@ -0,0 +1,23 @@
-- Insert system actor with predefined UUID
-- This actor is used for system-initiated booking actions
INSERT INTO "AuditActor" (
id,
type,
"userUuid",
"attendeeId",
email,
phone,
name,
"createdAt"
)
VALUES (
'00000000-0000-0000-0000-000000000000',
'system',
NULL,
NULL,
NULL,
NULL,
'System',
NOW()
)
ON CONFLICT (id) DO NOTHING;
+107
View File
@@ -2627,6 +2627,113 @@ model RolePermission {
@@index([action])
}
enum BookingAuditType {
RECORD_CREATED @map("record_created")
RECORD_UPDATED @map("record_updated")
RECORD_DELETED @map("record_deleted")
}
enum BookingAuditAction {
// Booking lifecycle
CREATED @map("created")
// Status changes
CANCELLED @map("cancelled")
ACCEPTED @map("accepted")
REJECTED @map("rejected")
PENDING @map("pending")
AWAITING_HOST @map("awaiting_host")
RESCHEDULED @map("rescheduled")
// Attendee management
ATTENDEE_ADDED @map("attendee_added")
ATTENDEE_REMOVED @map("attendee_removed")
// Assignment/Reassignment (keep integration version - simpler)
REASSIGNMENT @map("reassignment")
// Meeting details
LOCATION_CHANGED @map("location_changed")
// No-show tracking
HOST_NO_SHOW_UPDATED @map("host_no_show_updated")
ATTENDEE_NO_SHOW_UPDATED @map("attendee_no_show_updated")
// Rescheduling
RESCHEDULE_REQUESTED @map("reschedule_requested")
}
enum AuditActorType {
USER @map("user") // Registered Cal.com user (stored here for audit retention even after user deletion)
GUEST @map("guest") // Non-registered user
ATTENDEE @map("attendee") // Has Attendee record with us
SYSTEM @map("system") // Automated actions
}
model AuditActor {
id String @id @default(uuid())
type AuditActorType
// References for different actor types (soft references, no FK constraints)
// These fields intentionally do NOT have foreign key constraints to preserve audit trail integrity:
// - When a User or Attendee is deleted, their AuditActor record persists with these IDs intact
// - This maintains immutable audit history even after source records are removed
userUuid String? @db.Uuid // For USER type - references User.uuid without FK constraint
attendeeId Int? // For ATTENDEE type - references Attendee.id without FK constraint
// Identity fields - only for GUEST/SYSTEM(System too might not have all) type. Attendee and User maintain their own identity fields.
// They could be set as anonymized for User/Attendee record as well when they are deleted to preserve the audit trail.
email String?
phone String?
name String?
createdAt DateTime @default(now())
bookingAudits BookingAudit[]
// TODO: Add pseudonymizedAt and related fields when we anonymize the data on deletion
@@unique([userUuid])
@@unique([attendeeId])
@@unique([email]) // Prevent duplicate email actors
@@unique([phone]) // Prevent duplicate phone actors
@@index([email])
@@index([userUuid])
@@index([attendeeId])
}
model BookingAudit {
id String @id @default(uuid(7)) @db.Uuid
// bookingUid is stored as a plain string (not a foreign key relation) to preserve the audit trail
// even after the booking is deleted. This is intentional for audit log integrity:
// - Audit logs are immutable historical records that should persist independently
// - When a booking is deleted, we still need to know which booking the audit log belonged to
// - Using a plain string instead of a relation prevents bookingUid from becoming NULL on booking deletion
// - This allows users to view complete audit history for deleted bookings
bookingUid String
// Actor who performed the action (USER, GUEST, or SYSTEM)
// Stored in AuditActor table to maintain audit trail even after user deletion
actorId String
// Restrict onDelete to prevent deletion of audit actor if there are any booking audits associated with it
actor AuditActor @relation(fields: [actorId], references: [id], onDelete: Restrict)
type BookingAuditType
action BookingAuditAction
// Timestamp of the actual booking change (business event time)
// Important: May differ from createdAt if audit is processed asynchronously
timestamp DateTime
// Database record timestamps
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
data Json?
@@index([actorId])
@@index([bookingUid])
@@index([timestamp])
}
enum PhoneNumberSubscriptionStatus {
ACTIVE
PAST_DUE