# `Countersign.Signable`
[🔗](https://github.com/mkreyman/countersign/blob/v0.1.0/lib/countersign/signable.ex#L1)

The behaviour for the thing being signed: a contract, a consent form, a
plan of care. Countersign never stores the subject; it stores
`subject_type` and `subject_id` and asks this module for everything else.

# `input`

```elixir
@type input() :: %{
  :signer_name =&gt; String.t(),
  :method =&gt; :drawn | :typed,
  optional(:image) =&gt; binary(),
  optional(atom()) =&gt; term()
}
```

What the signer entered on the signing page: at least `:signer_name` and
`:method` (`:drawn` or `:typed`), and `:image` (PNG bytes) for a drawn
signature.

# `purpose`

```elixir
@type purpose() :: :review | :copy
```

Why a PDF is rendered: for the signer to read before signing, or as their signed copy.

# `request_meta`

```elixir
@type request_meta() :: %{
  optional(:ip_address) =&gt; String.t(),
  optional(:user_agent) =&gt; String.t()
}
```

Facts about the signer's HTTP request: `:ip_address` and `:user_agent`.

# `subject`

```elixir
@type subject() :: term()
```

Whatever the host returns from `c:fetch/3`; Countersign passes it back untouched.

# `content_hash`

```elixir
@callback content_hash(subject()) :: String.t()
```

The subject's content hash: 64 lowercase hex characters. A request is sent
against this hash, and a signature is accepted only while it is unchanged.

# `fetch`

```elixir
@callback fetch(
  Countersign.Host.scope_id(),
  subject_type :: String.t(),
  subject_id :: String.t()
) ::
  {:ok, subject()} | {:error, :not_found}
```

Loads the subject, scoped to `scope_id`. Answers `{:error, :not_found}` for anything outside the scope.

# `record_signature`

```elixir
@callback record_signature(subject(), Countersign.Request.t(), input(), request_meta()) ::
  {:ok, signature_ref :: String.t()}
  | {:error, :changed}
  | {:error, :already_signed}
  | {:error, term()}
```

Writes the signature into the host's own records, inside the sign
transaction.

Answers `{:ok, signature_ref}` (stored as the request's `signature_ref`),
`{:error, :changed}` when the content moved since the send,
`{:error, :already_signed}` when the role's line is already signed, or
`{:error, term}` for anything else, which rolls the transaction back.

# `render_pdf`

```elixir
@callback render_pdf(subject(), purpose()) :: {:ok, binary()} | {:error, :busy | term()}
```

Renders the subject as a PDF for `purpose`. Answers `{:error, :busy}` when
the renderer is saturated, which the signing page reports as "try again".

# `role_label`

```elixir
@callback role_label(subject(), role :: String.t()) :: String.t()
```

The human label of `role` on this subject, as the signing page shows it.

# `signable?`

```elixir
@callback signable?(subject()) :: boolean()
```

Whether the subject still takes signatures (for example, not voided).

# `store_document`

```elixir
@callback store_document(
  subject(),
  :certificate,
  pdf :: binary(),
  Countersign.Request.t()
) ::
  {:ok, document_ref :: String.t()} | {:cancel, term()} | {:error, term()}
```

Stores a generated document (the signing certificate) against the subject.
`Countersign.Workers.Certificate` calls it with the request row locked and
pins the answer as `certificate_ref`. `{:error, :busy}` or
`{:error, :timeout}` snoozes the job, `{:cancel, reason}` ends it for good
(the owner is gone), and any other error retries it.

# `type_label`

```elixir
@callback type_label(subject()) :: String.t()
```

The human name of the subject's kind ("Plan of Care"), as the signing certificate shows it.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
