> ## 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.

# Architecture

> The components of digital contracting — the portal wizard, the contract backend, the asynchronous workers and the signature engine — and the boundaries between them.

## Components

```mermaid theme={null}
flowchart LR
    subgraph portal [frontend/portal-customer]
        wizard["Contract wizard<br/>/[locale]/contract/[contractId]"]
    end
    subgraph api [horizon-api]
        register["Registration<br/>POST /api/v3/auth/register/contract"]
        bff["Contract backend<br/>/api/v3/contract/*"]
        gestures["Signer gestures<br/>/api/v3/signature/*/signer/*"]
        workers["Contract workers<br/>A · B · enterprise · C · email"]
        engine["Signature engine<br/>internal-signer"]
    end
    subgraph external [External services]
        floid["Floid"]
        fintoc["Fintoc"]
        pusher["Pusher"]
    end
    store[("Supabase<br/>Postgres + contracts bucket")]
    ops["Maihue operations"]

    register --> store
    wizard -->|"HTTPS + contract session"| bff
    wizard -->|"OTP and sign"| gestures
    bff --> floid
    bff --> fintoc
    bff -->|"enqueue"| workers
    gestures --> engine
    workers --> engine
    ops -->|"review"| bff
    bff --> store
    workers --> store
    engine --> store
    workers -->|"publish"| pusher
    bff -->|"publish"| pusher
    pusher -.->|"events"| wizard
```

| Component | Responsibility | Code |
| - | - | - |
| Contract wizard | Renders the step the backend decides, sends each step and listens to realtime events | `frontend/portal-customer/app/[locale]/(public)/contract/[contractId]/`, `frontend/portal-customer/app/components/digital-contract/` |
| Registration | Creates or reuses the account and the subscription, writes the signer roster and a `draft` contract, then sends the one-time link | `app/api/v3/auth/register/contract/`, `lib/endpoint/auth/register-contract/` |
| Contract backend | One endpoint per wizard step plus state, review, cancel and recovery | `app/api/v3/contract/[document_id]/*`, `lib/endpoint/contract-*` |
| Signer gestures | Requests the one-time code and registers the sign or reject gesture | `app/api/v3/signature/[document_id]/signer/[signer_id]/`, `lib/endpoint/signature-signing/` |
| Contract workers | Generate the PDFs, open the signature group, run Maihue's countersignature, activate the contract and send the confirmation email | `lib/queue/contract/handlers/` |
| Signature engine | One-time codes, signer evidence, PDF sealing and the audit chain | `lib/integration/signature-provider/internal-signer/` |
| Floid | Identity verification against the Chilean civil registry | `lib/endpoint/integration/floid/` |
| Fintoc | Card and bank-account mandate enrollment | `lib/endpoint/payment-method-enrollment/providers/fintoc/` |
| Pusher | Realtime events on `private-contract-{id}` (mirrored on `private-account-{id}`) | `lib/integration/pusher/` |
| Supabase | Contract, signature and identity tables; original and signed PDFs in the private `contracts` bucket | `lib/common/storage/contracts-bucket.ts` |

Backend paths are relative to `backend/horizon-api/` unless they start with
`frontend/`.

## The backend decides the step

The backend decides the stage; the portal only adds local views on top of
it (looking back at earlier steps, the welcome and the password). Every read
of `GET /api/v3/contract/{id}/state` returns a `ContractState` whose `stage`
is derived on the server from the contract status and the customer's
progress; a cancelled, rejected or expired contract answers `400`:

```text theme={null}
NOT_STARTED → IDENTITY → SUMMARY → PAYMENT → SIGNATURE → COMPLETED
```

Most wizard actions answer with a small patch (`stage` and flags); rewind and
creating the password return the full state, and starting the Fintoc
enrollment returns the Fintoc URL. Realtime events then bring the rest of the
state, so a tab that reloads always lands on the right step. See
[Flows](https://docs.bbrands.io/digital-contracting/flows.md) for what each stage does.

## Asynchronous workers

Long work never runs inside a portal request. It runs in five workers on the
contract queue, delivered through the platform queue transport and tracked in
the queue ledger:

| Worker | Topic | What it does |
| - | - | - |
| A | `contract-pdf-generation` | Renders the contract and annex PDFs and stores the originals |
| B | `contract-send-to-signer` | Opens the signature group with the customer signers and Maihue's representatives |
| Enterprise | `contract-enterprise-sign` | Applies Maihue's countersignature and seals the PDFs |
| C | `contract-activated` | Stores the sealed version, activates the contract and the subscription |
| Email | `contract-email` | Sends the signed-contract confirmation |

The signing pipeline starts when the customer confirms the payment method
(Worker A may prepare the PDFs earlier, when the period is picked). The
enterprise worker starts when operations approve the review (or, for
workflows with `company_phase = first`, when the signature group opens).

## Realtime

The portal subscribes to the private channel `private-contract-{id}`. The
backend publishes a small patch after most wizard steps and a full state
snapshot after a rewind and after the holder's password; a patch with the
documents and seats when the signature is requested; and a full snapshot on
status changes, when the contract is signed and on the Fintoc enrollment
events. The channel, the authorizer and the event contract are
documented in [Realtime — Channels & Events](https://docs.bbrands.io/realtime/channels-and-events.md).

## Boundaries

| Concern | Owner |
| - | - |
| When to sign, for which contract, and the review before Maihue countersigns | Digital contracting |
| How a signature is captured, evidenced and sealed | Signature engine |
| Identity documents and the person account a signer belongs to | Account and identity |
| Table columns and types | Generated schema pages: [Contract](https://docs.bbrands.io/schemas/contract/contract.md), [Contract signer](https://docs.bbrands.io/schemas/contract/contract-signer.md), [Signature](https://docs.bbrands.io/schemas/signature/signature.md) |

> [!NOTE]
>
> Contracting reaches the signature engine mainly through
> `internalSignerClient` and `resolveSignatureWorkflow`. Every signature
> document references the contract and its annex, so the engine and the
> contract domain share their tables.
> See [MaihueSigner — Consumer integration](https://docs.bbrands.io/signature-engine/consumer-integration.md).


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