FF-132: Get wallet by external id (#154)

* Upgrade swag-wallets

* Add getWalletByExternalID

* Fix swag commit

* Bump swag-wallets

* Simplify getting internal id

* Use ff_external_id for id mapping

* Add test

* Remove redundant matching

* Switch back to using bender_client
This commit is contained in:
Toporkov Igor 2019-12-10 18:24:55 +03:00 committed by GitHub
parent 87eb4aca4c
commit fe90905f66
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 63 additions and 12 deletions

View File

@ -15,6 +15,7 @@
-export([check_in/2]).
-export([bind/3]).
-export([get_internal_id/2]).
-export([get_ff_internal_id/3]).
-export([construct_external_id/2]).
@ -68,6 +69,13 @@ get_internal_id(EntityName, ExternalID) ->
ID = create_id(EntityName, ExternalID),
get_(ID).
-spec get_ff_internal_id(entity_name(), ff_party:id(), external_id()) ->
check_result() | {error, notfound}.
get_ff_internal_id(Type, PartyID, ExternalID) ->
FistfulID = construct_external_id(PartyID, ExternalID),
get_internal_id(Type, FistfulID).
-spec construct_external_id(ff_party:id(), external_id()) ->
external_id().

View File

@ -25,6 +25,7 @@
-export([get_identity_challenge_event/2]).
-export([get_wallet/2]).
-export([get_wallet_by_external_id/2]).
-export([create_wallet/2]).
-export([get_wallet_account/2]).
-export([list_wallets/2]).
@ -247,6 +248,19 @@ get_identity_challenge_event(#{
get_wallet(WalletID, Context) ->
do(fun() -> to_swag(wallet, get_state(wallet, WalletID, Context)) end).
-spec get_wallet_by_external_id(external_id(), ctx()) -> result(map(),
{wallet, notfound} |
{wallet, unauthorized}
).
get_wallet_by_external_id(ExternalID, #{woody_context := WoodyContext} = Context) ->
AuthContext = wapi_handler_utils:get_auth_context(Context),
PartyID = get_party_id(AuthContext),
IdempotentKey = bender_client:get_idempotent_key(?BENDER_DOMAIN, wallet, PartyID, ExternalID),
case bender_client:get_internal_id(IdempotentKey, WoodyContext) of
{ok, WalletID, _} -> get_wallet(WalletID, Context);
{error, internal_id_not_found} -> {error, {wallet, notfound}}
end.
-spec create_wallet(params(), ctx()) -> result(map(),
invalid |
{identity, unauthorized} |
@ -966,6 +980,10 @@ process_stat_result(StatType, Result) ->
{error, {400, [], bad_request_error(invalidRequest, Reason)}}
end.
get_party_id(AuthContext) ->
{{PartyID, _}, _} = AuthContext,
PartyID.
get_time(Key, Req) ->
case genlib_map:get(Key, Req) of
Timestamp when is_binary(Timestamp) ->

View File

@ -177,6 +177,12 @@ process_request('GetWallet', #{'walletID' := WalletId}, Context, _Opts) ->
{error, {wallet, notfound}} -> wapi_handler_utils:reply_ok(404);
{error, {wallet, unauthorized}} -> wapi_handler_utils:reply_ok(404)
end;
process_request('GetWalletByExternalID', #{externalID := ExternalID}, Context, _Opts) ->
case wapi_wallet_ff_backend:get_wallet_by_external_id(ExternalID, Context) of
{ok, Wallet} -> wapi_handler_utils:reply_ok(200, Wallet);
{error, {wallet, notfound}} -> wapi_handler_utils:reply_ok(404);
{error, {wallet, unauthorized}} -> wapi_handler_utils:reply_ok(404)
end;
process_request('CreateWallet', #{'Wallet' := Params}, Context, Opts) ->
case wapi_wallet_ff_backend:create_wallet(Params, Context) of
{ok, Wallet = #{<<"id">> := WalletId}} ->

View File

@ -288,14 +288,10 @@ bender_to_fistful_sync(C) ->
ok = offset_ff_sequence(identity, FFSeqStart),
TargetID = integer_to_binary(FFSeqStart + BenderOffset),
{ok, #{<<"id">> := TargetID}} = wapi_wallet_ff_backend:create_identity(Params0, Ctx),
{ok, TargetID} = get_ff_internal_id(identity, Party, ExternalID).
{ok, TargetID} = ff_external_id:get_ff_internal_id(identity, Party, ExternalID).
%%
get_ff_internal_id(Type, PartyID, ExternalID) ->
FistfulID = ff_external_id:construct_external_id(PartyID, ExternalID),
ff_external_id:get_internal_id(Type, FistfulID).
offset_ff_sequence(_Type, 0) ->
ok;
offset_ff_sequence(Type, Amount) ->

View File

@ -24,6 +24,7 @@
-export([unknown_withdrawal_test/1]).
-export([quote_withdrawal_test/1]).
-export([not_allowed_currency_test/1]).
-export([get_wallet_by_external_id/1]).
-export([consume_eventsinks/1]).
@ -52,7 +53,8 @@ groups() ->
withdrawal_to_bank_card_test,
withdrawal_to_crypto_wallet_test,
withdrawal_to_ripple_wallet_test,
unknown_withdrawal_test
unknown_withdrawal_test,
get_wallet_by_external_id
]},
{quote, [], [
quote_encode_decode_test,
@ -409,6 +411,23 @@ woody_retry_test(C) ->
true = (Time > 3000000) and (Time < 6000000),
ok = application:set_env(wapi_woody_client, service_urls, Urls).
-spec get_wallet_by_external_id(config()) ->
test_return().
get_wallet_by_external_id(C) ->
Name = <<"Keyn Fawkes">>,
Provider = <<"quote-owner">>,
Class = ?ID_CLASS,
IdentityID = create_identity(Name, Provider, Class, C),
ExternalID = ?STRING,
WalletID = create_wallet(IdentityID, #{<<"externalID">> => ExternalID}, C),
{ok, Wallet} = call_api(
fun swag_client_wallet_wallets_api:get_wallet_by_external_id/3,
#{qs_val => #{<<"externalID">> => ExternalID}},
ct_helper:cfg(context, C)
),
WalletID = maps:get(<<"id">>, Wallet).
%%
-spec call_api(function(), map(), wapi_client_lib:context()) ->
@ -475,16 +494,20 @@ check_identity(Name, IdentityID, Provider, Class, C) ->
ok.
create_wallet(IdentityID, C) ->
{ok, Wallet} = call_api(
fun swag_client_wallet_wallets_api:create_wallet/3,
#{body => #{
create_wallet(IdentityID, #{}, C).
create_wallet(IdentityID, Params, C) ->
DefaultParams = #{
<<"name">> => <<"Worldwide PHP Awareness Initiative">>,
<<"identity">> => IdentityID,
<<"currency">> => <<"RUB">>,
<<"metadata">> => #{
?STRING => ?STRING
}
}},
}
},
{ok, Wallet} = call_api(
fun swag_client_wallet_wallets_api:create_wallet/3,
#{body => maps:merge(DefaultParams, Params)},
ct_helper:cfg(context, C)
),
maps:get(<<"id">>, Wallet).

@ -1 +1 @@
Subproject commit b8132552350b2473215a700192b1dc9d117140a6
Subproject commit 620133d70a8bcddf34dc862b4dfb3fedcf1cf055