diff --git a/applications/tasks/src/kz_account_crawler.erl b/applications/tasks/src/kz_account_crawler.erl index 67c9fd801f8..8bad9ad176e 100644 --- a/applications/tasks/src/kz_account_crawler.erl +++ b/applications/tasks/src/kz_account_crawler.erl @@ -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) -> @@ -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) -> @@ -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'. diff --git a/core/kazoo_transactions/src/kz_topup.erl b/core/kazoo_transactions/src/kz_topup.erl index f9d78a22c34..61cfe3cd354 100644 --- a/core/kazoo_transactions/src/kz_topup.erl +++ b/core/kazoo_transactions/src/kz_topup.erl @@ -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(). @@ -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. %%-------------------------------------------------------------------- @@ -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. %%-------------------------------------------------------------------- @@ -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. diff --git a/core/kazoo_transactions/src/kz_transaction.erl b/core/kazoo_transactions/src/kz_transaction.erl index 90923ad1ef7..c8d8047e231 100644 --- a/core/kazoo_transactions/src/kz_transaction.erl +++ b/core/kazoo_transactions/src/kz_transaction.erl @@ -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 @@ -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()}), @@ -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. %%--------------------------------------------------------------------