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

# Workflows

> How MaihueSigner configures the company side of a signature: workflows per business unit, the phase order, attended and unattended representative seats, the quorum, the frozen snapshot and the admin API.

A **workflow** holds the rules for the company side of a signature: which
representatives sign, how many of them are needed, whether they sign in
person or unattended, and whether the company signs before or after the
customers. The customer side comes from the consumer; the company side always
comes from a workflow.

## What a workflow holds

| Field | Values | Meaning |
| - | - | - |
| Scope | `business_unit` | Each business unit has its own workflows and at most one active default |
| `company_phase` | `first`, `last` | Whether the company side signs before the customers or after them |
| `company_quorum` | `all`, `count` | Every required representative signs, or only the first N |
| `company_quorum_count` | number | N, when the quorum is `count` |
| Memberships | representative, `signing_mode`, `signing_order`, `is_required` | Which representatives belong to the workflow, how each one signs and in which order |

A representative comes from the catalogue `signature_enterprise_signer`. Each
one has a display name, a credential reference for its signing certificate,
a reference to the proof that it represents the company and, for unattended
signing, a reference to its recorded consent.

| Signing mode | How the seat signs |
| - | - |
| `attended` | The representative receives a one-time code and signs like a customer |
| `unattended` | A background job signs with the representative's certificate, with no one present |

The seeded defaults (home, horeca and business) use `company_phase = last`
and a `count` quorum of one representative; the admin API can change them.

## Resolution and snapshot

```mermaid theme={null}
flowchart TD
    open["A consumer opens a group"] --> assigned{"Workflow assigned<br/>to the contract?"}
    assigned -->|yes| wf["Use that workflow"]
    assigned -->|no| default["Use the active default<br/>for the business unit"]
    wf --> seats["Keep only usable representatives"]
    default --> seats
    seats --> quorum["Pick the seats<br/>the quorum needs"]
    quorum --> snapshot["Freeze the workflow as<br/>signature.workflow_snapshot"]
```

1. **Pick the workflow.** The workflow assigned to the contract wins;
   otherwise the active default of its business unit. With neither, the group
   cannot open.
2. **Keep usable representatives.** Inactive representatives, or those whose
   validity has ended, are skipped. An unattended seat without its consent
   reference stops the group from opening, and so does a workflow with no
   usable representative left.
3. **Apply the quorum.** With `count`, the first N required seats by signing
   order are used. The group cannot open if the workflow has fewer. With
   `all`, every required seat is used, or every seat when none is required.
4. **Freeze it.** The resolved workflow, with its seats, is stored on the
   group as `workflow_snapshot`. Changes to the workflow itself do not alter
   a group that is already open. The representatives are a different matter:
   when an unattended seat signs, the engine checks the representative's
   current status, validity and credential, and sealing uses its current
   credential. A representative revoked while a group is open stops that
   group's unattended signature while that seat is still pending.

## Phase order

The phase gate decides which side may act now.

| `company_phase` | Order | A code request or a signature from the other side before its turn |
| - | - | - |
| `last` | Customers first; the company side opens when every customer seat has signed | Refused with `409` |
| `first` | Company side first; customer seats open when the company side is complete | Refused with `409` |

When both sides are complete, the engine seals the PDFs.

> [!NOTE]
>
> A consumer can hold the company phase back. Digital contracting parks a
> `last` group until Maihue operations approve the contract, and only then
> starts the unattended company signature. See
> [Digital contracting — Signing](https://docs.bbrands.io/digital-contracting/signing.md#operations-review).

## Admin API

| Method | Path | Permission | Purpose |
| - | - | - | - |
| `GET` | `/api/v3/signature/workflow` | `signature-workflow:find-all` | List workflows |
| `POST` | `/api/v3/signature/workflow` | `signature-workflow:resolve`, `signature-workflow:create` | Resolve the workflow for a `business_unit_id` and an optional `contract_workflow_id`, or create a workflow |
| `PATCH`, `DELETE` | `/api/v3/signature/workflow/{id}` | `signature-workflow:update`, `signature-workflow:delete` | Change or remove a workflow |
| `POST` | `/api/v3/signature/workflow/enterprise-signer` | `signature-workflow-enterprise-signer:create` | Add a representative to a workflow |
| `DELETE` | `/api/v3/signature/workflow/enterprise-signer/{id}` | `signature-workflow-enterprise-signer:delete` | Remove a representative from a workflow |
| `GET`, `POST` | `/api/v3/signature/enterprise-signer` | `signature-enterprise-signer:find-all`, `signature-enterprise-signer:create` | List or add representatives |
| `PATCH` | `/api/v3/signature/enterprise-signer/{id}` | `signature-enterprise-signer:update` | Change a representative |
| `POST` | `/api/v3/signature/enterprise-signer/{id}/revoke` | `signature-enterprise-signer:revoke` | Revoke a representative; open groups that still need it can no longer complete their unattended signature |

Replacing a representative is a revoke plus a new representative added to
the workflow. New groups pick up the new one; an open group whose unattended seat for the
revoked one is still pending cannot complete its signature.

## Code

| Concern | Path |
| - | - |
| Resolution, seat selection and snapshot | `lib/integration/signature-provider/internal-signer/signature-workflow.resolve.ts` |
| Phase gate | `lib/integration/signature-provider/internal-signer/signature-phase.service.ts` |
| Admin service | `lib/endpoint/signature-workflow-admin/` |
| Admin routes | `app/api/v3/signature/workflow/`, `app/api/v3/signature/enterprise-signer/` |
| Catalogue values | `lib/common/catalog/contract-flow.constants.ts` |
| Schema pages | [Signature workflow](https://docs.bbrands.io/schemas/signature/signature-workflow.md), [Enterprise signer](https://docs.bbrands.io/schemas/signature/signature-enterprise-signer.md), [Workflow enterprise signer](https://docs.bbrands.io/schemas/signature/signature-workflow-enterprise-signer.md) |

Paths are relative to `backend/horizon-api/`.


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