[WIP] Implement GetResidence / GetCurrency

This commit is contained in:
Andrey Mayorov 2018-07-06 18:46:34 +03:00
parent c79954c062
commit 5f8bbcb544
4 changed files with 71 additions and 9 deletions

View File

@ -11,6 +11,7 @@
-type id() :: symcode().
-type symcode() :: binary().
-type currency() :: #{
id := id(),
name := binary(),
symcode := symcode(),
numcode := integer(),
@ -37,6 +38,7 @@ get(ID) ->
do(fun () ->
Currency = unwrap(ff_domain_config:object({currency, #domain_CurrencyRef{symbolic_code = ID}})),
#{
id => ID,
name => Currency#domain_Currency.name,
symcode => Currency#domain_Currency.symbolic_code,
numcode => Currency#domain_Currency.numeric_code,

View File

@ -9,8 +9,9 @@
%%
-type id() :: atom().
-type id() :: dmsl_domain_thrift:'Residence'().
-type residence() :: #{
id := id(),
name := binary(),
flag => binary()
}.
@ -23,10 +24,13 @@
%%
-spec get(id()) ->
residence().
ff_map:result(residence()).
get('rus') ->
#{
get(ID = 'rus') ->
{ok, #{
id => ID,
name => <<"Российская федерация">>,
flag => <<"🇷🇺">>
}.
}};
get(_) ->
{error, notfound}.

View File

@ -30,11 +30,16 @@
-export([get_withdrawal_events/2]).
-export([get_withdrawal_event/3]).
-export([get_currency/2]).
-export([get_residence/2]).
%% Helper API
-export([not_implemented/0]).
%% API
-type wctx() :: woody_context:ctx().
%% Providers
-spec get_providers(_, _) -> no_return().
@ -218,6 +223,22 @@ get_withdrawal_event(WithdrawalId, EventId, _Context) ->
Error = {error, _} -> Error
end.
-spec get_currency(binary(), wctx()) -> map().
get_currency(CurrencyId, _Context) ->
ff_pipeline:do(fun () ->
to_swag(currency_object,
ff_pipeline:unwrap(ff_currency:get(from_swag(currency, CurrencyId)))
)
end).
-spec get_residence(binary(), wctx()) -> map().
get_residence(Residence, _Context) ->
ff_pipeline:do(fun () ->
to_swag(residence_object,
ff_pipeline:unwrap(ff_residence:get(from_swag(residence, Residence)))
)
end).
%% Helper API
-spec not_implemented() -> no_return().
@ -301,6 +322,16 @@ from_swag(withdrawal_params, Params) ->
};
from_swag(withdrawal_body, Body) ->
{maps:get(<<"amount">>, Body), maps:get(<<"currency">>, Body)};
from_swag(currency, V) ->
V;
from_swag(residence, V) ->
try erlang:binary_to_existing_atom(genlib_string:to_lower(V)) catch
error:badarg ->
% TODO
% - Essentially this is incorrect, we should reply with 400 instead
undefined
end;
from_swag(list, {Type, List}) ->
lists:map(fun(V) -> from_swag(Type, V) end, List).
@ -319,6 +350,12 @@ to_swag(provider, {Id, Provider}) ->
});
to_swag(residence, Residence) ->
genlib_string:to_upper(genlib:to_binary(Residence));
to_swag(residence_object, V) ->
to_swag(map, #{
<<"id">> => to_swag(residence, maps:get(id, V)),
<<"name">> => maps:get(name, V),
<<"flag">> => maps:get(flag, V, undefined)
});
to_swag(identity_class, Class) ->
to_swag(map, maps:with([id, name], Class));
to_swag(identity, #{identity := Identity, times := {CreatedAt, _}, ctx := Ctx}) ->
@ -454,10 +491,19 @@ to_swag(timestamp, {{Date, Time}, Usec}) ->
rfc3339:format({Date, Time, Usec, undefined});
to_swag(currency, Currency) ->
genlib_string:to_upper(genlib:to_binary(Currency));
to_swag(currency_object, V) ->
to_swag(map, #{
<<"id">> => to_swag(currency, maps:get(id, V)),
<<"name">> => maps:get(name, V),
<<"numericCode">> => maps:get(numcode, V),
<<"exponent">> => maps:get(exponent, V),
<<"sign">> => maps:get(sign, V, undefined)
});
to_swag(is_blocked, {ok, accessible}) ->
false;
to_swag(is_blocked, _) ->
true;
to_swag(_Type, V) when is_map(V) ->
to_swag(map, V);
to_swag(list, {Type, List}) ->

View File

@ -241,12 +241,22 @@ process_request('GetWithdrawalEvents', #{
end;
%% Residences
process_request('GetResidence', _Req, _Context, _Opts) ->
wapi_wallet_ff_backend:not_implemented();
process_request('GetResidence', #{
'residence' := Residence
}, #{woody_context := Context}, _Opts) ->
case wapi_wallet_ff_backend:get_residence(Residence, Context) of
{ok, Currency} -> wapi_handler_utils:reply_ok(200, Currency);
{error, notfound} -> wapi_handler_utils:reply_ok(404)
end;
%% Currencies
process_request('GetCurrency', _Req, _Context, _Opts) ->
wapi_wallet_ff_backend:not_implemented().
process_request('GetCurrency', #{
'currencyID' := CurrencyId
}, #{woody_context := Context}, _Opts) ->
case wapi_wallet_ff_backend:get_currency(CurrencyId, Context) of
{ok, Currency} -> wapi_handler_utils:reply_ok(200, Currency);
{error, notfound} -> wapi_handler_utils:reply_ok(404)
end.
%% Internal functions