diff --git a/applications/ecallmgr/src/ecallmgr_originate.erl b/applications/ecallmgr/src/ecallmgr_originate.erl index a27e57ab5d4..1b9cd530c46 100644 --- a/applications/ecallmgr/src/ecallmgr_originate.erl +++ b/applications/ecallmgr/src/ecallmgr_originate.erl @@ -249,7 +249,7 @@ handle_cast({'originate_ready'}, #state{node=_Node}=State) -> ,server_id=ServerId ,queue=Q }=State1} -> - CtrlQ = gen_listener:queue_name(Pid), + CtrlQ = call_control_queue(Pid), _ = publish_originate_ready(CtrlQ, UUID, JObj, Q, ServerId), {'noreply', State1#state{tref=start_abandon_timer()}}; {'error', _E} -> @@ -290,7 +290,7 @@ handle_cast({'originate_execute'}, #state{dialstrings=Dialstrings lager:debug("originate is executing, waiting for completion"), erlang:monitor_node(Node, 'true'), bind_to_call_events(CallId), - CtrlQ = ecallmgr_call_control:queue_name(CtrlPid), + CtrlQ = call_control_queue(CtrlPid), _ = publish_originate_started(ServerId, CallId, JObj, CtrlQ), {'noreply', State#state{uuid={'api', CallId}}}; {'error', Error} -> @@ -595,7 +595,6 @@ update_uuid(OldUUID, NewUUID) -> bind_to_call_events(NewUUID), 'ok'. - -spec create_uuid(atom()) -> created_uuid(). create_uuid(_Node) -> {'fs', kz_binary:rand_hex(18)}. @@ -617,15 +616,17 @@ create_uuid(Endpoint, _JObj, Node) -> get_unset_vars(JObj) -> %% Refactor (Karl wishes he had unit tests here for you to use) ExportProps = [{K, <<>>} || K <- kz_json:get_value(<<"Export-Custom-Channel-Vars">>, JObj, [])], - Export = [K || KV <- lists:foldr(fun ecallmgr_fs_xml:kazoo_var_to_fs_var/2 - ,[] - ,[{<<"Custom-Channel-Vars">>, kz_json:from_list(ExportProps)}] - ), + FSVars = lists:foldr(fun ecallmgr_fs_xml:kazoo_var_to_fs_var/2 + ,[] + ,[{<<"Custom-Channel-Vars">>, kz_json:from_list(ExportProps)}] + ), + Export = [K || KV <- FSVars, ([K, _] = string:tokens(binary_to_list(KV), "=")) =/= 'undefined' ], case ["unset:" ++ K - || KV <- lists:foldr(fun ecallmgr_fs_xml:kazoo_var_to_fs_var/2, [], kz_json:to_proplist(JObj)) - ,not lists:member(begin [K, _] = string:tokens(binary_to_list(KV), "="), K end, Export)] + || KV <- lists:foldr(fun ecallmgr_fs_xml:kazoo_var_to_fs_var/2, [], kz_json:to_proplist(JObj)), + not lists:member(begin [K, _] = string:tokens(binary_to_list(KV), "="), K end, Export) + ] of [] -> ""; Unset -> @@ -662,8 +663,7 @@ maybe_fix_fs_auto_answer_bug(Export) -> -spec maybe_fix_caller_id(kz_term:strings(), kz_json:object()) -> string(). maybe_fix_caller_id(Export, JObj) -> - Fix = [ - {lists:member("origination_callee_id_name", Export), kz_json:get_value(<<"Outbound-Callee-ID-Name">>, JObj), "effective_caller_id_name"} + Fix = [{lists:member("origination_callee_id_name", Export), kz_json:get_value(<<"Outbound-Callee-ID-Name">>, JObj), "effective_caller_id_name"} ,{lists:member("origination_callee_id_number", Export), kz_json:get_value(<<"Outbound-Callee-ID-Number">>, JObj), "effective_caller_id_number"} ], string:join([ "^set:" ++ Key ++ "=" ++ erlang:binary_to_list(Value) || {IsTrue, Value, Key} <- Fix, IsTrue ], ":"). @@ -674,7 +674,7 @@ publish_error(Error, {_, UUID}, Request, ServerId) -> publish_error(Error, UUID, Request, ServerId); publish_error(Error, UUID, Request, ServerId) -> lager:debug("originate error: ~s", [Error]), - E = [{<<"Msg-ID">>, kz_json:get_value(<<"Msg-ID">>, Request)} + E = [{<<"Msg-ID">>, kz_api:msg_id(Request)} ,{<<"Call-ID">>, UUID} ,{<<"Request">>, Request} ,{<<"Error-Message">>, cleanup_error(Error)} @@ -691,7 +691,7 @@ publish_originate_ready(CtrlQ, {_, UUID}, Request, Q, ServerId) -> publish_originate_ready(CtrlQ, UUID, Request, Q, ServerId); publish_originate_ready(CtrlQ, UUID, Request, Q, ServerId) -> lager:debug("originate command is ready, waiting for originate_execute"), - Props = [{<<"Msg-ID">>, kz_json:get_value(<<"Msg-ID">>, Request, UUID)} + Props = [{<<"Msg-ID">>, kz_api:msg_id(Request, UUID)} ,{<<"Call-ID">>, UUID} ,{<<"Control-Queue">>, CtrlQ} | kz_api:default_headers(Q, ?APP_NAME, ?APP_VERSION) @@ -703,7 +703,9 @@ publish_originate_resp('undefined', _) -> 'ok'; publish_originate_resp(ServerId, JObj) -> Resp = kz_json:set_values([{<<"Event-Category">>, <<"resource">>} ,{<<"Event-Name">>, <<"originate_resp">>} - ], JObj), + ] + ,JObj + ), kapi_resource:publish_originate_resp(ServerId, Resp). -spec publish_originate_resp(kz_term:api_binary(), kz_json:object(), kz_term:ne_binary()) -> 'ok'. @@ -713,7 +715,9 @@ publish_originate_resp(ServerId, JObj, UUID) -> ,{<<"Application-Response">>, <<"SUCCESS">>} ,{<<"Event-Name">>, <<"originate_resp">>} ,{<<"Call-ID">>, UUID} - ], JObj), + ] + ,JObj + ), kapi_resource:publish_originate_resp(ServerId, Resp). -spec publish_originate_started(kz_term:api_binary(), kz_term:ne_binary(), kz_json:object(), kz_term:ne_binary()) -> 'ok'. @@ -721,7 +725,7 @@ publish_originate_started('undefined', _, _, _) -> 'ok'; publish_originate_started(ServerId, CallId, JObj, CtrlQ) -> Resp = kz_json:from_list( [{<<"Call-ID">>, CallId} - ,{<<"Msg-ID">>, kz_json:get_value(<<"Msg-ID">>, JObj)} + ,{<<"Msg-ID">>, kz_api:msg_id(JObj)} ,{<<"Control-Queue">>, CtrlQ} | kz_api:default_headers(?APP_NAME, ?APP_VERSION) ]), @@ -732,7 +736,7 @@ publish_originate_uuid('undefined', _, _, _) -> 'ok'; publish_originate_uuid(ServerId, UUID, JObj, CtrlQueue) -> Resp = props:filter_undefined( [{<<"Outbound-Call-ID">>, UUID} - ,{<<"Msg-ID">>, kz_json:get_value(<<"Msg-ID">>, JObj)} + ,{<<"Msg-ID">>, kz_api:msg_id(JObj)} ,{<<"Outbound-Call-Control-Queue">>, CtrlQueue} | kz_api:default_headers(?APP_NAME, ?APP_VERSION) ]), @@ -743,9 +747,20 @@ publish_originate_uuid(ServerId, UUID, JObj, CtrlQueue) -> maybe_send_originate_uuid({_, UUID}, Pid, #state{server_id=ServerId ,originate_req=JObj }) -> - CtlQ = gen_listener:queue_name(Pid), + CtlQ = call_control_queue(Pid), publish_originate_uuid(ServerId, UUID, JObj, CtlQ). +-spec call_control_queue(pid()) -> kz_term:ne_binary(). +call_control_queue(Pid) -> + call_control_queue(Pid, ecallmgr_call_control:queue_name(Pid)). + +-spec call_control_queue(pid(), kz_term:api_ne_binary()) -> kz_term:ne_binary(). +call_control_queue(_Pid, <>) -> QueueName; +call_control_queue(Pid, 'undefined') -> + lager:debug("waiting on call control ~p to get setup", [Pid]), + timer:sleep(100), + call_control_queue(Pid). + -spec find_originate_timeout(kz_json:object()) -> pos_integer(). find_originate_timeout(JObj) -> OTimeout = case kz_json:get_integer_value(<<"Timeout">>, JObj) of