AuroraMeter.Schema.CreditAllocation (Aurora Meter v1.0.0)

View Source

One movement of value between two buckets of one AuroraMeter.Schema.CreditLot, naming the ledger entry that caused it.

The allocation trail is what makes a lot's quantities reconstructible rather than merely asserted: folding a lot's allocations in seq order reproduces its available, reserved, consumed, reversed and expired exactly.

kindMovement
reserveavailable to reserved (a hold)
unreservereserved to available (a release, or a settlement below its hold)
consumeavailable or reserved to consumed (a debit, a settlement, a debt repayment)
expireavailable or reserved to expired
reverseavailable, consumed or reserved to reversed (a refund or a chargeback)
restorereversed back to available

from_bucket and to_bucket record the movement exactly, and they are not redundant with kind: a consume can come out of available (a debit) or out of reserved (a settlement against its own hold), an expire out of either, and a reverse out of any of three. Folding a lot's allocations back into its five quantities needs the source, and kind alone would leave it guessing.

There is deliberately no uniqueness on (transaction_id, lot_id, kind): one transaction legitimately produces several allocations of the same kind on different lots, and a settlement produces both a consume and an unreserve on the same lot. Idempotency lives on the ledger entry's (kind, reference), which is decided before any allocation is written.

seq is the order, not inserted_at: see AuroraMeter.Schema.CreditLot.

Summary

Functions

The buckets a movement can run between.

Builds a changeset for an allocation.

The allocation kinds.

Types

bucket()

@type bucket() :: :available | :reserved | :consumed | :reversed | :expired

kind()

@type kind() :: :reserve | :unreserve | :consume | :expire | :reverse | :restore

t()

@type t() :: %AuroraMeter.Schema.CreditAllocation{
  __meta__: term(),
  amount: term(),
  from_bucket: term(),
  id: term(),
  inserted_at: term(),
  kind: term(),
  lot_id: term(),
  seq: term(),
  tenant_key: term(),
  to_bucket: term(),
  transaction_id: term()
}

Functions

buckets()

@spec buckets() :: [bucket()]

The buckets a movement can run between.

Examples

iex> AuroraMeter.Schema.CreditAllocation.buckets()
[:available, :reserved, :consumed, :reversed, :expired]

changeset(allocation, attrs)

@spec changeset(t(), map()) :: Ecto.Changeset.t()

Builds a changeset for an allocation.

kinds()

@spec kinds() :: [kind()]

The allocation kinds.

Examples

iex> :unreserve in AuroraMeter.Schema.CreditAllocation.kinds()
true