cover/Elixir.WalletLimitsFees.Queries.CalculateFee.html

1 defmodule WalletLimitsFees.Queries.CalculateFee do
2 @moduledoc """
3 Query handler for calculating the applicable fee for a transfer.
4
5 Delegates to PolicyEngine and captures a PolicyDecisionCaptured event.
6 """
7
8 alias WalletLimitsFees.PolicyEngine
9 alias WalletLimitsFees.Events.PolicyDecisionCaptured
10
11 @doc """
12 Calculates the fee for the given tier, currency, transfer type, and amount.
13
14 Returns `{:ok, fee_amount}` where fee_amount is 0 when no policy exists.
15 """
16 @spec execute(
17 tier :: atom(),
18 currency :: String.t(),
19 transfer_type :: atom(),
20 amount :: non_neg_integer()
21 ) :: {:ok, non_neg_integer()}
22 def execute(tier, currency, transfer_type, amount) do
23
:-(
result = PolicyEngine.calculate_fee(tier, currency, transfer_type, amount)
24
25
:-(
{:ok, fee} = result
26
27
:-(
emit_event(
28 PolicyDecisionCaptured.build(:fee_calculation, {:ok, fee},
29 tier: tier,
30 currency: currency,
31 amount: amount,
32 details: %{transfer_type: transfer_type, fee: fee}
33 )
34 )
35
36
:-(
result
37 end
38
39
:-(
defp emit_event(event) do
40
:-(
pubsub = Application.get_env(:wallet_limits_fees, :pubsub, WalletWeb.PubSub)
41
:-(
apply(Phoenix.PubSub, :broadcast, [pubsub, "wallet_limits_fees:events", {:domain_event, event}])
42 rescue
43
:-(
_ -> :ok
44 end
45 end
Line Hits Source