// VR-P22b ยท Public API auth, scopes and webhooks. // // AD-21: public API owns this schema file. Shared core models like ApiKey and // Tenant are referenced by bare String scalars unless a relation is already // owned by the target module. enum PublicWebhookAuthMode { api_key hmac url_token } enum PublicWebhookEndpointStatus { active paused archived } enum PublicWebhookDeliveryStatus { queued delivering success failed retrying disabled } model PublicIncomingWebhookEndpoint { id String @id @default(cuid()) createdAt DateTime @default(now()) updatedAt DateTime @updatedAt tenantId String slug String name String authMode PublicWebhookAuthMode apiKeyId String? tokenHash String? tokenPrefix String? tokenLastFour String? hmacSecretHash String? hmacSecretPrefix String? hmacSecretLastFour String? payloadMapping Json @default("{}") autoMatchRules Json @default("{}") status PublicWebhookEndpointStatus @default(active) pausedAt DateTime? archivedAt DateTime? isDemoData Boolean @default(false) ingestionLogs PublicWebhookIngestionLog[] @@unique([tenantId, slug]) @@index([tenantId, status, archivedAt]) @@index([apiKeyId]) } model PublicWebhookIngestionLog { id String @id @default(cuid()) createdAt DateTime @default(now()) tenantId String endpointId String? endpointSlug String authMode PublicWebhookAuthMode? requestId String? sourceIp String? status Int result String payloadHash String? payloadPreview Json? leadId String? matchedKontaktId String? automationRunId String? error String? endpoint PublicIncomingWebhookEndpoint? @relation(fields: [endpointId], references: [id], onDelete: SetNull) @@index([tenantId, createdAt]) @@index([tenantId, endpointId, createdAt]) @@index([tenantId, leadId]) } model PublicWebhookSubscription { id String @id @default(cuid()) createdAt DateTime @default(now()) updatedAt DateTime @updatedAt tenantId String name String targetUrl String eventTypes Json @default("[]") signingSecretHash String signingSecretPrefix String signingSecretLastFour String status PublicWebhookEndpointStatus @default(active) failureCount Int @default(0) lastDeliveredAt DateTime? lastErrorAt DateTime? autoDisabledAt DateTime? pausedAt DateTime? archivedAt DateTime? createdByUserId String? isDemoData Boolean @default(false) deliveries PublicWebhookDeliveryLog[] @@unique([tenantId, name]) @@index([tenantId, status, archivedAt]) } model PublicWebhookDeliveryLog { id String @id @default(cuid()) createdAt DateTime @default(now()) updatedAt DateTime @updatedAt tenantId String subscriptionId String eventType String targetUrl String status PublicWebhookDeliveryStatus @default(queued) httpStatus Int? attempt Int @default(1) maxAttempts Int @default(5) nextAttemptAt DateTime? idempotencyKey String payloadHash String error String? responsePreview Json? sentAt DateTime? subscription PublicWebhookSubscription @relation(fields: [subscriptionId], references: [id], onDelete: Cascade) @@unique([subscriptionId, idempotencyKey]) @@index([tenantId, status, nextAttemptAt]) @@index([tenantId, eventType, createdAt]) }