// 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? createdAt DateTime @default(now()) updatedAt DateTime @updatedAt recoveryCodes RecoveryCode[] tokens EmailToken[] loginEvents LoginEvent[] acceptances NoticeAcceptance[] consentChanges ConsentChange[] exports DataExport[] deletionRequests DeletionRequest[] @@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? 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? createdAt DateTime @default(now()) updatedAt DateTime @updatedAt deliveries Delivery[] } 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 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]) }