Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

WHISTLE-788: send low balance notifications via API

  • Loading branch information...
commit c3381625bce7a57989deab2e678e916afcbd9264 1 parent 55baf70
James Aimonetti jamesaimonetti authored
41 whistle_apps/apps/jonny5/src/j5_acctmgr.erl
View
@@ -551,26 +551,35 @@ try_inbound(CallID, #state{inbound=In, trunks_in_use=Dict, ledger_db=DB}=State)
try_prepay(CallID, #state{prepay=Pre, acct_id=AcctId, trunks_in_use=Dict, ledger_db=LedgerDB}=State, PerMinCharge) when Pre =< PerMinCharge ->
?LOG_SYS(CallID, "failed to authz a per_min trunk", []),
- %% Alert admins of the situation
- whapps_util:alert(<<"emerg">>, ["Source: ~s(~p)~n"
- ,"Alert: Insufficient prepay to authorize the call.~n"
- ,"Call-ID: ~s~n"
- ,"Account-ID: ~s~n"
- ,"Current Prepay Balance: ~p~n"
- ]
- ,[?MODULE, ?LINE, CallID, AcctId, wapi_money:units_to_dollars(Pre)]),
-
case whapps_config:get_is_true(<<"jonny5">>, <<"authz_on_no_prepay">>, true) of
true ->
- ?LOG("authz_on_no_prepay set to true, authz the call"),
+ HowLow = wapi_money:dollars_to_units(whapps_config:get_float(<<"jonny5">>, <<"how_low_can_you_go">>, 0.0)),
PrepayLeft = Pre - PerMinCharge,
- ?LOG_SYS(CallID, "authz a per_min trunk; ~b prepay left, ~b charged up-front", [PrepayLeft, PerMinCharge]),
- {ok, Pid} = monitor_call(CallID, LedgerDB, per_min, PerMinCharge),
- erlang:monitor(process, Pid),
+ case HowLow > PrepayLeft of
+ true -> % 0 > -1
+ %% Alert admins of the situation
+ whapps_util:alert(<<"emerg">>, ["Source: ~s(~p)~n"
+ ,"Alert: Insufficient prepay to authorize the call.~n"
+ ,"Call-ID: ~s~n"
+ ,"Account-ID: ~s~n"
+ ,"Current Prepay Balance: ~p~n"
+ ]
+ ,[?MODULE, ?LINE, CallID, AcctId, wapi_money:units_to_dollars(Pre)]),
+
+ catch(wapi_notifications:publish_low_balance([{<<"Account-ID">>, AcctId}, {<<"Current-Balance">>, wapi_money:units_to_dollars(Pre)}])),
+
+ ?LOG(CallID, "howlow (~p) > prepay (~p), noauthz this call!", [HowLow, Pre]),
+ {{false, [{<<"Error">>, <<"Insufficient Funds">>}]}, State};
+ false ->
+ ?LOG(CallID, "authz_on_no_prepay set to true, and prepay (~p) still > howlow (~p)", [PrepayLeft, HowLow]),
- {{true, [{<<"Trunk-Type">>, <<"per_min">>}]}
- ,State#state{trunks_in_use=dict:store(CallID, {per_min, Pid}, Dict), prepay=PrepayLeft}
- };
+ {ok, Pid} = monitor_call(CallID, LedgerDB, per_min, PerMinCharge),
+ erlang:monitor(process, Pid),
+
+ {{true, [{<<"Trunk-Type">>, <<"per_min">>}]}
+ ,State#state{trunks_in_use=dict:store(CallID, {per_min, Pid}, Dict), prepay=PrepayLeft}
+ }
+ end;
false ->
?LOG("authz_on_no_prepay set to false, denying the call"),
{{false, [{<<"Error">>, <<"Insufficient Funds">>}]}, State}
2  whistle_apps/apps/jonny5/src/j5_util.erl
View
@@ -51,7 +51,7 @@ fetch_account_handler(AcctID, Cache) when is_pid(Cache) ->
-spec store_account_handler/2 :: (ne_binary(), pid() | 'undefined') -> 'ok'.
-spec store_account_handler/3 :: (ne_binary(), pid() | 'undefined', pid()) -> 'ok'.
-store_account_handler(AcctID, J5Pid) when is_pid(J5Pid) ->
+store_account_handler(AcctID, J5Pid) ->
{ok, Cache} = jonny5_sup:cache_proc(),
store_account_handler(AcctID, J5Pid, Cache).
store_account_handler(AcctID, undefined, Cache) when is_pid(Cache) ->
Please sign in to comment.
Something went wrong with that request. Please try again.