Built motion from commit aab208717af8d15f4fa224cdb6a202877019af9b. Version 3.0.0...
[motion-next.git] / server / services / ami / trigger / integration / sugarcrm.js
1 const a1143_0x2c34=['The\x20Ticket\x20ID\x20is:','toUpperCase','debug','direction','name','Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...','name_value_list','idField','get_entry_list','users','destcalleridname','%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s','Recording\x20is\x20enabled!','Ticket\x20created!','version','merge','xCALLY\x20Motion\x20integration','recordingFormat','contacts','uniqueid','%s/index.php?action=ajaxui#ajaxUILoc=index.php%3Fmodule%3DCalls%26action%3DEditView%26record%3D%s','Creating\x20new\x20ticket...','first_name','user_id','contacts.phone_home\x20=\x20\x22%s\x22','none','PLAIN','util','charAt','forEach','outbound','request-promise-native','recordingURL','slice','date_start','created_by','Account\x20%d\x20is\x20not\x20valid','JSON','Enduser\x20created.','Calls','trigger:browser:url','uri','POST','getString','error','emit','catch','duration_hours','Agent\x20not\x20available,\x20using\x20the\x20admin\x20account!','resolve','monitor_format','The\x20Enduser\x20ID\x20is:','info','calleridnum','entry_list','sessionID','calleridname','format','<unknown>','destcalleridnum','parent_id','lodash','The\x20User\x20ID\x20is:','isNil','remoteUri','unmanaged','Getting\x20agent\x20with\x20name','Employees','serverUrl','Contacts','lastevent','stripTrailingSlash','replace','variableName','%s/service/v4_1/rest.php','Fields','Outbound','Enduser\x20not\x20found,\x20creating\x20a\x20new\x20enduser!','then','phone_home','sugarcrm','Agent\x20found!','find','parent_name','toISOString','assigned_user_id','value','[QUEUE]','password','length','Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!','Descriptions','stringify','get_server_info','[OUTBOUND]','parent_type','set_entry','user_name','Caller\x20-\x20%s','set_relationship','Subjects','Inbound','queue','%s/#bwc/index.php?module=Calls&action=EditView&record=%s'];(function(_0x352364,_0x2c34df){const _0x531785=function(_0xfdedba){while(--_0xfdedba){_0x352364['push'](_0x352364['shift']());}};_0x531785(++_0x2c34df);}(a1143_0x2c34,0x195));const a1143_0x5317=function(_0x352364,_0x2c34df){_0x352364=_0x352364-0x0;let _0x531785=a1143_0x2c34[_0x352364];return _0x531785;};const _0x1dee1f=a1143_0x5317;'use strict';const _=require(_0x1dee1f('0x48')),rp=require(_0x1dee1f('0x2a')),md5=require('md5'),util=require(_0x1dee1f('0x26')),intUtil=require('./util'),logger=require('../../../../config/logger')(_0x1dee1f('0x5b')),{getSocket}=require('../../../../rediscache'),io=getSocket(),def={'method':_0x1dee1f('0x35'),'json':!![]};function emit(_0xfdedba,_0x2f894f,_0x1cfae7){const _0x30a629=_0x1dee1f;io['to'](_0xfdedba)[_0x30a629('0x38')](_0x2f894f,_0x1cfae7);}function getTicket(_0x4ebd05,_0xd14066,_0x2a0eee,_0x1ce6f6,_0x45511a,_0x3a5912,_0x3fbb33,_0x46a9dc){const _0x3d4436=_0x1dee1f;logger[_0x3d4436('0x3f')](_0x3d4436('0x20'));const _0xcc9ab9=new Date(),_0x80c6c1=[{'name':_0x3d4436('0xf'),'value':intUtil[_0x3d4436('0x36')](_0x1ce6f6[_0x3d4436('0x7')],_0x45511a,'\x20')},{'name':_0x3d4436('0x60'),'value':_0xd14066['id']},{'name':_0x3d4436('0x2e'),'value':_0xd14066['id']},{'name':'description','value':intUtil[_0x3d4436('0x36')](_0x1ce6f6[_0x3d4436('0x66')],_0x45511a,'\x0a')},{'name':_0x3d4436('0x2d'),'value':_0xcc9ab9[_0x3d4436('0x5f')]()[_0x3d4436('0x2c')](0x0,0x13)[_0x3d4436('0x53')]('T','\x20')},{'name':_0x3d4436('0x3a'),'value':'0'},{'name':'duration_minutes','value':'15'},{'name':_0x3d4436('0x47'),'value':_0x2a0eee['id']},{'name':_0x3d4436('0x2'),'value':_0x3d4436('0x50')},{'name':_0x3d4436('0x5e'),'value':_0x2a0eee[_0x3d4436('0xf')]},{'name':_0x3d4436('0xe'),'value':_0x46a9dc}];return _0x3a5912[_0x3d4436('0x64')]&&_[_0x3d4436('0x28')](_0x3a5912,function(_0x2e06a7){const _0x231140=_0x3d4436;_0x45511a[_0x2e06a7[_0x231140('0x54')]]&&_0x80c6c1['push']({'name':_0x2e06a7[_0x231140('0x12')],'value':_0x45511a[_0x2e06a7[_0x231140('0x54')]]});}),request({'form':{'method':'set_entry','input_type':_0x3d4436('0x30'),'response_type':'JSON','rest_data':JSON[_0x3d4436('0x67')]({'session':_0x3fbb33['id'],'module_name':_0x3d4436('0x32'),'name_value_list':_0x80c6c1})}});}function getServerInfo(_0x596e66){const _0x482ba5=_0x1dee1f;return request({'form':{'method':_0x482ba5('0x0'),'input_type':_0x482ba5('0x30'),'response_type':_0x482ba5('0x30'),'rest_data':JSON[_0x482ba5('0x67')]({'session':_0x596e66['id']})}});}function getUser(_0x40e073,_0x4b11d0,_0x4b6871){const _0x4639f9=_0x1dee1f;return!_[_0x4639f9('0x4a')](_0x4b11d0)?(logger[_0x4639f9('0x3f')](_0x4639f9('0x4d'),_0x4b11d0),request({'form':{'method':_0x4639f9('0x13'),'input_type':_0x4639f9('0x30'),'response_type':_0x4639f9('0x30'),'rest_data':JSON[_0x4639f9('0x67')]({'session':_0x4b6871['id'],'module_name':_0x4639f9('0x4e'),'query':util['format']('INSTR\x20(\x20users.user_name,\x20\x22%s\x22)\x20>\x200',_0x4b11d0),'order_by':'','offset':'0','select_fields':['id',_0x4639f9('0xf'),_0x4639f9('0x4')],'link_name_to_fields_array':[],'max_results':-0x1,'deleted':'0'})}})[_0x4639f9('0x59')](function(_0x55f49f){const _0x358b88=_0x4639f9;if(_0x55f49f[_0x358b88('0x41')]['length']){const _0x42923b=_['find'](_0x55f49f[_0x358b88('0x41')],function(_0x5c4271){const _0xb33736=_0x358b88;return _0x5c4271[_0xb33736('0x11')][_0xb33736('0x4')][_0xb33736('0x61')]===_0x4b11d0;});if(_0x42923b)return logger['info'](_0x358b88('0x5c')),Promise[_0x358b88('0x3c')]({'id':_0x42923b['name_value_list']['id'][_0x358b88('0x61')],'name':_0x42923b[_0x358b88('0x11')]['name'][_0x358b88('0x61')]});}return logger[_0x358b88('0x3f')](_0x358b88('0x65')),Promise[_0x358b88('0x3c')]({'id':_0x4b6871[_0x358b88('0x11')][_0x358b88('0x22')]['value'],'name':_0x4b6871[_0x358b88('0x11')][_0x358b88('0x4')][_0x358b88('0x61')]});})):(logger[_0x4639f9('0x3f')](_0x4639f9('0x3b')),Promise[_0x4639f9('0x3c')]({'id':_0x4b6871['name_value_list'][_0x4639f9('0x22')][_0x4639f9('0x61')],'name':_0x4b6871[_0x4639f9('0x11')][_0x4639f9('0x4')][_0x4639f9('0x61')]}));}function getEndUser(_0x4955c3,_0x2a82f1,_0x4afde0,_0x325214){const _0x3752f1=_0x1dee1f;return logger[_0x3752f1('0x3f')]('Searching\x20enduser\x20with\x20phone',_0x4afde0),request({'form':{'method':_0x3752f1('0x13'),'input_type':'JSON','response_type':'JSON','rest_data':JSON[_0x3752f1('0x67')]({'session':_0x325214['id'],'module_name':'Contacts','query':util[_0x3752f1('0x44')](_0x3752f1('0x23'),_0x4afde0),'order_by':'','offset':'0','select_fields':['id',_0x3752f1('0xf'),_0x3752f1('0x5a')],'link_name_to_fields_array':[],'max_results':-0x1,'deleted':'0'})}})[_0x3752f1('0x59')](function(_0x1e64a8){const _0x40202d=_0x3752f1;if(_0x1e64a8[_0x40202d('0x41')][_0x40202d('0x64')]){const _0x42f747=_[_0x40202d('0x5d')](_0x1e64a8['entry_list'],function(_0x5205cd){const _0xca27d0=_0x40202d;return _0x5205cd[_0xca27d0('0x11')]['phone_home'][_0xca27d0('0x61')]===_0x4afde0;});if(_0x42f747)return logger[_0x40202d('0x3f')]('Enduser\x20found!'),Promise['resolve']({'id':_0x42f747[_0x40202d('0x11')]['id'][_0x40202d('0x61')],'name':_0x42f747['name_value_list']['name']['value'],'phone':_0x42f747[_0x40202d('0x11')][_0x40202d('0x5a')]['value']});}else return logger['info'](_0x40202d('0x58')),request({'form':{'method':_0x40202d('0x3'),'input_type':_0x40202d('0x30'),'response_type':'JSON','rest_data':JSON['stringify']({'session':_0x325214['id'],'module_name':_0x40202d('0x50'),'name_value_list':[{'name':'first_name','value':_0x2a82f1!==_0x40202d('0x45')?_0x2a82f1:util[_0x40202d('0x44')](_0x40202d('0x5'),_0x4afde0)},{'name':_0x40202d('0x5a'),'value':_0x4afde0}]})}})[_0x40202d('0x59')](function(_0x286163){const _0x390628=_0x40202d;return logger[_0x390628('0x3f')](_0x390628('0x31')),Promise[_0x390628('0x3c')]({'id':_0x286163['id'],'name':_0x286163[_0x390628('0x41')][_0x390628('0x21')]['value'],'phone':_0x286163[_0x390628('0x41')][_0x390628('0x5a')][_0x390628('0x61')]});});});}function request(_0x5c056a){const _0x36756a=_0x1dee1f;return logger[_0x36756a('0xd')]('[HTTP]',JSON[_0x36756a('0x67')](_0x5c056a)),rp(_[_0x36756a('0x1a')](def,_0x5c056a));}function getConnection(_0x57eb25){const _0x4aeb13=_0x1dee1f;return request({'method':_0x4aeb13('0x35'),'form':{'method':'login','input_type':_0x4aeb13('0x30'),'response_type':_0x4aeb13('0x30'),'rest_data':JSON[_0x4aeb13('0x67')]({'user_auth':{'user_name':_0x57eb25['username'],'password':_0x57eb25[_0x4aeb13('0x63')],'encryption':_0x4aeb13('0x25')},'application':_0x4aeb13('0x1b')})}})[_0x4aeb13('0x59')](function(_0x331235){const _0x2f6026=_0x4aeb13,_0x492ac2=_0x331235;if(_0x492ac2['id'])return def[_0x2f6026('0x42')]=_0x492ac2['id'],Promise[_0x2f6026('0x3c')](_0x492ac2);else throw new Error(util[_0x2f6026('0x44')](_0x2f6026('0x2f'),_0x57eb25['id']));});}function setRelationship(_0x374656,_0x249339,_0x3d15f1,_0x5a1475){const _0x5dcb4d=_0x1dee1f;return request({'form':{'method':_0x5dcb4d('0x6'),'input_type':_0x5dcb4d('0x30'),'response_type':'JSON','rest_data':JSON[_0x5dcb4d('0x67')]({'session':_0x374656['id'],'module_name':_0x5dcb4d('0x32'),'module_id':_0x3d15f1['id'],'link_field_name':_0x5a1475,'related_ids':[_0x249339['id']]})}});}exports[_0x1dee1f('0x9')]=function(_0x3c2c25,_0x3abb2c,_0x44b162,_0x82d5bd,_0x507337,_0x24829a){const _0x39f4e9=_0x1dee1f;let _0x199b1e,_0x1921f5,_0x2cfd82,_0x17f044;return _0x24829a[_0x39f4e9('0x4b')]=intUtil[_0x39f4e9('0x52')](_0x24829a['remoteUri']),_0x24829a['serverUrl']=intUtil[_0x39f4e9('0x52')](_0x24829a[_0x39f4e9('0x4f')]),def['uri']=util[_0x39f4e9('0x44')](_0x39f4e9('0x55'),_0x24829a['remoteUri']),_0x507337&&_0x507337[_0x39f4e9('0x3d')]?(logger[_0x39f4e9('0x3f')](_0x39f4e9('0x62'),'Recording\x20is\x20enabled!'),_0x3abb2c[_0x39f4e9('0x2b')]=util[_0x39f4e9('0x44')](_0x39f4e9('0x16'),_0x24829a['serverUrl'],_0x3abb2c['uniqueid'],md5(_0x3abb2c[_0x39f4e9('0x1e')]))):_0x3abb2c[_0x39f4e9('0x2b')]='',getConnection(_0x24829a)[_0x39f4e9('0x59')](function(_0x25b196){const _0x3369ec=_0x39f4e9;return _0x17f044=_0x25b196,getUser(_0x24829a,_0x82d5bd[_0x3369ec('0xf')],_0x17f044);})['then'](function(_0x3f906){const _0x35e595=_0x39f4e9;return _0x199b1e=_0x3f906,logger['info'](_0x35e595('0x49'),_0x199b1e['id']),getEndUser(_0x24829a,_0x3abb2c['calleridname'],_0x3abb2c[_0x35e595('0x40')],_0x17f044);})['then'](function(_0x371996){const _0x16fb2a=_0x39f4e9;return _0x1921f5=_0x371996,logger[_0x16fb2a('0x3f')](_0x16fb2a('0x3e'),_0x1921f5['id']),getTicket(_0x24829a,_0x199b1e,_0x1921f5,_0x44b162,_0x3abb2c,_0x44b162[_0x16fb2a('0x56')],_0x17f044,_0x16fb2a('0x8'));})['then'](function(_0xc03520){const _0x37eb37=_0x39f4e9;return _0x2cfd82=_0xc03520,logger[_0x37eb37('0x3f')](_0x37eb37('0x18')),logger[_0x37eb37('0x3f')](_0x37eb37('0xb'),_0x2cfd82['id']),setRelationship(_0x17f044,_0x199b1e,_0x2cfd82,_0x37eb37('0x14'));})['then'](function(){return setRelationship(_0x17f044,_0x1921f5,_0x2cfd82,'contacts');})['then'](function(){return getServerInfo(_0x17f044);})[_0x39f4e9('0x59')](function(_0x5e9246){const _0x5bc487=_0x39f4e9,_0x5579f6=parseInt(_0x5e9246[_0x5bc487('0x19')][_0x5bc487('0x27')](0x0));let _0x23c3bc=util[_0x5bc487('0x44')](_0x5bc487('0x1f'),_0x24829a[_0x5bc487('0x4b')],_0x2cfd82['id']);_0x5579f6>0x6&&(_0x23c3bc=util[_0x5bc487('0x44')](_0x5bc487('0xa'),_0x24829a['remoteUri'],_0x2cfd82['id'])),logger[_0x5bc487('0x3f')](_0x5bc487('0x62'),_0x5bc487('0x10')),emit(util[_0x5bc487('0x44')]('user:%s',_0x82d5bd['name']),_0x5bc487('0x33'),{'uri':_0x23c3bc});})[_0x39f4e9('0x39')](function(_0x5c82e5){const _0x3f4614=_0x39f4e9;logger['error'](_0x3f4614('0x62'),JSON['stringify'](_0x5c82e5));});},exports[_0x1dee1f('0x29')]=function(_0x5649aa,_0x1d39f8,_0x1135ff,_0x365a4b,_0x89ca77,_0x100f59,_0x113ba2){const _0x3446a8=_0x1dee1f;let _0xedb1a9,_0x3e0252,_0x58ed6d,_0x1e22ce;return _0x100f59[_0x3446a8('0x4b')]=intUtil[_0x3446a8('0x52')](_0x100f59[_0x3446a8('0x4b')]),_0x100f59[_0x3446a8('0x4f')]=intUtil[_0x3446a8('0x52')](_0x100f59['serverUrl']),def['uri']=util['format']('%s/service/v4_1/rest.php',_0x100f59['remoteUri']),_0x113ba2&&_0x113ba2[_0x3446a8('0x1c')]&&_0x113ba2[_0x3446a8('0x1c')]!==_0x3446a8('0x24')&&(logger['info']('[OUTBOUND]','Recording\x20is\x20enabled!'),_0x1d39f8['recordingURL']=util[_0x3446a8('0x44')](_0x3446a8('0x16'),_0x100f59[_0x3446a8('0x4f')],_0x1d39f8[_0x3446a8('0x1e')],md5(_0x1d39f8[_0x3446a8('0x1e')]))),getConnection(_0x100f59)[_0x3446a8('0x59')](function(_0x1aa622){const _0x111c3a=_0x3446a8;return _0x1e22ce=_0x1aa622,getUser(_0x100f59,_0x365a4b[_0x111c3a('0xf')],_0x1e22ce);})[_0x3446a8('0x59')](function(_0x59de1d){const _0x1075c5=_0x3446a8;return _0xedb1a9=_0x59de1d,logger[_0x1075c5('0x3f')]('The\x20User\x20ID\x20is:',_0xedb1a9['id']),getEndUser(_0x100f59,_0x1d39f8[_0x1075c5('0x15')],_0x1d39f8[_0x1075c5('0x46')],_0x1e22ce);})[_0x3446a8('0x59')](function(_0x12592c){const _0x5a8ecf=_0x3446a8;return _0x3e0252=_0x12592c,logger['info']('The\x20Enduser\x20ID\x20is:',_0x3e0252['id']),getTicket(_0x100f59,_0xedb1a9,_0x3e0252,_0x1135ff,_0x1d39f8,_0x1135ff[_0x5a8ecf('0x56')],_0x1e22ce,_0x5a8ecf('0x57'));})['then'](function(_0x49fb69){const _0x334e73=_0x3446a8;return _0x58ed6d=_0x49fb69,logger['info']('Ticket\x20created!'),logger['info'](_0x334e73('0xb'),_0x58ed6d['id']),setRelationship(_0x1e22ce,_0xedb1a9,_0x58ed6d,'users');})['then'](function(){const _0xc7e85=_0x3446a8;return setRelationship(_0x1e22ce,_0x3e0252,_0x58ed6d,_0xc7e85('0x1d'));})[_0x3446a8('0x59')](function(){return getServerInfo(_0x1e22ce);})[_0x3446a8('0x59')](function(_0x26c5fa){const _0x1cc59a=_0x3446a8,_0x51f1e0=parseInt(_0x26c5fa[_0x1cc59a('0x19')]['charAt'](0x0));let _0xc422be=util[_0x1cc59a('0x44')](_0x1cc59a('0x1f'),_0x100f59[_0x1cc59a('0x4b')],_0x58ed6d['id']);_0x51f1e0>0x6&&(_0xc422be=util[_0x1cc59a('0x44')]('%s/#bwc/index.php?module=Calls&action=EditView&record=%s',_0x100f59[_0x1cc59a('0x4b')],_0x58ed6d['id'])),logger['info'](_0x1cc59a('0x1'),'Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...'),emit(util[_0x1cc59a('0x44')]('user:%s',_0x365a4b[_0x1cc59a('0xf')]),_0x1cc59a('0x33'),{'uri':_0xc422be});})[_0x3446a8('0x39')](function(_0xb4fb73){const _0x96e82f=_0x3446a8;logger[_0x96e82f('0x37')](_0x96e82f('0x1'),JSON[_0x96e82f('0x67')](_0xb4fb73));});},exports[_0x1dee1f('0x4c')]=function(_0x30fb59,_0x5b6ea2,_0x1560cb,_0x448aee,_0x273c29,_0x37fcfd){const _0x3fc6fe=_0x1dee1f;let _0x3cb1c4,_0x58d6c5,_0xe010da,_0xbf3c91;return _0x37fcfd[_0x3fc6fe('0x4b')]=intUtil[_0x3fc6fe('0x52')](_0x37fcfd['remoteUri']),_0x37fcfd['serverUrl']=intUtil[_0x3fc6fe('0x52')](_0x37fcfd['serverUrl']),def[_0x3fc6fe('0x34')]=util[_0x3fc6fe('0x44')](_0x3fc6fe('0x55'),_0x37fcfd[_0x3fc6fe('0x4b')]),_0x273c29&&_0x273c29[_0x3fc6fe('0x3d')]?(logger[_0x3fc6fe('0x3f')]('['+_0x5b6ea2[_0x3fc6fe('0x51')][_0x3fc6fe('0xc')]()+']',_0x3fc6fe('0x17')),_0x5b6ea2['recordingURL']=util[_0x3fc6fe('0x44')](_0x3fc6fe('0x16'),_0x37fcfd[_0x3fc6fe('0x4f')],_0x5b6ea2[_0x3fc6fe('0x1e')],md5(_0x5b6ea2[_0x3fc6fe('0x1e')]))):_0x5b6ea2['recordingURL']='',getConnection(_0x37fcfd)[_0x3fc6fe('0x59')](function(_0x1f6e94){const _0x3a8040=_0x3fc6fe;return _0xbf3c91=_0x1f6e94,!_[_0x3a8040('0x4a')](_0x448aee)?getUser(_0x37fcfd,_0x448aee[_0x3a8040('0xf')],_0xbf3c91):getUser(_0x37fcfd,null,_0xbf3c91);})[_0x3fc6fe('0x59')](function(_0x28a231){const _0x349908=_0x3fc6fe;return _0x3cb1c4=_0x28a231,logger[_0x349908('0x3f')](_0x349908('0x49'),_0x3cb1c4['id']),getEndUser(_0x37fcfd,_0x5b6ea2[_0x349908('0x43')],_0x5b6ea2[_0x349908('0x40')],_0xbf3c91);})['then'](function(_0x2ef486){const _0x569570=_0x3fc6fe;return _0x58d6c5=_0x2ef486,logger['info'](_0x569570('0x3e'),_0x58d6c5['id']),getTicket(_0x37fcfd,_0x3cb1c4,_0x58d6c5,_0x1560cb,_0x5b6ea2,_0x1560cb[_0x569570('0x56')],_0xbf3c91,_0x569570('0x8'));})[_0x3fc6fe('0x59')](function(_0x3352e5){const _0x4cfe25=_0x3fc6fe;return _0xe010da=_0x3352e5,logger['info'](_0x4cfe25('0x18')),logger[_0x4cfe25('0x3f')](_0x4cfe25('0xb'),_0xe010da['id']),setRelationship(_0xbf3c91,_0x3cb1c4,_0xe010da,_0x4cfe25('0x14'));})['then'](function(){return setRelationship(_0xbf3c91,_0x58d6c5,_0xe010da,'contacts');})[_0x3fc6fe('0x59')](function(){return getServerInfo(_0xbf3c91);})['catch'](function(_0xb9dbe5){const _0x54084e=_0x3fc6fe;logger[_0x54084e('0x37')]('['+_0x5b6ea2[_0x54084e('0x51')][_0x54084e('0xc')]()+']',JSON[_0x54084e('0x67')](_0xb9dbe5));});};