| 1 |
|
defmodule WalletTransfers.Commands.FailTransfer do |
| 2 |
|
@moduledoc """ |
| 3 |
|
Command handler for failing a reserved transfer. |
| 4 |
|
|
| 5 |
|
Acquires a per-transfer lock, retrieves the transfer, applies the fail |
| 6 |
|
state transition with a reason, persists the updated transfer, releases |
| 7 |
|
the lock, and emits domain and audit events. |
| 8 |
|
""" |
| 9 |
|
|
| 10 |
|
alias WalletTransfers.{Transfer, TransferStore, LockStore} |
| 11 |
|
alias WalletTransfers.Events.TransferFailed |
| 12 |
|
alias WalletObservability.AuditEvent |
| 13 |
|
|
| 14 |
|
@doc """ |
| 15 |
|
Executes the fail transfer command. |
| 16 |
|
|
| 17 |
|
Options: |
| 18 |
|
- `correlation_id` — propagated into events. |
| 19 |
|
|
| 20 |
|
Returns `{:ok, transfer}` or `{:error, :locked | :not_found | :invalid_transition}`. |
| 21 |
|
""" |
| 22 |
|
@spec execute(transfer_id :: String.t(), reason :: String.t(), opts :: keyword()) :: |
| 23 |
|
{:ok, Transfer.t()} | {:error, :locked | :not_found | :invalid_transition} |
| 24 |
6 |
def execute(transfer_id, reason, opts \\ []) do |
| 25 |
6 |
correlation_id = |
| 26 |
|
Keyword.get(opts, :correlation_id, WalletSharedKernel.Correlation.new_correlation_id()) |
| 27 |
|
|
| 28 |
6 |
case LockStore.acquire(transfer_id) do |
| 29 |
:-( |
{:error, :locked} -> |
| 30 |
|
{:error, :locked} |
| 31 |
|
|
| 32 |
|
:ok -> |
| 33 |
6 |
result = |
| 34 |
3 |
with {:ok, transfer} <- TransferStore.get(transfer_id), |
| 35 |
5 |
{:ok, failed_transfer} <- Transfer.fail(transfer, reason), |
| 36 |
3 |
:ok <- TransferStore.update(failed_transfer) do |
| 37 |
|
{:ok, failed_transfer} |
| 38 |
|
end |
| 39 |
|
|
| 40 |
6 |
LockStore.release(transfer_id) |
| 41 |
|
|
| 42 |
6 |
case result do |
| 43 |
|
{:ok, failed_transfer} -> |
| 44 |
3 |
emit_event( |
| 45 |
3 |
TransferFailed.build(transfer_id, failed_transfer.user_id, |
| 46 |
|
correlation_id: correlation_id, |
| 47 |
3 |
amount: failed_transfer.amount, |
| 48 |
3 |
currency: failed_transfer.currency, |
| 49 |
|
failure_reason: reason, |
| 50 |
3 |
failed_at: failed_transfer.failed_at |
| 51 |
|
) |
| 52 |
|
) |
| 53 |
|
|
| 54 |
3 |
audit = |
| 55 |
|
AuditEvent.build( |
| 56 |
|
:financial, |
| 57 |
|
"transfer_failed", |
| 58 |
|
"transfer", |
| 59 |
|
transfer_id, |
| 60 |
|
:failure, |
| 61 |
3 |
actor_id: failed_transfer.user_id, |
| 62 |
|
correlation_id: correlation_id, |
| 63 |
|
metadata: %{ |
| 64 |
|
reason: reason, |
| 65 |
3 |
amount: failed_transfer.amount, |
| 66 |
3 |
currency: failed_transfer.currency |
| 67 |
|
} |
| 68 |
|
) |
| 69 |
|
|
| 70 |
3 |
:telemetry.execute([:wallet_transfers, :audit], %{}, audit) |
| 71 |
|
|
| 72 |
|
{:ok, failed_transfer} |
| 73 |
|
|
| 74 |
|
error -> |
| 75 |
3 |
error |
| 76 |
|
end |
| 77 |
|
end |
| 78 |
|
end |
| 79 |
|
|
| 80 |
3 |
defp emit_event(event) do |
| 81 |
3 |
pubsub = Application.get_env(:wallet_transfers, :pubsub, WalletWeb.PubSub) |
| 82 |
|
|
| 83 |
3 |
apply(Phoenix.PubSub, :broadcast, [ |
| 84 |
|
pubsub, |
| 85 |
|
"wallet_transfers:events", |
| 86 |
|
{:domain_event, event} |
| 87 |
|
]) |
| 88 |
|
rescue |
| 89 |
:-( |
_ -> :ok |
| 90 |
|
end |
| 91 |
|
end |