Karta.Provisioning (Karta v1.0.0)

What an external system does through the provisioning API (KartaWeb.Provisioning) to set a customer up: an organisation named by the system's own reference, with a mail server to send from. Karta knows nothing about the system; the reference is an opaque text.

Every operation is idempotent and safe to repeat: the reference (and, further down an organisation, a domain name or a credential's reference) is the natural key, and a request for what exists returns it. Operations on one organisation are serialised with a transaction-level advisory lock, so two identical requests at the same moment make one record.

  • put_organisation/2 makes the organisation (and its mail server, named "Default") or returns it, renaming it when the name differs.
  • An organisation made here starts restricted (Karta.Quotas) with a daily limit and a per-recipient limit (default_limits/0), so a new customer can send only low-volume transactional mail until the system lifts that with put_limits/2.
  • suspend/2 and resume/1 stop and restart all sending: the organisation's servers are suspended (their credentials are refused, queued mail waits), the way a server's own suspension works (Karta.Servers). Resuming lifts only the suspensions the organisation's own made.
  • delete/1 ends the organisation: sending stops, credentials are revoked, webhooks are switched off, domains are removed and the reference is free again. The organisation's mail is kept until retention removes it, and the organisation no longer shows anywhere.

Summary

Functions

What a new organisation starts with: restricted, 100 recipients a day, 10 messages a day to one recipient. Set with config :karta, Karta.Provisioning, defaults: [daily_send_limit: ..., per_recipient_daily_limit: ...] for another instance-wide start.

Ends the organisation (see the module doc). :ok also when there is nothing to end.

The organisation with this reference, or nil (a deleted one is gone).

The trust level and limits of the organisation, with what it has sent today.

Sets the trust level and limits named in attrs (trust_level, daily_send_limit, per_recipient_daily_limit; a limit of nil removes it); what is left out stays. Repeating it changes nothing.

Makes the organisation with this reference, or returns it. attrs: the name (required; it renames an existing organisation). Returns {:ok, :created | :existing, organisation}.

Lifts the suspension: the servers it suspended send again and queued mail goes out.

The organisation's mail server, the one provisioning made. Credentials, webhooks and limits belong to it.

Suspends sending for the organisation, for reason (at most 230 characters; the organisation sees it). Suspending a suspended organisation changes only the reason.

Runs fun holding the advisory lock of the reference, in a transaction.

Functions

default_limits()

@spec default_limits() :: map()

What a new organisation starts with: restricted, 100 recipients a day, 10 messages a day to one recipient. Set with config :karta, Karta.Provisioning, defaults: [daily_send_limit: ..., per_recipient_daily_limit: ...] for another instance-wide start.

delete(ref)

@spec delete(String.t()) :: :ok

Ends the organisation (see the module doc). :ok also when there is nothing to end.

get_organisation(ref)

@spec get_organisation(String.t()) :: Karta.Accounts.Organisation.t() | nil

The organisation with this reference, or nil (a deleted one is gone).

limits(organisation)

@spec limits(Karta.Accounts.Organisation.t()) :: map()

The trust level and limits of the organisation, with what it has sent today.

put_limits(organisation, attrs)

@spec put_limits(Karta.Accounts.Organisation.t(), map()) ::
  {:ok, map()} | {:error, Ecto.Changeset.t()}

Sets the trust level and limits named in attrs (trust_level, daily_send_limit, per_recipient_daily_limit; a limit of nil removes it); what is left out stays. Repeating it changes nothing.

put_organisation(ref, attrs)

@spec put_organisation(String.t(), map()) ::
  {:ok, :created | :existing, Karta.Accounts.Organisation.t()}
  | {:error, Ecto.Changeset.t()}

Makes the organisation with this reference, or returns it. attrs: the name (required; it renames an existing organisation). Returns {:ok, :created | :existing, organisation}.

resume(organisation)

Lifts the suspension: the servers it suspended send again and queued mail goes out.

server(organisation)

The organisation's mail server, the one provisioning made. Credentials, webhooks and limits belong to it.

suspend(organisation, reason)

@spec suspend(Karta.Accounts.Organisation.t(), String.t() | nil) ::
  {:ok, Karta.Accounts.Organisation.t()} | {:error, Ecto.Changeset.t()}

Suspends sending for the organisation, for reason (at most 230 characters; the organisation sees it). Suspending a suspended organisation changes only the reason.

transaction(ref, fun)

@spec transaction(String.t(), (-> term())) :: term()

Runs fun holding the advisory lock of the reference, in a transaction.