TD-511: Adds app configuration flag to disable currency conversion (#19)

* Adds app configuration flag to disable currency conversion

* Revert turnover test suite

* Adds testcase for disabled currency conversion

* Changes currency_conversion default to disabled

* Simplify currency conversion testcases setup
This commit is contained in:
Aleksey Kashapov 2023-02-20 11:29:50 +03:00 committed by GitHub
parent 7c416e2533
commit d7febd557c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 16 deletions

View File

@ -14,7 +14,7 @@
%%
-spec compute(t(), stage(), lim_config_machine:config(), lim_context:t()) ->
{ok, amount()} | {error, invalid_request_error()}.
{ok, amount()} | {error, lim_rates:conversion_error()} | {error, invalid_request_error()}.
compute(number, hold, Config, LimitContext) ->
#{amount := Amount} = get_body(Config, LimitContext),
{ok, sign(Amount)};
@ -49,7 +49,13 @@ get_commit_body(Config, LimitContext) ->
denominate(#{amount := Amount, currency := Currency}, Currency, _Config, _LimitContext) ->
{ok, Amount};
denominate(#{currency := Currency}, DestinationCurrency, _Config, _LimitContext) ->
denominate(Body = #{currency := Currency}, DestinationCurrency, Config, LimitContext) ->
case genlib_app:env(limiter, currency_conversion, disabled) of
disabled -> invalid_request_currencies_mismatch(Currency, DestinationCurrency);
enabled -> convert_currency(Body, DestinationCurrency, Config, LimitContext)
end.
invalid_request_currencies_mismatch(Currency, DestinationCurrency) ->
{error,
{invalid_request, [
genlib:format(
@ -57,14 +63,14 @@ denominate(#{currency := Currency}, DestinationCurrency, _Config, _LimitContext)
[Currency, DestinationCurrency]
)
]}}.
%% NOTE conversion disabled temporarily
%%denominate(Body = #{}, DestinationCurrency, Config, LimitContext) ->
%% case lim_rates:convert(Body, DestinationCurrency, Config, LimitContext) of
%% {ok, #{amount := AmountConverted}} ->
%% {ok, AmountConverted};
%% {error, _} = Error ->
%% Error
%% end.
convert_currency(Body, DestinationCurrency, Config, LimitContext) ->
case lim_rates:convert(Body, DestinationCurrency, Config, LimitContext) of
{ok, #{amount := AmountConverted}} ->
{ok, AmountConverted};
{error, _} = Error ->
Error
end.
sign(Amount) when Amount > 0 ->
+1;

View File

@ -17,6 +17,7 @@
-export([commit_with_default_exchange/1]).
-export([partial_commit_with_exchange/1]).
-export([commit_with_exchange/1]).
-export([commit_with_disabled_exchange/1]).
-export([get_limit_ok/1]).
-export([get_limit_notfound/1]).
-export([hold_ok/1]).
@ -68,10 +69,10 @@ groups() ->
[
{default, [], [
commit_with_long_change_id,
% NOTE disabled to stop exchange
%% commit_with_default_exchange,
%% partial_commit_with_exchange,
commit_with_default_exchange,
partial_commit_with_exchange,
commit_with_exchange,
commit_with_disabled_exchange,
get_limit_ok,
get_limit_notfound,
hold_ok,
@ -181,6 +182,7 @@ commit_with_long_change_id(C) ->
-spec commit_with_default_exchange(config()) -> _.
commit_with_default_exchange(C) ->
ok = application:set_env(limiter, currency_conversion, enabled),
Rational = #base_Rational{p = 1000000, q = 100},
_ = mock_exchange(Rational, C),
ID = configure_limit(?time_range_month(), ?global(), C),
@ -191,6 +193,7 @@ commit_with_default_exchange(C) ->
-spec partial_commit_with_exchange(config()) -> _.
partial_commit_with_exchange(C) ->
ok = application:set_env(limiter, currency_conversion, enabled),
Rational = #base_Rational{p = 800000, q = 100},
_ = mock_exchange(Rational, C),
ID = configure_limit(?time_range_month(), ?global(), C),
@ -202,14 +205,25 @@ partial_commit_with_exchange(C) ->
-spec commit_with_exchange(config()) -> _.
commit_with_exchange(C) ->
ok = application:set_env(limiter, currency_conversion, enabled),
Rational = #base_Rational{p = 1000000, q = 100},
_ = mock_exchange(Rational, C),
ID = configure_limit(?time_range_month(), ?global(), C),
Cost = ?cash(10000, <<"USD">>),
Context = ?payproc_ctx_invoice(Cost),
{exception, #base_InvalidRequest{}} = lim_client:hold(?LIMIT_CHANGE(ID), Context, ?config(client, C)).
%% {ok, {vector, _}} = hold_and_commit(?LIMIT_CHANGE(ID), Context, ?config(client, C)),
%% {ok, #limiter_Limit{amount = 10500}} = lim_client:get(ID, Context, ?config(client, C)).
{ok, {vector, _}} = hold_and_commit(?LIMIT_CHANGE(ID), Context, ?config(client, C)),
{ok, #limiter_Limit{amount = 10500}} = lim_client:get(ID, Context, ?config(client, C)).
-spec commit_with_disabled_exchange(config()) -> _.
commit_with_disabled_exchange(C) ->
ok = application:set_env(limiter, currency_conversion, disabled),
Rational = #base_Rational{p = 1000000, q = 100},
_ = mock_exchange(Rational, C),
ID = configure_limit(?time_range_month(), ?global(), C),
Cost = ?cash(10000, <<"USD">>),
Context = ?payproc_ctx_invoice(Cost),
{exception, #base_InvalidRequest{}} =
lim_client:hold(?LIMIT_CHANGE(ID), Context, ?config(client, C)).
-spec get_limit_ok(config()) -> _.
get_limit_ok(C) ->

View File

@ -22,6 +22,9 @@
}
}},
% Enables currency conversion for turnover metric (default = disabled)
{currency_conversion, disabled},
{exchange_factors, #{
<<"DEFAULT">> => {1, 1},
<<"USD">> => {105, 100},