Skip to content

Commit

Permalink
KAZOO-544: Re-adapt cb_transactionis for new transaction types
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Defebvre committed Mar 7, 2013
1 parent 86a04de commit 092079d
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 103 deletions.
140 changes: 39 additions & 101 deletions whistle_apps/apps/crossbar/src/modules/cb_transactions.erl
Expand Up @@ -16,6 +16,12 @@

-include("src/crossbar.hrl").

%% 1 month
-define(FETCH_DEFAULT, 60*60*24*30).
%% 1 year
-define(FETCH_MAX, 60*60*24*30*12).


%%%===================================================================
%%% API
%%%===================================================================
Expand Down Expand Up @@ -77,64 +83,49 @@ validate(#cb_context{req_verb = <<"get">>, query_json=Query}=Context) ->
Reason = wh_json:get_value(<<"reason">>, Query, undefined),
case Reason of
<<"no_call">> ->
Reasons = wh_transaction:get_reasons(false),
fetch_by_date_and_reason(From, Reasons, Context);
Reasons = wht_util:reasons(2000),
fetch(From, Context, Reasons);
_ ->
fetch_by_date(From, Context)
fetch(From, Context)
end.

validate(#cb_context{req_verb = <<"get">>, account_id=AccountId}=Context, <<"current_balance">>) ->
Balance = wh_transactions:get_current_balance(AccountId),
Balance = wht_util:units_to_dollars(wht_util:current_balance(AccountId)),
JObj = wh_json:from_list([{<<"balance">>, Balance}]),
Context#cb_context{resp_status=success, resp_data=JObj};
validate(#cb_context{req_verb = <<"get">>, account_id=AccountId}=Context, TransactionId) ->
try wh_transaction:fetch(AccountId, TransactionId) of
Transaction ->
JObj = clean_json_obj(wh_transaction:to_json(Transaction)),
Context#cb_context{resp_status=success, resp_data=JObj}
catch
_:_ ->
cb_context:add_system_error(bad_identifier, [{details, <<"Unknow transaction ID">>}], Context)
end.
validate(Context, _) ->
cb_context:add_system_error(bad_identifier, Context).


%%--------------------------------------------------------------------
%% @private
%% @doc
%%
%% @end
%%--------------------------------------------------------------------
-spec fetch_by_date/2 :: (integer(), #cb_context{}) -> #cb_context{}.
fetch_by_date(From, Context) ->
-spec fetch/2 :: (integer(), #cb_context{}) -> #cb_context{}.
-spec fetch/3 :: (integer(), #cb_context{}, ne_binary()) -> #cb_context{}.
fetch(From, Context) ->
case validate_date(From) of
{true, Date} ->
Resp = fetch_since(Date, Context),
send_resp(Resp, Context);
filter(Date, Context);
{false, undefined} ->
Month = (wh_util:current_tstamp() - 60*60*24*30),
Resp = fetch_since(Month, Context),
send_resp(Resp, Context);
Month = (wh_util:current_tstamp() - ?FETCH_DEFAULT),
filter(Month, Context);
{false, R} ->
cb_context:add_validation_error(<<"created_from">>
,<<"date_range">>
,R
,Context
)
end.

%%--------------------------------------------------------------------
%% @private
%% @doc
%%
%% @end
%%--------------------------------------------------------------------
-spec fetch_by_date_and_reason/3 :: (integer(), ne_binary(), #cb_context{}) -> #cb_context{}.
fetch_by_date_and_reason(From, Reason, Context) ->
fetch(From, Context, Reason) ->
case validate_date(From) of
{true, Date} ->
filter_by_date_reason(Date, Reason, Context);
filter(Date, Context, Reason);
{false, undefined} ->
Month = (wh_util:current_tstamp() - 60*60*24*30),
filter_by_date_reason(Month, Reason, Context);
Month = (wh_util:current_tstamp() - ?FETCH_DEFAULT),
filter(Month, Context, Reason);
{false, R} ->
cb_context:add_validation_error(<<"created_from">>
,<<"date_range">>
Expand All @@ -149,45 +140,39 @@ fetch_by_date_and_reason(From, Reason, Context) ->
%%
%% @end
%%--------------------------------------------------------------------
-spec fetch_since/2 :: (integer(), cb_context:context()) -> cb_context:context().
fetch_since(Date, #cb_context{account_id=AccountId}=Context) ->
-spec filter/2 :: (integer(), cb_context:context()) -> cb_context:context().
-spec filter/3 :: (integer(), #cb_context{}, ne_binary()) -> #cb_context{}.
filter(Date, #cb_context{account_id=AccountId}=Context) ->
try wh_transactions:fetch_since(AccountId, Date) of
Transactions ->
{ok, Transactions}
send_resp({ok, Transactions}, Context)
catch
_:_ ->
{error, Context}
send_resp({error, Context}, Context)
end.

%%--------------------------------------------------------------------
%% @private
%% @doc
%%
%% @end
%%--------------------------------------------------------------------
-spec filter_by_date_reason/3 :: (integer(), ne_binary(), #cb_context{}) -> #cb_context{}.
filter_by_date_reason(Date, Reason, Context) ->
case fetch_since(Date, Context) of
{ok, Transactions} ->
filter(Date, #cb_context{account_id=AccountId}=Context, Reason) ->
try wh_transactions:fetch_since(AccountId, Date) of
Transactions ->
Filtered = wh_transactions:filter_by_reason(Reason, Transactions),
send_resp({ok, Filtered}, Context);
Error ->
send_resp(Error, Context)
send_resp({ok, Filtered}, Context)
catch
_:_ ->
send_resp({error, Context}, Context)
end.

%%--------------------------------------------------------------------
%% @private
%% @private
%% @doc
%%
%% @end
%%--------------------------------------------------------------------
send_resp(Resp, Context) ->
case Resp of
{ok, Transactions} ->
JObj = transactions_to_jobj(Transactions),
JObj = wh_transactions:to_public_json(Transactions),
Context#cb_context{resp_status=success, resp_data=JObj};
{error, C} ->
cb_context:add_system_error(bad_identifier, [{details,<<"Unknow transaction ID">>}], C)
cb_context:add_system_error(bad_identifier, [{details,<<"something went wrong while fetching the transaction">>}], C)
end.


Expand All @@ -202,7 +187,7 @@ validate_date(undefined) ->
{false, undefined};
validate_date(Date) when is_integer(Date) ->
Now = wh_util:current_tstamp(),
Max = 60*60*24*30*12,
Max = ?FETCH_MAX,
Diff = Now - Date,
case {Diff < 0, Diff > Max} of
{true, _} ->
Expand All @@ -220,50 +205,3 @@ validate_date(Date) ->
_:_ ->
{false, <<"created_from filter is not a timestamp">>}
end.

%%--------------------------------------------------------------------
%% @private
%% @doc
%%
%% @end
%%--------------------------------------------------------------------
-spec transactions_to_jobj/1 :: (wh_transaction:wh_transactions()) -> [wh_json:object(), ...].
transactions_to_jobj(Transactions) ->
JObj = [wh_transaction:to_json(Tr) || Tr <- Transactions],
[clean_json_obj(Obj) || Obj <- JObj].

%%--------------------------------------------------------------------
%% @private
%% @doc
%%
%% @end
%%--------------------------------------------------------------------
-spec clean_json_obj/1 :: (wh_json:object()) -> wh_json:object().
clean_json_obj(JObj) ->
CleanKeys = [{<<"_id">>, <<"id">>}
,{<<"pvt_amount">>, <<"amount">>}
,{<<"pvt_reason">>, <<"reason">>}
,{<<"pvt_type">>, <<"type">>}
,{<<"pvt_created">>, <<"created">>}
,{<<"pvt_vsn">>, <<"version">>}
],
RemoveKeys = [<<"pvt_account_db">>
,<<"pvt_account_id">>
,<<"pvt_modified">>
],
CleanJObj = clean(CleanKeys, JObj),
wh_json:delete_keys(RemoveKeys, CleanJObj).

%%--------------------------------------------------------------------
%% @private
%% @doc
%%
%% @end
%%--------------------------------------------------------------------
-spec clean/2 :: ([{ne_binary(), ne_binary()}, ...] ,wh_json:object()) -> wh_json:object().
clean([], JObj) ->
JObj;
clean([{OldKey, NewKey} | T], JObj) ->
Value = wh_json:get_value(OldKey, JObj),
J1 = wh_json:set_value(NewKey, Value, JObj),
clean(T, wh_json:delete_key(OldKey, J1)).
47 changes: 47 additions & 0 deletions whistle_apps/lib/whistle_transactions-1.0.0/src/wh_transaction.erl
Expand Up @@ -39,6 +39,7 @@
-export([set_sub_account_id/2]).
-export([is_reason/2]).
-export([to_json/1]).
-export([to_public_json/1]).
-export([from_json/1]).
-export([remove/1]).
-export([save/1]).
Expand Down Expand Up @@ -355,6 +356,52 @@ to_json(#wh_transaction{}=T) ->
],
wh_json:from_list(props:filter_undefined(Props)).


%%--------------------------------------------------------------------
%% @public
%% @doc
%% Transform Json Object to transaction record
%% @end
%%--------------------------------------------------------------------
-spec to_public_json/1 :: (transaction()) -> wh_json:object().
to_public_json(Transaction) ->
JObj = to_json(Transaction),
clean_jobj(JObj).


%%--------------------------------------------------------------------
%% @private
%% @doc
%%
%% @end
%%--------------------------------------------------------------------
-spec clean_jobj/1 :: (wh_json:object()) -> wh_json:object().
-spec clean_jobj/2 :: ([{ne_binary(), ne_binary()}, ...] ,wh_json:object()) -> wh_json:object().
clean_jobj(JObj) ->
CleanKeys = [{<<"_id">>, <<"id">>}
,{<<"pvt_amount">>, <<"amount">>}
,{<<"pvt_reason">>, <<"reason">>}
,{<<"pvt_type">>, <<"type">>}
,{<<"pvt_created">>, <<"created">>}
,{<<"pvt_vsn">>, <<"version">>}
,{<<"pvt_code">>, <<"code">>}
],
RemoveKeys = [<<"pvt_account_db">>
,<<"pvt_account_id">>
,<<"pvt_modified">>
,<<"_rev">>
],
CleanJObj = clean_jobj(CleanKeys, JObj),
wh_json:delete_keys(RemoveKeys, CleanJObj).
clean_jobj([], JObj) ->
JObj;
clean_jobj([{OldKey, NewKey} | T], JObj) ->
Value = wh_json:get_value(OldKey, JObj),
J1 = wh_json:set_value(NewKey, Value, JObj),
clean_jobj(T, wh_json:delete_key(OldKey, J1)).



%%--------------------------------------------------------------------
%% @public
%% @doc
Expand Down
Expand Up @@ -19,6 +19,8 @@
-export([fetch_last/2]).
-export([save/1]).
-export([remove/1]).
-export([to_json/1]).
-export([to_public_json/1]).

-type wh_transactions() :: [wh_transaction:transaction(), ...].
-export_type([wh_transactions/0]).
Expand Down Expand Up @@ -205,6 +207,27 @@ remove([Transaction | Transactions], Acc) ->
remove(Transactions, [{'error', Transaction} | Acc])
end.


%%--------------------------------------------------------------------
%% @public
%% @doc
%%
%% @end
%%--------------------------------------------------------------------
-spec to_json/1 :: (wh_transactions()) -> [wh_json:object(), ...].
to_json(Transactions) ->
[wh_transaction:to_json(Tr) || Tr <- Transactions].

%%--------------------------------------------------------------------
%% @public
%% @doc
%%
%% @end
%%--------------------------------------------------------------------
-spec to_public_json/1 :: (wh_transactions()) -> [wh_json:object(), ...].
to_public_json(Transactions) ->
[wh_transaction:to_public_json(Tr) || Tr <- Transactions].

%%--------------------------------------------------------------------
%% @private
%% @doc
Expand All @@ -213,5 +236,4 @@ remove([Transaction | Transactions], Acc) ->
%%--------------------------------------------------------------------
-spec viewres_to_recordlist/1 :: (list()) -> wh_transaction:wh_transactions().
viewres_to_recordlist(ViewRes) ->
L = [wh_json:get_value(<<"doc">>, Tr) || Tr <- ViewRes],
[wh_transaction:from_json(Tr) || Tr <- L].
[wh_transaction:from_json(wh_json:get_value(<<"doc">>, Tr)) || Tr <- ViewRes].
16 changes: 16 additions & 0 deletions whistle_apps/lib/whistle_transactions-1.0.0/src/wht_util.erl
Expand Up @@ -8,6 +8,9 @@
%%%-------------------------------------------------------------------
-module(wht_util).

-export([reasons/0
,reasons/1
,reasons/2]).
-export([dollars_to_units/1]).
-export([units_to_dollars/1]).
-export([base_call_cost/3]).
Expand Down Expand Up @@ -38,6 +41,19 @@

-include_lib("whistle/include/wh_types.hrl").

reasons() ->
?REASONS.
reasons(Min) ->
reasons(Min, 10000).
reasons(Min, Max) ->
reasons(Min, Max, ?REASONS, []).
reasons(_, _, [], Acc) ->
Acc;
reasons(Min, Max, [{R, C} | T], Acc) when C > Min andalso C < Max ->
reasons(Min, Max, T, [R | Acc]);
reasons(Min, Max, [_ | T], Acc) ->
reasons(Min, Max, T, Acc).

%%--------------------------------------------------------------------
%% @public
%% @doc
Expand Down

0 comments on commit 092079d

Please sign in to comment.