Skip to content

Commit d92ea95

Browse files
committed
Moves missing helper function into ff_range module for fistful
1 parent f07644a commit d92ea95

2 files changed

Lines changed: 36 additions & 1 deletion

File tree

apps/ff_core/src/ff_range.erl

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33

44
-module(ff_range).
55

6+
-include_lib("damsel/include/dmsl_domain_thrift.hrl").
7+
8+
-type cash_range() :: dmsl_domain_thrift:'CashRange'().
9+
-type cash() :: dmsl_domain_thrift:'Cash'().
610
-type range(T) :: {'maybe'(bound(T)), 'maybe'(bound(T))}.
711
-type bound(T) :: {exclusive | inclusive, ord(T)}.
812

@@ -13,11 +17,30 @@
1317
-export_type([range/1]).
1418
-export_type([bound/1]).
1519

20+
-export([is_inside/2]).
1621
-export([intersect/2]).
1722
-export([contains/2]).
1823

1924
%%
2025

26+
-spec is_inside(cash(), cash_range()) -> within | {exceeds, lower | upper}.
27+
is_inside(Cash, #domain_CashRange{lower = Lower, upper = Upper} = CashRange) ->
28+
case
29+
{
30+
compare_cash(fun erlang:'>'/2, Cash, Lower),
31+
compare_cash(fun erlang:'<'/2, Cash, Upper)
32+
}
33+
of
34+
{true, true} ->
35+
within;
36+
{false, true} ->
37+
{exceeds, lower};
38+
{true, false} ->
39+
{exceeds, upper};
40+
_ ->
41+
error({misconfiguration, {'Invalid cash range specified', CashRange, Cash}})
42+
end.
43+
2144
-spec intersect(range(T), range(T)) -> range(T) | undefined.
2245
intersect(R1, R2) ->
2346
B1 = max_bound(lower(R1), lower(R2)),
@@ -35,6 +58,18 @@ contains(R1, R2) ->
3558

3659
%%
3760

61+
-define(CASH(Amount, SymCode), #domain_Cash{
62+
amount = Amount,
63+
currency = #domain_CurrencyRef{symbolic_code = SymCode}
64+
}).
65+
66+
compare_cash(_, V, {inclusive, V}) ->
67+
true;
68+
compare_cash(F, ?CASH(A, C), {_, ?CASH(Am, C)}) ->
69+
F(A, Am);
70+
compare_cash(_, _, _) ->
71+
error.
72+
3873
compare_bounds(B1, B1) ->
3974
eq;
4075
compare_bounds(_B1, neginf) ->

apps/ff_transfer/src/ff_withdrawal_routing.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ validate_currencies(_NotReducedSelector, _VS) ->
377377
{ok, valid}
378378
| {error, Error :: term()}.
379379
validate_cash_limit({value, CashRange}, #{cost := Cash}) ->
380-
case hg_cash_range:is_inside(Cash, CashRange) of
380+
case ff_range:is_inside(Cash, CashRange) of
381381
within ->
382382
{ok, valid};
383383
_NotInRange ->

0 commit comments

Comments
 (0)