Built motion from commit e1760111.|2.6.18
[motion2.git] / server / services / ami / trigger / integration / sugarcrm.js
index d7eb8ea..b5dc2ea 100644 (file)
@@ -15,4 +15,4 @@
 // * treaties. The SOFTWARE PRODUCT is licensed, not sold.                        *
 // *                                                                       *
 // *************************************************************************
-var _0xb67b=['uniqueid','destcalleridname','destcalleridnum','Fields','trigger:browser:url','error','toUpperCase','calleridname','calleridnum','lastevent','lodash','request-promise','bluebird','md5','util','ioredis','./util','../../../../config/logger','sugarcrm','redis','localhost','Creating\x20new\x20ticket...','Subjects','created_by','getString','toISOString','slice','duration_hours','parent_id','Contacts','parent_name','name','direction','variableName','idField','set_entry','stringify','Calls','format','Basic\x20%s','toString','base64','JSON','isNil','Getting\x20agent\x20with\x20name','get_entry_list','Employees','user_name','then','length','find','entry_list','name_value_list','value','info','Agent\x20found!','Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!','resolve','user_id','phone_home','Enduser\x20not\x20found,\x20creating\x20a\x20new\x20enduser!','Caller\x20-\x20%s','Enduser\x20created.','debug','[HTTP]','merge','map','forEach','type','string','nameField','customField','content','custom_fields','variable','customVariable','Variable','toLowerCase','login','PLAIN','xCALLY\x20Motion\x20integration','set_relationship','remoteUri','stripTrailingSlash','serverUrl','uri','%s/service/v4_1/rest.php','monitor_format','[QUEUE]','Recording\x20is\x20enabled!','recordingURL','The\x20User\x20ID\x20is:','The\x20Enduser\x20ID\x20is:','Ticket\x20created!','The\x20Ticket\x20ID\x20is:','users','contacts','version','charAt','%s/index.php?action=ajaxui#ajaxUILoc=index.php%3Fmodule%3DCalls%26action%3DEditView%26record%3D%s','%s/#bwc/index.php?module=Calls&action=EditView&record=%s','Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...','user:%s','catch','outbound','recordingFormat','none','[OUTBOUND]','%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s'];(function(_0x2d2ca2,_0x51873f){var _0x48489d=function(_0x1a2cf7){while(--_0x1a2cf7){_0x2d2ca2['push'](_0x2d2ca2['shift']());}};_0x48489d(++_0x51873f);}(_0xb67b,0xe4));var _0xbb67=function(_0x128443,_0x2fe025){_0x128443=_0x128443-0x0;var _0x1a4ce1=_0xb67b[_0x128443];return _0x1a4ce1;};'use strict';var _=require(_0xbb67('0x0'));var rp=require(_0xbb67('0x1'));var BPromise=require(_0xbb67('0x2'));var md5=require(_0xbb67('0x3'));var util=require(_0xbb67('0x4'));var Redis=require(_0xbb67('0x5'));var intUtil=require(_0xbb67('0x6'));var config=require('../../../../config/environment');var logger=require(_0xbb67('0x7'))(_0xbb67('0x8'));config[_0xbb67('0x9')]=_['defaults'](config[_0xbb67('0x9')],{'host':_0xbb67('0xa'),'port':0x18eb});var io=require('socket.io-emitter')(new Redis(config[_0xbb67('0x9')]));var def={'method':'POST','json':!![]};function emit(_0x16cde8,_0x46b73a,_0x27d27e){io['to'](_0x16cde8)['emit'](_0x46b73a,_0x27d27e);}function getTicket(_0x38fb84,_0xefa6b7,_0x3a5f5d,_0x4fb52c,_0x2f0ee6,_0x36cec9,_0x589f2e,_0x500ad5){logger['info'](_0xbb67('0xb'));var _0x1c8151=new Date();var _0x4a2678=[{'name':'name','value':intUtil['getString'](_0x4fb52c[_0xbb67('0xc')],_0x2f0ee6,'\x20')},{'name':'assigned_user_id','value':_0xefa6b7['id']},{'name':_0xbb67('0xd'),'value':_0xefa6b7['id']},{'name':'description','value':intUtil[_0xbb67('0xe')](_0x4fb52c['Descriptions'],_0x2f0ee6,'\x0a')},{'name':'date_start','value':_0x1c8151[_0xbb67('0xf')]()[_0xbb67('0x10')](0x0,0x13)['replace']('T','\x20')},{'name':_0xbb67('0x11'),'value':'0'},{'name':'duration_minutes','value':'15'},{'name':_0xbb67('0x12'),'value':_0x3a5f5d['id']},{'name':'parent_type','value':_0xbb67('0x13')},{'name':_0xbb67('0x14'),'value':_0x3a5f5d[_0xbb67('0x15')]},{'name':_0xbb67('0x16'),'value':_0x500ad5}];if(_0x36cec9['length']){_['forEach'](_0x36cec9,function(_0x31e59d){if(_0x2f0ee6[_0x31e59d[_0xbb67('0x17')]]){_0x4a2678['push']({'name':_0x31e59d[_0xbb67('0x18')],'value':_0x2f0ee6[_0x31e59d[_0xbb67('0x17')]]});}});}return request({'form':{'method':_0xbb67('0x19'),'input_type':'JSON','response_type':'JSON','rest_data':JSON[_0xbb67('0x1a')]({'session':_0x589f2e['id'],'module_name':_0xbb67('0x1b'),'name_value_list':_0x4a2678})}});}function getAuth(_0x3580a6){return util[_0xbb67('0x1c')](_0xbb67('0x1d'),new Buffer(_0x3580a6['apiKey']+':X')[_0xbb67('0x1e')](_0xbb67('0x1f')));}function getServerInfo(_0x31c340){return request({'form':{'method':'get_server_info','input_type':'JSON','response_type':_0xbb67('0x20'),'rest_data':JSON[_0xbb67('0x1a')]({'session':_0x31c340['id']})}});}function getUser(_0x3427c2,_0x50ea79,_0x1d6e9d){if(!_[_0xbb67('0x21')](_0x50ea79)){logger['info'](_0xbb67('0x22'),_0x50ea79);return request({'form':{'method':_0xbb67('0x23'),'input_type':_0xbb67('0x20'),'response_type':_0xbb67('0x20'),'rest_data':JSON['stringify']({'session':_0x1d6e9d['id'],'module_name':_0xbb67('0x24'),'query':util[_0xbb67('0x1c')]('INSTR\x20(\x20users.user_name,\x20\x22%s\x22)\x20>\x200',_0x50ea79),'order_by':'','offset':'0','select_fields':['id',_0xbb67('0x15'),_0xbb67('0x25')],'link_name_to_fields_array':[],'max_results':-0x1,'deleted':'0'})}})[_0xbb67('0x26')](function(_0x36e7f8){if(_0x36e7f8['entry_list'][_0xbb67('0x27')]){var _0x4a4373=_[_0xbb67('0x28')](_0x36e7f8[_0xbb67('0x29')],function(_0x4ba342){return _0x4ba342[_0xbb67('0x2a')]['user_name'][_0xbb67('0x2b')]===_0x50ea79;});if(_0x4a4373){logger[_0xbb67('0x2c')](_0xbb67('0x2d'));return BPromise['resolve']({'id':_0x4a4373['name_value_list']['id']['value'],'name':_0x4a4373[_0xbb67('0x2a')][_0xbb67('0x15')][_0xbb67('0x2b')]});}}logger[_0xbb67('0x2c')](_0xbb67('0x2e'));return BPromise[_0xbb67('0x2f')]({'id':_0x1d6e9d['name_value_list'][_0xbb67('0x30')][_0xbb67('0x2b')],'name':_0x1d6e9d[_0xbb67('0x2a')][_0xbb67('0x25')][_0xbb67('0x2b')]});});}else{logger[_0xbb67('0x2c')]('Agent\x20not\x20available,\x20using\x20the\x20admin\x20account!');return BPromise[_0xbb67('0x2f')]({'id':_0x1d6e9d[_0xbb67('0x2a')]['user_id']['value'],'name':_0x1d6e9d[_0xbb67('0x2a')][_0xbb67('0x25')][_0xbb67('0x2b')]});}}function getEndUser(_0x6a4120,_0x296f0e,_0xf7a07a,_0x3304c6){logger[_0xbb67('0x2c')]('Searching\x20enduser\x20with\x20phone',_0xf7a07a);return request({'form':{'method':'get_entry_list','input_type':'JSON','response_type':_0xbb67('0x20'),'rest_data':JSON[_0xbb67('0x1a')]({'session':_0x3304c6['id'],'module_name':_0xbb67('0x13'),'query':util[_0xbb67('0x1c')]('contacts.phone_home\x20=\x20\x22%s\x22',_0xf7a07a),'order_by':'','offset':'0','select_fields':['id',_0xbb67('0x15'),_0xbb67('0x31')],'link_name_to_fields_array':[],'max_results':-0x1,'deleted':'0'})}})[_0xbb67('0x26')](function(_0x12dcba){if(_0x12dcba[_0xbb67('0x29')][_0xbb67('0x27')]){var _0x3138e2=_[_0xbb67('0x28')](_0x12dcba['entry_list'],function(_0x2a54f1){return _0x2a54f1[_0xbb67('0x2a')]['phone_home']['value']===_0xf7a07a;});if(_0x3138e2){logger['info']('Enduser\x20found!');return BPromise[_0xbb67('0x2f')]({'id':_0x3138e2[_0xbb67('0x2a')]['id'][_0xbb67('0x2b')],'name':_0x3138e2[_0xbb67('0x2a')][_0xbb67('0x15')][_0xbb67('0x2b')],'phone':_0x3138e2[_0xbb67('0x2a')][_0xbb67('0x31')][_0xbb67('0x2b')]});}}else{logger[_0xbb67('0x2c')](_0xbb67('0x32'));return request({'form':{'method':_0xbb67('0x19'),'input_type':_0xbb67('0x20'),'response_type':_0xbb67('0x20'),'rest_data':JSON[_0xbb67('0x1a')]({'session':_0x3304c6['id'],'module_name':_0xbb67('0x13'),'name_value_list':[{'name':'first_name','value':_0x296f0e!=='<unknown>'?_0x296f0e:util['format'](_0xbb67('0x33'),_0xf7a07a)},{'name':_0xbb67('0x31'),'value':_0xf7a07a}]})}})[_0xbb67('0x26')](function(_0x3d0220){logger[_0xbb67('0x2c')](_0xbb67('0x34'));return BPromise['resolve']({'id':_0x3d0220['id'],'name':_0x3d0220['entry_list']['first_name'][_0xbb67('0x2b')],'phone':_0x3d0220['entry_list']['phone_home'][_0xbb67('0x2b')]});});}});}function request(_0x241b0d){logger[_0xbb67('0x35')](_0xbb67('0x36'),JSON[_0xbb67('0x1a')](_0x241b0d));return rp(_[_0xbb67('0x37')](def,_0x241b0d));}function getTags(_0x3a6d72,_0x18f948){var _0x4d581b=[];if(_0x3a6d72&&_0x3a6d72[_0xbb67('0x27')]>0x0){_0x4d581b=_[_0xbb67('0x38')](_0x3a6d72,_0x18f948);}return _0x4d581b;}function getCustomFields(_0x3061c2,_0xc26b61){var _0x4bef60=[];var _0x1aa9b1={'custom_fields':[]};if(_0x3061c2[_0xbb67('0x27')]>0x0){_[_0xbb67('0x39')](_0x3061c2,function(_0x115252){if(_0x115252[_0xbb67('0x3a')]===_0xbb67('0x3b')&&_0x115252[_0xbb67('0x3c')]&&!_0x115252[_0xbb67('0x3d')]){_0x1aa9b1[_0x115252[_0xbb67('0x3c')]['toLowerCase']()]=_0x115252[_0xbb67('0x3e')]['toString']();}else if(_0x115252['type']===_0xbb67('0x3b')&&_0x115252['idField']&&_0x115252[_0xbb67('0x3d')]){_0x1aa9b1[_0xbb67('0x3f')]['push']({'id':_0x115252[_0xbb67('0x18')],'value':_0x115252['content'][_0xbb67('0x1e')]()});}else if(_0x115252[_0xbb67('0x3a')]===_0xbb67('0x40')&&_0x115252[_0xbb67('0x17')]&&_0x115252['idField']&&_0x115252[_0xbb67('0x3d')]){_0xc26b61[_0x115252['variableName']]&&_0x1aa9b1[_0xbb67('0x3f')]['push']({'id':_0x115252[_0xbb67('0x18')],'value':_0xc26b61[_0x115252[_0xbb67('0x17')]][_0xbb67('0x1e')]()});}else if(_0x115252[_0xbb67('0x3a')]===_0xbb67('0x41')&&_0x115252['Variable']&&_0x115252[_0xbb67('0x42')]['name']&&_0x115252[_0xbb67('0x18')]&&_0x115252[_0xbb67('0x3d')]){_0xc26b61[_0x115252[_0xbb67('0x42')][_0xbb67('0x15')][_0xbb67('0x43')]()]&&_0x1aa9b1[_0xbb67('0x3f')]['push']({'id':_0x115252[_0xbb67('0x18')],'value':_0xc26b61[_0x115252['Variable'][_0xbb67('0x15')][_0xbb67('0x43')]()][_0xbb67('0x1e')]()});}});}return _0x1aa9b1;}function getConnection(_0x5d2bd8){return request({'method':'POST','form':{'method':_0xbb67('0x44'),'input_type':_0xbb67('0x20'),'response_type':_0xbb67('0x20'),'rest_data':JSON['stringify']({'user_auth':{'user_name':_0x5d2bd8['username'],'password':_0x5d2bd8['password'],'encryption':_0xbb67('0x45')},'application':_0xbb67('0x46')})}})['then'](function(_0x5bac24){var _0x3b0bd3=_0x5bac24;if(_0x3b0bd3['id']){def['sessionID']=_0x3b0bd3['id'];return BPromise[_0xbb67('0x2f')](_0x3b0bd3);}else{throw new Error(util[_0xbb67('0x1c')]('Account\x20%d\x20is\x20not\x20valid',_0x5d2bd8['id']));}});}function setRelationship(_0xdc691e,_0x4f3b28,_0x1c2b44,_0x17b5fc){return request({'form':{'method':_0xbb67('0x47'),'input_type':_0xbb67('0x20'),'response_type':'JSON','rest_data':JSON[_0xbb67('0x1a')]({'session':_0xdc691e['id'],'module_name':_0xbb67('0x1b'),'module_id':_0x1c2b44['id'],'link_field_name':_0x17b5fc,'related_ids':[_0x4f3b28['id']],'name_value_list':[],'delete':0x0})}});}exports['queue']=function(_0x58a9b6,_0x1c1a74,_0x227d93,_0xb4849f,_0x50b863,_0x56aa31){var _0x17b58a,_0x3c3592,_0x51e761,_0x41e26c;_0x56aa31[_0xbb67('0x48')]=intUtil['stripTrailingSlash'](_0x56aa31[_0xbb67('0x48')]);_0x56aa31['serverUrl']=intUtil[_0xbb67('0x49')](_0x56aa31[_0xbb67('0x4a')]);def[_0xbb67('0x4b')]=util[_0xbb67('0x1c')](_0xbb67('0x4c'),_0x56aa31['remoteUri']);if(_0x50b863&&_0x50b863[_0xbb67('0x4d')]){logger[_0xbb67('0x2c')](_0xbb67('0x4e'),_0xbb67('0x4f'));_0x1c1a74[_0xbb67('0x50')]=util[_0xbb67('0x1c')]('%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s',_0x56aa31[_0xbb67('0x4a')],_0x1c1a74['uniqueid'],md5(_0x1c1a74['uniqueid']));}else{_0x1c1a74[_0xbb67('0x50')]='';}return getConnection(_0x56aa31)[_0xbb67('0x26')](function(_0x13b3e1){_0x41e26c=_0x13b3e1;return getUser(_0x56aa31,_0xb4849f['name'],_0x41e26c);})[_0xbb67('0x26')](function(_0x414371){_0x17b58a=_0x414371;logger[_0xbb67('0x2c')](_0xbb67('0x51'),_0x17b58a['id']);return getEndUser(_0x56aa31,_0x1c1a74['calleridname'],_0x1c1a74['calleridnum'],_0x41e26c);})[_0xbb67('0x26')](function(_0x4c3a8d){_0x3c3592=_0x4c3a8d;logger[_0xbb67('0x2c')](_0xbb67('0x52'),_0x3c3592['id']);return getTicket(_0x56aa31,_0x17b58a,_0x3c3592,_0x227d93,_0x1c1a74,_0x227d93['Fields'],_0x41e26c,'Inbound');})[_0xbb67('0x26')](function(_0xac10e9){_0x51e761=_0xac10e9;logger[_0xbb67('0x2c')](_0xbb67('0x53'));logger[_0xbb67('0x2c')](_0xbb67('0x54'),_0x51e761['id']);return setRelationship(_0x41e26c,_0x17b58a,_0x51e761,_0xbb67('0x55'));})[_0xbb67('0x26')](function(){return setRelationship(_0x41e26c,_0x3c3592,_0x51e761,_0xbb67('0x56'));})[_0xbb67('0x26')](function(){return getServerInfo(_0x41e26c);})[_0xbb67('0x26')](function(_0x4a1d82){var _0x586780=parseInt(_0x4a1d82[_0xbb67('0x57')][_0xbb67('0x58')](0x0));var _0x3e9107=util[_0xbb67('0x1c')](_0xbb67('0x59'),_0x56aa31[_0xbb67('0x48')],_0x51e761['id']);if(_0x586780>0x6){_0x3e9107=util['format'](_0xbb67('0x5a'),_0x56aa31[_0xbb67('0x48')],_0x51e761['id']);}logger[_0xbb67('0x2c')](_0xbb67('0x4e'),_0xbb67('0x5b'));emit(util[_0xbb67('0x1c')](_0xbb67('0x5c'),_0xb4849f[_0xbb67('0x15')]),'trigger:browser:url',{'uri':_0x3e9107});})[_0xbb67('0x5d')](function(_0x59258f){logger['error'](_0xbb67('0x4e'),JSON[_0xbb67('0x1a')](_0x59258f));});};exports[_0xbb67('0x5e')]=function(_0x32e549,_0x498f51,_0x264c6d,_0x4e64f1,_0x34f73c,_0x1fa11a,_0x579ff8){var _0x35c6a3,_0x443cbe,_0x4cee6d,_0x2ed2d9;_0x1fa11a[_0xbb67('0x48')]=intUtil[_0xbb67('0x49')](_0x1fa11a[_0xbb67('0x48')]);_0x1fa11a[_0xbb67('0x4a')]=intUtil[_0xbb67('0x49')](_0x1fa11a[_0xbb67('0x4a')]);def[_0xbb67('0x4b')]=util[_0xbb67('0x1c')](_0xbb67('0x4c'),_0x1fa11a[_0xbb67('0x48')]);if(_0x579ff8&&_0x579ff8[_0xbb67('0x5f')]&&_0x579ff8[_0xbb67('0x5f')]!==_0xbb67('0x60')){logger[_0xbb67('0x2c')](_0xbb67('0x61'),_0xbb67('0x4f'));_0x498f51[_0xbb67('0x50')]=util[_0xbb67('0x1c')](_0xbb67('0x62'),_0x1fa11a[_0xbb67('0x4a')],_0x498f51[_0xbb67('0x63')],md5(_0x498f51[_0xbb67('0x63')]));}return getConnection(_0x1fa11a)['then'](function(_0x39b195){_0x2ed2d9=_0x39b195;return getUser(_0x1fa11a,_0x4e64f1[_0xbb67('0x15')],_0x2ed2d9);})[_0xbb67('0x26')](function(_0x3b9155){_0x35c6a3=_0x3b9155;logger[_0xbb67('0x2c')](_0xbb67('0x51'),_0x35c6a3['id']);return getEndUser(_0x1fa11a,_0x498f51[_0xbb67('0x64')],_0x498f51[_0xbb67('0x65')],_0x2ed2d9);})[_0xbb67('0x26')](function(_0x542bac){_0x443cbe=_0x542bac;logger[_0xbb67('0x2c')](_0xbb67('0x52'),_0x443cbe['id']);return getTicket(_0x1fa11a,_0x35c6a3,_0x443cbe,_0x264c6d,_0x498f51,_0x264c6d[_0xbb67('0x66')],_0x2ed2d9,'Outbound');})['then'](function(_0x33189c){_0x4cee6d=_0x33189c;logger[_0xbb67('0x2c')](_0xbb67('0x53'));logger[_0xbb67('0x2c')](_0xbb67('0x54'),_0x4cee6d['id']);return setRelationship(_0x2ed2d9,_0x35c6a3,_0x4cee6d,_0xbb67('0x55'));})[_0xbb67('0x26')](function(){return setRelationship(_0x2ed2d9,_0x443cbe,_0x4cee6d,_0xbb67('0x56'));})[_0xbb67('0x26')](function(){return getServerInfo(_0x2ed2d9);})['then'](function(_0x5325fa){var _0x5c6b0d=parseInt(_0x5325fa[_0xbb67('0x57')][_0xbb67('0x58')](0x0));var _0x112dbe=util[_0xbb67('0x1c')](_0xbb67('0x59'),_0x1fa11a[_0xbb67('0x48')],_0x4cee6d['id']);if(_0x5c6b0d>0x6){_0x112dbe=util['format']('%s/#bwc/index.php?module=Calls&action=EditView&record=%s',_0x1fa11a[_0xbb67('0x48')],_0x4cee6d['id']);}logger[_0xbb67('0x2c')](_0xbb67('0x61'),_0xbb67('0x5b'));emit(util[_0xbb67('0x1c')](_0xbb67('0x5c'),_0x4e64f1[_0xbb67('0x15')]),_0xbb67('0x67'),{'uri':_0x112dbe});})['catch'](function(_0x411636){logger[_0xbb67('0x68')]('[OUTBOUND]',JSON[_0xbb67('0x1a')](_0x411636));});};exports['unmanaged']=function(_0xb8d48c,_0x5138d9,_0x44640c,_0x726f82,_0x46ada9,_0x4a6f11){var _0x3161a0,_0x1183f4,_0x2eaeb8,_0x2c3239;_0x4a6f11[_0xbb67('0x48')]=intUtil[_0xbb67('0x49')](_0x4a6f11[_0xbb67('0x48')]);_0x4a6f11[_0xbb67('0x4a')]=intUtil['stripTrailingSlash'](_0x4a6f11[_0xbb67('0x4a')]);def[_0xbb67('0x4b')]=util[_0xbb67('0x1c')]('%s/service/v4_1/rest.php',_0x4a6f11[_0xbb67('0x48')]);if(_0x46ada9&&_0x46ada9[_0xbb67('0x4d')]){logger[_0xbb67('0x2c')]('['+_0x5138d9['lastevent'][_0xbb67('0x69')]()+']','Recording\x20is\x20enabled!');_0x5138d9[_0xbb67('0x50')]=util[_0xbb67('0x1c')]('%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s',_0x4a6f11[_0xbb67('0x4a')],_0x5138d9[_0xbb67('0x63')],md5(_0x5138d9[_0xbb67('0x63')]));}else{_0x5138d9['recordingURL']='';}return getConnection(_0x4a6f11)[_0xbb67('0x26')](function(_0xd94704){_0x2c3239=_0xd94704;if(!_[_0xbb67('0x21')](_0x726f82)){return getUser(_0x4a6f11,_0x726f82[_0xbb67('0x15')],_0x2c3239);}else{return getUser(_0x4a6f11,null,_0x2c3239);}})['then'](function(_0x331201){_0x3161a0=_0x331201;logger[_0xbb67('0x2c')](_0xbb67('0x51'),_0x3161a0['id']);return getEndUser(_0x4a6f11,_0x5138d9[_0xbb67('0x6a')],_0x5138d9[_0xbb67('0x6b')],_0x2c3239);})[_0xbb67('0x26')](function(_0x2b499b){_0x1183f4=_0x2b499b;logger[_0xbb67('0x2c')]('The\x20Enduser\x20ID\x20is:',_0x1183f4['id']);return getTicket(_0x4a6f11,_0x3161a0,_0x1183f4,_0x44640c,_0x5138d9,_0x44640c[_0xbb67('0x66')],_0x2c3239,'Inbound');})['then'](function(_0x5deec1){_0x2eaeb8=_0x5deec1;logger['info'](_0xbb67('0x53'));logger[_0xbb67('0x2c')]('The\x20Ticket\x20ID\x20is:',_0x2eaeb8['id']);return setRelationship(_0x2c3239,_0x3161a0,_0x2eaeb8,_0xbb67('0x55'));})[_0xbb67('0x26')](function(){return setRelationship(_0x2c3239,_0x1183f4,_0x2eaeb8,'contacts');})[_0xbb67('0x26')](function(){return getServerInfo(_0x2c3239);})[_0xbb67('0x5d')](function(_0x4a2772){logger[_0xbb67('0x68')]('['+_0x5138d9[_0xbb67('0x6c')]['toUpperCase']()+']',JSON[_0xbb67('0x1a')](_0x4a2772));});};
\ No newline at end of file
+var _0x88f2=['name_value_list','value','Agent\x20found!','resolve','Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!','user_id','user_name','Searching\x20enduser\x20with\x20phone','contacts.phone_home\x20=\x20\x22%s\x22','phone_home','Enduser\x20found!','Enduser\x20not\x20found,\x20creating\x20a\x20new\x20enduser!','first_name','<unknown>','Caller\x20-\x20%s','Enduser\x20created.','debug','merge','map','type','nameField','customField','toLowerCase','content','toString','string','custom_fields','variable','push','customVariable','Variable','login','password','PLAIN','xCALLY\x20Motion\x20integration','sessionID','set_relationship','remoteUri','stripTrailingSlash','serverUrl','uri','%s/service/v4_1/rest.php','monitor_format','[QUEUE]','Recording\x20is\x20enabled!','recordingURL','uniqueid','The\x20User\x20ID\x20is:','calleridname','calleridnum','The\x20Enduser\x20ID\x20is:','Fields','Inbound','Ticket\x20created!','The\x20Ticket\x20ID\x20is:','users','version','charAt','%s/index.php?action=ajaxui#ajaxUILoc=index.php%3Fmodule%3DCalls%26action%3DEditView%26record%3D%s','Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...','trigger:browser:url','outbound','recordingFormat','none','%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s','destcalleridname','destcalleridnum','Outbound','contacts','%s/#bwc/index.php?module=Calls&action=EditView&record=%s','user:%s','error','[OUTBOUND]','unmanaged','lastevent','toUpperCase','isNil','catch','lodash','request-promise','util','ioredis','../../../../config/environment','../../../../config/logger','sugarcrm','redis','defaults','localhost','socket.io-emitter','POST','emit','Creating\x20new\x20ticket...','name','getString','assigned_user_id','description','Descriptions','toISOString','slice','duration_minutes','parent_id','parent_type','Contacts','direction','forEach','variableName','idField','set_entry','JSON','stringify','Calls','format','Basic\x20%s','apiKey','base64','get_server_info','info','get_entry_list','INSTR\x20(\x20users.user_name,\x20\x22%s\x22)\x20>\x200','then','entry_list','length','find'];(function(_0x321bb2,_0x202794){var _0x9c12ed=function(_0x263dd3){while(--_0x263dd3){_0x321bb2['push'](_0x321bb2['shift']());}};_0x9c12ed(++_0x202794);}(_0x88f2,0x144));var _0x288f=function(_0x44f33f,_0x1349f5){_0x44f33f=_0x44f33f-0x0;var _0x39603c=_0x88f2[_0x44f33f];return _0x39603c;};'use strict';var _=require(_0x288f('0x0'));var rp=require(_0x288f('0x1'));var BPromise=require('bluebird');var md5=require('md5');var util=require(_0x288f('0x2'));var Redis=require(_0x288f('0x3'));var intUtil=require('./util');var config=require(_0x288f('0x4'));var logger=require(_0x288f('0x5'))(_0x288f('0x6'));config[_0x288f('0x7')]=_[_0x288f('0x8')](config['redis'],{'host':_0x288f('0x9'),'port':0x18eb});var io=require(_0x288f('0xa'))(new Redis(config[_0x288f('0x7')]));var def={'method':_0x288f('0xb'),'json':!![]};function emit(_0x3d2819,_0x50caf7,_0x3d0fc5){io['to'](_0x3d2819)[_0x288f('0xc')](_0x50caf7,_0x3d0fc5);}function getTicket(_0xe5aa87,_0x5ba8a8,_0x418a6c,_0x3ca74a,_0x350c34,_0x486abd,_0x261a74,_0x763bde){logger['info'](_0x288f('0xd'));var _0x1000df=new Date();var _0x3da9b8=[{'name':_0x288f('0xe'),'value':intUtil[_0x288f('0xf')](_0x3ca74a['Subjects'],_0x350c34,'\x20')},{'name':_0x288f('0x10'),'value':_0x5ba8a8['id']},{'name':'created_by','value':_0x5ba8a8['id']},{'name':_0x288f('0x11'),'value':intUtil['getString'](_0x3ca74a[_0x288f('0x12')],_0x350c34,'\x0a')},{'name':'date_start','value':_0x1000df[_0x288f('0x13')]()[_0x288f('0x14')](0x0,0x13)['replace']('T','\x20')},{'name':'duration_hours','value':'0'},{'name':_0x288f('0x15'),'value':'15'},{'name':_0x288f('0x16'),'value':_0x418a6c['id']},{'name':_0x288f('0x17'),'value':_0x288f('0x18')},{'name':'parent_name','value':_0x418a6c[_0x288f('0xe')]},{'name':_0x288f('0x19'),'value':_0x763bde}];if(_0x486abd['length']){_[_0x288f('0x1a')](_0x486abd,function(_0x24650f){if(_0x350c34[_0x24650f[_0x288f('0x1b')]]){_0x3da9b8['push']({'name':_0x24650f[_0x288f('0x1c')],'value':_0x350c34[_0x24650f[_0x288f('0x1b')]]});}});}return request({'form':{'method':_0x288f('0x1d'),'input_type':_0x288f('0x1e'),'response_type':_0x288f('0x1e'),'rest_data':JSON[_0x288f('0x1f')]({'session':_0x261a74['id'],'module_name':_0x288f('0x20'),'name_value_list':_0x3da9b8})}});}function getAuth(_0x3d2358){return util[_0x288f('0x21')](_0x288f('0x22'),new Buffer(_0x3d2358[_0x288f('0x23')]+':X')['toString'](_0x288f('0x24')));}function getServerInfo(_0x31dc20){return request({'form':{'method':_0x288f('0x25'),'input_type':_0x288f('0x1e'),'response_type':_0x288f('0x1e'),'rest_data':JSON[_0x288f('0x1f')]({'session':_0x31dc20['id']})}});}function getUser(_0xc000f4,_0x50709d,_0x1ce548){if(!_['isNil'](_0x50709d)){logger[_0x288f('0x26')]('Getting\x20agent\x20with\x20name',_0x50709d);return request({'form':{'method':_0x288f('0x27'),'input_type':'JSON','response_type':_0x288f('0x1e'),'rest_data':JSON[_0x288f('0x1f')]({'session':_0x1ce548['id'],'module_name':'Employees','query':util[_0x288f('0x21')](_0x288f('0x28'),_0x50709d),'order_by':'','offset':'0','select_fields':['id',_0x288f('0xe'),'user_name'],'link_name_to_fields_array':[],'max_results':-0x1,'deleted':'0'})}})[_0x288f('0x29')](function(_0x1aed32){if(_0x1aed32[_0x288f('0x2a')][_0x288f('0x2b')]){var _0x2a3cb8=_[_0x288f('0x2c')](_0x1aed32['entry_list'],function(_0x14376a){return _0x14376a[_0x288f('0x2d')]['user_name'][_0x288f('0x2e')]===_0x50709d;});if(_0x2a3cb8){logger[_0x288f('0x26')](_0x288f('0x2f'));return BPromise[_0x288f('0x30')]({'id':_0x2a3cb8[_0x288f('0x2d')]['id'][_0x288f('0x2e')],'name':_0x2a3cb8['name_value_list'][_0x288f('0xe')][_0x288f('0x2e')]});}}logger['info'](_0x288f('0x31'));return BPromise[_0x288f('0x30')]({'id':_0x1ce548[_0x288f('0x2d')]['user_id'][_0x288f('0x2e')],'name':_0x1ce548[_0x288f('0x2d')]['user_name']['value']});});}else{logger[_0x288f('0x26')]('Agent\x20not\x20available,\x20using\x20the\x20admin\x20account!');return BPromise['resolve']({'id':_0x1ce548['name_value_list'][_0x288f('0x32')][_0x288f('0x2e')],'name':_0x1ce548['name_value_list'][_0x288f('0x33')][_0x288f('0x2e')]});}}function getEndUser(_0x18adbb,_0x214e05,_0x3373cc,_0x2c1bdd){logger[_0x288f('0x26')](_0x288f('0x34'),_0x3373cc);return request({'form':{'method':_0x288f('0x27'),'input_type':_0x288f('0x1e'),'response_type':_0x288f('0x1e'),'rest_data':JSON[_0x288f('0x1f')]({'session':_0x2c1bdd['id'],'module_name':_0x288f('0x18'),'query':util[_0x288f('0x21')](_0x288f('0x35'),_0x3373cc),'order_by':'','offset':'0','select_fields':['id','name','phone_home'],'link_name_to_fields_array':[],'max_results':-0x1,'deleted':'0'})}})[_0x288f('0x29')](function(_0x46ed66){if(_0x46ed66[_0x288f('0x2a')][_0x288f('0x2b')]){var _0x1c72d5=_[_0x288f('0x2c')](_0x46ed66[_0x288f('0x2a')],function(_0x545a77){return _0x545a77[_0x288f('0x2d')][_0x288f('0x36')][_0x288f('0x2e')]===_0x3373cc;});if(_0x1c72d5){logger[_0x288f('0x26')](_0x288f('0x37'));return BPromise[_0x288f('0x30')]({'id':_0x1c72d5[_0x288f('0x2d')]['id']['value'],'name':_0x1c72d5[_0x288f('0x2d')][_0x288f('0xe')][_0x288f('0x2e')],'phone':_0x1c72d5[_0x288f('0x2d')][_0x288f('0x36')][_0x288f('0x2e')]});}}else{logger['info'](_0x288f('0x38'));return request({'form':{'method':'set_entry','input_type':_0x288f('0x1e'),'response_type':_0x288f('0x1e'),'rest_data':JSON['stringify']({'session':_0x2c1bdd['id'],'module_name':_0x288f('0x18'),'name_value_list':[{'name':_0x288f('0x39'),'value':_0x214e05!==_0x288f('0x3a')?_0x214e05:util[_0x288f('0x21')](_0x288f('0x3b'),_0x3373cc)},{'name':_0x288f('0x36'),'value':_0x3373cc}]})}})[_0x288f('0x29')](function(_0x1b16c1){logger[_0x288f('0x26')](_0x288f('0x3c'));return BPromise[_0x288f('0x30')]({'id':_0x1b16c1['id'],'name':_0x1b16c1[_0x288f('0x2a')][_0x288f('0x39')][_0x288f('0x2e')],'phone':_0x1b16c1[_0x288f('0x2a')]['phone_home'][_0x288f('0x2e')]});});}});}function request(_0xc1bc4c){logger[_0x288f('0x3d')]('[HTTP]',JSON['stringify'](_0xc1bc4c));return rp(_[_0x288f('0x3e')](def,_0xc1bc4c));}function getTags(_0x177e18,_0x144074){var _0x3cc089=[];if(_0x177e18&&_0x177e18[_0x288f('0x2b')]>0x0){_0x3cc089=_[_0x288f('0x3f')](_0x177e18,_0x144074);}return _0x3cc089;}function getCustomFields(_0x26182b,_0x292ae8){var _0x4a52a5=[];var _0x55af53={'custom_fields':[]};if(_0x26182b[_0x288f('0x2b')]>0x0){_[_0x288f('0x1a')](_0x26182b,function(_0x5f4388){if(_0x5f4388[_0x288f('0x40')]==='string'&&_0x5f4388[_0x288f('0x41')]&&!_0x5f4388[_0x288f('0x42')]){_0x55af53[_0x5f4388['nameField'][_0x288f('0x43')]()]=_0x5f4388[_0x288f('0x44')][_0x288f('0x45')]();}else if(_0x5f4388['type']===_0x288f('0x46')&&_0x5f4388[_0x288f('0x1c')]&&_0x5f4388[_0x288f('0x42')]){_0x55af53[_0x288f('0x47')]['push']({'id':_0x5f4388[_0x288f('0x1c')],'value':_0x5f4388[_0x288f('0x44')]['toString']()});}else if(_0x5f4388[_0x288f('0x40')]===_0x288f('0x48')&&_0x5f4388[_0x288f('0x1b')]&&_0x5f4388[_0x288f('0x1c')]&&_0x5f4388[_0x288f('0x42')]){_0x292ae8[_0x5f4388[_0x288f('0x1b')]]&&_0x55af53[_0x288f('0x47')][_0x288f('0x49')]({'id':_0x5f4388[_0x288f('0x1c')],'value':_0x292ae8[_0x5f4388['variableName']][_0x288f('0x45')]()});}else if(_0x5f4388['type']===_0x288f('0x4a')&&_0x5f4388['Variable']&&_0x5f4388[_0x288f('0x4b')][_0x288f('0xe')]&&_0x5f4388[_0x288f('0x1c')]&&_0x5f4388[_0x288f('0x42')]){_0x292ae8[_0x5f4388[_0x288f('0x4b')]['name'][_0x288f('0x43')]()]&&_0x55af53[_0x288f('0x47')]['push']({'id':_0x5f4388[_0x288f('0x1c')],'value':_0x292ae8[_0x5f4388[_0x288f('0x4b')][_0x288f('0xe')]['toLowerCase']()][_0x288f('0x45')]()});}});}return _0x55af53;}function getConnection(_0x38e036){return request({'method':_0x288f('0xb'),'form':{'method':_0x288f('0x4c'),'input_type':'JSON','response_type':_0x288f('0x1e'),'rest_data':JSON[_0x288f('0x1f')]({'user_auth':{'user_name':_0x38e036['username'],'password':_0x38e036[_0x288f('0x4d')],'encryption':_0x288f('0x4e')},'application':_0x288f('0x4f')})}})[_0x288f('0x29')](function(_0x286981){var _0x43773d=_0x286981;if(_0x43773d['id']){def[_0x288f('0x50')]=_0x43773d['id'];return BPromise[_0x288f('0x30')](_0x43773d);}else{throw new Error(util['format']('Account\x20%d\x20is\x20not\x20valid',_0x38e036['id']));}});}function setRelationship(_0xe3f48f,_0x139f40,_0x572622,_0x4fe37f){return request({'form':{'method':_0x288f('0x51'),'input_type':_0x288f('0x1e'),'response_type':_0x288f('0x1e'),'rest_data':JSON[_0x288f('0x1f')]({'session':_0xe3f48f['id'],'module_name':'Calls','module_id':_0x572622['id'],'link_field_name':_0x4fe37f,'related_ids':[_0x139f40['id']],'name_value_list':[],'delete':0x0})}});}exports['queue']=function(_0x36b56c,_0x436f05,_0x5184b6,_0x20f4e0,_0x547552,_0x30bcfd){var _0x4dddbc,_0x344889,_0x4f4d20,_0xd23fc;_0x30bcfd[_0x288f('0x52')]=intUtil['stripTrailingSlash'](_0x30bcfd['remoteUri']);_0x30bcfd['serverUrl']=intUtil[_0x288f('0x53')](_0x30bcfd[_0x288f('0x54')]);def[_0x288f('0x55')]=util[_0x288f('0x21')](_0x288f('0x56'),_0x30bcfd['remoteUri']);if(_0x547552&&_0x547552[_0x288f('0x57')]){logger[_0x288f('0x26')](_0x288f('0x58'),_0x288f('0x59'));_0x436f05[_0x288f('0x5a')]=util[_0x288f('0x21')]('%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s',_0x30bcfd[_0x288f('0x54')],_0x436f05['uniqueid'],md5(_0x436f05[_0x288f('0x5b')]));}else{_0x436f05[_0x288f('0x5a')]='';}return getConnection(_0x30bcfd)['then'](function(_0x7a70da){_0xd23fc=_0x7a70da;return getUser(_0x30bcfd,_0x20f4e0[_0x288f('0xe')],_0xd23fc);})['then'](function(_0x5e5435){_0x4dddbc=_0x5e5435;logger[_0x288f('0x26')](_0x288f('0x5c'),_0x4dddbc['id']);return getEndUser(_0x30bcfd,_0x436f05[_0x288f('0x5d')],_0x436f05[_0x288f('0x5e')],_0xd23fc);})[_0x288f('0x29')](function(_0x2e62cf){_0x344889=_0x2e62cf;logger[_0x288f('0x26')](_0x288f('0x5f'),_0x344889['id']);return getTicket(_0x30bcfd,_0x4dddbc,_0x344889,_0x5184b6,_0x436f05,_0x5184b6[_0x288f('0x60')],_0xd23fc,_0x288f('0x61'));})[_0x288f('0x29')](function(_0x579aba){_0x4f4d20=_0x579aba;logger[_0x288f('0x26')](_0x288f('0x62'));logger[_0x288f('0x26')](_0x288f('0x63'),_0x4f4d20['id']);return setRelationship(_0xd23fc,_0x4dddbc,_0x4f4d20,_0x288f('0x64'));})['then'](function(){return setRelationship(_0xd23fc,_0x344889,_0x4f4d20,'contacts');})[_0x288f('0x29')](function(){return getServerInfo(_0xd23fc);})[_0x288f('0x29')](function(_0x56b507){var _0x5a40a5=parseInt(_0x56b507[_0x288f('0x65')][_0x288f('0x66')](0x0));var _0x21b5b8=util['format'](_0x288f('0x67'),_0x30bcfd[_0x288f('0x52')],_0x4f4d20['id']);if(_0x5a40a5>0x6){_0x21b5b8=util[_0x288f('0x21')]('%s/#bwc/index.php?module=Calls&action=EditView&record=%s',_0x30bcfd[_0x288f('0x52')],_0x4f4d20['id']);}logger[_0x288f('0x26')](_0x288f('0x58'),_0x288f('0x68'));emit(util[_0x288f('0x21')]('user:%s',_0x20f4e0[_0x288f('0xe')]),_0x288f('0x69'),{'uri':_0x21b5b8});})['catch'](function(_0x6d2b4b){logger['error'](_0x288f('0x58'),JSON[_0x288f('0x1f')](_0x6d2b4b));});};exports[_0x288f('0x6a')]=function(_0x1000bc,_0x3180af,_0x1f8bcb,_0x1c2ad2,_0x4b89dc,_0x63d751,_0xcfa036){var _0x441f2d,_0x5dfcde,_0x45377d,_0x55d078;_0x63d751['remoteUri']=intUtil[_0x288f('0x53')](_0x63d751[_0x288f('0x52')]);_0x63d751['serverUrl']=intUtil[_0x288f('0x53')](_0x63d751[_0x288f('0x54')]);def[_0x288f('0x55')]=util[_0x288f('0x21')](_0x288f('0x56'),_0x63d751['remoteUri']);if(_0xcfa036&&_0xcfa036[_0x288f('0x6b')]&&_0xcfa036[_0x288f('0x6b')]!==_0x288f('0x6c')){logger[_0x288f('0x26')]('[OUTBOUND]','Recording\x20is\x20enabled!');_0x3180af[_0x288f('0x5a')]=util['format'](_0x288f('0x6d'),_0x63d751[_0x288f('0x54')],_0x3180af[_0x288f('0x5b')],md5(_0x3180af[_0x288f('0x5b')]));}return getConnection(_0x63d751)[_0x288f('0x29')](function(_0x2b4ee4){_0x55d078=_0x2b4ee4;return getUser(_0x63d751,_0x1c2ad2[_0x288f('0xe')],_0x55d078);})['then'](function(_0x19ccfa){_0x441f2d=_0x19ccfa;logger['info'](_0x288f('0x5c'),_0x441f2d['id']);return getEndUser(_0x63d751,_0x3180af[_0x288f('0x6e')],_0x3180af[_0x288f('0x6f')],_0x55d078);})['then'](function(_0x5f563f){_0x5dfcde=_0x5f563f;logger[_0x288f('0x26')]('The\x20Enduser\x20ID\x20is:',_0x5dfcde['id']);return getTicket(_0x63d751,_0x441f2d,_0x5dfcde,_0x1f8bcb,_0x3180af,_0x1f8bcb[_0x288f('0x60')],_0x55d078,_0x288f('0x70'));})[_0x288f('0x29')](function(_0x2a160){_0x45377d=_0x2a160;logger[_0x288f('0x26')]('Ticket\x20created!');logger[_0x288f('0x26')](_0x288f('0x63'),_0x45377d['id']);return setRelationship(_0x55d078,_0x441f2d,_0x45377d,_0x288f('0x64'));})[_0x288f('0x29')](function(){return setRelationship(_0x55d078,_0x5dfcde,_0x45377d,_0x288f('0x71'));})['then'](function(){return getServerInfo(_0x55d078);})[_0x288f('0x29')](function(_0x38a10a){var _0x2b1088=parseInt(_0x38a10a['version']['charAt'](0x0));var _0x280ce2=util['format'](_0x288f('0x67'),_0x63d751[_0x288f('0x52')],_0x45377d['id']);if(_0x2b1088>0x6){_0x280ce2=util[_0x288f('0x21')](_0x288f('0x72'),_0x63d751[_0x288f('0x52')],_0x45377d['id']);}logger[_0x288f('0x26')]('[OUTBOUND]',_0x288f('0x68'));emit(util[_0x288f('0x21')](_0x288f('0x73'),_0x1c2ad2[_0x288f('0xe')]),_0x288f('0x69'),{'uri':_0x280ce2});})['catch'](function(_0x31568a){logger[_0x288f('0x74')](_0x288f('0x75'),JSON[_0x288f('0x1f')](_0x31568a));});};exports[_0x288f('0x76')]=function(_0x1de62e,_0x498bf3,_0x59194e,_0x49704c,_0x17bcbf,_0x551184){var _0xe1042c,_0x3eabb8,_0x5885d3,_0x10836c;_0x551184[_0x288f('0x52')]=intUtil[_0x288f('0x53')](_0x551184[_0x288f('0x52')]);_0x551184['serverUrl']=intUtil[_0x288f('0x53')](_0x551184[_0x288f('0x54')]);def[_0x288f('0x55')]=util[_0x288f('0x21')](_0x288f('0x56'),_0x551184[_0x288f('0x52')]);if(_0x17bcbf&&_0x17bcbf[_0x288f('0x57')]){logger['info']('['+_0x498bf3[_0x288f('0x77')][_0x288f('0x78')]()+']',_0x288f('0x59'));_0x498bf3[_0x288f('0x5a')]=util[_0x288f('0x21')](_0x288f('0x6d'),_0x551184[_0x288f('0x54')],_0x498bf3[_0x288f('0x5b')],md5(_0x498bf3[_0x288f('0x5b')]));}else{_0x498bf3[_0x288f('0x5a')]='';}return getConnection(_0x551184)[_0x288f('0x29')](function(_0x479aa1){_0x10836c=_0x479aa1;if(!_[_0x288f('0x79')](_0x49704c)){return getUser(_0x551184,_0x49704c['name'],_0x10836c);}else{return getUser(_0x551184,null,_0x10836c);}})[_0x288f('0x29')](function(_0x4d054f){_0xe1042c=_0x4d054f;logger['info'](_0x288f('0x5c'),_0xe1042c['id']);return getEndUser(_0x551184,_0x498bf3['calleridname'],_0x498bf3[_0x288f('0x5e')],_0x10836c);})[_0x288f('0x29')](function(_0x3d1094){_0x3eabb8=_0x3d1094;logger['info'](_0x288f('0x5f'),_0x3eabb8['id']);return getTicket(_0x551184,_0xe1042c,_0x3eabb8,_0x59194e,_0x498bf3,_0x59194e[_0x288f('0x60')],_0x10836c,_0x288f('0x61'));})[_0x288f('0x29')](function(_0x3c8104){_0x5885d3=_0x3c8104;logger['info'](_0x288f('0x62'));logger[_0x288f('0x26')](_0x288f('0x63'),_0x5885d3['id']);return setRelationship(_0x10836c,_0xe1042c,_0x5885d3,_0x288f('0x64'));})[_0x288f('0x29')](function(){return setRelationship(_0x10836c,_0x3eabb8,_0x5885d3,_0x288f('0x71'));})[_0x288f('0x29')](function(){return getServerInfo(_0x10836c);})[_0x288f('0x7a')](function(_0x3c3500){logger[_0x288f('0x74')]('['+_0x498bf3[_0x288f('0x77')][_0x288f('0x78')]()+']',JSON[_0x288f('0x1f')](_0x3c3500));});};
\ No newline at end of file