Skip to content

Commit

Permalink
KAZOO-5131 (4.0): use topup treshold to determine if it should top up (
Browse files Browse the repository at this point in the history
…#2736)

* KAZOO-5131: use topup treshold to determine if it should top up

* KAZOO-5131: service_save should return transaction record
  • Loading branch information
icehess authored and fenollp committed Oct 26, 2016
1 parent 3ab0d75 commit 3adcffd
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 39 deletions.
59 changes: 32 additions & 27 deletions applications/tasks/src/kz_account_crawler.erl
Expand Up @@ -326,20 +326,38 @@ maybe_test_for_low_balance(AccountId, AccountJObj) ->

-spec test_for_low_balance(ne_binary(), kz_account:doc()) -> 'ok'.
test_for_low_balance(AccountId, AccountJObj) ->
Threshold = kz_account:low_balance_threshold(AccountJObj),
CurrentBalance = wht_util:current_balance(AccountId),
lager:debug("checking if account ~s balance $~w is below $~w"
,[AccountId, wht_util:units_to_dollars(CurrentBalance), Threshold]),
case is_account_balance_too_low(CurrentBalance, Threshold) of
mayby_notify_for_low_balance(AccountJObj, CurrentBalance),
maybe_topup_account(AccountJObj, CurrentBalance).

-spec mayby_notify_for_low_balance(kz_account:doc(), kz_transaction:units()) -> 'ok'.
mayby_notify_for_low_balance(AccountJObj, CurrentBalance) ->
AccountId = kz_account:id(AccountJObj),
Threshold = kz_account:low_balance_threshold(AccountJObj),
lager:info("checking if account ~s balance $~w is below notification threshold $~w"
,[AccountId, wht_util:units_to_dollars(CurrentBalance), Threshold]),
case is_balance_below_notify_threshold(CurrentBalance, Threshold) of
'false' -> maybe_reset_low_balance_sent(AccountJObj);
'true' ->
maybe_low_balance_notify(AccountJObj, CurrentBalance),
maybe_topup_account(AccountJObj, CurrentBalance)
'true' -> maybe_low_balance_notify(AccountJObj, CurrentBalance)
end.

-spec is_account_balance_too_low(kz_transaction:units(), number()) -> boolean().
is_account_balance_too_low(CurrentBalance, Threshold) ->
CurrentBalance < wht_util:dollars_to_units(Threshold).
-spec is_balance_below_notify_threshold(kz_transaction:units(), number()) -> boolean().
is_balance_below_notify_threshold(CurrentBalance, Threshold) ->
CurrentBalance =< wht_util:dollars_to_units(Threshold).

-spec maybe_topup_account(kz_account:doc(), kz_transaction:units()) -> 'ok'.
maybe_topup_account(AccountJObj, CurrentBalance) ->
AccountId = kz_account:id(AccountJObj),
lager:info("checking topup for account ~s with balance $~w"
,[AccountId, wht_util:units_to_dollars(CurrentBalance)]),
case kz_topup:init(AccountId, CurrentBalance) of
'ok' ->
maybe_reset_low_balance_sent(AccountJObj),
lager:info("topup successful for ~s", [AccountId]);
{'error', _Error = topup_disabled} -> 'ok';
{'error', _Error} ->
lager:error("topup failed for ~s: ~p", [AccountId, _Error])
end.

-spec maybe_reset_low_balance_sent(kz_account:doc()) -> 'ok' | {'error', any()}.
maybe_reset_low_balance_sent(AccountJObj) ->
Expand All @@ -355,22 +373,8 @@ reset_low_balance_sent(AccountJObj0) ->
lager:debug("resetting low balance sent"),
AccountJObj1 = kz_account:reset_low_balance_sent(AccountJObj0),
AccountJObj2 = kz_account:remove_low_balance_tstamp(AccountJObj1),
kz_util:account_update(AccountJObj2).

-spec maybe_topup_account(kz_account:doc(), kz_transaction:units()) -> 'ok' | kz_topup:error().
maybe_topup_account(AccountJObj, CurrentBalance) ->
AccountId = kz_account:id(AccountJObj),
case kz_topup:init(AccountId, CurrentBalance) of
'ok' ->
_ = maybe_reset_low_balance_sent(AccountJObj),
lager:debug("topup successful for ~s", [AccountId]);
{'error', Error=topup_disabled} ->
lager:warning("topup failed for ~s: ~p", [AccountId, Error]),
Error;
{'error', Error} ->
lager:error("topup failed for ~s: ~p", [AccountId, Error]),
Error
end.
_ = kz_util:account_update(AccountJObj2),
'ok'.

-spec maybe_low_balance_notify(kz_account:doc(), kz_transaction:units()) -> 'ok'.
maybe_low_balance_notify(AccountJObj, CurrentBalance) ->
Expand Down Expand Up @@ -418,4 +422,5 @@ notify_of_low_balance(AccountJObj, CurrentBalance) ->
update_account_low_balance_sent(AccountJObj0) ->
AccountJObj1 = kz_account:set_low_balance_sent(AccountJObj0),
AccountJObj2 = kz_account:set_low_balance_tstamp(AccountJObj1),
kz_util:account_update(AccountJObj2).
_ = kz_util:account_update(AccountJObj2),
'ok'.
25 changes: 16 additions & 9 deletions core/kazoo_transactions/src/kz_topup.erl
Expand Up @@ -16,9 +16,9 @@
-type error() :: 'topup_disabled' |
'topup_undefined' |
'amount_undefined' |
'limit_undefined' |
'threshold_undefined' |
'balance_above_threshold' |
'undefined' |
'amount_and_threshold_undefined' |
atom().


Expand All @@ -31,11 +31,14 @@
-spec init(api_binary(), integer()) ->
'ok' |
{'error', error()}.
init(Account, Balance) ->
init(Account, CurrentBalance) ->
Balance = wht_util:units_to_dollars(CurrentBalance),
case get_top_up(Account) of
{'error', _}=E -> E;
{'ok', Amount, Threshold} ->
maybe_top_up(Account, wht_util:units_to_dollars(Balance), Amount, Threshold)
lager:info("checking if account ~s balance $~w is below top up threshold $~w"
,[Account, Balance, Threshold]),
maybe_top_up(Account, Balance, Amount, Threshold)
end.

%%--------------------------------------------------------------------
Expand Down Expand Up @@ -66,11 +69,11 @@ get_top_up(JObj) ->
}
of
{'undefined', _} -> {'error', 'amount_undefined'};
{_, 'undefined'} -> {'error', 'limit_undefined'};
{_, 'undefined'} -> {'error', 'threshold_undefined'};
{Amount, Threshold} when Amount > 0
andalso Threshold > 0 ->
{'ok', Amount, Threshold};
{_, _} -> {'error', 'undefined'}
{_, _} -> {'error', 'amount_and_threshold_undefined'}
end.

%%--------------------------------------------------------------------
Expand Down Expand Up @@ -124,8 +127,12 @@ trying_top_up(Account, Amount, [JObj|JObjs]) ->
top_up(Account, Amount) ->
Transaction = kz_transaction:debit(Account, wht_util:dollars_to_units(Amount)),
Transaction1 = kz_transaction:set_reason(<<"topup">>, Transaction),
lager:info("attemptting to top up account ~s for ~p", [Account, Amount]),
case kz_transaction:service_save(Transaction1) of
{'error', _E}=E -> E;
{'ok', _} ->
lager:info("account ~s top up for ~p", [Account, Amount])
{'error', _R}=E ->
lager:warning("failed to top up account ~s: ~p", [Account, _R]),
E;
{'ok', SavedTransaction} ->
lager:info("account ~s top up for ~p, transaction id ~s"
,[Account, Amount, kz_transaction:id(SavedTransaction)])
end.
13 changes: 10 additions & 3 deletions core/kazoo_transactions/src/kz_transaction.erl
Expand Up @@ -547,7 +547,7 @@ save_transaction(#kz_transaction{pvt_account_id=AccountId
%% @end
%%--------------------------------------------------------------------
-spec service_save(transaction()) ->
{'ok', kz_json:object()} |
{'ok', transaction()} |
{'error', any()}.
service_save(#kz_transaction{}=Transaction) ->
case prepare_transaction(Transaction) of
Expand All @@ -557,7 +557,7 @@ service_save(#kz_transaction{}=Transaction) ->
end.

-spec service_save_transaction(transaction()) ->
{'ok', kz_json:object()} |
{'ok', transaction()} |
{'error', any()}.
service_save_transaction(#kz_transaction{pvt_account_id=AccountId}=Transaction) ->
TransactionJObj = to_json(Transaction#kz_transaction{pvt_modified=kz_util:current_tstamp()}),
Expand All @@ -571,7 +571,14 @@ service_save_transaction(#kz_transaction{pvt_account_id=AccountId}=Transaction)
[{<<"transactions">>, [TransactionJObj|Transactions]}
,{<<"pvt_dirty">>, 'true'}
], JObj),
kz_datamgr:save_doc(?KZ_SERVICES_DB, JObj1)
case kz_datamgr:save_doc(?KZ_SERVICES_DB, JObj1) of
{'ok', _SavedJObj1} ->
{'ok', from_json(TransactionJObj)};
{'error', _R} = Error ->
lager:debug("failed to save account ~s services doc for transaction ~s: ~p"
,[AccountId, kz_doc:id(TransactionJObj), _R]),
Error
end
end.

%%--------------------------------------------------------------------
Expand Down

1 comment on commit 3adcffd

@k-anderson
Copy link
Contributor

Choose a reason for hiding this comment

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

@fenollp @lazedo bug fix should be against the 4.0 branch as well.... any other bug fixes that were missed since the last merge down should also be pulled in. :)

Please sign in to comment.