Current section

Files

Jump to
commanded test example_domain bank_account account_balance_handler.ex
Raw

test/example_domain/bank_account/account_balance_handler.ex

defmodule Commanded.ExampleDomain.BankAccount.AccountBalanceHandler do
@moduledoc false
use Commanded.Event.Handler, name: __MODULE__
alias Commanded.ExampleDomain.BankAccount.Events.{
BankAccountOpened,
MoneyDeposited,
MoneyWithdrawn
}
@agent_name {:global, __MODULE__}
def init do
with {:ok, _pid} <- Agent.start_link(fn -> 0 end, name: @agent_name) do
:ok
end
end
def handle(%BankAccountOpened{initial_balance: initial_balance}, _metadata) do
Agent.update(@agent_name, fn _ -> initial_balance end)
end
def handle(%MoneyDeposited{balance: balance}, _metadata) do
Agent.update(@agent_name, fn _ -> balance end)
end
def handle(%MoneyWithdrawn{balance: balance}, _metadata) do
Agent.update(@agent_name, fn _ -> balance end)
end
def subscribed? do
try do
Agent.get(@agent_name, fn _ -> true end)
catch
:exit, _reason -> false
end
end
def current_balance do
try do
Agent.get(@agent_name, fn balance -> balance end)
catch
# catch agent not started exits, return `nil` balance
:exit, _reason ->
nil
end
end
end