// Marketing-Funnel Phase 1 (Phase 05 Task #78). // // `Lead` is the system-of-record for marketing-side prospects coming in via the // public `/api/leads/capture` ingestion endpoint and the `marketing.js` SDK. // Leads are NOT tenant-scoped — they belong to the operator's sales/marketing // org until they convert to a Tenant (status=customer). Attribution columns // preserve first-touch UTM/referrer state so multi-page visits attribute the // same lead source even when the form lives on a deeper page. // // `LeadEvent` is the activity timeline append-log: re-captures, status // transitions, assignment changes, inline notes, conversions. The admin // detail-view renders these chronologically. model Lead { id String @id @default(cuid()) email String firstName String? lastName String? phone String? company String? /// Free-text label identifying the originating surface (e.g. "landing-pro", /// "blog-cta", "webinar-form"). Distinct from utmSource which captures the /// paid-acquisition channel. source String? status LeadStatus @default(new) // --- Attribution (first-touch, never overwritten on re-capture) ----------- utmSource String? utmMedium String? utmCampaign String? utmTerm String? utmContent String? gclid String? fbclid String? referrer String? landingPage String? firstSeenAt DateTime @default(now()) lastTouchAt DateTime @default(now()) /// Lead-scoring numeric — manual override + future automated bumps. score Int @default(0) /// Free-form admin notes payload (markdown body + last-edited metadata). notes Json? /// Tenant created from this lead (status=customer). NULL until conversion. convertedToTenantId String? convertedAt DateTime? /// SHA-256 hash of last-touch IP. Raw IP is never persisted. lastIpHash String? userAgent String? createdAt DateTime @default(now()) updatedAt DateTime @updatedAt events LeadEvent[] @@unique([email]) @@index([status, createdAt(sort: Desc)]) @@index([utmSource, createdAt(sort: Desc)]) @@index([source, createdAt(sort: Desc)]) @@index([createdAt(sort: Desc)]) } model LeadEvent { id String @id @default(cuid()) leadId String /// "captured" | "re-captured" | "status-changed" | "assigned" | "note-added" | "score-changed" | "converted" type String /// Optional acting admin user id; null for SDK-driven events. actorId String? payload Json createdAt DateTime @default(now()) lead Lead @relation(fields: [leadId], references: [id], onDelete: Cascade) @@index([leadId, createdAt(sort: Desc)]) @@index([type, createdAt(sort: Desc)]) } enum LeadStatus { new contacted qualified trial customer churned disqualified } // Marketing-Funnel Phase 2 (Phase 05 Task #79). // // `TenantAcquisition` is the attribution snapshot frozen at the moment a // Tenant is created from a Lead (or manually seeded). `tenantId` is unique // (one acquisition row per tenant); `leadId` may be NULL for tenants created // without a lead origin. UTM / referrer / landing-page columns preserve the // lead's first-touch attribution so funnel reports remain stable even when // the source `Lead` row is later anonymised under GDPR right-to-be-forgotten. // // `MarketingIntegration` is the operator-configured outbound webhook / API // integration. Triggered by outbox-events with eventType matching // `triggerEvents`. Sensitive fields (API keys, webhook-URLs with tokens) // live encrypted inside `configJson` (AES-256-GCM via the existing // CRYPTO_SECRET). model TenantAcquisition { id String @id @default(cuid()) tenantId String @unique leadId String? /// Free-text source label (e.g. "self-service", "admin-manual", /// "partner-referral"). Distinct from utmSource. source String? utmSource String? utmMedium String? utmCampaign String? utmTerm String? utmContent String? gclid String? fbclid String? referrer String? landingPage String? /// First-touch Lead.firstSeenAt copied at conversion time. NULL when /// no Lead linked. capturedAt DateTime? convertedAt DateTime @default(now()) /// Free-form path label ("self-service" | "admin-override" | other). conversionPath String? customFields Json? createdAt DateTime @default(now()) updatedAt DateTime @updatedAt @@index([tenantId]) @@index([leadId]) @@index([utmCampaign, convertedAt(sort: Desc)]) @@index([utmSource, convertedAt(sort: Desc)]) @@index([source, convertedAt(sort: Desc)]) @@index([convertedAt(sort: Desc)]) } model MarketingIntegration { id String @id @default(cuid()) /// "slack" | "discord" | "mailchimp" | "hubspot" | "brevo" | "webhook" type String name String /// JSON object with encrypted-at-rest secrets (e.g. webhookUrl, apiKey, /// listId). Shape is type-specific — see packages/marketing-outbound. configJson Json enabled Boolean @default(true) /// Event-type list this integration listens to. Matched as exact strings /// against outbox eventType (e.g. ["lead.captured","lead.converted_to_trial"]). triggerEvents String[] /// Last delivery diagnostics — small embedded snapshot to surface in UI /// without scanning AuditEvents. lastDeliveredAt DateTime? lastError String? createdAt DateTime @default(now()) updatedAt DateTime @updatedAt @@index([enabled, createdAt(sort: Desc)]) @@index([type, createdAt(sort: Desc)]) }