DoubleEntryLedger.Stores.TransactionStoreHelper (double_entry_ledger v0.3.0)

View Source

Provides helper functions for building Ecto.Multi operations related to transactions in the double-entry ledger system.

This module focuses on constructing changesets and multi-step operations for creating and updating transactions, ensuring that all necessary validations and business rules are applied.

Key Functionality

  • Transaction Creation: Build Ecto.Multi operations for creating new transactions
  • Transaction Updates: Build Ecto.Multi operations for updating existing transactions
  • Error Handling: Manage potential errors such as Ecto.StaleEntryError during concurrent updates
  • Status Transitions: Handle transaction status changes with appropriate validations

Usage Examples

Summary

Functions

Builds an Ecto.Multi to create a new transaction. This is used as a building block for more complex operations.

Builds an Ecto.Multi to update a transaction. This is used as a building block for more complex operations.

Functions

build_create(multi, step, transaction, repo \\ Repo)

@spec build_create(Ecto.Multi.t(), atom(), map(), Ecto.Repo.t()) :: Ecto.Multi.t()

Builds an Ecto.Multi to create a new transaction. This is used as a building block for more complex operations.

It also handles the Ecto.StaleEntryError exception that can be raised when accounts associated with the transaction have been updated in the meantime. In this case it returns an error tuple which is then converted to an Ecto.Multi.failure() to be handled by the caller.

Parameters

  • multi - The existing Ecto.Multi struct.
  • step - An atom representing the name of the operation.
  • transaction - A map of transaction attributes.
  • repo - The repository module (defaults to Repo).

Returns

  • An Ecto.Multi struct with the create operation added.

build_update(multi, step, transaction_or_step, attrs, repo \\ Repo)

@spec build_update(
  Ecto.Multi.t(),
  atom(),
  DoubleEntryLedger.Transaction.t() | atom(),
  map(),
  Ecto.Repo.t()
) :: Ecto.Multi.t()

Builds an Ecto.Multi to update a transaction. This is used as a building block for more complex operations.

It also handles the Ecto.StaleEntryError exception that can be raised when accounts associated with the transaction have been updated in the meantime. In this case it returns an error tuple which is then converted to an Ecto.Multi.failure() to be handled by the caller.

Parameters

  • multi - The existing Ecto.Multi struct.
  • step - An atom representing the name of the operation.
  • transaction - The Transaction struct to be updated.
  • attrs - A map of attributes for the update.
  • repo - The repository module (defaults to Repo).

Returns

  • An Ecto.Multi struct with the update operation added.