| 1 |
|
defmodule WalletLimitsFees.Queries.EvaluateTransferLimits do |
| 2 |
|
@moduledoc """ |
| 3 |
|
Query handler for evaluating whether a transfer is within configured limits. |
| 4 |
|
|
| 5 |
|
Delegates to PolicyEngine and captures a PolicyDecisionCaptured event for |
| 6 |
|
audit traceability per ADR 0011. |
| 7 |
|
""" |
| 8 |
|
|
| 9 |
|
alias WalletLimitsFees.{PolicyEngine} |
| 10 |
|
alias WalletLimitsFees.Events.PolicyDecisionCaptured |
| 11 |
|
|
| 12 |
|
@doc """ |
| 13 |
|
Evaluates transfer limits for the given tier, currency, and amount. |
| 14 |
|
|
| 15 |
|
`context` may include `:daily_used` and `:monthly_used`. |
| 16 |
|
|
| 17 |
|
Returns: |
| 18 |
|
- `{:ok, :within_limits}` — all limits satisfied. |
| 19 |
|
- `{:ok, :no_policy}` — no policy configured; permissive. |
| 20 |
|
- `{:error, limit_type, details}` — a limit was exceeded. |
| 21 |
|
""" |
| 22 |
|
@spec execute( |
| 23 |
|
tier :: atom(), |
| 24 |
|
currency :: String.t(), |
| 25 |
|
amount :: pos_integer(), |
| 26 |
|
context :: map() |
| 27 |
|
) :: |
| 28 |
|
{:ok, :within_limits | :no_policy} |
| 29 |
|
| {:error, atom(), map()} |
| 30 |
:-( |
def execute(tier, currency, amount, context \\ %{}) do |
| 31 |
:-( |
result = PolicyEngine.evaluate_limits(tier, currency, amount, context) |
| 32 |
|
|
| 33 |
:-( |
{outcome, details} = |
| 34 |
|
case result do |
| 35 |
:-( |
{:ok, outcome} -> {outcome, %{}} |
| 36 |
:-( |
{:error, ltype, d} -> {{:error, ltype}, d} |
| 37 |
|
end |
| 38 |
|
|
| 39 |
:-( |
emit_event( |
| 40 |
|
PolicyDecisionCaptured.build(:limits_check, outcome, |
| 41 |
|
tier: tier, |
| 42 |
|
currency: currency, |
| 43 |
|
amount: amount, |
| 44 |
|
details: details |
| 45 |
|
) |
| 46 |
|
) |
| 47 |
|
|
| 48 |
:-( |
result |
| 49 |
|
end |
| 50 |
|
|
| 51 |
:-( |
defp emit_event(event) do |
| 52 |
:-( |
pubsub = Application.get_env(:wallet_limits_fees, :pubsub, WalletWeb.PubSub) |
| 53 |
:-( |
apply(Phoenix.PubSub, :broadcast, [pubsub, "wallet_limits_fees:events", {:domain_event, event}]) |
| 54 |
|
rescue |
| 55 |
:-( |
_ -> :ok |
| 56 |
|
end |
| 57 |
|
end |