mirror of
https://github.com/valitydev/fistful-server.git
synced 2024-11-06 02:35:18 +00:00
[WIP] A couple of renames / fixes
This commit is contained in:
parent
ff96b43bbd
commit
29ae8477ed
12
apps/ff_withdraw/src/ff_adapter.erl
Normal file
12
apps/ff_withdraw/src/ff_adapter.erl
Normal file
@ -0,0 +1,12 @@
|
||||
%%% Withdrawal adapter generic
|
||||
-module(ff_adapter).
|
||||
|
||||
%%
|
||||
%% Types
|
||||
%%
|
||||
|
||||
-type adapter() :: ff_woody_client:client().
|
||||
-type state() :: any().
|
||||
|
||||
-export_type([adapter/0]).
|
||||
-export_type([state/0]).
|
@ -5,7 +5,8 @@
|
||||
-include_lib("dmsl/include/dmsl_withdrawals_provider_adapter_thrift.hrl").
|
||||
|
||||
%% API
|
||||
-export([process_withdrawal/5]).
|
||||
|
||||
-export([process_withdrawal/4]).
|
||||
|
||||
%%
|
||||
%% Internal types
|
||||
@ -14,16 +15,27 @@
|
||||
-type id() :: machinery:id().
|
||||
-type identity_id() :: id().
|
||||
|
||||
-type withdrawal() :: ff_adpt_withdrawal:withdrawal().
|
||||
-type destination() :: ff_adpt_withdrawal:destination().
|
||||
-type cash() :: ff_adpt_withdrawal:cash().
|
||||
-type destination() :: ff_destination:destination().
|
||||
-type identity() :: ff_identity:identity().
|
||||
-type cash() :: ff_transfer:body().
|
||||
|
||||
-type adapter() :: ff_wthadpt:adapter().
|
||||
-type withdrawal() :: #{
|
||||
id => binary(),
|
||||
destination => destination(),
|
||||
cash => cash(),
|
||||
sender => identity() | undefined,
|
||||
receiver => identity() | undefined
|
||||
}.
|
||||
|
||||
-export_type([withdrawal/0]).
|
||||
|
||||
-type adapter() :: ff_adapter:adapter().
|
||||
-type intent() :: {finish, status()} | {sleep, timer()}.
|
||||
-type status() :: {success, trx_info()} | {failure, ff_adpt:failure()}.
|
||||
-type status() :: {success, trx_info()} | {failure, failure()}.
|
||||
-type timer() :: dmsl_base_thrift:'Timer'().
|
||||
-type trx_info() :: dmsl_domain_thrift:'TransactionInfo'().
|
||||
-type adapter_state() :: ff_adpt:adapter_state().
|
||||
-type failure() :: dmsl_domain_thrift:'Failure'().
|
||||
-type adapter_state() :: ff_adapter:state().
|
||||
-type process_result() :: {ok, intent(), adapter_state()} | {ok, intent()}.
|
||||
|
||||
-type domain_withdrawal() :: dmsl_withdrawals_provider_adapter_thrift:'Withdrawal'().
|
||||
@ -31,22 +43,22 @@
|
||||
-type domain_currency() :: dmsl_domain_thrift:'Currency'().
|
||||
-type domain_destination() :: dmsl_withdrawals_provider_adapter_thrift:'Destination'().
|
||||
-type domain_identity() :: dmsl_withdrawals_provider_adapter_thrift:'Identity'().
|
||||
|
||||
-type backend() :: machinery:backend(_).
|
||||
-type domain_internal_state() :: dmsl_withdrawals_provider_adapter_thrift:'InternalState'().
|
||||
|
||||
%%
|
||||
%% API
|
||||
%%
|
||||
|
||||
-spec process_withdrawal(Adapter, Withdrawal, ASt, AOpt, Be) -> process_result() when
|
||||
Adapter :: adapter(),
|
||||
Withdrawal :: withdrawal(),
|
||||
ASt :: adapter_state(),
|
||||
AOpt :: map(),
|
||||
Be :: backend().
|
||||
process_withdrawal(Adapter, Withdrawal, ASt, AOpt, Be) ->
|
||||
DomainWithdrawal = build_and_encode_withdrawal(Withdrawal, Be),
|
||||
{ok, Result} = call(Adapter, 'ProcessWithdrawal', [DomainWithdrawal, ASt, AOpt]),
|
||||
-spec process_withdrawal(Adapter, Withdrawal, ASt, AOpt) ->
|
||||
process_result() when
|
||||
Adapter :: adapter(),
|
||||
Withdrawal :: withdrawal(),
|
||||
ASt :: adapter_state(),
|
||||
AOpt :: map().
|
||||
|
||||
process_withdrawal(Adapter, Withdrawal, ASt, AOpt) ->
|
||||
DomainWithdrawal = encode_withdrawal(Withdrawal),
|
||||
{ok, Result} = call(Adapter, 'ProcessWithdrawal', [DomainWithdrawal, encode_adapter_state(ASt), AOpt]),
|
||||
decode_result(Result).
|
||||
|
||||
%%
|
||||
@ -59,28 +71,27 @@ call(Adapter, Function, Args) ->
|
||||
|
||||
%% Encoders
|
||||
|
||||
-spec build_and_encode_withdrawal(Withdrawal, Be) -> domain_withdrawal() when
|
||||
Withdrawal :: withdrawal(),
|
||||
Be :: backend().
|
||||
build_and_encode_withdrawal(Withdrawal, Be) ->
|
||||
-spec encode_withdrawal(Withdrawal) -> domain_withdrawal() when
|
||||
Withdrawal :: withdrawal().
|
||||
encode_withdrawal(Withdrawal) ->
|
||||
#{
|
||||
id := WId,
|
||||
id := ID,
|
||||
cash := Cash,
|
||||
destination := Dest,
|
||||
sender := Sender,
|
||||
receiver := Receiver
|
||||
} = Withdrawal,
|
||||
#wthadpt_Withdrawal{
|
||||
id = WId,
|
||||
id = ID,
|
||||
body = encode_body(Cash),
|
||||
destination = encode_destination(Dest),
|
||||
sender = fetch_and_encode_identity(Sender, Be),
|
||||
receiver = fetch_and_encode_identity(Receiver, Be)
|
||||
sender = encode_identity(Sender),
|
||||
receiver = encode_identity(Receiver)
|
||||
}.
|
||||
|
||||
-spec encode_body(cash()) -> domain_cash().
|
||||
encode_body({Amount, CurrencyId}) ->
|
||||
Currency = ff_currency:get(CurrencyId),
|
||||
encode_body({Amount, CurrencyID}) ->
|
||||
Currency = ff_currency:get(CurrencyID),
|
||||
DomainCurrency = encode_currency(Currency),
|
||||
#wthadpt_Cash{amount = Amount, currency = DomainCurrency}.
|
||||
|
||||
@ -114,18 +125,23 @@ encode_destination(Destination) ->
|
||||
masked_pan = MaskedPan
|
||||
}}.
|
||||
|
||||
-spec fetch_and_encode_identity
|
||||
(identity_id(), backend()) -> domain_identity();
|
||||
(undefined, backend()) -> undefined.
|
||||
fetch_and_encode_identity(undefined, _Be) ->
|
||||
-spec encode_identity
|
||||
(identity_id()) -> domain_identity();
|
||||
(undefined) -> undefined.
|
||||
encode_identity(undefined) ->
|
||||
undefined;
|
||||
fetch_and_encode_identity(IdentityId, _Be) ->
|
||||
% {ok, Identity} = ff_identity:get(IdentityId, Be),
|
||||
encode_identity(IdentityID) ->
|
||||
% TODO: Add documents and contract fields
|
||||
#wthdm_Identity{
|
||||
id = IdentityId
|
||||
id = IdentityID
|
||||
}.
|
||||
|
||||
-spec encode_adapter_state(adapter_state()) -> domain_internal_state().
|
||||
encode_adapter_state(undefined) ->
|
||||
{nl, #msgpack_Nil{}};
|
||||
encode_adapter_state(ASt) ->
|
||||
ASt.
|
||||
|
||||
-spec decode_result(dmsl_withdrawals_provider_adapter_thrift:'ProcessResult'()) -> process_result().
|
||||
decode_result(#wthadpt_ProcessResult{intent = Intent, next_state = undefined}) ->
|
||||
{ok, decode_intent(Intent)};
|
@ -1,15 +0,0 @@
|
||||
%%% Withdrawal adapter generic
|
||||
-module(ff_adpt).
|
||||
|
||||
%%
|
||||
%% Types
|
||||
%%
|
||||
|
||||
-type adapter() :: atom().
|
||||
-type adapter_state() :: any().
|
||||
|
||||
-type withdrawal() :: ff_adpt_withdrawal:withdrawal().
|
||||
|
||||
-export_type([adapter/0]).
|
||||
-export_type([withdrawal/0]).
|
||||
-export_type([adapter_state/0]).
|
@ -1,22 +0,0 @@
|
||||
-module(ff_adpt_withdrawal).
|
||||
|
||||
%%
|
||||
%% Types
|
||||
%%
|
||||
|
||||
-type destination() :: ff_destination:destination().
|
||||
-type identity() :: ff_identity:identity().
|
||||
-type cash() :: ff_transfer:body().
|
||||
|
||||
-type withdrawal() :: #{
|
||||
id => binary(),
|
||||
destination => destination(),
|
||||
cash => cash(),
|
||||
sender => identity() | undefined,
|
||||
receiver => identity() | undefined
|
||||
}.
|
||||
|
||||
-export_type([destination/0]).
|
||||
-export_type([identity/0]).
|
||||
-export_type([cash/0]).
|
||||
-export_type([withdrawal/0]).
|
@ -1,10 +1,10 @@
|
||||
%%%
|
||||
%%% Withdrawal session machine
|
||||
%%%
|
||||
-module(ff_adpt_session_machine).
|
||||
-module(ff_withdrawal_session_machine).
|
||||
-behaviour(machinery).
|
||||
|
||||
-define(NS, adpt_session).
|
||||
-define(NS, 'withdrawal/session').
|
||||
|
||||
%% API
|
||||
-export([create/4]).
|
||||
@ -28,16 +28,14 @@
|
||||
|
||||
-type session_result() :: {success, trx_info()} | {failed, ff_adpt:failure()}.
|
||||
|
||||
-type session_status() :: new
|
||||
| active
|
||||
-type session_status() :: active
|
||||
| {finished, session_result()}.
|
||||
|
||||
-type ev() :: {created, session()}
|
||||
| started
|
||||
| {next_state, ff_adpt:adapter_state()}
|
||||
| {finished, session_result()}.
|
||||
|
||||
-type adapter() :: {ff_adpt:adapter(), map()}.
|
||||
-type adapter() :: {ff_adapter:adapter(), Opts :: #{binary() => binary()}}.
|
||||
|
||||
%%
|
||||
%% Internal types
|
||||
@ -49,7 +47,7 @@
|
||||
|
||||
-type auxst() :: #{}.
|
||||
|
||||
-type withdrawal() :: ff_adpt_withdrawal:withdrawal().
|
||||
-type withdrawal() :: ff_adapter_withdrawal:withdrawal().
|
||||
-type machine() :: machinery:machine(ev(), auxst()).
|
||||
-type result() :: machinery:result(ev(), auxst()).
|
||||
-type handler_opts() :: machinery:handler_opts().
|
||||
@ -58,7 +56,7 @@
|
||||
|
||||
-type st() :: #{
|
||||
session => session(),
|
||||
adapter_state => ff_adpt:adapter_state()
|
||||
adapter_state => ff_adapter:state()
|
||||
}.
|
||||
|
||||
%% Pipeline
|
||||
@ -69,23 +67,22 @@
|
||||
%% API
|
||||
%%
|
||||
|
||||
-spec create(Id, Adapter, Withdrawal, Be) -> {ok, session()} | Error when
|
||||
Id :: id(),
|
||||
-spec create(ID, Adapter, Withdrawal, Be) -> ok | Error when
|
||||
ID :: id(),
|
||||
Adapter :: adapter(),
|
||||
Withdrawal :: withdrawal(),
|
||||
Be :: backend(),
|
||||
Error :: {error, exists}.
|
||||
create(Id, Adapter, Withdrawal, Be) ->
|
||||
Session = create_session(Id, Adapter, Withdrawal),
|
||||
create(ID, Adapter, Withdrawal, Be) ->
|
||||
Session = create_session(ID, Adapter, Withdrawal),
|
||||
do(fun () ->
|
||||
ok = unwrap(machinery:start(?NS, Id, Session, Be)),
|
||||
unwrap(get(Id, Be))
|
||||
unwrap(machinery:start(?NS, ID, Session, Be))
|
||||
end).
|
||||
|
||||
-spec get(id(), backend()) -> {ok, session()} | {error, any()}.
|
||||
get(Id, Be) ->
|
||||
-spec get(id(), backend()) -> {ok, session()} | {error, notfound}.
|
||||
get(ID, Be) ->
|
||||
do(fun () ->
|
||||
session(collapse(unwrap(machinery:get(?NS, Id, Be))))
|
||||
session(collapse(unwrap(machinery:get(?NS, ID, Be))))
|
||||
end).
|
||||
|
||||
%%
|
||||
@ -97,7 +94,7 @@ get(Id, Be) ->
|
||||
init(Session, #{}, _, _Opts) ->
|
||||
#{
|
||||
events => emit_ts_event({created, Session}),
|
||||
action => timer_action({timeout, 0})
|
||||
action => continue
|
||||
}.
|
||||
|
||||
-spec process_timeout(machine(), handler_args(), handler_opts()) ->
|
||||
@ -121,8 +118,8 @@ process_session(#{session := #{status := active} = Session} = St) ->
|
||||
adapter := {Adapter, AdapterOpts},
|
||||
withdrawal := Withdrawal
|
||||
} = Session,
|
||||
ASt = maps:get(adapter_state, St, []),
|
||||
case ff_adpt_client:process_withdrawal(Adapter, Withdrawal, ASt, AdapterOpts) of
|
||||
ASt = maps:get(adapter_state, St, undefined),
|
||||
case ff_adapter_withdrawal:process_withdrawal(Adapter, Withdrawal, ASt, AdapterOpts) of
|
||||
{ok, Intent, NextState} ->
|
||||
process_intent(Intent, NextState);
|
||||
{ok, Intent} ->
|
||||
@ -147,12 +144,12 @@ process_intent({sleep, Timer}) ->
|
||||
%%
|
||||
|
||||
-spec create_session(id(), adapter(), ff_adpt:withdrawal()) -> session().
|
||||
create_session(Id, Adapter, Withdrawal) ->
|
||||
create_session(ID, Adapter, Withdrawal) ->
|
||||
#{
|
||||
id => Id,
|
||||
id => ID,
|
||||
withdrawal => Withdrawal,
|
||||
adapter => Adapter,
|
||||
status => new
|
||||
status => active
|
||||
}.
|
||||
|
||||
-spec set_session_status(session_status(), session()) -> session().
|
||||
@ -181,8 +178,6 @@ merge_event({_ID, _Ts, {ev, _Ts, EvBody}}, St) ->
|
||||
-spec merge_event_body(ev(), st()) -> st().
|
||||
merge_event_body({created, Session}, St) ->
|
||||
St#{session => Session};
|
||||
merge_event_body(started, #{session := Session} = St) ->
|
||||
St#{session => set_session_status(active, Session)};
|
||||
merge_event_body({next_state, AdapterState}, St) ->
|
||||
St#{adapter_state => AdapterState};
|
||||
merge_event_body({finished, Result}, #{session := Session} = St) ->
|
@ -56,8 +56,10 @@ new(Url) when is_binary(Url); is_list(Url) ->
|
||||
{ok, woody:result()} |
|
||||
{exception, woody_error:business_error()}.
|
||||
|
||||
call(ServiceID, Request) ->
|
||||
call(ServiceID, Request) when is_atom(ServiceID) ->
|
||||
Client = get_service_client(ServiceID),
|
||||
woody_client:call(Request, Client, ff_woody_ctx:get());
|
||||
call(Client, Request) when is_map(Client) ->
|
||||
woody_client:call(Request, Client, ff_woody_ctx:get()).
|
||||
|
||||
%%
|
||||
|
Loading…
Reference in New Issue
Block a user