3272191c98fb85397aa66c3e6c62c63456e5295e
[motion2.git] / server / services / ami / trigger / integration / vtiger.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 _0x090f=['util','ioredis','../../../../config/environment','../../../../config/logger','redis','defaults','socket.io-emitter','length','forEach','string','idField','toString','type','variable','variableName','Variable','name','toLowerCase','info','Creating\x20new\x20ticket...','merge','Normal','Open','Subjects','getString','POST','%s/webservice.php','remoteUri','create','username','sessionName','HelpDesk','stringify','resolve','result','format','Ticket\x20could\x20not\x20be\x20created\x20due\x20to:\x20%s','error','catch','statusCode','[vTiger],\x20%s,\x20%s,\x20%s','createTicket','validateEmail','query','SELECT\x20first_name,\x20last_name,\x20email1\x20FROM\x20Users\x20WHERE\x20email1=\x27%s\x27;','success','Agent\x20found!','email1','first_name','Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!','userId','admin','getUser','GET','SELECT\x20id,\x20firstname,\x20lastname,\x20email,\x20phone,\x20mobile,\x20homephone,\x20otherphone,\x20assistantphone\x20FROM\x20Contacts\x20WHERE\x20phone\x20=\x20\x27%s\x27\x20OR\x20mobile\x20=\x27%s\x27\x20OR\x20homephone\x20=\x27%s\x27\x20OR\x20otherphone\x20=\x27%s\x27\x20OR\x20assistantphone\x20=\x27%s\x27;','then','Enduser\x20found!','firstname','lastname','Enduser\x20not\x20found,\x20creating\x20a\x20new\x20enduser!','Contacts','phone','Account\x20%d\x20is\x20not\x20valid','[HTTP]','stripTrailingSlash','[QUEUE]','recordingURL','%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s','serverUrl','uniqueid','calleridname','The\x20Enduser\x20ID\x20is:','The\x20Ticket\x20ID\x20is:','%s/index.php?module=HelpDesk&view=Detail&record=%s&app=SUPPORT','Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...','user:%s','trigger:browser:url','outbound','recordingFormat','none','[OUTBOUND]','Recording\x20is\x20enabled!','email','The\x20User\x20ID\x20is:','destcalleridname','destcalleridnum','Fields','Ticket\x20created!','split','monitor_format','lastevent','toUpperCase','calleridnum','lodash','request-promise','bluebird','md5'];(function(_0x4c13ba,_0x24aa66){var _0x2deeb8=function(_0x57a0e3){while(--_0x57a0e3){_0x4c13ba['push'](_0x4c13ba['shift']());}};_0x2deeb8(++_0x24aa66);}(_0x090f,0x1e1));var _0xf090=function(_0x321bb2,_0x202794){_0x321bb2=_0x321bb2-0x0;var _0x9c12ed=_0x090f[_0x321bb2];return _0x9c12ed;};'use strict';var _=require(_0xf090('0x0'));var rp=require(_0xf090('0x1'));var BPromise=require(_0xf090('0x2'));var md5=require(_0xf090('0x3'));var util=require(_0xf090('0x4'));var Redis=require(_0xf090('0x5'));var intUtil=require('./util');var config=require(_0xf090('0x6'));var logger=require(_0xf090('0x7'))('vTiger');config[_0xf090('0x8')]=_[_0xf090('0x9')](config[_0xf090('0x8')],{'host':'localhost','port':0x18eb});var io=require(_0xf090('0xa'))(new Redis(config[_0xf090('0x8')]));function emit(_0x24d794,_0x15e65a,_0x4af574){io['to'](_0x24d794)['emit'](_0x15e65a,_0x4af574);}function getCustomFields(_0x617856,_0x34e078){var _0x29fed8={};if(_0x617856[_0xf090('0xb')]>0x0){_[_0xf090('0xc')](_0x617856,function(_0x33b779){if(_0x33b779['type']===_0xf090('0xd')&&_0x33b779[_0xf090('0xe')]){_0x29fed8[_0x33b779['idField']]=_0x33b779['content'][_0xf090('0xf')]();}else if(_0x33b779[_0xf090('0x10')]===_0xf090('0x11')&&_0x33b779[_0xf090('0x12')]&&_0x33b779[_0xf090('0xe')]&&_0x34e078[_0x33b779[_0xf090('0x12')]]){_0x29fed8[_0x33b779[_0xf090('0xe')]]=_0x34e078[_0x33b779[_0xf090('0x12')]]['toString']();}else if(_0x33b779[_0xf090('0x10')]==='customVariable'&&_0x33b779[_0xf090('0x13')]&&_0x33b779[_0xf090('0x13')]['name']&&_0x33b779['idField']&&_0x34e078[_0x33b779['Variable'][_0xf090('0x14')][_0xf090('0x15')]()]){_0x29fed8[_0x33b779[_0xf090('0xe')]]=_0x34e078[_0x33b779[_0xf090('0x13')]['name']['toLowerCase']()][_0xf090('0xf')]();}});}return _0x29fed8;}function createTicket(_0x2b39d2,_0x563349,_0x4409aa,_0x55d516,_0x3473ef,_0x2819c4,_0x198bd2){logger[_0xf090('0x16')](_0xf090('0x17'));var _0x412f97=_[_0xf090('0x18')]({'parent_id':'','assigned_user_id':_0x563349['id'],'ticketpriorities':_0xf090('0x19'),'ticketstatus':_0xf090('0x1a'),'ticket_title':intUtil['getString'](_0x55d516[_0xf090('0x1b')],_0x3473ef,'\x20'),'description':intUtil[_0xf090('0x1c')](_0x55d516['Descriptions'],_0x3473ef,'\x0a'),'contact_id':_0x4409aa['id']},getCustomFields(_0x2819c4,_0x3473ef));var _0x54262a={'method':_0xf090('0x1d'),'uri':util['format'](_0xf090('0x1e'),_0x2b39d2[_0xf090('0x1f')]),'form':{'operation':_0xf090('0x20'),'username':_0x2b39d2[_0xf090('0x21')],'sessionName':_0x198bd2[_0xf090('0x22')],'elementType':_0xf090('0x23'),'element':JSON[_0xf090('0x24')](_0x412f97)},'json':!![]};return request(_0x54262a)['then'](function(_0x15dfb5){if(_0x15dfb5['success']){return BPromise[_0xf090('0x25')]({'id':_0x15dfb5[_0xf090('0x26')]['id']});}else{throw new Error(util[_0xf090('0x27')](_0xf090('0x28'),JSON[_0xf090('0x24')](_0x15dfb5[_0xf090('0x29')])));}})[_0xf090('0x2a')](function(_0x34ddeb){var _0x40cd18=_0x34ddeb[_0xf090('0x2b')]||0x1f4;logger[_0xf090('0x29')](_0xf090('0x2c'),_0xf090('0x2d'),_0x40cd18,JSON['stringify'](_0x34ddeb));});}function getUser(_0x1a5b13,_0x26c044,_0x365f75){if(intUtil[_0xf090('0x2e')](_0x26c044)){logger[_0xf090('0x16')]('Getting\x20agent\x20with\x20email',_0x26c044);return request({'method':'GET','uri':util[_0xf090('0x27')](_0xf090('0x1e'),_0x1a5b13[_0xf090('0x1f')]),'qs':{'operation':_0xf090('0x2f'),'username':_0x1a5b13[_0xf090('0x21')],'sessionName':_0x365f75[_0xf090('0x22')],'query':util[_0xf090('0x27')](_0xf090('0x30'),_0x26c044)},'json':!![]})['then'](function(_0x4540e5){if(_0x4540e5[_0xf090('0x31')]&&_0x4540e5[_0xf090('0x26')]['length']){logger[_0xf090('0x16')](_0xf090('0x32'));return BPromise['resolve']({'id':_0x4540e5[_0xf090('0x26')][0x0]['id'],'email':_0x4540e5[_0xf090('0x26')][0x0][_0xf090('0x33')],'name':_0x4540e5[_0xf090('0x26')][0x0][_0xf090('0x34')]+'\x20'+_0x4540e5[_0xf090('0x26')]['last_name']});}else{logger[_0xf090('0x16')](_0xf090('0x35'));return BPromise[_0xf090('0x25')]({'id':_0x365f75[_0xf090('0x36')],'email':_0x1a5b13[_0xf090('0x21')],'name':_0xf090('0x37')});}})[_0xf090('0x2a')](function(_0x435fb0){var _0x1d160f=_0x435fb0[_0xf090('0x2b')]||0x1f4;logger['error'](_0xf090('0x2c'),_0xf090('0x38'),_0x1d160f,JSON[_0xf090('0x24')](_0x435fb0));});}else{logger[_0xf090('0x16')](_0xf090('0x35'));return BPromise[_0xf090('0x25')]({'id':_0x365f75['userId'],'email':_0x1a5b13['username'],'name':_0xf090('0x37')})[_0xf090('0x2a')](function(_0x477599){var _0x385ca1=_0x477599[_0xf090('0x2b')]||0x1f4;logger[_0xf090('0x29')]('[vTiger],\x20%s,\x20%s,\x20%s','getUser',_0x385ca1,JSON[_0xf090('0x24')](_0x477599));});}}function getEndUser(_0x4b9582,_0x5535e5,_0x4fc0c3,_0x10a146){logger[_0xf090('0x16')]('Searching\x20end\x20user\x20with\x20phone',_0x4fc0c3);return request({'method':_0xf090('0x39'),'uri':util[_0xf090('0x27')](_0xf090('0x1e'),_0x4b9582[_0xf090('0x1f')]),'qs':{'operation':'query','username':_0x4b9582[_0xf090('0x21')],'sessionName':_0x10a146['sessionName'],'query':util['format'](_0xf090('0x3a'),_0x4fc0c3,_0x4fc0c3,_0x4fc0c3,_0x4fc0c3,_0x4fc0c3)},'json':!![]})[_0xf090('0x3b')](function(_0xe6d0d6){if(_0xe6d0d6['success']&&_0xe6d0d6[_0xf090('0x26')]['length']){logger[_0xf090('0x16')](_0xf090('0x3c'));return BPromise['resolve']({'id':_0xe6d0d6[_0xf090('0x26')][0x0]['id'],'name':_0xe6d0d6[_0xf090('0x26')][0x0][_0xf090('0x3d')]+'\x20'+_0xe6d0d6[_0xf090('0x26')][0x0][_0xf090('0x3e')],'phone':_0x4fc0c3});}else{logger[_0xf090('0x16')](_0xf090('0x3f'));return request({'method':'POST','uri':util[_0xf090('0x27')](_0xf090('0x1e'),_0x4b9582[_0xf090('0x1f')]),'form':{'operation':_0xf090('0x20'),'sessionName':_0x10a146['sessionName'],'elementType':_0xf090('0x40'),'element':JSON['stringify']({'lastname':_0x5535e5,'phone':_0x4fc0c3,'assigned_user_id':'\x20'})},'json':!![]})['then'](function(_0x2ede84){logger[_0xf090('0x16')]('Enduser\x20created.');return BPromise[_0xf090('0x25')]({'id':_0x2ede84[_0xf090('0x26')]['id'],'name':_0x2ede84[_0xf090('0x26')][_0xf090('0x3e')],'phone':_0x2ede84[_0xf090('0x26')][_0xf090('0x41')]});})[_0xf090('0x2a')](function(_0x4a11d3){var _0x4acb97=_0x4a11d3[_0xf090('0x2b')]||0x1f4;logger[_0xf090('0x29')](_0xf090('0x2c'),'getEndUser',_0x4acb97,JSON[_0xf090('0x24')](_0x4a11d3));});}});}function getConnection(_0x2e4789){var _0xfab9cf=_0x2e4789['username'];var _0x292bf7=_0x2e4789['accessKey'];return request({'method':'GET','uri':util['format']('%s/webservice.php',_0x2e4789[_0xf090('0x1f')]),'qs':{'operation':'getchallenge','username':_0xfab9cf},'json':!![]})['then'](function(_0x265763){if(_0x265763[_0xf090('0x31')]){return request({'method':_0xf090('0x1d'),'uri':util['format'](_0xf090('0x1e'),_0x2e4789['remoteUri']),'form':{'operation':'login','username':_0xfab9cf,'accessKey':md5(_0x265763[_0xf090('0x26')]['token']+_0x292bf7)},'json':!![]});}})[_0xf090('0x3b')](function(_0x48ef72){if(_0x48ef72[_0xf090('0x31')]){return BPromise[_0xf090('0x25')](_0x48ef72[_0xf090('0x26')]);}else{throw new Error(util[_0xf090('0x27')](_0xf090('0x42'),_0x2e4789['id']));}})[_0xf090('0x2a')](function(_0x4a977c){var _0xc312b2=_0x4a977c[_0xf090('0x2b')]||0x1f4;logger['error'](_0xf090('0x2c'),'getConnection',_0xc312b2,JSON[_0xf090('0x24')](_0x4a977c));});}function request(_0x344d34){logger[_0xf090('0x16')](_0xf090('0x43'),JSON['stringify'](_0x344d34));return rp(_0x344d34);}exports['queue']=function(_0xb73687,_0x54f655,_0xd45256,_0x5a60ec,_0x24e222,_0x4593d5){var _0x8b9afa,_0x57dc19,_0x14ed7b,_0x1bb8dc;_0x4593d5[_0xf090('0x1f')]=intUtil[_0xf090('0x44')](_0x4593d5['remoteUri']);if(_0x24e222&&_0x24e222['monitor_format']){logger[_0xf090('0x16')](_0xf090('0x45'),'Recording\x20is\x20enabled!');_0x54f655[_0xf090('0x46')]=util[_0xf090('0x27')](_0xf090('0x47'),_0x4593d5[_0xf090('0x48')],_0x54f655[_0xf090('0x49')],md5(_0x54f655['uniqueid']));}else{_0x54f655[_0xf090('0x46')]='';}return getConnection(_0x4593d5)['then'](function(_0x5aee0d){_0x1bb8dc=_0x5aee0d;return getUser(_0x4593d5,_0x5a60ec['email'],_0x1bb8dc);})[_0xf090('0x3b')](function(_0x54b398){_0x8b9afa=_0x54b398;logger['info']('The\x20User\x20ID\x20is:',_0x8b9afa['id']);return getEndUser(_0x4593d5,_0x54f655[_0xf090('0x4a')],_0x54f655['calleridnum'],_0x1bb8dc);})['then'](function(_0xd85e1d){_0x57dc19=_0xd85e1d;logger[_0xf090('0x16')](_0xf090('0x4b'),_0x57dc19['id']);return createTicket(_0x4593d5,_0x8b9afa,_0x57dc19,_0xd45256,_0x54f655,_0xd45256['Fields'],_0x1bb8dc);})[_0xf090('0x3b')](function(_0x3dade1){_0x14ed7b=_0x3dade1;logger['info']('Ticket\x20created!');logger[_0xf090('0x16')](_0xf090('0x4c'),_0x14ed7b['id']);var _0xd2c30d=util[_0xf090('0x27')](_0xf090('0x4d'),_0x4593d5[_0xf090('0x1f')],_['split'](_0x14ed7b['id'],'x')[0x1]);logger['info'](_0xf090('0x45'),_0xf090('0x4e'));emit(util[_0xf090('0x27')](_0xf090('0x4f'),_0x5a60ec[_0xf090('0x14')]),_0xf090('0x50'),{'uri':_0xd2c30d});})[_0xf090('0x2a')](function(_0x50cb1a){logger[_0xf090('0x29')]('[QUEUE]',JSON['stringify'](_0x50cb1a));});};exports[_0xf090('0x51')]=function(_0x23ab77,_0x2c6b7e,_0x4ea74a,_0x5229d9,_0x8016c0,_0x4ac440,_0x290f4b){var _0xd0c5b6,_0xc8496f,_0x12dd58,_0x126f0f;_0x4ac440[_0xf090('0x1f')]=intUtil['stripTrailingSlash'](_0x4ac440[_0xf090('0x1f')]);_0x4ac440['serverUrl']=intUtil[_0xf090('0x44')](_0x4ac440[_0xf090('0x48')]);if(_0x290f4b&&_0x290f4b[_0xf090('0x52')]&&_0x290f4b[_0xf090('0x52')]!==_0xf090('0x53')){logger[_0xf090('0x16')](_0xf090('0x54'),_0xf090('0x55'));_0x2c6b7e[_0xf090('0x46')]=util['format'](_0xf090('0x47'),_0x4ac440[_0xf090('0x48')],_0x2c6b7e[_0xf090('0x49')],md5(_0x2c6b7e['uniqueid']));}return getConnection(_0x4ac440)[_0xf090('0x3b')](function(_0x4a2600){_0x126f0f=_0x4a2600;return getUser(_0x4ac440,_0x5229d9[_0xf090('0x56')],_0x126f0f);})[_0xf090('0x3b')](function(_0x1f088a){_0xd0c5b6=_0x1f088a;logger[_0xf090('0x16')](_0xf090('0x57'),_0xd0c5b6['id']);return getEndUser(_0x4ac440,_0x2c6b7e[_0xf090('0x58')],_0x2c6b7e[_0xf090('0x59')],_0x126f0f);})['then'](function(_0x4414f0){_0xc8496f=_0x4414f0;logger[_0xf090('0x16')](_0xf090('0x4b'),_0xc8496f['id']);return createTicket(_0x4ac440,_0xd0c5b6,_0xc8496f,_0x4ea74a,_0x2c6b7e,_0x4ea74a[_0xf090('0x5a')],_0x126f0f);})[_0xf090('0x3b')](function(_0x14824c){_0x12dd58=_0x14824c;logger['info'](_0xf090('0x5b'));logger[_0xf090('0x16')](_0xf090('0x4c'),_0x12dd58['id']);var _0x62bd1=util[_0xf090('0x27')]('%s/index.php?module=HelpDesk&view=Detail&record=%s&app=SUPPORT',_0x4ac440['remoteUri'],_[_0xf090('0x5c')](_0x12dd58['id'],'x')[0x1]);logger[_0xf090('0x16')]('[OUTBOUND]',_0xf090('0x4e'));emit(util[_0xf090('0x27')](_0xf090('0x4f'),_0x5229d9[_0xf090('0x14')]),_0xf090('0x50'),{'uri':_0x62bd1});})[_0xf090('0x2a')](function(_0x341b89){logger['error'](_0xf090('0x54'),JSON[_0xf090('0x24')](_0x341b89));});};exports['unmanaged']=function(_0x3e61f7,_0x103a8e,_0x527cfb,_0x5826c8,_0x575a64,_0x5561ee){var _0x36370e,_0x3c0936,_0x2efba4,_0x444c12;_0x5561ee['remoteUri']=intUtil[_0xf090('0x44')](_0x5561ee['remoteUri']);_0x5561ee[_0xf090('0x48')]=intUtil[_0xf090('0x44')](_0x5561ee[_0xf090('0x48')]);if(_0x575a64&&_0x575a64[_0xf090('0x5d')]){logger['info']('['+_0x103a8e[_0xf090('0x5e')][_0xf090('0x5f')]()+']',_0xf090('0x55'));_0x103a8e[_0xf090('0x46')]=util[_0xf090('0x27')]('%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s',_0x5561ee[_0xf090('0x48')],_0x103a8e['uniqueid'],md5(_0x103a8e['uniqueid']));}else{_0x103a8e[_0xf090('0x46')]='';}return getConnection(_0x5561ee)[_0xf090('0x3b')](function(_0x50d11a){_0x444c12=_0x50d11a;return getUser(_0x5561ee,'',_0x444c12);})[_0xf090('0x3b')](function(_0x19ecac){_0x36370e=_0x19ecac;logger['info'](_0xf090('0x57'),_0x36370e['id']);return getEndUser(_0x5561ee,_0x103a8e[_0xf090('0x4a')],_0x103a8e[_0xf090('0x60')],_0x444c12);})[_0xf090('0x3b')](function(_0xd679e5){_0x3c0936=_0xd679e5;logger['info'](_0xf090('0x4b'),_0x3c0936['id']);return createTicket(_0x5561ee,_0x36370e,_0x3c0936,_0x527cfb,_0x103a8e,_0x527cfb[_0xf090('0x5a')],_0x444c12);})[_0xf090('0x3b')](function(_0x3c55f8){_0x2efba4=_0x3c55f8;logger[_0xf090('0x16')]('Ticket\x20created!');logger[_0xf090('0x16')](_0xf090('0x4c'),_0x2efba4['id']);})[_0xf090('0x2a')](function(_0x452646){logger[_0xf090('0x29')]('['+_0x103a8e['lastevent']['toUpperCase']()+']',JSON[_0xf090('0x24')](_0x452646));});};