Skip to content

Commit

Permalink
Merged in KAZ-346 (pull request 2600hz#39)
Browse files Browse the repository at this point in the history
Kaz 346
  • Loading branch information
BorigTheDwarf committed Jul 5, 2016
2 parents 5f870b7 + 9410904 commit 1c17383
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 1 deletion.
88 changes: 88 additions & 0 deletions applications/callflow/src/module/cf_log_in_out_ring_group.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
%%%-------------------------------------------------------------------
%%% @doc
%%%
%%% @end
%%% @contributors
%%% Max Lay
%%%-------------------------------------------------------------------
-module(cf_log_in_out_ring_group).

-include("../callflow.hrl").

-export([handle/2]).

-define(ON_VAL, 0).
-define(OFF_VAL, 66269664000).

%%--------------------------------------------------------------------
%% @public
%% @doc
%% Entry point for this module, attempts to call an endpoint as defined
%% in the Data payload. Returns continue if fails to connect or
%% stop when successfull.
%% @end
%%--------------------------------------------------------------------
-spec handle(wh_json:object(), whapps_call:call()) -> 'ok'.
handle(Data, Call) ->
_ = whapps_call_command:answer(Call),
lager:debug("Module data ~p", [Data]),
CallflowId = wh_json:get_value(<<"callflow_id">>, Data),
DisableUntil = case wh_json:get_value(<<"login">>, Data) of
'true' -> ?ON_VAL;
'false' -> ?OFF_VAL
end,
AccountDb = whapps_call:account_db(Call),
{'ok', Callflow} = couch_mgr:open_doc(AccountDb, CallflowId),
UpdatedFlow = find_and_change_ring_group(wh_json:get_value(<<"flow">>, Callflow), Call, DisableUntil),
couch_mgr:save_doc(AccountDb, wh_json:set_value(<<"flow">>, UpdatedFlow, Callflow)),
lager:debug("Updated flow to ~p", [wh_json:encode(UpdatedFlow)]),
cf_exe:continue(Call).

-spec find_and_change_ring_group(wh_json:object(), whapps_call:call(), boolean()) -> wh_json:object().
find_and_change_ring_group(Module, Call, DisableUntil) ->
NewModule = case wh_json:get_value(<<"module">>, Module) of
<<"ring_group">> -> alter_endpoints(Module, Call, DisableUntil);
_ -> Module
end,
case wh_json:get_value(<<"children">>, NewModule) of
'undefined' -> NewModule;
Children -> wh_json:set_value(<<"children">>, maybe_modify_children(Children, Call, DisableUntil), NewModule)
end.

-spec maybe_modify_children(wh_json:object(), whapps_call:call(), boolean()) -> wh_json:object().
maybe_modify_children(Children, Call, DisableUntil) ->
lists:foldl(fun(Key, JObj) ->
SubModule = find_and_change_ring_group(wh_json:get_value(Key, JObj), Call, DisableUntil),
wh_json:set_value(Key, SubModule, JObj)
end, Children, wh_json:get_keys(Children)).

-spec alter_endpoints(wh_json:object(), whapps_call:call(), boolean()) -> wh_json:object().
alter_endpoints(Module, Call, DisableUntil) ->
EndpointsPath = [<<"data">>, <<"endpoints">>],
OldEndpoints = wh_json:get_value(EndpointsPath, Module),
{Found, EndPoints} = lists:foldl(fun(Endpoint, {Found, UpdatedEndpoints}) ->
{ThisFound, Res} = maybe_alter_disable_until(wh_json:get_value(<<"endpoint_type">>, Endpoint), Endpoint, Call, DisableUntil),
{Found or ThisFound, [Res | UpdatedEndpoints]}
end, {'false', []}, OldEndpoints),
Media = case {Found, DisableUntil} of
{'false', _} ->
%% User not found
wh_media_util:get_prompt(<<"agent-invalid_choice">>, Call);
{'true', ?ON_VAL} ->
wh_media_util:get_prompt(<<"agent-logged_in">>, Call);
{'true', _} ->
wh_media_util:get_prompt(<<"agent-logged_out">>, Call)
end,
whapps_call_command:play(Media, Call),
wh_json:set_value(EndpointsPath, EndPoints, Module).

-spec maybe_alter_disable_until(ne_binary(), wh_json:object(), whapps_call:call(), number()) -> {boolean(), wh_json:object()}.
maybe_alter_disable_until(<<"user">>, Endpoint, Call, DisableUntil) ->
lager:debug("Comparing owner ~p, to endpoint ~p", [whapps_call:owner_id(Call), Endpoint]),
case whapps_call:owner_id(Call) =:= wh_json:get_value(<<"id">>, Endpoint) of
'true' -> {'true', wh_json:set_value([<<"disable_until">>], DisableUntil, Endpoint)};
'false' -> {'false', Endpoint}
end;

maybe_alter_disable_until(_EndpointType, Endpoint, _Call, _DisableUntil) ->
{'false', Endpoint}.
11 changes: 10 additions & 1 deletion applications/callflow/src/module/cf_ring_group.erl
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,19 @@ start_builder(EndpointId, Member, Call) ->
end
).

-spec member_active(wh_json:object()) -> boolean().
member_active(Member) ->
case wh_json:get_value(<<"disable_until">>, Member) of
'undefined' -> 'true';
Time -> calendar:datetime_to_gregorian_seconds(calendar:universal_time()) > Time
end.

-spec resolve_endpoint_ids(wh_json:object(), whapps_call:call()) -> wh_proplist().
resolve_endpoint_ids(Data, Call) ->
Members = wh_json:get_value(<<"endpoints">>, Data, []),
ResolvedEndpoints = resolve_endpoint_ids(Members, [], Data, Call),
FilteredMembers = lists:filter(fun member_active/1, Members),
lager:debug("Filtered members ~p", [FilteredMembers]),
ResolvedEndpoints = resolve_endpoint_ids(FilteredMembers, [], Data, Call),
FilteredEndpoints = [{Weight, {Id, wh_json:set_value(<<"source">>, ?MODULE, Member)}}
|| {Type, Id, Weight, Member} <- ResolvedEndpoints
,Type =:= <<"device">>
Expand Down

0 comments on commit 1c17383

Please sign in to comment.