Skip to content

Commit

Permalink
PISTON-652: add Export-All option for kapps_call_command:set (2600hz#…
Browse files Browse the repository at this point in the history
…5404)

* PISTON-652: add Export-All option for kapps_call_command:set

* PISTON-652: Handle undefined Export-All and move to own function
  • Loading branch information
danielfinke authored and k-anderson committed Jan 9, 2019
1 parent e96f1fd commit 383451d
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 14 deletions.
4 changes: 2 additions & 2 deletions applications/callflow/src/module/cf_set_variables.erl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ get_custom_application_vars(Data) ->
-spec set_variables(boolean(), kz_term:proplist(), kapps_call:call()) -> kapps_call:call().
set_variables('true', CAVs, Call) ->
lager:debug("exporting custom app vars: ~p", [CAVs]),
kapps_call:set_custom_application_vars(CAVs, Call);
kapps_call:set_custom_application_vars(CAVs, Call, 'true');
set_variables('false', CAVs, Call) ->
lager:debug("setting custom app vars: ~p", [CAVs]),
kapps_call:set_custom_application_vars(CAVs, Call).
kapps_call:set_custom_application_vars(CAVs, Call, 'false').
3 changes: 3 additions & 0 deletions applications/crossbar/priv/api/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -13910,6 +13910,9 @@
],
"type": "string"
},
"Export-All": {
"type": "boolean"
},
"Insert-At": {
"enum": [
"head",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
],
"type": "string"
},
"Export-All": {
"type": "boolean"
},
"Insert-At": {
"enum": [
"head",
Expand Down
13 changes: 11 additions & 2 deletions applications/ecallmgr/src/ecallmgr_call_command.erl
Original file line number Diff line number Diff line change
Expand Up @@ -474,9 +474,12 @@ get_fs_app(Node, UUID, JObj, <<"set">>) ->
CallVars = kz_json:to_proplist(kz_json:get_json_value(<<"Custom-Call-Vars">>, JObj, kz_json:new())),
AppVars = kz_json:to_proplist(kz_json:get_json_value(<<"Custom-Application-Vars">>, JObj, kz_json:new())),

Command = get_set_command(JObj),

props:filter_undefined(
[{<<"kz_multiset">>, maybe_multi_set(Node, UUID, ChannelVars)}
,{<<"kz_multiset">>, maybe_multi_set(Node, UUID, [{?CAV(K), V} || {K, V} <- AppVars])}
[{Command, maybe_multi_set(Node, UUID, ChannelVars)}
,{Command, maybe_multi_set(Node, UUID, [{?CAV(K), V} || {K, V} <- AppVars])}
%% CallVars are always exported
,{<<"kz_export">>, maybe_multi_set(Node, UUID, CallVars)}
])
end;
Expand Down Expand Up @@ -571,6 +574,12 @@ media_macro_to_file_string(Macro) ->
Paths = lists:map(fun ecallmgr_util:media_path/1, Macro),
list_to_binary(["file_string://", kz_binary:join(Paths, <<"!">>)]).

-spec get_set_command(kz_json:object()) -> kz_term:ne_binary().
get_set_command(JObj) ->
case kz_json:get_boolean_value(<<"Export-All">>, JObj, 'false') of
'true' -> <<"kz_export">>;
'false' -> <<"kz_multiset">>
end.

-spec maybe_multi_set(atom(), kz_term:ne_binary(), kz_term:proplist()) -> kz_term:api_binary().
maybe_multi_set(_Node, _UUID, []) -> 'undefined';
Expand Down
2 changes: 2 additions & 0 deletions core/kazoo_amqp/src/api/kapi_dialplan.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,7 @@
]).
-define(OPTIONAL_SET_REQ_HEADERS, [<<"Insert-At">>
,<<"Custom-Application-Vars">>
,<<"Export-All">>
]).
-define(SET_REQ_VALUES, [{<<"Event-Category">>, <<"call">>}
,{<<"Event-Name">>, <<"command">>}
Expand All @@ -459,6 +460,7 @@
-define(SET_REQ_TYPES, [{<<"Custom-Application-Vars">>, fun kz_json:is_json_object/1}
,{<<"Custom-Channel-Vars">>,fun kz_json:is_json_object/1}
,{<<"Custom-Call-Vars">>, fun kz_json:is_json_object/1}
,{<<"Export-All">>, fun is_boolean/1}
]).

%% Set Terminators
Expand Down
17 changes: 11 additions & 6 deletions core/kazoo_call/src/kapps_call.erl
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@

-export([set_custom_application_var/3
,insert_custom_application_var/3
,set_custom_application_vars/2
,set_custom_application_vars/2, set_custom_application_vars/3
,custom_application_var/3
,custom_application_var/2
,custom_application_vars/1
Expand Down Expand Up @@ -1266,19 +1266,24 @@ insert_custom_application_var(Key, Value, #kapps_call{cavs=CAVs}=Call) ->
Call#kapps_call{cavs=kz_json:set_value(Key, Value, CAVs)}.

-spec set_custom_application_vars(kz_term:proplist(), call()) -> call().
set_custom_application_vars(Props, #kapps_call{cavs=CAVs}=Call) ->
set_custom_application_vars(Props, Call) ->
set_custom_application_vars(Props, Call, 'false').

-spec set_custom_application_vars(kz_term:proplist(), call(), boolean()) -> call().
set_custom_application_vars(Props, #kapps_call{cavs=CAVs}=Call, Export) ->
NewCAVs = kz_json:set_values(Props, CAVs),

maybe_update_call_cavs(Call, Props, kz_json:to_proplist(CAVs)),
maybe_update_call_cavs(Call, Props, kz_json:to_proplist(CAVs), Export),

Call#kapps_call{cavs=NewCAVs}.

-spec maybe_update_call_cavs(call(), kz_term:proplist(), kz_term:proplist()) -> 'ok'.
maybe_update_call_cavs(Call, NewCAVs, ExistingCAVs) ->
-spec maybe_update_call_cavs(call(), kz_term:proplist(), kz_term:proplist(), boolean()) -> 'ok'.
maybe_update_call_cavs(Call, NewCAVs, ExistingCAVs, Export) ->
%% Modeled after updateable_ccvs/2
case NewCAVs -- ExistingCAVs of
[] -> 'ok';
Updates -> kapps_call_command:set('undefined', 'undefined', kz_json:from_list(Updates), Call)
Updates ->
kapps_call_command:set('undefined', 'undefined', kz_json:from_list(Updates), Export, Call)
end.

-spec custom_application_var(any(), Default, call()) -> Default | _.
Expand Down
12 changes: 8 additions & 4 deletions core/kazoo_call/src/kapps_call_command.erl
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
,hangup/1, hangup/2
,break/1
,queued_hangup/1
,set/3, set/4, set_terminators/2
,set/3, set/4, set/5, set_terminators/2
,fetch/1, fetch/2
]).
-export([echo/1]).
Expand Down Expand Up @@ -834,9 +834,12 @@ set(ChannelVars, CallVars, Call) ->
set(ChannelVars, CallVars, 'undefined', Call).

-spec set(kz_term:api_object(), kz_term:api_object(), kz_term:api_object(), kapps_call:call()) -> 'ok'.
set('undefined', CallVars, AppVars, Call) -> set(kz_json:new(), CallVars, AppVars, Call);
set(ChannelVars, 'undefined', AppVars, Call) -> set(ChannelVars, kz_json:new(), AppVars, Call);
set(ChannelVars, CallVars, AppVars, Call) ->
set(ChannelVars, CallVars, AppVars, Call) -> set(ChannelVars, CallVars, AppVars, 'false', Call).

-spec set(kz_term:api_object(), kz_term:api_object(), kz_term:api_object(), boolean(), kapps_call:call()) -> 'ok'.
set('undefined', CallVars, AppVars, ExportAll, Call) -> set(kz_json:new(), CallVars, AppVars, ExportAll, Call);
set(ChannelVars, 'undefined', AppVars, ExportAll, Call) -> set(ChannelVars, kz_json:new(), AppVars, ExportAll, Call);
set(ChannelVars, CallVars, AppVars, ExportAll, Call) ->
case kz_json:is_empty(ChannelVars)
andalso kz_json:is_empty(CallVars)
andalso kz_json:is_empty(AppVars)
Expand All @@ -847,6 +850,7 @@ set(ChannelVars, CallVars, AppVars, Call) ->
,{<<"Custom-Application-Vars">>, AppVars}
,{<<"Custom-Call-Vars">>, CallVars}
,{<<"Custom-Channel-Vars">>, ChannelVars}
,{<<"Export-All">>, ExportAll}
,{<<"Insert-At">>, <<"now">>}
],
send_command(Command, Call)
Expand Down

0 comments on commit 383451d

Please sign in to comment.