// AK-P16b (P16) Booking — Calendly-style public booking pages, timezone-aware // slot calculation, full appointment lifecycle. Aligned to ENTITY-MODEL Domäne 9 // (M06-booking). // // AD-21: self-contained schema. `tenantId` / `providerUserId` / `kontaktId` / // `leadId` / `formSubmissionId` are BARE String scalars (no `@relation` to // Tenant/User/Kontakt/Lead/Form) → zero merge-conflict surface against the shared // core + neighbouring module schemas (crm.prisma / calendar.prisma / forms). The // CRM/Form linkage runs through those bare scalars in the service layer. // Intra-module relations (Page → Provider / Appointment, Appointment self-reschedule) // stay inside this file. // // Cross-ticket gaps handled per user decision (2026-06-30): // - leadId stays NULLABLE, reserved for R-P7 (typed CRM Lead not built yet). v1 // post-booking matches/creates a Kontakt + logs an Activity instead. // - formSubmissionId stays NULLABLE, reserved for AK-P16c (Forms module not built // yet). v1 stores the standard mini-form answers in `intakeAnswers` JSON. // // Slot availability subtracts the Google busy intervals from AK-P16a // (CalendarEvent.isBusyForAvailability) via the pure availability.ts helpers — // leak-protection holds: only busy {from,to} intervals cross over, never titles. // // RLS ships separately in prisma/rls/booking-isolation.sql (deferred cutover like // the other modules). The hard double-booking guard (PostgreSQL EXCLUDE USING gist // over a tstzrange per provider) ships in prisma/rls/booking-constraints.sql — it // cannot be expressed in the Prisma schema. On the feature branch: `prisma db push` // + manual `psql -f` for the constraint; the merge migration folds both in (AD-21). /// Provider-assignment strategy (M06 A.2). v1 implements `first_available` only /// ("Erster der frei ist"); round_robin / specific_provider_choice → v2. The enum /// carries all three so the v2 switch is additive (no migration of stored values). enum BookingAssignmentStrategy { round_robin specific_provider_choice first_available } /// Weekday of a weekly availability rule (M06 A.3). enum BookingDayOfWeek { monday tuesday wednesday thursday friday saturday sunday } /// Date-specific override kind (M06 A.4). `unavailable` blocks the whole day; /// `custom_window` replaces the standard rule for that date. enum AvailabilityExceptionKind { unavailable custom_window } /// Appointment lifecycle status (M06 A.5 / M129). `rescheduled` keeps the old row /// for history and points forward via `rescheduledFromId` on the successor. enum AppointmentStatus { scheduled held cancelled no_show rescheduled } /// Who triggered a cancellation (M06 A.5) — drives the Klartext + auto-mail copy. enum AppointmentCancelledByKind { customer provider system } /// A public booking page (M06 A.1). Tenant-scoped, soft-deletable. `slug` is unique /// per tenant and powers the public URL `/b/{tenant}/{slug}`. `intakeFormId` is a /// bare reserved scalar for the AK-P16c Forms module (null = standard mini-form). model BookingPage { id String @id @default(cuid()) createdAt DateTime @default(now()) updatedAt DateTime @updatedAt tenantId String createdByUserId String? slug String title String description String? durationMinutes Int bufferBeforeMinutes Int @default(0) bufferAfterMinutes Int @default(0) slotIncrementMinutes Int @default(15) minNoticeHours Int @default(4) maxAdvanceDays Int @default(60) assignmentStrategy BookingAssignmentStrategy @default(first_available) confirmationEmailSubject String confirmationEmailBody String internalEventTitleTemplate String? redirectUrlAfterBooking String? // Reserved bare scalar for AK-P16c (Forms). Null → standard mini-form. intakeFormId String? isPublished Boolean @default(false) isDemoData Boolean @default(false) archivedAt DateTime? providers BookingPageProvider[] appointments Appointment[] @@unique([tenantId, slug]) @@index([tenantId, archivedAt, isPublished]) } /// M:N between BookingPage and tenant users who hold its appointments (M06 A.2). /// `providerUserId` is a bare scalar (no @relation to User, AD-21). model BookingPageProvider { id String @id @default(cuid()) createdAt DateTime @default(now()) tenantId String bookingPageId String providerUserId String displayOrder Int @default(0) isActive Boolean @default(true) bookingPage BookingPage @relation(fields: [bookingPageId], references: [id], onDelete: Cascade) @@unique([bookingPageId, providerUserId]) @@index([tenantId, providerUserId]) } /// Weekly standard availability per tenant user (M06 A.3). Several rows per weekday /// allowed (e.g. 09:00-12:00 + 14:00-17:00). Times are local "HH:mm" in `timezone`. model AvailabilityRule { id String @id @default(cuid()) createdAt DateTime @default(now()) updatedAt DateTime @updatedAt tenantId String providerUserId String dayOfWeek BookingDayOfWeek startTime String endTime String timezone String @@index([tenantId, providerUserId, dayOfWeek]) } /// Date-specific override of the standard rules (M06 A.4). `startTime`/`endTime` /// only set when kind = custom_window (null for a full-day `unavailable`). model AvailabilityException { id String @id @default(cuid()) createdAt DateTime @default(now()) updatedAt DateTime @updatedAt tenantId String providerUserId String date DateTime @db.Date kind AvailabilityExceptionKind startTime String? endTime String? reason String? timezone String @@index([tenantId, providerUserId, date]) } /// A booked appointment (M06 A.5). Tenant-scoped. `startAt`/`endAt` are UTC; /// `timezone` records the booker's IANA zone for display. CRM/Form links are bare /// nullable scalars (kontaktId set on match/create; leadId reserved for R-P7, /// formSubmissionId reserved for AK-P16c). `intakeAnswers` holds the v1 mini-form /// payload. `internalCalendarEventId` points at the local CalendarEvent mirror that /// blocks the provider's availability (AK-P16a write-path pushes it external later). model Appointment { id String @id @default(cuid()) createdAt DateTime @default(now()) updatedAt DateTime @updatedAt tenantId String bookingPageId String providerUserId String startAt DateTime endAt DateTime timezone String status AppointmentStatus @default(scheduled) // CRM linkage (bare scalars, AD-21). leadId reserved for R-P7. kontaktId String? leadId String? // Reserved for AK-P16c Forms. v1 answers live in intakeAnswers. formSubmissionId String? intakeAnswers Json @default("{}") // Local CalendarEvent mirror id + external provider event ids (M06 J.2/J.3). internalCalendarEventId String? externalCalendarEventIds Json @default("[]") cancelledAt DateTime? cancelledByKind AppointmentCancelledByKind? cancellationReason String? rescheduledFromId String? notesInternal String? isDemoData Boolean @default(false) bookingPage BookingPage @relation(fields: [bookingPageId], references: [id], onDelete: Cascade) rescheduledFrom Appointment? @relation("AppointmentReschedule", fields: [rescheduledFromId], references: [id], onDelete: SetNull) rescheduledTo Appointment[] @relation("AppointmentReschedule") @@index([tenantId, providerUserId, startAt]) @@index([tenantId, status, startAt]) @@index([tenantId, kontaktId, startAt]) @@index([bookingPageId, startAt]) }