DoubleEntryLedger.Workers.CommandWorker.AccountCommandMapResponseHandler (double_entry_ledger v0.3.0)

View Source

Response handler for account-related event processing operations.

This module provides standardized response handling for AccountCommandMap processing operations, including success and error scenarios. It handles the translation of database transaction results into appropriate response formats and performs comprehensive logging for audit and debugging purposes.

Key Features

  • Response Translation: Converts Ecto.Multi transaction results into standardized responses
  • Error Mapping: Maps validation errors from Events and Accounts back to AccountCommandMap changesets
  • Comprehensive Logging: Provides detailed logging for success and failure scenarios
  • Changeset Propagation: Ensures validation errors are properly propagated to calling code

Usage

This module is typically used by CommandWorker modules that process AccountCommandMap structures, providing a consistent interface for handling transaction results.

Error Handling

The module handles several types of errors:

  • Command validation errors (mapped to event-level changeset errors)
  • Account validation errors (mapped to payload-level changeset errors)
  • Multi step failures (logged and returned as string errors)

Summary

Types

Error response containing either a changeset with validation errors or a string error message.

Complete response type for event processing operations.

Success response tuple containing the processed account and associated event.

Types

error_response()

@type error_response() ::
  {:error,
   Ecto.Changeset.t(DoubleEntryLedger.Command.AccountCommandMap.t())
   | String.t()}

Error response containing either a changeset with validation errors or a string error message.

logable()

response()

@type response() :: success_tuple() | error_response()

Complete response type for event processing operations.

success_tuple()

@type success_tuple() ::
  {:ok, DoubleEntryLedger.Account.t(), DoubleEntryLedger.Command.t()}

Success response tuple containing the processed account and associated event.

Functions

default_response_handler(response, command_map)

@spec default_response_handler(
  {:ok,
   %{
     account: DoubleEntryLedger.Account.t(),
     command_success: DoubleEntryLedger.Command.t()
   }}
  | {:error, :atom, any(), map()},
  DoubleEntryLedger.Command.AccountCommandMap.t()
) :: response()

Handles responses from account event processing operations.

This function processes the results of Ecto.Multi transactions for account operations, providing standardized response formatting, error handling, and logging. It translates database transaction results into appropriate response formats for client consumption.

Parameters

  • response - The result from an Ecto.Multi transaction (success or error tuple)
  • command_map - The original AccountCommandMap that was being processed
  • module_name - String identifier of the calling module for logging purposes

Returns

  • {:ok, Account.t(), Command.t()} - Success with the created/updated account and event
  • {:error, Changeset.t(AccountCommandMap.t())} - Validation error with changeset
  • {:error, String.t()} - System error with descriptive message

Error Handling

  • :new_command errors: Command validation failures mapped to event-level changeset errors
  • :account errors: Account validation failures mapped to payload-level changeset errors
  • Other step failures: Logged and returned as descriptive string errors

Examples

iex> account = %Account{}
iex> event = %Command{command_queue_item: %{status: :processed}, command_map: %{}}
iex> response = {:ok, %{account: account, command_success: event}}
iex> {:ok, ^account, ^event} = AccountCommandMapResponseHandler.default_response_handler(response, %AccountCommandMap{})

iex> changeset = %Ecto.Changeset{}
iex> response = {:error, :account, changeset, %{}}
iex> command_map = %AccountCommandMap{payload: %AccountData{}}
iex> {:error, %Ecto.Changeset{} = _changeset} = AccountCommandMapResponseHandler.default_response_handler(response, command_map)

error(message, logable, changeset)

@spec error(String.t(), logable(), any()) :: {:ok, String.t()}

info(message, logable, schema)

@spec info(String.t(), logable(), any()) :: {:ok, String.t()}

warn(message, logable)

@spec warn(String.t(), logable()) :: {:ok, String.t()}

warn(message, logable, changeset)

@spec warn(String.t(), logable(), any()) :: {:ok, String.t()}