> ## Documentation Index
> Fetch the complete documentation index at: https://docs.bbrands.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Data model

> How the tables behind digital contracting relate — contract, signers, annex, subscription, identity and the signature group — and which step writes each one.

This page shows how the tables relate and which step writes them. Columns,
types and constraints are on the generated schema pages linked below, which
always match the current database.

## Relationships

```mermaid theme={null}
erDiagram
    account ||--o{ contract : owns
    account ||--o{ subscription : owns
    contract }o--|| contract_status : "is in"
    contract }o--o| contract_review_status : "reviewed as"
    contract }o--o| signature_workflow : "signs with"
    contract ||--|{ contract_signer : "is signed by"
    contract_signer }o--o| account : "person account"
    contract ||--o{ contract_annex : has
    contract_annex ||--o{ contract_annex_subscription_product : lists
    contract_annex_subscription_product }o--|| subscription_product : "product"
    subscription ||--o{ subscription_product : has
    contract ||--o{ contract_notification : "notified by"
    signature ||--|{ signature_document : groups
    signature_document }o--o| contract : references
    signature_document }o--o| contract_annex : references
    signature_document ||--o{ signature_document_version : versions
    signature ||--|{ signature_signer : seats
    signature_signer }o--o| account : "signer account"
    user_dni ||--o{ user_identity_verification : "verified by"
    user_dni }o--o| account : "person account"
```

## Contract tables

These belong to digital contracting.

| Table | What it holds | Schema |
| - | - | - |
| `contract` | One row per contract: owning account, business unit, status, signature workflow, when the wizard started, the frozen accepted summary, the billing period and payment confirmation, and the review decision | [Contract](https://docs.bbrands.io/schemas/contract/contract.md) |
| `contract_status` | Status catalogue (`draft` … `active`, plus `rejected`, `cancelled`, `expired`) | [Contract status](https://docs.bbrands.io/schemas/contract/contract-status.md) |
| `contract_review_status` | Review catalogue (`pending`, `approve`, `reject`) | [Contract review status](https://docs.bbrands.io/schemas/contract/contract-review-status.md) |
| `contract_signer` | The customer signers of a contract: login, person account, role (`primary` or `signer`) and signing order (unique per contract) | [Contract signer](https://docs.bbrands.io/schemas/contract/contract-signer.md) |
| `contract_annex` | The annex of a contract, with its own status | [Contract annex](https://docs.bbrands.io/schemas/contract/contract-annex.md) |
| `contract_annex_subscription_product` | The subscription products an annex covers | [Contract annex subscription product](https://docs.bbrands.io/schemas/contract/contract-annex-subscription-product.md) |
| `contract_notification` | Links a contract (and optionally a signer) to a notification, with its purpose | [Contract notification](https://docs.bbrands.io/schemas/contract/contract-notification.md) |

A later company administrator's summary confirmation is not a column: it is
recorded as a `contract.signer_summary_accepted` event in the queue ledger.

## Identity tables

These belong to account and identity; contracting writes and reads them.

| Table | What it holds | Schema |
| - | - | - |
| `user_dni` | The signer's RUT, one row per user and document type, linked to the person account | [User DNI](https://docs.bbrands.io/schemas/user/user-dni.md) |
| `user_identity_verification` | Each Floid verification: status, provider, case id, attempts, `verified_at`, `expires_at` | [User identity verification](https://docs.bbrands.io/schemas/user/user-identity-verification.md) |
| `account`, `account_user` | The person or company account and its members | [Account](https://docs.bbrands.io/schemas/account/account.md), [Account user](https://docs.bbrands.io/schemas/account/account-user.md) |

## Signature tables

These belong to the signature engine; contracting creates them through the
engine and reads them to build the `ContractState`.

| Table | Link to contracting | Schema |
| - | - | - |
| `signature` | One signature group per send, with the workflow snapshot it was opened with | [Signature](https://docs.bbrands.io/schemas/signature/signature.md) |
| `signature_document` | Two rows per group (the contract and the annex, told apart by `name`); each references the contract and the current annex (a database check requires at least one) | [Signature document](https://docs.bbrands.io/schemas/signature/signature-document.md) |
| `signature_document_version` | The sealed PDF, written by Worker C as version 2; the originals live only in the bucket | [Signature document version](https://docs.bbrands.io/schemas/signature/signature-document-version.md) |
| `signature_signer` | One seat per signer: customer seats come from `contract_signer`, enterprise seats from the workflow representatives | [Signature signer](https://docs.bbrands.io/schemas/signature/signature-signer.md) |
| `signature_signer_otp`, `signature_signer_evidence` | Signing codes (stored hashed) and the evidence of each gesture | [Signer OTP](https://docs.bbrands.io/schemas/signature/signature-signer-otp.md), [Signer evidence](https://docs.bbrands.io/schemas/signature/signature-signer-evidence.md) |
| `signature_provider_request`, `signature_provider_event` | The request and its append-only audit chain | [Provider request](https://docs.bbrands.io/schemas/signature/signature-provider-request.md), [Provider event](https://docs.bbrands.io/schemas/signature/signature-provider-event.md) |
| `signature_workflow`, `signature_workflow_enterprise_signer`, `signature_enterprise_signer` | The workflow per business unit and Maihue's representatives | [Signature workflow](https://docs.bbrands.io/schemas/signature/signature-workflow.md) |

## What each step writes

| Step | Writes |
| - | - |
| Registration | `account` (with its base role in `account_role`), `account_user`, the auth user, `user_metadata`, `contract` (`draft`), `contract_signer`, `subscription` (pending) |
| Start | `contract.started_at` |
| Identity | `user_dni`, `user_identity_verification`, the `user.identity_verified` ledger event |
| Summary | `contract.accept_terms_at` and the frozen summary |
| Later administrator summary | The `contract.signer_summary_accepted` ledger event |
| Payment | Payment method enrollment tables, `contract.billing_period`, `contract.payment_method_confirmed_at` |
| Pipeline start | `contract_annex` (with no subscription products); the subscription's payment method; contract status |
| Worker A | The original PDFs in the `contracts` bucket |
| Worker B | `signature`, `signature_document`, `signature_signer`, `signature_provider_request`, `signature_provider_event`; contract status |
| Customer signing | `signature_signer_otp`, `signature_signer_evidence`, `signature_provider_event`, `signature_signer`, the `signature` status; contract status and `contract.review_status` (`pending`). With `company_phase = first`, the last signature also seals the PDFs into `contracts` and sets the contract to `signed` |
| Review | `contract.review_status`, `review_by`, `review_reason`; contract status; on reject, the `signature` status (`cancelled`) |
| Enterprise worker | Signs the enterprise `signature_signer` seats (created by Worker B), `signature_signer_evidence`, `signature_provider_event`; only when the group is complete: the sealed PDFs in `contracts` and the contract status (`signed`). With `first` it opens the customer phase instead |
| Worker C | `signature_document_version` (v2); contract and subscription status |

Contract PDFs are stored in the private `contracts` bucket, under an
`original` and a `signed` folder per contract.

## Code

| Concern | Path |
| - | - |
| Drizzle schemas | `database/horizon-drizzle/database/schemas/(contract)/`, `(signature)/`, `(user)/`, `(account)/`, `(subscription)/` |
| Migrations | `database/horizon-drizzle/drizzle/` |
| Status catalogue constants | `backend/horizon-api/lib/common/catalog/contract-flow.constants.ts` |
| Contract bucket paths | `backend/horizon-api/lib/common/storage/contracts-bucket.ts` |


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.