Files
crosscode-platform/apps/accounts/backend/prisma/schema.prisma
T
joshuaandClaude Opus 5.5 ad488c3280 Accounts 1.9.0, @crosscode/demo 1.0.0: messaging.get, WhatsApp opt-out, demo scenarios
Accounts 1.9.0 (one migration):
- messaging.get (sealed settings) and the messaging.changed hint to products
  that asked, on admin or product changes
- the WhatsApp opt-out for every product: whatsapp.optout, Accounts' own WAWP
  webhook, WhatsAppOptOut kept per number, whatsapp_opted_out in user.updated,
  no WhatsApp from Accounts to an opted-out person, "Turn WhatsApp back on"
- demo scenarios: npm run demo -- --profile <scenario> --force replaces the
  church profile (with an SVG logo and icon at /api/demo-assets/) and the demo
  people, ids kept across loads

@crosscode/demo 1.0.0 (new): the eight scenarios' churches, people and logos.

crosscode script: crosscode demo <church> accounts, --scenario on every demo,
Accounts installs and builds packages/demo.

Checked on PostgreSQL 18: migrations, scenario loads, opt-out, npm run demo.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 11:05:22 +02:00

342 lines
11 KiB
Plaintext

// Accounts 1.0.0 (docs/ACCOUNTS-1.0-PLAN.md). One database per church.
generator client {
provider = "prisma-client"
// Its own output, so hoisted clients of other apps never overwrite it (EVENTS-2.2-PLAN §2).
output = "../src/generated/prisma"
moduleFormat = "esm"
importFileExtension = "js"
}
datasource db {
provider = "postgresql"
}
enum AccountRole {
member
admin
}
enum AccountStatus {
active
deactivated
deleted
}
enum TwoFactorMethod {
email
whatsapp
totp
}
// A person. `id` is the OIDC `sub` every product keys its own records on.
model Account {
id String @id @default(uuid())
name String
givenName String?
familyName String?
email String @unique // lower-case
emailVerified Boolean @default(false)
phone String?
phoneVerified Boolean @default(false)
// Argon2id; bcrypt hashes imported from Cross Events verify too and are re-hashed at sign-in.
passwordHash String?
role AccountRole @default(member)
status AccountStatus @default(active)
twoFactorMethod TwoFactorMethod?
totpSecret String? // encrypted
totpLastStep Int?
newsEmail Boolean @default(false)
newsWhatsApp Boolean @default(false)
newsAskedAt DateTime?
noticeVersionAccepted Int @default(0)
failedSignIns Int @default(0)
lockedUntil DateTime?
lastSignInAt DateTime?
deactivatedAt DateTime?
anonymisedAt DateTime?
// 1.1.0: moved in from a product with 2FA but without recovery codes (Events keys its codes with
// its own secret, so they can't move): new ones are shown after the next sign-in's second step.
recoveryCodesNeeded Boolean @default(false)
// 1.9.0: replied STOP to the church's WhatsApp number (APP-1.0-PLAN §8): no product sends them
// WhatsApp until they turn it back on here. Sent to products as `whatsapp_opted_out`.
whatsappOptedOut Boolean @default(false)
whatsappOptedOutAt DateTime?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
recoveryCodes RecoveryCode[]
tokens EmailToken[]
loginEvents LoginEvent[]
acceptances NoticeAcceptance[]
consentChanges ConsentChange[]
exports DataExport[]
deletionRequests DeletionRequest[]
products ProductPerson[]
@@index([status])
}
model RecoveryCode {
id String @id @default(uuid())
accountId String
account Account @relation(fields: [accountId], references: [id], onDelete: Cascade)
codeHash String @unique
usedAt DateTime?
createdAt DateTime @default(now())
@@index([accountId])
}
// One sign-in in progress (an oidc-provider interaction), through every step: password, 2FA,
// verifying a new email, the news question and the privacy notice. Keyed by the interaction uid.
model PendingSignIn {
uid String @id
accountId String?
stage String // password | two_factor | setup_two_factor | verify_email | notice | news | done
codeHash String?
codeSentAt DateTime?
attempts Int @default(0)
amr String[]
pendingTotpSecret String? // encrypted, while setting up an authenticator app during sign-in
remember Boolean @default(false)
expiresAt DateTime
createdAt DateTime @default(now())
@@index([expiresAt])
}
// Links sent by email: verify an address, reset a password. Only the hash is kept.
model EmailToken {
id String @id @default(uuid())
accountId String
account Account @relation(fields: [accountId], references: [id], onDelete: Cascade)
purpose String // verify_email | reset_password
tokenHash String @unique
// The sign-in the link belongs to, so it carries on to the product afterwards (§5).
interactionUid String?
// 1.1.0: a "set your password" link a product asked for (people.ensure) ends on that product.
returnUrl String?
expiresAt DateTime
usedAt DateTime?
createdAt DateTime @default(now())
@@index([accountId, purpose])
}
// Sign-ins and changes to how someone signs in (the person and admins see these).
model LoginEvent {
id String @id @default(uuid())
accountId String?
account Account? @relation(fields: [accountId], references: [id], onDelete: Cascade)
type String // sign_in | sign_in_failed | locked | password_reset | password_changed | two_factor_on | two_factor_off | recovery_code_used | recovery_codes_replaced | signed_out_everywhere
clientId String? // the product it was for
ip String?
userAgent String?
detail Json?
createdAt DateTime @default(now())
@@index([accountId, createdAt])
}
model PrivacyNotice {
version Int @id
title String
body String
publishedAt DateTime @default(now())
publishedById String?
}
model NoticeAcceptance {
id String @id @default(uuid())
accountId String
account Account @relation(fields: [accountId], references: [id], onDelete: Cascade)
version Int
acceptedAt DateTime @default(now())
@@index([accountId])
}
// Proof of news consent (POPIA): every change, with where it was given.
model ConsentChange {
id String @id @default(uuid())
accountId String
account Account @relation(fields: [accountId], references: [id], onDelete: Cascade)
channel String // email | whatsapp
granted Boolean
source String // sign_up | news_question | account_page | admin
createdAt DateTime @default(now())
@@index([accountId])
}
// Admins viewing or exporting a person.
model AccessLog {
id String @id @default(uuid())
actorId String
targetId String
action String // view | export
ip String?
createdAt DateTime @default(now())
@@index([targetId, createdAt])
}
model AuditLog {
id String @id @default(uuid())
actorId String?
actorRole String
action String
targetType String
targetId String?
metadata Json?
ip String?
createdAt DateTime @default(now())
@@index([createdAt])
}
model AppSetting {
key String @id
value String
updatedAt DateTime @updatedAt
}
// A linked product (Connect, crosscode.link/1). Each is also an OIDC client (§4): client_id is
// installId, the client secret is derived from `secret`, the addresses are fixed paths on `url`.
model ProductLink {
installId String @id
product String // events | website | app
url String
publicKey String
secret String // encrypted
connectedAt DateTime
connectedById String?
// 1.1.0: a move of this product's users into Accounts is under way (import.begin … finish).
importStartedAt DateTime?
// 1.9.0: asked for the church's email and WhatsApp settings (messaging.get), so it's sent
// messaging.changed when they change.
messagingWantedAt DateTime?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
deliveries Delivery[]
people ProductPerson[]
requests ProductRequest[]
}
// 1.1.0: the people a linked product knows (imported by it, added by it, or signed in through it).
// A product's people.* requests only reach these.
model ProductPerson {
id String @id @default(uuid())
linkId String
link ProductLink @relation(fields: [linkId], references: [installId], onDelete: Cascade)
accountId String
account Account @relation(fields: [accountId], references: [id], onDelete: Cascade)
via String // import | ensure | sign_in
createdAt DateTime @default(now())
@@unique([linkId, accountId])
@@index([accountId])
}
// 1.1.0: crosscode.identity-request/1 requests already handled, so a retry gets the same answer.
model ProductRequest {
requestId String @id
linkId String
link ProductLink @relation(fields: [linkId], references: [installId], onDelete: Cascade)
type String
reply Json
createdAt DateTime @default(now())
@@index([createdAt])
}
// 1.9.0: numbers that replied STOP (APP-1.0-PLAN §8), kept with or without an account, so an
// account that gets the number later starts opted out. Removed when the person turns WhatsApp on.
model WhatsAppOptOut {
phone String @id // E.164
keyword String
source String // the product that received it, or "accounts" (its own webhook)
receivedAt DateTime
createdAt DateTime @default(now())
}
model LinkNonce {
nonce String @id
seenAt DateTime @default(now())
@@index([seenAt])
}
// oidc-provider's storage (sessions, grants, codes, interactions…), one row per model and id.
model OidcPayload {
id String @id // "<model>:<id>"
model String
payload Json
grantId String?
userCode String?
uid String?
// Sessions only: whose, so "sign out everywhere" can find them.
accountId String?
expiresAt DateTime?
consumedAt DateTime?
@@index([grantId])
@@index([uid])
@@index([accountId])
@@index([expiresAt])
}
// Back-channel calls to linked products (user.updated, user.deleted, user.export, church.changed),
// retried until they're answered.
model Delivery {
id String @id @default(uuid())
linkId String
link ProductLink @relation(fields: [linkId], references: [installId], onDelete: Cascade)
callId String @unique
type String
path String
body Json
status String @default("pending") // pending | delivered | failed
attempts Int @default(0)
nextAttemptAt DateTime @default(now())
lastError String?
reply Json?
deliveredAt DateTime?
createdAt DateTime @default(now())
@@index([status, nextAttemptAt])
@@index([linkId])
}
// "Download my data": Accounts' part plus each product's (user.export), bundled into one file.
model DataExport {
id String @id @default(uuid())
accountId String
account Account @relation(fields: [accountId], references: [id], onDelete: Cascade)
status String @default("collecting") // collecting | ready | expired
parts Json @default("{}")
file String?
createdAt DateTime @default(now())
readyAt DateTime?
expiresAt DateTime
@@index([accountId])
}
// "Delete my account": deactivated at once, then an admin approves, then every product answers.
model DeletionRequest {
id String @id @default(uuid())
accountId String
account Account @relation(fields: [accountId], references: [id], onDelete: Cascade)
status String @default("waiting_approval") // waiting_approval | approved | done | cancelled
requestedAt DateTime @default(now())
approvedAt DateTime?
approvedById String?
// { [installId]: { status: "pending" | "deleted" | "deferred", reason?, retryAfter? } }
products Json @default("{}")
completedAt DateTime?
@@index([status])
}