Signed /installs/ai/status and /installs/ai/complete (protocol 3b): the Hub holds the Claude key, holds each call's worst case against the licence's monthly cap in a locked transaction, calls Claude Haiku 5.5 and meters real cost and charged amount (markup) in rand. Settings -> AI, licence markup override, AI use on the install page. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
230 lines
7.2 KiB
Plaintext
230 lines
7.2 KiB
Plaintext
// Cross Events Hub — customers, licences, installs and their heartbeats.
|
||
|
||
generator client {
|
||
provider = "prisma-client-js"
|
||
}
|
||
|
||
datasource db {
|
||
provider = "postgresql"
|
||
}
|
||
|
||
// Someone who logs in to the Hub (Joshua, later support staff). Two-factor (authenticator
|
||
// app) is required: totpSecret is set up on first login.
|
||
model AdminUser {
|
||
id String @id @default(uuid())
|
||
email String @unique
|
||
name String
|
||
password String
|
||
// AES-GCM encrypted (utils/crypto.js); null until two-factor is set up.
|
||
totpSecret String?
|
||
totpEnabled Boolean @default(false)
|
||
// Bumped to sign out everywhere.
|
||
tokenVersion Int @default(0)
|
||
lastLoginAt DateTime?
|
||
createdAt DateTime @default(now())
|
||
auditLogs AuditLog[]
|
||
}
|
||
|
||
model Customer {
|
||
id String @id @default(uuid())
|
||
name String
|
||
contactName String?
|
||
email String?
|
||
phone String?
|
||
notes String?
|
||
createdAt DateTime @default(now())
|
||
updatedAt DateTime @updatedAt
|
||
licences Licence[]
|
||
installs Install[]
|
||
}
|
||
|
||
enum LicenceStatus {
|
||
active
|
||
suspended
|
||
revoked
|
||
}
|
||
|
||
model Licence {
|
||
id String @id @default(uuid())
|
||
customerId String
|
||
customer Customer @relation(fields: [customerId], references: [id], onDelete: Restrict)
|
||
// CEH-XXXX-XXXX-XXXX-XXXX, generated by the Hub.
|
||
key String @unique
|
||
plan String @default("standard")
|
||
status LicenceStatus @default(active)
|
||
// Null = no end date.
|
||
expiresAt DateTime?
|
||
graceDays Int @default(14)
|
||
// Cross Events 2.0: monthly AI spend cap (in cents, rand), counted in charged rand. 0 = AI off.
|
||
aiMonthlyCapCents Int @default(0)
|
||
// Markup on the real AI cost, in %. Null = the Hub default (Settings → AI).
|
||
aiMarkupPercent Int?
|
||
notes String?
|
||
createdAt DateTime @default(now())
|
||
updatedAt DateTime @updatedAt
|
||
// One licence = one install.
|
||
install Install?
|
||
}
|
||
|
||
model Install {
|
||
id String @id @default(uuid())
|
||
customerId String
|
||
customer Customer @relation(fields: [customerId], references: [id], onDelete: Restrict)
|
||
licenceId String? @unique
|
||
licence Licence? @relation(fields: [licenceId], references: [id], onDelete: SetNull)
|
||
name String
|
||
url String
|
||
// The install's HMAC secret, AES-GCM encrypted: the Hub needs it to check signatures.
|
||
secret String
|
||
version String?
|
||
protocolVersion Int @default(1)
|
||
adminContact Json?
|
||
server Json?
|
||
timezone String?
|
||
features Json?
|
||
// From the latest heartbeat.
|
||
lastHeartbeatAt DateTime?
|
||
health Json?
|
||
stats Json?
|
||
notes String?
|
||
registeredAt DateTime @default(now())
|
||
// Set when Joshua releases the licence (e.g. the church moved domain): heartbeats are refused.
|
||
releasedAt DateTime?
|
||
heartbeats Heartbeat[]
|
||
events InstallEvent[]
|
||
aiUsage AiUsage[]
|
||
|
||
@@index([lastHeartbeatAt])
|
||
}
|
||
|
||
enum AiUsageStatus {
|
||
// The worst case is held against the cap while the call runs.
|
||
reserved
|
||
done
|
||
failed
|
||
}
|
||
|
||
// One AI call from an install (Cross Events 2.0). No prompt or response text is kept — only what's
|
||
// needed to meter it. Amounts are in millicents (1/1000 of a cent, rand): a call costs a few cents.
|
||
model AiUsage {
|
||
id String @id @default(uuid())
|
||
installId String
|
||
install Install @relation(fields: [installId], references: [id], onDelete: Cascade)
|
||
helper String
|
||
model String
|
||
// The billing month, YYYY-MM in South African time.
|
||
month String
|
||
status AiUsageStatus @default(reserved)
|
||
inputTokens Int @default(0)
|
||
outputTokens Int @default(0)
|
||
cacheReadTokens Int @default(0)
|
||
cacheWriteTokens Int @default(0)
|
||
// What Claude cost (real) and what the church is charged (real × (1 + markup)).
|
||
realMillicents Int @default(0)
|
||
chargedMillicents Int @default(0)
|
||
markupPercent Int
|
||
// Rand per US dollar used for this call.
|
||
usdZar Float
|
||
createdAt DateTime @default(now())
|
||
|
||
@@index([installId, month])
|
||
@@index([month])
|
||
}
|
||
|
||
// Every heartbeat for 30 days, then one per install per day.
|
||
model Heartbeat {
|
||
id String @id @default(uuid())
|
||
installId String
|
||
install Install @relation(fields: [installId], references: [id], onDelete: Cascade)
|
||
receivedAt DateTime @default(now())
|
||
version String?
|
||
health Json?
|
||
stats Json?
|
||
|
||
@@index([installId, receivedAt])
|
||
}
|
||
|
||
enum EventLevel {
|
||
info
|
||
warning
|
||
critical
|
||
}
|
||
|
||
// Things that happened to an install: alerts (offline, backup failing, disk low, errors,
|
||
// licence expiring), version changes, registration. An alert stays open (resolvedAt null)
|
||
// until the condition clears, so it's emailed once.
|
||
model InstallEvent {
|
||
id String @id @default(uuid())
|
||
installId String
|
||
install Install @relation(fields: [installId], references: [id], onDelete: Cascade)
|
||
type String
|
||
level EventLevel @default(info)
|
||
message String
|
||
createdAt DateTime @default(now())
|
||
resolvedAt DateTime?
|
||
|
||
@@index([installId, createdAt])
|
||
@@index([type, resolvedAt])
|
||
}
|
||
|
||
model AuditLog {
|
||
id String @id @default(uuid())
|
||
actorId String?
|
||
actor AdminUser? @relation(fields: [actorId], references: [id], onDelete: SetNull)
|
||
action String
|
||
target String?
|
||
metadata Json?
|
||
ip String?
|
||
createdAt DateTime @default(now())
|
||
|
||
@@index([createdAt])
|
||
}
|
||
|
||
model Setting {
|
||
key String @id
|
||
value String
|
||
}
|
||
|
||
// A Cross Events version and what's new in it, shown to installs on an older version so the church
|
||
// can decide whether to ask for the update. The highest version is the latest (kept in the
|
||
// latest_version setting). Urgent releases (security or serious bug fixes) can't be put off.
|
||
model Release {
|
||
id String @id @default(uuid())
|
||
version String @unique
|
||
// Plain text; lines starting with "- " show as a list.
|
||
notes String @default("")
|
||
urgent Boolean @default(false)
|
||
releasedAt DateTime @default(now())
|
||
createdAt DateTime @default(now())
|
||
updatedAt DateTime @updatedAt
|
||
}
|
||
|
||
enum NoticeLevel {
|
||
info
|
||
maintenance
|
||
warning
|
||
}
|
||
|
||
// A message from Joshua to churches: a banner on their site for site admins and supervisors
|
||
// (between startsAt and endsAt), an email to the customer and the install's admin contact, or both.
|
||
model Notice {
|
||
id String @id @default(uuid())
|
||
title String
|
||
message String
|
||
level NoticeLevel @default(info)
|
||
// Every install, or only the ones listed.
|
||
allInstalls Boolean @default(true)
|
||
installIds String[] @default([])
|
||
showBanner Boolean @default(true)
|
||
// Null = straight away / until ended.
|
||
startsAt DateTime?
|
||
endsAt DateTime?
|
||
// Set when the email went out (sent once, when the notice is created).
|
||
emailedAt DateTime?
|
||
emailedTo Int @default(0)
|
||
createdAt DateTime @default(now())
|
||
updatedAt DateTime @updatedAt
|
||
|
||
@@index([endsAt])
|
||
}
|