Built motion from commit (unavailable).|2.2.0
[motion2.git] / server / services / ami / trigger / integration / sugarcrm.js
1 // *************************************************************************
2 // *                                                                       *
3 // * xCALLY Motion -  The Omnichannel Contact Center                       *
4 // * Copyright (c) Xenialab s.r.l. All Rights Reserved                     *
5 // *                                                                       *
6 // *************************************************************************
7 // *                                                                       *
8 // * Email: info@xcally.com                                                *
9 // * Website: https://www.xcally.com                                       *
10 // *                                                                       *
11 // *************************************************************************
12 // *                                                                       *
13 // * The SOFTWARE PRODUCT is protected by copyright laws and international *
14 // * copyright treaties, as well as other intellectual property laws and   *
15 // * treaties. The SOFTWARE PRODUCT is licensed, not sold.                         *
16 // *                                                                       *
17 // *************************************************************************
18 var _0x323f=['monitor_format','[QUEUE]','Recording\x20is\x20enabled!','%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s','uniqueid','The\x20User\x20ID\x20is:','calleridname','calleridnum','Fields','Ticket\x20created!','The\x20Ticket\x20ID\x20is:','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','error','outbound','recordingFormat','none','recordingURL','destcalleridname','destcalleridnum','The\x20Enduser\x20ID\x20is:','users','[OUTBOUND]','[ABANDONED]','request-promise','md5','util','ioredis','./util','../../../../config/environment','../../../../config/logger','sugarcrm','redis','defaults','socket.io-emitter','POST','emit','info','Creating\x20new\x20ticket...','name','getString','Subjects','created_by','description','Descriptions','date_start','toISOString','duration_hours','duration_minutes','parent_id','parent_type','Contacts','length','forEach','push','idField','variableName','set_entry','JSON','stringify','format','Basic\x20%s','apiKey','base64','get_server_info','Getting\x20agent\x20with\x20name','get_entry_list','Employees','INSTR\x20(\x20users.user_name,\x20\x22%s\x22)\x20>\x200','user_name','entry_list','name_value_list','value','Agent\x20found!','resolve','Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!','user_id','Searching\x20enduser\x20with\x20phone','then','find','phone_home','first_name','<unknown>','Caller\x20-\x20%s','Enduser\x20created.','debug','[HTTP]','map','type','nameField','toLowerCase','string','customField','custom_fields','content','toString','variable','Variable','login','username','password','PLAIN','xCALLY\x20Motion\x20integration','set_relationship','Calls','remoteUri','stripTrailingSlash','serverUrl','uri','%s/service/v4_1/rest.php'];(function(_0x45783c,_0x49c56d){var _0x1161da=function(_0x3c5d41){while(--_0x3c5d41){_0x45783c['push'](_0x45783c['shift']());}};_0x1161da(++_0x49c56d);}(_0x323f,0x1ee));var _0xf323=function(_0x5b644e,_0x2e5e07){_0x5b644e=_0x5b644e-0x0;var _0x49c6bf=_0x323f[_0x5b644e];return _0x49c6bf;};'use strict';var _=require('lodash');var rp=require(_0xf323('0x0'));var BPromise=require('bluebird');var md5=require(_0xf323('0x1'));var util=require(_0xf323('0x2'));var Redis=require(_0xf323('0x3'));var intUtil=require(_0xf323('0x4'));var config=require(_0xf323('0x5'));var logger=require(_0xf323('0x6'))(_0xf323('0x7'));config[_0xf323('0x8')]=_[_0xf323('0x9')](config[_0xf323('0x8')],{'host':'localhost','port':0x18eb});var io=require(_0xf323('0xa'))(new Redis(config[_0xf323('0x8')]));var def={'method':_0xf323('0xb'),'json':!![]};function emit(_0x2c8271,_0x8abeac,_0x3c416a){io['to'](_0x2c8271)[_0xf323('0xc')](_0x8abeac,_0x3c416a);}function getTicket(_0x2b24a2,_0x10d1ff,_0x21e82e,_0x22fd48,_0x4196b5,_0x8825ef,_0x20c013){logger[_0xf323('0xd')](_0xf323('0xe'));var _0x11a363=new Date();var _0x4e4ec3=[{'name':_0xf323('0xf'),'value':intUtil[_0xf323('0x10')](_0x22fd48[_0xf323('0x11')],_0x4196b5,'\x20')},{'name':'assigned_user_id','value':_0x10d1ff['id']},{'name':_0xf323('0x12'),'value':_0x10d1ff['id']},{'name':_0xf323('0x13'),'value':intUtil[_0xf323('0x10')](_0x22fd48[_0xf323('0x14')],_0x4196b5,'\x0a')},{'name':_0xf323('0x15'),'value':_0x11a363[_0xf323('0x16')]()['slice'](0x0,0x13)['replace']('T','\x20')},{'name':_0xf323('0x17'),'value':'0'},{'name':_0xf323('0x18'),'value':'15'},{'name':_0xf323('0x19'),'value':_0x21e82e['id']},{'name':_0xf323('0x1a'),'value':_0xf323('0x1b')},{'name':'parent_name','value':_0x21e82e[_0xf323('0xf')]}];if(_0x8825ef[_0xf323('0x1c')]){_[_0xf323('0x1d')](_0x8825ef,function(_0x44671c){if(_0x4196b5[_0x44671c['variableName']]){_0x4e4ec3[_0xf323('0x1e')]({'name':_0x44671c[_0xf323('0x1f')],'value':_0x4196b5[_0x44671c[_0xf323('0x20')]]});}});}return request({'form':{'method':_0xf323('0x21'),'input_type':'JSON','response_type':_0xf323('0x22'),'rest_data':JSON[_0xf323('0x23')]({'session':_0x20c013['id'],'module_name':'Calls','name_value_list':_0x4e4ec3})}});}function getAuth(_0x32d7b0){return util[_0xf323('0x24')](_0xf323('0x25'),new Buffer(_0x32d7b0[_0xf323('0x26')]+':X')['toString'](_0xf323('0x27')));}function getServerInfo(_0x1831bd){return request({'form':{'method':_0xf323('0x28'),'input_type':'JSON','response_type':_0xf323('0x22'),'rest_data':JSON[_0xf323('0x23')]({'session':_0x1831bd['id']})}});}function getUser(_0x3e1c49,_0x3450d6,_0x13ec1f){logger[_0xf323('0xd')](_0xf323('0x29'),_0x3450d6);return request({'form':{'method':_0xf323('0x2a'),'input_type':_0xf323('0x22'),'response_type':'JSON','rest_data':JSON[_0xf323('0x23')]({'session':_0x13ec1f['id'],'module_name':_0xf323('0x2b'),'query':util[_0xf323('0x24')](_0xf323('0x2c'),_0x3450d6),'order_by':'','offset':'0','select_fields':['id',_0xf323('0xf'),_0xf323('0x2d')],'link_name_to_fields_array':[],'max_results':-0x1,'deleted':'0'})}})['then'](function(_0x30ed93){if(_0x30ed93['entry_list'][_0xf323('0x1c')]){var _0x3902eb=_['find'](_0x30ed93[_0xf323('0x2e')],function(_0x3c95ce){return _0x3c95ce[_0xf323('0x2f')][_0xf323('0x2d')][_0xf323('0x30')]===_0x3450d6;});if(_0x3902eb){logger['info'](_0xf323('0x31'));return BPromise[_0xf323('0x32')]({'id':_0x3902eb[_0xf323('0x2f')]['id'][_0xf323('0x30')],'name':_0x3902eb[_0xf323('0x2f')][_0xf323('0xf')][_0xf323('0x30')]});}}else{logger[_0xf323('0xd')](_0xf323('0x33'));return BPromise['resolve']({'id':_0x13ec1f[_0xf323('0x2f')][_0xf323('0x34')]['id'],'name':_0x13ec1f[_0xf323('0x2f')][_0xf323('0x2d')]['id']});}});}function getEndUser(_0x3beeb1,_0x5cd5c0,_0x9bd304,_0x41bc31){logger[_0xf323('0xd')](_0xf323('0x35'),_0x9bd304);return request({'form':{'method':_0xf323('0x2a'),'input_type':'JSON','response_type':_0xf323('0x22'),'rest_data':JSON[_0xf323('0x23')]({'session':_0x41bc31['id'],'module_name':_0xf323('0x1b'),'query':util[_0xf323('0x24')]('contacts.phone_home\x20=\x20\x22%s\x22',_0x9bd304),'order_by':'','offset':'0','select_fields':['id',_0xf323('0xf'),'phone_home'],'link_name_to_fields_array':[],'max_results':-0x1,'deleted':'0'})}})[_0xf323('0x36')](function(_0x14f9fd){if(_0x14f9fd['entry_list'][_0xf323('0x1c')]){var _0x47ba23=_[_0xf323('0x37')](_0x14f9fd['entry_list'],function(_0x323841){return _0x323841['name_value_list'][_0xf323('0x38')][_0xf323('0x30')]===_0x9bd304;});if(_0x47ba23){logger['info']('Enduser\x20found!');return BPromise['resolve']({'id':_0x47ba23[_0xf323('0x2f')]['id'][_0xf323('0x30')],'name':_0x47ba23[_0xf323('0x2f')][_0xf323('0xf')][_0xf323('0x30')],'phone':_0x47ba23['name_value_list']['phone_home']['value']});}}else{logger[_0xf323('0xd')]('Enduser\x20not\x20found,\x20creating\x20a\x20new\x20enduser!');return request({'form':{'method':_0xf323('0x21'),'input_type':'JSON','response_type':_0xf323('0x22'),'rest_data':JSON[_0xf323('0x23')]({'session':_0x41bc31['id'],'module_name':_0xf323('0x1b'),'name_value_list':[{'name':_0xf323('0x39'),'value':_0x5cd5c0!==_0xf323('0x3a')?_0x5cd5c0:util['format'](_0xf323('0x3b'),_0x9bd304)},{'name':_0xf323('0x38'),'value':_0x9bd304}]})}})[_0xf323('0x36')](function(_0x27ddcc){logger[_0xf323('0xd')](_0xf323('0x3c'));return BPromise[_0xf323('0x32')]({'id':_0x27ddcc['id'],'name':_0x27ddcc[_0xf323('0x2e')][_0xf323('0x39')][_0xf323('0x30')],'phone':_0x27ddcc[_0xf323('0x2e')][_0xf323('0x38')][_0xf323('0x30')]});});}});}function request(_0x388d80){logger[_0xf323('0x3d')](_0xf323('0x3e'),JSON['stringify'](_0x388d80));return rp(_['merge'](def,_0x388d80));}function getTags(_0x1818c4,_0x16ac6d){var _0x2864f7=[];if(_0x1818c4&&_0x1818c4[_0xf323('0x1c')]>0x0){_0x2864f7=_[_0xf323('0x3f')](_0x1818c4,_0x16ac6d);}return _0x2864f7;}function getCustomFields(_0xcdbb9a,_0x2746e3){var _0x125047=[];var _0x1c0136={'custom_fields':[]};if(_0xcdbb9a[_0xf323('0x1c')]>0x0){_[_0xf323('0x1d')](_0xcdbb9a,function(_0x247149){if(_0x247149[_0xf323('0x40')]==='string'&&_0x247149[_0xf323('0x41')]&&!_0x247149['customField']){_0x1c0136[_0x247149['nameField'][_0xf323('0x42')]()]=_0x247149['content']['toString']();}else if(_0x247149[_0xf323('0x40')]===_0xf323('0x43')&&_0x247149[_0xf323('0x1f')]&&_0x247149[_0xf323('0x44')]){_0x1c0136[_0xf323('0x45')][_0xf323('0x1e')]({'id':_0x247149[_0xf323('0x1f')],'value':_0x247149[_0xf323('0x46')][_0xf323('0x47')]()});}else if(_0x247149[_0xf323('0x40')]===_0xf323('0x48')&&_0x247149['variableName']&&_0x247149[_0xf323('0x1f')]&&_0x247149['customField']){_0x2746e3[_0x247149[_0xf323('0x20')]]&&_0x1c0136[_0xf323('0x45')][_0xf323('0x1e')]({'id':_0x247149[_0xf323('0x1f')],'value':_0x2746e3[_0x247149['variableName']]['toString']()});}else if(_0x247149[_0xf323('0x40')]==='customVariable'&&_0x247149[_0xf323('0x49')]&&_0x247149['Variable'][_0xf323('0xf')]&&_0x247149[_0xf323('0x1f')]&&_0x247149['customField']){_0x2746e3[_0x247149[_0xf323('0x49')][_0xf323('0xf')][_0xf323('0x42')]()]&&_0x1c0136[_0xf323('0x45')][_0xf323('0x1e')]({'id':_0x247149[_0xf323('0x1f')],'value':_0x2746e3[_0x247149[_0xf323('0x49')][_0xf323('0xf')][_0xf323('0x42')]()][_0xf323('0x47')]()});}});}return _0x1c0136;}function getConnection(_0x417801){return request({'method':_0xf323('0xb'),'form':{'method':_0xf323('0x4a'),'input_type':'JSON','response_type':_0xf323('0x22'),'rest_data':JSON[_0xf323('0x23')]({'user_auth':{'user_name':_0x417801[_0xf323('0x4b')],'password':_0x417801[_0xf323('0x4c')],'encryption':_0xf323('0x4d')},'application':_0xf323('0x4e')})}})['then'](function(_0x46006f){var _0x7eaba4=_0x46006f;if(_0x7eaba4['id']){def['sessionID']=_0x7eaba4['id'];return BPromise[_0xf323('0x32')](_0x7eaba4);}else{throw new Error(util[_0xf323('0x24')]('Account\x20%d\x20is\x20not\x20valid',_0x417801['id']));}});}function setRelationship(_0x55754e,_0x46d8fc,_0x3178e0,_0x227d1b){return request({'form':{'method':_0xf323('0x4f'),'input_type':_0xf323('0x22'),'response_type':_0xf323('0x22'),'rest_data':JSON[_0xf323('0x23')]({'session':_0x55754e['id'],'module_name':_0xf323('0x50'),'module_id':_0x3178e0['id'],'link_field_name':_0x227d1b,'related_ids':[_0x46d8fc['id']]})}});}exports['queue']=function(_0x342a0d,_0x198a2b,_0x36b04e,_0x3b82e5,_0x67f66c,_0x146b7e){var _0x35f0c1,_0x55943f,_0x2cc056,_0x4499f8;_0x146b7e[_0xf323('0x51')]=intUtil[_0xf323('0x52')](_0x146b7e[_0xf323('0x51')]);_0x146b7e[_0xf323('0x53')]=intUtil[_0xf323('0x52')](_0x146b7e[_0xf323('0x53')]);def[_0xf323('0x54')]=util['format'](_0xf323('0x55'),_0x146b7e[_0xf323('0x51')]);if(_0x67f66c&&_0x67f66c[_0xf323('0x56')]){logger['info'](_0xf323('0x57'),_0xf323('0x58'));_0x198a2b['recordingURL']=util[_0xf323('0x24')](_0xf323('0x59'),_0x146b7e['serverUrl'],_0x198a2b[_0xf323('0x5a')],md5(_0x198a2b[_0xf323('0x5a')]));}else{_0x198a2b['recordingURL']='';}return getConnection(_0x146b7e)[_0xf323('0x36')](function(_0x557a86){_0x4499f8=_0x557a86;return getUser(_0x146b7e,_0x3b82e5[_0xf323('0xf')],_0x4499f8);})[_0xf323('0x36')](function(_0x129a44){_0x35f0c1=_0x129a44;logger[_0xf323('0xd')](_0xf323('0x5b'),_0x35f0c1['id']);return getEndUser(_0x146b7e,_0x198a2b[_0xf323('0x5c')],_0x198a2b[_0xf323('0x5d')],_0x4499f8);})['then'](function(_0xd1902b){_0x55943f=_0xd1902b;logger[_0xf323('0xd')]('The\x20Enduser\x20ID\x20is:',_0x55943f['id']);return getTicket(_0x146b7e,_0x35f0c1,_0x55943f,_0x36b04e,_0x198a2b,_0x36b04e[_0xf323('0x5e')],_0x4499f8);})[_0xf323('0x36')](function(_0x5ad5e7){_0x2cc056=_0x5ad5e7;logger[_0xf323('0xd')](_0xf323('0x5f'));logger[_0xf323('0xd')](_0xf323('0x60'),_0x2cc056['id']);return setRelationship(_0x4499f8,_0x35f0c1,_0x2cc056,'users');})[_0xf323('0x36')](function(){return setRelationship(_0x4499f8,_0x55943f,_0x2cc056,_0xf323('0x61'));})[_0xf323('0x36')](function(){return getServerInfo(_0x4499f8);})[_0xf323('0x36')](function(_0x4f7162){var _0x219327=parseInt(_0x4f7162[_0xf323('0x62')][_0xf323('0x63')](0x0));var _0x3aa631=util[_0xf323('0x24')](_0xf323('0x64'),_0x146b7e[_0xf323('0x51')],_0x2cc056['id']);if(_0x219327>0x6){_0x3aa631=util[_0xf323('0x24')](_0xf323('0x65'),_0x146b7e[_0xf323('0x51')],_0x2cc056['id']);}logger[_0xf323('0xd')](_0xf323('0x57'),_0xf323('0x66'));emit(util[_0xf323('0x24')](_0xf323('0x67'),_0x3b82e5['name']),'trigger:browser:url',{'uri':_0x3aa631});})[_0xf323('0x68')](function(_0x3f49d1){logger[_0xf323('0x69')](_0xf323('0x57'),JSON[_0xf323('0x23')](_0x3f49d1));});};exports[_0xf323('0x6a')]=function(_0x550a6a,_0x1c606b,_0x642320,_0x3c103e,_0x3add7c,_0x5544fa,_0x5e0382){var _0x123ac3,_0x2e7f68,_0x456e25,_0x3feccc;_0x5544fa[_0xf323('0x51')]=intUtil[_0xf323('0x52')](_0x5544fa[_0xf323('0x51')]);_0x5544fa['serverUrl']=intUtil['stripTrailingSlash'](_0x5544fa[_0xf323('0x53')]);def[_0xf323('0x54')]=util[_0xf323('0x24')](_0xf323('0x55'),_0x5544fa[_0xf323('0x51')]);if(_0x5e0382&&_0x5e0382[_0xf323('0x6b')]&&_0x5e0382['recordingFormat']!==_0xf323('0x6c')){logger[_0xf323('0xd')]('[OUTBOUND]','Recording\x20is\x20enabled!');_0x1c606b[_0xf323('0x6d')]=util[_0xf323('0x24')]('%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s',_0x5544fa[_0xf323('0x53')],_0x1c606b[_0xf323('0x5a')],md5(_0x1c606b[_0xf323('0x5a')]));}return getConnection(_0x5544fa)['then'](function(_0x189f5f){_0x3feccc=_0x189f5f;return getUser(_0x5544fa,_0x3c103e['name'],_0x3feccc);})['then'](function(_0x28db0b){_0x123ac3=_0x28db0b;logger[_0xf323('0xd')](_0xf323('0x5b'),_0x123ac3['id']);return getEndUser(_0x5544fa,_0x1c606b[_0xf323('0x6e')],_0x1c606b[_0xf323('0x6f')],_0x3feccc);})['then'](function(_0xe4b735){_0x2e7f68=_0xe4b735;logger[_0xf323('0xd')](_0xf323('0x70'),_0x2e7f68['id']);return getTicket(_0x5544fa,_0x123ac3,_0x2e7f68,_0x642320,_0x1c606b,_0x642320[_0xf323('0x5e')],_0x3feccc);})['then'](function(_0x282629){_0x456e25=_0x282629;logger[_0xf323('0xd')](_0xf323('0x5f'));logger[_0xf323('0xd')](_0xf323('0x60'),_0x456e25['id']);return setRelationship(_0x3feccc,_0x123ac3,_0x456e25,_0xf323('0x71'));})[_0xf323('0x36')](function(){return setRelationship(_0x3feccc,_0x2e7f68,_0x456e25,_0xf323('0x61'));})[_0xf323('0x36')](function(){return getServerInfo(_0x3feccc);})['then'](function(_0x1779e2){var _0x36e7b9=parseInt(_0x1779e2[_0xf323('0x62')]['charAt'](0x0));var _0x4b1b9c=util[_0xf323('0x24')]('%s/index.php?action=ajaxui#ajaxUILoc=index.php%3Fmodule%3DCalls%26action%3DEditView%26record%3D%s',_0x5544fa[_0xf323('0x51')],_0x456e25['id']);if(_0x36e7b9>0x6){_0x4b1b9c=util[_0xf323('0x24')](_0xf323('0x65'),_0x5544fa[_0xf323('0x51')],_0x456e25['id']);}logger['info'](_0xf323('0x72'),_0xf323('0x66'));emit(util[_0xf323('0x24')](_0xf323('0x67'),_0x3c103e[_0xf323('0xf')]),'trigger:browser:url',{'uri':_0x4b1b9c});})[_0xf323('0x68')](function(_0x14b0f0){logger[_0xf323('0x69')](_0xf323('0x72'),JSON['stringify'](_0x14b0f0));});};exports['abandoned']=function(_0xc8c5d5,_0xc34f70,_0x5de786,_0x32fcbd,_0x49de05,_0x42c885){var _0x21ce80,_0x106b81,_0x517a5b,_0x2a7de0;_0x42c885[_0xf323('0x51')]=intUtil['stripTrailingSlash'](_0x42c885[_0xf323('0x51')]);_0x42c885[_0xf323('0x53')]=intUtil[_0xf323('0x52')](_0x42c885[_0xf323('0x53')]);def[_0xf323('0x54')]=util['format'](_0xf323('0x55'),_0x42c885[_0xf323('0x51')]);if(_0x49de05&&_0x49de05[_0xf323('0x56')]){logger[_0xf323('0xd')](_0xf323('0x73'),'Recording\x20is\x20enabled!');_0xc34f70[_0xf323('0x6d')]=util['format']('%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s',_0x42c885['serverUrl'],_0xc34f70[_0xf323('0x5a')],md5(_0xc34f70[_0xf323('0x5a')]));}else{_0xc34f70[_0xf323('0x6d')]='';}return getConnection(_0x42c885)['then'](function(_0x2fba27){_0x2a7de0=_0x2fba27;return getUser(_0x42c885,_0x32fcbd[_0xf323('0xf')],_0x2a7de0);})['then'](function(_0x20425f){_0x21ce80=_0x20425f;logger[_0xf323('0xd')](_0xf323('0x5b'),_0x21ce80['id']);return getEndUser(_0x42c885,_0xc34f70[_0xf323('0x5c')],_0xc34f70[_0xf323('0x5d')],_0x2a7de0);})[_0xf323('0x36')](function(_0x1ecd12){_0x106b81=_0x1ecd12;logger[_0xf323('0xd')]('The\x20Enduser\x20ID\x20is:',_0x106b81['id']);return getTicket(_0x42c885,_0x21ce80,_0x106b81,_0x5de786,_0xc34f70,_0x5de786[_0xf323('0x5e')],_0x2a7de0);})[_0xf323('0x36')](function(_0x2bc12c){_0x517a5b=_0x2bc12c;logger['info']('Ticket\x20created!');logger[_0xf323('0xd')](_0xf323('0x60'),_0x517a5b['id']);return setRelationship(_0x2a7de0,_0x21ce80,_0x517a5b,_0xf323('0x71'));})[_0xf323('0x36')](function(){return setRelationship(_0x2a7de0,_0x106b81,_0x517a5b,_0xf323('0x61'));})['then'](function(){return getServerInfo(_0x2a7de0);})['catch'](function(_0x34e953){logger[_0xf323('0x69')]('[ABANDONED]',JSON[_0xf323('0x23')](_0x34e953));});};