173 lines
6.3 KiB
Plaintext
173 lines
6.3 KiB
Plaintext
// AK-P15a (P15) Telephony (Aircall) — typed provider-neutral domain model.
|
|
//
|
|
// AD-21: this module owns its schema. Cross-module identifiers such as
|
|
// `dataConnectionId`, `tenantUserId`, and CRM match ids remain bare scalars so
|
|
// no back-relations are required in shared schema files. The generated RLS
|
|
// contract enforces same-tenant ownership for every security-critical scalar
|
|
// reference on insert and update.
|
|
//
|
|
// OAuth credentials and the raw webhook token live only in the Apps Engine's
|
|
// encrypted DataConnection credentials. TelephonyConnection stores the
|
|
// one-way webhook token hash needed by the public bootstrap resolver; secrets
|
|
// are never duplicated in this module.
|
|
|
|
enum TelephonyProvider {
|
|
aircall
|
|
hallopetra
|
|
}
|
|
|
|
enum TelephonyBindingSource {
|
|
email_auto
|
|
manual
|
|
}
|
|
|
|
enum TelephonyCallDirection {
|
|
inbound
|
|
outbound
|
|
}
|
|
|
|
enum TelephonyCallStatus {
|
|
ringing
|
|
answered
|
|
completed
|
|
missed
|
|
failed
|
|
}
|
|
|
|
enum TelephonyMatchConfidence {
|
|
hard
|
|
soft
|
|
manual
|
|
none
|
|
}
|
|
|
|
enum TelephonyWebhookProcessingStatus {
|
|
pending
|
|
processing
|
|
completed
|
|
failed
|
|
skipped
|
|
}
|
|
|
|
/// Tenant-specific provider extension of one Apps Engine DataConnection.
|
|
/// Credentials and lifecycle status remain canonical on DataConnection.
|
|
model TelephonyConnection {
|
|
id String @id @default(cuid())
|
|
createdAt DateTime @default(now())
|
|
updatedAt DateTime @updatedAt
|
|
tenantId String
|
|
dataConnectionId String @unique
|
|
provider TelephonyProvider @default(aircall)
|
|
providerAccountId String?
|
|
providerAccountName String?
|
|
webhookId String?
|
|
// SHA-256 one-way lookup digest only. The raw token remains encrypted in the
|
|
// linked DataConnection and must never be serialized or persisted in events.
|
|
webhookTokenHash String? @unique
|
|
|
|
userBindings TelephonyUserBinding[]
|
|
callSessions TelephonyCallSession[]
|
|
webhookEvents TelephonyWebhookEvent[]
|
|
|
|
// One historical slot per provider and tenant. The service layer permits at
|
|
// most one active telephony provider while revoked slots remain auditable.
|
|
@@unique([tenantId, provider])
|
|
@@index([tenantId, dataConnectionId])
|
|
@@index([tenantId, providerAccountId])
|
|
}
|
|
|
|
/// Mapping between a HiveGrid TenantUser and a provider user (Aircall in v1).
|
|
model TelephonyUserBinding {
|
|
id String @id @default(cuid())
|
|
createdAt DateTime @default(now())
|
|
updatedAt DateTime @updatedAt
|
|
tenantId String
|
|
connectionId String
|
|
tenantUserId String
|
|
providerUserId String
|
|
providerEmail String?
|
|
providerDisplayName String?
|
|
defaultProviderNumberId String?
|
|
source TelephonyBindingSource @default(manual)
|
|
|
|
connection TelephonyConnection @relation(fields: [connectionId], references: [id], onDelete: Cascade)
|
|
|
|
@@unique([connectionId, tenantUserId])
|
|
@@unique([connectionId, providerUserId])
|
|
@@index([tenantId, tenantUserId])
|
|
@@index([tenantId, providerEmail])
|
|
}
|
|
|
|
/// Normalized inbound or outbound call, optionally matched to one CRM entity.
|
|
model TelephonyCallSession {
|
|
id String @id @default(cuid())
|
|
createdAt DateTime @default(now())
|
|
updatedAt DateTime @updatedAt
|
|
tenantId String
|
|
connectionId String
|
|
providerCallId String
|
|
providerUserId String?
|
|
tenantUserId String?
|
|
direction TelephonyCallDirection
|
|
status TelephonyCallStatus
|
|
fromNumber String
|
|
toNumber String
|
|
startedAt DateTime
|
|
answeredAt DateTime?
|
|
endedAt DateTime?
|
|
durationSeconds Int?
|
|
matchedKontaktId String?
|
|
matchedLeadId String?
|
|
matchedProjektId String?
|
|
matchConfidence TelephonyMatchConfidence @default(none)
|
|
manualOverrideAt DateTime?
|
|
manualOverrideUserId String?
|
|
noteMarkdown String?
|
|
recordingUrl String?
|
|
isDemoData Boolean @default(false)
|
|
archivedAt DateTime?
|
|
|
|
connection TelephonyConnection @relation(fields: [connectionId], references: [id], onDelete: Cascade)
|
|
webhookEvents TelephonyWebhookEvent[]
|
|
|
|
@@unique([connectionId, providerCallId])
|
|
@@index([tenantId, startedAt(sort: Desc)])
|
|
@@index([tenantId, status, startedAt(sort: Desc)])
|
|
@@index([tenantId, matchedKontaktId, startedAt(sort: Desc)])
|
|
@@index([tenantId, matchedLeadId, startedAt(sort: Desc)])
|
|
@@index([tenantId, matchedProjektId, startedAt(sort: Desc)])
|
|
@@index([tenantId, tenantUserId, startedAt(sort: Desc)])
|
|
}
|
|
|
|
/// Redacted provider webhook envelope. Provider payload data is immutable;
|
|
/// only processing metadata changes. Completed rows expire after 30 days and
|
|
/// failed rows after 90 days through the tenant-scoped retention worker.
|
|
model TelephonyWebhookEvent {
|
|
id String @id @default(cuid())
|
|
receivedAt DateTime @default(now())
|
|
tenantId String
|
|
connectionId String
|
|
callSessionId String?
|
|
eventType String
|
|
dedupeKey String
|
|
authenticationValid Boolean @default(true)
|
|
notificationPayload Json
|
|
processingStatus TelephonyWebhookProcessingStatus @default(pending)
|
|
// A renewable, crash-recoverable worker claim. A redelivery may reclaim a
|
|
// processing event only after this lease expires.
|
|
processingLeaseId String?
|
|
processingLeaseUntil DateTime?
|
|
processingAttempt Int @default(0)
|
|
processingErrorText String?
|
|
processedAt DateTime?
|
|
|
|
connection TelephonyConnection @relation(fields: [connectionId], references: [id], onDelete: Cascade)
|
|
callSession TelephonyCallSession? @relation(fields: [callSessionId], references: [id], onDelete: SetNull)
|
|
|
|
@@unique([tenantId, dedupeKey])
|
|
@@index([tenantId, processingStatus, receivedAt])
|
|
@@index([tenantId, processingStatus, processingLeaseUntil])
|
|
@@index([tenantId, connectionId, receivedAt(sort: Desc)])
|
|
@@index([receivedAt])
|
|
}
|