Skip to content

Implements GetWalletAccount operation bypassing fistful service #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
{thrift, {git, "https://github.com/valitydev/thrift-erlang.git", {tag, "v1.0.0"}}},
{woody, {git, "https://github.com/valitydev/woody_erlang.git", {tag, "v1.1.0"}}},
{dmt_client, {git, "https://github.com/valitydev/dmt-client.git", {tag, "v2.0.0"}}},
{damsel, {git, "https://github.com/valitydev/damsel.git", {tag, "v2.2.0"}}},
{damsel, {git, "https://github.com/valitydev/damsel.git", {branch, "refactor_party_objects"}}},
{identdocstore_proto, {git, "https://github.com/valitydev/identdocstore-proto.git", {branch, "master"}}},
{fistful_proto, {git, "https://github.com/valitydev/fistful-proto.git", {tag, "v2.0.0"}}},
{fistful_reporter_proto, {git, "https://github.com/valitydev/fistful-reporter-proto.git", {branch, "master"}}},
Expand Down
4 changes: 2 additions & 2 deletions rebar.lock
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
{<<"cowlib">>,{pkg,<<"cowlib">>,<<"2.11.0">>},2},
{<<"damsel">>,
{git,"https://github.com/valitydev/damsel.git",
{ref,"ba7414811590859d058817b8f22d2e9c22f627f8"}},
{ref,"6a4a1c927d96eae3d6c2adcf46a39485854d1a83"}},
0},
{<<"dmt_client">>,
{git,"https://github.com/valitydev/dmt-client.git",
Expand All @@ -41,7 +41,7 @@
0},
{<<"fistful_reporter_proto">>,
{git,"https://github.com/valitydev/fistful-reporter-proto.git",
{ref,"6d5695d2e8aa13247f93451937adefa70c6edeca"}},
{ref,"533e95934683665bc840a45ca8735b87a3e3e2ba"}},
0},
{<<"genlib">>,
{git,"https://github.com/valitydev/genlib.git",
Expand Down
38 changes: 11 additions & 27 deletions src/wapi_domain_backend.erl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

-export([get_currency/1]).
-export([get_party_config/1]).
-export([get_wallet_config/1]).
-export([get_object/1]).
-export([get_object/2]).

%%

Expand All @@ -23,27 +24,14 @@
-spec get_party_config(id()) -> {ok, {map(), id()}} | {error, notfound}.
get_party_config(PartyID) ->
do(fun() ->
Party = unwrap(object({party_config, #domain_PartyConfigRef{id = PartyID}})),
{#{<<"id">> => Party#domain_PartyConfig.id}, PartyID}
end).

-spec get_wallet_config(id()) -> {ok, {map(), id()}} | {error, notfound}.
get_wallet_config(WalletID) ->
do(fun() ->
Wallet = unwrap(object({wallet_config, #domain_WalletConfigRef{id = WalletID}})),
{
#{
<<"id">> => Wallet#domain_WalletConfig.id,
<<"partyID">> => Wallet#domain_WalletConfig.party_id
},
Wallet#domain_WalletConfig.party_id
}
_Party = unwrap(get_object({party_config, #domain_PartyConfigRef{id = PartyID}})),
{#{<<"id">> => PartyID}, PartyID}
end).

-spec get_currency(id()) -> {ok, response_data()} | {error, notfound}.
get_currency(ID) ->
do(fun() ->
Currency = unwrap(object({currency, #domain_CurrencyRef{symbolic_code = ID}})),
Currency = unwrap(get_object({currency, #domain_CurrencyRef{symbolic_code = ID}})),
#{
<<"id">> => genlib_string:to_upper(genlib:to_binary(ID)),
<<"name">> => Currency#domain_Currency.name,
Expand All @@ -52,18 +40,14 @@ get_currency(ID) ->
}
end).

%%
%% Internal
%%

-spec object(dmt_client:object_ref()) -> {ok, object_data()} | {error, notfound}.
object(ObjectRef) ->
object(latest, ObjectRef).
-spec get_object(dmt_client:object_ref()) -> {ok, object_data()} | {error, notfound}.
get_object(ObjectRef) ->
get_object(latest, ObjectRef).

-spec object(dmt_client:version(), dmt_client:object_ref()) -> {ok, object_data()} | {error, notfound}.
object(Ref, {Type, ObjectRef}) ->
-spec get_object(dmt_client:version(), dmt_client:object_ref()) -> {ok, object_data()} | {error, notfound}.
get_object(Ref, {Type, ObjectRef}) ->
try dmt_client:checkout_object(Ref, {Type, ObjectRef}) of
#domain_conf_v2_VersionedObject{object = {Type, {_RecordName, ObjectRef, ObjectData}}} ->
#domain_conf_v2_VersionedObject{object = {Type, {_, ObjectRef, ObjectData}}} ->
{ok, ObjectData}
catch
#domain_conf_v2_ObjectNotFound{} ->
Expand Down
86 changes: 86 additions & 0 deletions src/wapi_wallet_backend.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
-module(wapi_wallet_backend).

-type handler_context() :: wapi_handler_utils:handler_context().
-type response_data() :: wapi_handler_utils:response_data().
-type id() :: binary().

-export([get/2]).
-export([get_account/2]).

-include_lib("damsel/include/dmsl_domain_thrift.hrl").
-include_lib("damsel/include/dmsl_payproc_thrift.hrl").

-spec get(id(), handler_context()) -> {ok, response_data(), id()} | {error, {wallet, notfound}}.
get(WalletID, _HandlerContext) ->
case get_wallet_config(WalletID) of
{ok, WalletConfig} ->
{ok, unmarshal(wallet, {WalletID, WalletConfig}), WalletConfig#domain_WalletConfig.party_id};
{error, notfound} ->
{error, {wallet, notfound}}
end.

-spec get_account(id(), handler_context()) -> {ok, response_data()} | {error, {wallet, notfound}}.
get_account(WalletID, HandlerContext) ->
case get_wallet_config(WalletID) of
{ok, #domain_WalletConfig{party_id = PartyID, account = #domain_WalletAccount{settlement = AccountID}}} ->
Request = {config_manager, 'GetAccountState', {PartyID, AccountID}},
case wapi_handler_utils:service_call(Request, HandlerContext) of
{ok, AccountBalanceThrift} ->
{ok, unmarshal(account_state, AccountBalanceThrift)};
{exception, #payproc_PartyNotFound{}} ->
{error, {wallet, notfound}};
{exception, #payproc_AccountNotFound{}} ->
{error, {wallet, notfound}}
end;
{error, notfound} ->
{error, {wallet, notfound}}
end.

%% Internal

get_wallet_config(WalletID) ->
ObjectRef = {wallet_config, #domain_WalletConfigRef{id = WalletID}},
wapi_domain_backend:get_object(ObjectRef).

%% Marshaling

unmarshal(
wallet,
{WalletID, #domain_WalletConfig{
name = Name,
block = Blocking,
account = #domain_WalletAccount{currency = #domain_CurrencyRef{symbolic_code = Currency}},
party_id = PartyID
}}
) ->
%% FIXME Temporary stub
CreatedAt = ~b"1970-01-01T00:00:00Z",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

кек)

genlib_map:compact(#{
<<"id">> => unmarshal(id, WalletID),
<<"name">> => unmarshal(string, Name),
<<"createdAt">> => CreatedAt,
<<"isBlocked">> => unmarshal(blocking, Blocking),
<<"party">> => PartyID,
<<"currency">> => Currency
});
unmarshal(blocking, {unblocked, _}) ->
false;
unmarshal(blocking, {blocked, _}) ->
true;
unmarshal(account_state, #payproc_AccountState{
own_amount = OwnAmount,
available_amount = AvailableAmount,
currency = #domain_Currency{symbolic_code = CurrencyCode}
}) ->
#{
<<"own">> => #{
<<"amount">> => OwnAmount,
<<"currency">> => CurrencyCode
},
<<"available">> => #{
<<"amount">> => AvailableAmount,
<<"currency">> => CurrencyCode
}
};
unmarshal(T, V) ->
wapi_codec:unmarshal(T, V).
33 changes: 27 additions & 6 deletions src/wapi_wallet_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,25 @@ mask_notfound(Resolution) ->
%% Providers
-spec prepare(operation_id(), request_data(), handler_context(), handler_opts()) -> {ok, request_state()}.

%% Wallets
prepare('GetWallet' = OperationID, #{'walletID' := WalletID}, Context, _Opts) ->
{ResultWallet, ResultWalletOwner} =
case wapi_wallet_backend:get(WalletID, Context) of
{ok, Wallet, Owner} -> {Wallet, Owner};
{error, {wallet, notfound}} -> {undefined, undefined}
end,
Authorize = fun() ->
Prototypes = [
{operation, #{wallet => WalletID, id => OperationID}},
{wallet, [wapi_bouncer_context:build_wallet_entity(wallet, ResultWallet, {party, ResultWalletOwner})]}
],
Resolution = mask_notfound(wapi_auth:authorize_operation(Prototypes, Context)),
{ok, Resolution}
end,
Process = fun() ->
wapi_handler_utils:reply_ok(200, ResultWallet)
end,
{ok, #{authorize => Authorize, process => Process}};
prepare('GetWalletAccount' = OperationID, #{'walletID' := WalletID}, Context, _Opts) ->
AuthContext = build_auth_context([{wallet, WalletID}], [], Context),
Authorize = fun() ->
Expand All @@ -83,8 +102,10 @@ prepare('GetWalletAccount' = OperationID, #{'walletID' := WalletID}, Context, _O
{ok, Resolution}
end,
Process = fun() ->
%% TODO: implement from new party service
wapi_handler_utils:reply_ok(404)
case wapi_wallet_backend:get_account(WalletID, Context) of
{ok, WalletAccount} -> wapi_handler_utils:reply_ok(200, WalletAccount);
{error, {wallet, notfound}} -> wapi_handler_utils:reply_ok(404)
end
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

надо бы еще глянуть что контекст для кошелька норм собирается - там же почти тоже самое - нужно вычитать кошелек и ео отмаршалить в баунсер контекст - не помню как я это сделал и делал ли

end,
{ok, #{authorize => Authorize, process => Process}};
%% Destinations
Expand Down Expand Up @@ -799,11 +820,11 @@ build_auth_context({party, PartyID}, _Context) ->
{error, notfound} -> {undefined, undefined}
end,
{party, {PartyID, ResultParty, ResultPartyOwner}};
build_auth_context({wallet, WalletID}, _Context) ->
build_auth_context({wallet, WalletID}, Context) ->
{ResultWallet, ResultWalletOwner} =
case wapi_domain_backend:get_wallet_config(WalletID) of
{ok, {WalletConfig, Owner}} -> {WalletConfig, Owner};
{error, notfound} -> {undefined, undefined}
case wapi_wallet_backend:get(WalletID, Context) of
{ok, Wallet, Owner} -> {Wallet, Owner};
{error, {wallet, notfound}} -> {undefined, undefined}
end,
{wallet, {WalletID, ResultWallet, ResultWalletOwner}};
build_auth_context({destination, DestinationID}, Context) ->
Expand Down
5 changes: 4 additions & 1 deletion src/wapi_woody_client.erl
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ get_service_modname(fistful_destination) ->
get_service_modname(fistful_withdrawal) ->
{fistful_wthd_thrift, 'Management'};
get_service_modname(webhook_manager) ->
{fistful_webhooker_thrift, 'WebhookManager'}.
{fistful_webhooker_thrift, 'WebhookManager'};
%% FIXME naming
get_service_modname(config_manager) ->
{dmsl_payproc_thrift, 'ConfigManagement'}.

-spec get_service_deadline(service_name()) -> undefined | woody_deadline:deadline().
get_service_deadline(ServiceName) ->
Expand Down
82 changes: 40 additions & 42 deletions test/wapi_ct_helper.erl
Original file line number Diff line number Diff line change
Expand Up @@ -133,52 +133,48 @@ start_app(woody = AppName) ->
{acceptors_pool_size, 4}
]);
start_app({dmt_client = AppName, SupPid}) ->
WalletConfig = #domain_WalletConfig{
id = ?STRING,
created_at = wapi_time:rfc3339(),
blocking =
{unblocked, #domain_Unblocked{
reason = <<"">>,
since = wapi_time:rfc3339()
}},
suspension =
{active, #domain_Active{
since = wapi_time:rfc3339()
}},
details = #domain_Details{
name = <<"Test Wallet">>,
description = <<"Test description">>
},
currency_configs = #{
#domain_CurrencyRef{symbolic_code = <<"RUB">>} => #domain_WalletCurrencyConfig{
WalletConfigObject = #domain_WalletConfigObject{
ref = #domain_WalletConfigRef{id = ?STRING},
data = #domain_WalletConfig{
name = ?STRING,
block =
{unblocked, #domain_Unblocked{
reason = <<"">>,
since = wapi_time:rfc3339()
}},
suspension =
{active, #domain_Active{
since = wapi_time:rfc3339()
}},
payment_institution = #domain_PaymentInstitutionRef{id = 1},
terms = #domain_TermSetHierarchyRef{id = 1},
account = #domain_WalletAccount{
currency = #domain_CurrencyRef{symbolic_code = <<"RUB">>},
settlement = ?INTEGER
}
},
payment_institution = #domain_PaymentInstitutionRef{id = 1},
terms = #domain_TermSetHierarchyRef{id = 1},
party_id = ?STRING
},
party_id = ?STRING
}
},
WalletConfigObject = #domain_WalletConfigObject{ref = #domain_WalletConfigRef{id = ?STRING}, data = WalletConfig},
PartyConfig = #domain_PartyConfig{
id = ?STRING,
contact_info = #domain_PartyContactInfo{
registration_email = <<"[email protected]">>
},
created_at = wapi_time:rfc3339(),
blocking =
{unblocked, #domain_Unblocked{
reason = <<"">>,
since = wapi_time:rfc3339()
}},
suspension =
{active, #domain_Active{
since = wapi_time:rfc3339()
}},
shops = [],
wallets = [#domain_WalletConfigRef{id = ?STRING}]
PartyConfigObject = #domain_PartyConfigObject{
ref = #domain_PartyConfigRef{id = ?STRING},
data = #domain_PartyConfig{
name = ?STRING,
block =
{unblocked, #domain_Unblocked{
reason = <<"">>,
since = wapi_time:rfc3339()
}},
suspension =
{active, #domain_Active{
since = wapi_time:rfc3339()
}},
shops = [],
wallets = [#domain_WalletConfigRef{id = ?STRING}],
contact_info = #domain_PartyContactInfo{
registration_email = <<"[email protected]">>
}
}
},
PartyConfigObject = #domain_PartyConfigObject{ref = #domain_PartyConfigRef{id = ?STRING}, data = PartyConfig},
Urls = mock_services_(
[
{domain_config_client, fun
Expand Down Expand Up @@ -373,6 +369,8 @@ mock_service_handler({ServiceName = domain_config, Fun}) ->
mock_service_handler(ServiceName, {dmsl_domain_conf_v2_thrift, 'Repository'}, Fun);
mock_service_handler({ServiceName = domain_config_client, Fun}) ->
mock_service_handler(ServiceName, {dmsl_domain_conf_v2_thrift, 'RepositoryClient'}, Fun);
mock_service_handler({ServiceName = config_manager, Fun}) ->
mock_service_handler(ServiceName, {dmsl_payproc_thrift, 'ConfigManagement'}, Fun);
mock_service_handler({ServiceName, Fun}) ->
mock_service_handler(ServiceName, wapi_woody_client:get_service_modname(ServiceName), Fun);
mock_service_handler({ServiceName, WoodyService, Fun}) ->
Expand Down
2 changes: 0 additions & 2 deletions test/wapi_wallet_dummy_data.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,6 @@

-define(LAST_DIGITS(CardNumber), string:slice(CardNumber, 12)).

-define(DESTINATION_STATUS, {authorized, #destination_Authorized{}}).

-define(DESTINATION(PartyID), ?DESTINATION(PartyID, ?RESOURCE_BANK_CARD)).

-define(DESTINATION(PartyID, Resource), #destination_DestinationState{
Expand Down
Loading
Loading