Logistiki.Relationships (logistiki v0.1.0)

Copy Markdown View Source

The context for entity-to-account relationships.

Business entities and virtual accounts are separate hierarchies. They connect through relationships that support many-to-many links, multiple relationship types between the same pair, and effective dating.

Effective dating

Each relationship has valid_from and valid_to (nil while active). List functions accept an :at option (a Date) to filter relationships active at a point in time.

Summary

Functions

Links entity to account with relationship_type and optional attributes.

Lists accounts linked to entity, optionally filtered.

Lists accounts linked to entity or any of its descendants.

Lists entities linked to account, optionally filtered.

Unlinks entity and account for relationship_type by setting valid_to to today. The relationship row is preserved for audit — only the active link is ended.

Functions

list_accounts_for_entity(entity_or_id, opts \\ [])

(since 0.1.0)

Lists accounts linked to entity, optionally filtered.

Arguments

  • entity_or_id%BusinessEntity{} or integer() entity id.
  • optskeyword() of options:
    • :relationship_typeatom() — filter by type (e.g. :owner)
    • :atDate.t — effective date; only relationships active on this date are returned. When omitted, only currently active relationships (valid_to IS NULL) are returned.
    • :currencyString.t — filter to a currency

Returns

  • [VirtualAccount.t()] — ordered by code. Empty list if none.

Examples

iex> Logistiki.Relationships.list_accounts_for_entity(acme_entity)
[%VirtualAccount{code: "LIABILITIES:CLIENT_DEPOSITS:USD:ACME:OPERATING", ...}]

iex> Logistiki.Relationships.list_accounts_for_entity(acme_entity, relationship_type: :owner)
[%VirtualAccount{code: "LIABILITIES:CLIENT_DEPOSITS:USD:ACME:OPERATING", ...}]

iex> Logistiki.Relationships.list_accounts_for_entity(acme_entity, at: ~D[2026-01-01])
[]

list_accounts_for_entity_tree(entity, opts \\ [])

(since 0.1.0)

Lists accounts linked to entity or any of its descendants.

Uses the business-entity closure table to find all descendant entity ids, then finds all accounts linked to any of them.

Arguments

  • entity%BusinessEntity{} — the root of the entity subtree.
  • optskeyword() — same options as list_accounts_for_entity/2.

Returns

  • [VirtualAccount.t()] — ordered by code. Empty list if none.

Examples

iex> Logistiki.Relationships.list_accounts_for_entity_tree(acme_holdings)
[%VirtualAccount{code: "LIABILITIES:CLIENT_DEPOSITS:USD:ACME:OPERATING", ...},
 %VirtualAccount{code: "LIABILITIES:CLIENT_DEPOSITS:USD:ACME:PAYROLL", ...}]

list_entities_for_account(account_or_id, opts \\ [])

(since 0.1.0)

Lists entities linked to account, optionally filtered.

Arguments

  • account_or_id%VirtualAccount{} or integer() account id.
  • optskeyword() of options:
    • :relationship_typeatom() — filter by type (e.g. :owner)
    • :atDate.t — effective date

Returns

  • [BusinessEntity.t()] — ordered by name. Empty list if none.

Examples

iex> Logistiki.Relationships.list_entities_for_account(operating_account)
[%BusinessEntity{name: "Acme Holdings", ...}]

iex> Logistiki.Relationships.list_entities_for_account(operating_account, relationship_type: :owner)
[%BusinessEntity{name: "Acme Holdings", ...}]