Built motion from commit (unavailable).|2.5.11
[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 _0x1f53=['bluebird','md5','util','ioredis','../../../../config/environment','vTiger','redis','localhost','socket.io-emitter','emit','length','type','string','idField','content','variableName','toString','customVariable','Variable','name','toLowerCase','merge','Open','Subjects','Descriptions','format','%s/webservice.php','remoteUri','create','username','then','resolve','result','Ticket\x20could\x20not\x20be\x20created\x20due\x20to:\x20%s','stringify','error','catch','statusCode','[vTiger],\x20%s,\x20%s,\x20%s','validateEmail','info','Getting\x20agent\x20with\x20email','GET','sessionName','SELECT\x20first_name,\x20last_name,\x20email1\x20FROM\x20Users\x20WHERE\x20email1=\x27%s\x27;','success','Agent\x20found!','Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!','userId','admin','getUser','Searching\x20end\x20user\x20with\x20phone','query','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;','Enduser\x20found!','firstname','lastname','Enduser\x20not\x20found,\x20creating\x20a\x20new\x20enduser!','POST','Enduser\x20created.','phone','accessKey','getchallenge','login','getConnection','queue','stripTrailingSlash','[QUEUE]','Recording\x20is\x20enabled!','recordingURL','%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s','serverUrl','uniqueid','email','calleridname','Fields','The\x20Ticket\x20ID\x20is:','%s/index.php?module=HelpDesk&view=Detail&record=%s&app=SUPPORT','split','Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...','user:%s','recordingFormat','none','[OUTBOUND]','destcalleridnum','The\x20Enduser\x20ID\x20is:','Ticket\x20created!','trigger:browser:url','unmanaged','monitor_format','lastevent','toUpperCase','The\x20User\x20ID\x20is:','calleridnum','lodash'];(function(_0x9abebc,_0x32e0ca){var _0x2d644b=function(_0x5e2aed){while(--_0x5e2aed){_0x9abebc['push'](_0x9abebc['shift']());}};_0x2d644b(++_0x32e0ca);}(_0x1f53,0xbd));var _0x31f5=function(_0x9c2c88,_0x313e41){_0x9c2c88=_0x9c2c88-0x0;var _0xad0ee9=_0x1f53[_0x9c2c88];return _0xad0ee9;};'use strict';var _=require(_0x31f5('0x0'));var rp=require('request-promise');var BPromise=require(_0x31f5('0x1'));var md5=require(_0x31f5('0x2'));var util=require(_0x31f5('0x3'));var Redis=require(_0x31f5('0x4'));var intUtil=require('./util');var config=require(_0x31f5('0x5'));var logger=require('../../../../config/logger')(_0x31f5('0x6'));config[_0x31f5('0x7')]=_['defaults'](config['redis'],{'host':_0x31f5('0x8'),'port':0x18eb});var io=require(_0x31f5('0x9'))(new Redis(config['redis']));function emit(_0x83ec08,_0x4f037b,_0x1aadc2){io['to'](_0x83ec08)[_0x31f5('0xa')](_0x4f037b,_0x1aadc2);}function getCustomFields(_0x41ada4,_0x54bdb8){var _0x11fd2f={};if(_0x41ada4[_0x31f5('0xb')]>0x0){_['forEach'](_0x41ada4,function(_0x80482e){if(_0x80482e[_0x31f5('0xc')]===_0x31f5('0xd')&&_0x80482e[_0x31f5('0xe')]){_0x11fd2f[_0x80482e['idField']]=_0x80482e[_0x31f5('0xf')]['toString']();}else if(_0x80482e[_0x31f5('0xc')]==='variable'&&_0x80482e[_0x31f5('0x10')]&&_0x80482e['idField']&&_0x54bdb8[_0x80482e['variableName']]){_0x11fd2f[_0x80482e[_0x31f5('0xe')]]=_0x54bdb8[_0x80482e[_0x31f5('0x10')]][_0x31f5('0x11')]();}else if(_0x80482e[_0x31f5('0xc')]===_0x31f5('0x12')&&_0x80482e[_0x31f5('0x13')]&&_0x80482e[_0x31f5('0x13')][_0x31f5('0x14')]&&_0x80482e[_0x31f5('0xe')]&&_0x54bdb8[_0x80482e[_0x31f5('0x13')][_0x31f5('0x14')][_0x31f5('0x15')]()]){_0x11fd2f[_0x80482e[_0x31f5('0xe')]]=_0x54bdb8[_0x80482e[_0x31f5('0x13')][_0x31f5('0x14')][_0x31f5('0x15')]()][_0x31f5('0x11')]();}});}return _0x11fd2f;}function createTicket(_0x5b6679,_0x4ecdce,_0x58a16c,_0x223ac1,_0x17a9e7,_0x220873,_0x26578c){logger['info']('Creating\x20new\x20ticket...');var _0xd9a8a7=_[_0x31f5('0x16')]({'parent_id':'','assigned_user_id':_0x4ecdce['id'],'ticketpriorities':'Normal','ticketstatus':_0x31f5('0x17'),'ticket_title':intUtil['getString'](_0x223ac1[_0x31f5('0x18')],_0x17a9e7,'\x20'),'description':intUtil['getString'](_0x223ac1[_0x31f5('0x19')],_0x17a9e7,'\x0a'),'contact_id':_0x58a16c['id']},getCustomFields(_0x220873,_0x17a9e7));var _0x5bb2ed={'method':'POST','uri':util[_0x31f5('0x1a')](_0x31f5('0x1b'),_0x5b6679[_0x31f5('0x1c')]),'form':{'operation':_0x31f5('0x1d'),'username':_0x5b6679[_0x31f5('0x1e')],'sessionName':_0x26578c['sessionName'],'elementType':'HelpDesk','element':JSON['stringify'](_0xd9a8a7)},'json':!![]};return request(_0x5bb2ed)[_0x31f5('0x1f')](function(_0x3dc987){if(_0x3dc987['success']){return BPromise[_0x31f5('0x20')]({'id':_0x3dc987[_0x31f5('0x21')]['id']});}else{throw new Error(util['format'](_0x31f5('0x22'),JSON[_0x31f5('0x23')](_0x3dc987[_0x31f5('0x24')])));}})[_0x31f5('0x25')](function(_0x9f5ba4){var _0x41da9e=_0x9f5ba4[_0x31f5('0x26')]||0x1f4;logger['error'](_0x31f5('0x27'),'createTicket',_0x41da9e,JSON['stringify'](_0x9f5ba4));});}function getUser(_0x3e4bf3,_0x4c120f,_0x4a3a76){if(intUtil[_0x31f5('0x28')](_0x4c120f)){logger[_0x31f5('0x29')](_0x31f5('0x2a'),_0x4c120f);return request({'method':_0x31f5('0x2b'),'uri':util[_0x31f5('0x1a')](_0x31f5('0x1b'),_0x3e4bf3[_0x31f5('0x1c')]),'qs':{'operation':'query','username':_0x3e4bf3['username'],'sessionName':_0x4a3a76[_0x31f5('0x2c')],'query':util['format'](_0x31f5('0x2d'),_0x4c120f)},'json':!![]})[_0x31f5('0x1f')](function(_0x559cdf){if(_0x559cdf[_0x31f5('0x2e')]&&_0x559cdf[_0x31f5('0x21')]['length']){logger[_0x31f5('0x29')](_0x31f5('0x2f'));return BPromise[_0x31f5('0x20')]({'id':_0x559cdf[_0x31f5('0x21')][0x0]['id'],'email':_0x559cdf[_0x31f5('0x21')][0x0]['email1'],'name':_0x559cdf['result'][0x0]['first_name']+'\x20'+_0x559cdf['result']['last_name']});}else{logger[_0x31f5('0x29')](_0x31f5('0x30'));return BPromise[_0x31f5('0x20')]({'id':_0x4a3a76[_0x31f5('0x31')],'email':_0x3e4bf3[_0x31f5('0x1e')],'name':_0x31f5('0x32')});}})['catch'](function(_0x228c93){var _0x298d59=_0x228c93['statusCode']||0x1f4;logger[_0x31f5('0x24')](_0x31f5('0x27'),_0x31f5('0x33'),_0x298d59,JSON[_0x31f5('0x23')](_0x228c93));});}else{logger['info']('Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!');return BPromise[_0x31f5('0x20')]({'id':_0x4a3a76[_0x31f5('0x31')],'email':_0x3e4bf3[_0x31f5('0x1e')],'name':_0x31f5('0x32')})[_0x31f5('0x25')](function(_0x18d480){var _0x14cc3c=_0x18d480[_0x31f5('0x26')]||0x1f4;logger['error'](_0x31f5('0x27'),'getUser',_0x14cc3c,JSON['stringify'](_0x18d480));});}}function getEndUser(_0xa56075,_0x2a59e3,_0x4a933b,_0x1814a6){logger[_0x31f5('0x29')](_0x31f5('0x34'),_0x4a933b);return request({'method':'GET','uri':util[_0x31f5('0x1a')](_0x31f5('0x1b'),_0xa56075['remoteUri']),'qs':{'operation':_0x31f5('0x35'),'username':_0xa56075[_0x31f5('0x1e')],'sessionName':_0x1814a6[_0x31f5('0x2c')],'query':util[_0x31f5('0x1a')](_0x31f5('0x36'),_0x4a933b,_0x4a933b,_0x4a933b,_0x4a933b,_0x4a933b)},'json':!![]})[_0x31f5('0x1f')](function(_0x4ffdd0){if(_0x4ffdd0['success']&&_0x4ffdd0['result'][_0x31f5('0xb')]){logger[_0x31f5('0x29')](_0x31f5('0x37'));return BPromise[_0x31f5('0x20')]({'id':_0x4ffdd0[_0x31f5('0x21')][0x0]['id'],'name':_0x4ffdd0[_0x31f5('0x21')][0x0][_0x31f5('0x38')]+'\x20'+_0x4ffdd0['result'][0x0][_0x31f5('0x39')],'phone':_0x4a933b});}else{logger[_0x31f5('0x29')](_0x31f5('0x3a'));return request({'method':_0x31f5('0x3b'),'uri':util[_0x31f5('0x1a')](_0x31f5('0x1b'),_0xa56075[_0x31f5('0x1c')]),'form':{'operation':_0x31f5('0x1d'),'sessionName':_0x1814a6[_0x31f5('0x2c')],'elementType':'Contacts','element':JSON['stringify']({'lastname':_0x2a59e3,'phone':_0x4a933b,'assigned_user_id':'\x20'})},'json':!![]})[_0x31f5('0x1f')](function(_0x1c178f){logger[_0x31f5('0x29')](_0x31f5('0x3c'));return BPromise[_0x31f5('0x20')]({'id':_0x1c178f[_0x31f5('0x21')]['id'],'name':_0x1c178f[_0x31f5('0x21')][_0x31f5('0x39')],'phone':_0x1c178f[_0x31f5('0x21')][_0x31f5('0x3d')]});})['catch'](function(_0x32e4a9){var _0x18e801=_0x32e4a9[_0x31f5('0x26')]||0x1f4;logger[_0x31f5('0x24')](_0x31f5('0x27'),'getEndUser',_0x18e801,JSON[_0x31f5('0x23')](_0x32e4a9));});}});}function getConnection(_0x3b43b7){var _0x5114a9=_0x3b43b7['username'];var _0x3da9f3=_0x3b43b7[_0x31f5('0x3e')];return request({'method':_0x31f5('0x2b'),'uri':util[_0x31f5('0x1a')](_0x31f5('0x1b'),_0x3b43b7['remoteUri']),'qs':{'operation':_0x31f5('0x3f'),'username':_0x5114a9},'json':!![]})[_0x31f5('0x1f')](function(_0x100083){if(_0x100083['success']){return request({'method':_0x31f5('0x3b'),'uri':util[_0x31f5('0x1a')](_0x31f5('0x1b'),_0x3b43b7['remoteUri']),'form':{'operation':_0x31f5('0x40'),'username':_0x5114a9,'accessKey':md5(_0x100083[_0x31f5('0x21')]['token']+_0x3da9f3)},'json':!![]});}})[_0x31f5('0x1f')](function(_0x144fd5){if(_0x144fd5['success']){return BPromise[_0x31f5('0x20')](_0x144fd5[_0x31f5('0x21')]);}else{throw new Error(util['format']('Account\x20%d\x20is\x20not\x20valid',_0x3b43b7['id']));}})[_0x31f5('0x25')](function(_0x1f25da){var _0x485dd2=_0x1f25da['statusCode']||0x1f4;logger['error'](_0x31f5('0x27'),_0x31f5('0x41'),_0x485dd2,JSON[_0x31f5('0x23')](_0x1f25da));});}function request(_0x3038ad){logger[_0x31f5('0x29')]('[HTTP]',JSON[_0x31f5('0x23')](_0x3038ad));return rp(_0x3038ad);}exports[_0x31f5('0x42')]=function(_0x7da3af,_0x24711b,_0x2e3717,_0x5a99e0,_0x3219e8,_0x45325a){var _0x4b1374,_0x39b8fc,_0x5267ee,_0x1213c6;_0x45325a[_0x31f5('0x1c')]=intUtil[_0x31f5('0x43')](_0x45325a['remoteUri']);if(_0x3219e8&&_0x3219e8['monitor_format']){logger[_0x31f5('0x29')](_0x31f5('0x44'),_0x31f5('0x45'));_0x24711b[_0x31f5('0x46')]=util[_0x31f5('0x1a')](_0x31f5('0x47'),_0x45325a[_0x31f5('0x48')],_0x24711b[_0x31f5('0x49')],md5(_0x24711b[_0x31f5('0x49')]));}else{_0x24711b[_0x31f5('0x46')]='';}return getConnection(_0x45325a)[_0x31f5('0x1f')](function(_0x3835ce){_0x1213c6=_0x3835ce;return getUser(_0x45325a,_0x5a99e0[_0x31f5('0x4a')],_0x1213c6);})[_0x31f5('0x1f')](function(_0x2f66c9){_0x4b1374=_0x2f66c9;logger[_0x31f5('0x29')]('The\x20User\x20ID\x20is:',_0x4b1374['id']);return getEndUser(_0x45325a,_0x24711b[_0x31f5('0x4b')],_0x24711b['calleridnum'],_0x1213c6);})[_0x31f5('0x1f')](function(_0x231ff0){_0x39b8fc=_0x231ff0;logger[_0x31f5('0x29')]('The\x20Enduser\x20ID\x20is:',_0x39b8fc['id']);return createTicket(_0x45325a,_0x4b1374,_0x39b8fc,_0x2e3717,_0x24711b,_0x2e3717[_0x31f5('0x4c')],_0x1213c6);})[_0x31f5('0x1f')](function(_0x3cf63e){_0x5267ee=_0x3cf63e;logger[_0x31f5('0x29')]('Ticket\x20created!');logger[_0x31f5('0x29')](_0x31f5('0x4d'),_0x5267ee['id']);var _0x218397=util[_0x31f5('0x1a')](_0x31f5('0x4e'),_0x45325a['remoteUri'],_[_0x31f5('0x4f')](_0x5267ee['id'],'x')[0x1]);logger[_0x31f5('0x29')]('[QUEUE]',_0x31f5('0x50'));emit(util[_0x31f5('0x1a')](_0x31f5('0x51'),_0x5a99e0[_0x31f5('0x14')]),'trigger:browser:url',{'uri':_0x218397});})[_0x31f5('0x25')](function(_0x18d0d5){logger[_0x31f5('0x24')](_0x31f5('0x44'),JSON['stringify'](_0x18d0d5));});};exports['outbound']=function(_0x1412d3,_0x149626,_0x244dbb,_0x4c2553,_0x3e4eaa,_0x16baf9,_0x1a63fe){var _0x297abe,_0x10fa89,_0x202ceb,_0x2517b5;_0x16baf9[_0x31f5('0x1c')]=intUtil[_0x31f5('0x43')](_0x16baf9['remoteUri']);_0x16baf9['serverUrl']=intUtil[_0x31f5('0x43')](_0x16baf9[_0x31f5('0x48')]);if(_0x1a63fe&&_0x1a63fe[_0x31f5('0x52')]&&_0x1a63fe[_0x31f5('0x52')]!==_0x31f5('0x53')){logger['info'](_0x31f5('0x54'),_0x31f5('0x45'));_0x149626[_0x31f5('0x46')]=util[_0x31f5('0x1a')](_0x31f5('0x47'),_0x16baf9[_0x31f5('0x48')],_0x149626[_0x31f5('0x49')],md5(_0x149626[_0x31f5('0x49')]));}return getConnection(_0x16baf9)[_0x31f5('0x1f')](function(_0x5e400e){_0x2517b5=_0x5e400e;return getUser(_0x16baf9,_0x4c2553['email'],_0x2517b5);})[_0x31f5('0x1f')](function(_0x2c1bf9){_0x297abe=_0x2c1bf9;logger['info']('The\x20User\x20ID\x20is:',_0x297abe['id']);return getEndUser(_0x16baf9,_0x149626['destcalleridname'],_0x149626[_0x31f5('0x55')],_0x2517b5);})[_0x31f5('0x1f')](function(_0x1116c1){_0x10fa89=_0x1116c1;logger[_0x31f5('0x29')](_0x31f5('0x56'),_0x10fa89['id']);return createTicket(_0x16baf9,_0x297abe,_0x10fa89,_0x244dbb,_0x149626,_0x244dbb[_0x31f5('0x4c')],_0x2517b5);})[_0x31f5('0x1f')](function(_0x33cc74){_0x202ceb=_0x33cc74;logger['info'](_0x31f5('0x57'));logger[_0x31f5('0x29')](_0x31f5('0x4d'),_0x202ceb['id']);var _0xf4f06d=util['format'](_0x31f5('0x4e'),_0x16baf9[_0x31f5('0x1c')],_['split'](_0x202ceb['id'],'x')[0x1]);logger[_0x31f5('0x29')](_0x31f5('0x54'),'Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...');emit(util[_0x31f5('0x1a')](_0x31f5('0x51'),_0x4c2553[_0x31f5('0x14')]),_0x31f5('0x58'),{'uri':_0xf4f06d});})[_0x31f5('0x25')](function(_0x2d6b70){logger[_0x31f5('0x24')](_0x31f5('0x54'),JSON[_0x31f5('0x23')](_0x2d6b70));});};exports[_0x31f5('0x59')]=function(_0x429635,_0x56b8d0,_0x29e985,_0x126cef,_0x1cd86d,_0x164c3a){var _0x164a12,_0x709dce,_0x5e525d,_0x53c45d;_0x164c3a[_0x31f5('0x1c')]=intUtil[_0x31f5('0x43')](_0x164c3a[_0x31f5('0x1c')]);_0x164c3a[_0x31f5('0x48')]=intUtil[_0x31f5('0x43')](_0x164c3a[_0x31f5('0x48')]);if(_0x1cd86d&&_0x1cd86d[_0x31f5('0x5a')]){logger[_0x31f5('0x29')]('['+_0x56b8d0[_0x31f5('0x5b')][_0x31f5('0x5c')]()+']',_0x31f5('0x45'));_0x56b8d0['recordingURL']=util['format'](_0x31f5('0x47'),_0x164c3a['serverUrl'],_0x56b8d0[_0x31f5('0x49')],md5(_0x56b8d0[_0x31f5('0x49')]));}else{_0x56b8d0[_0x31f5('0x46')]='';}return getConnection(_0x164c3a)[_0x31f5('0x1f')](function(_0x7b6185){_0x53c45d=_0x7b6185;return getUser(_0x164c3a,'',_0x53c45d);})[_0x31f5('0x1f')](function(_0x4b8459){_0x164a12=_0x4b8459;logger['info'](_0x31f5('0x5d'),_0x164a12['id']);return getEndUser(_0x164c3a,_0x56b8d0[_0x31f5('0x4b')],_0x56b8d0[_0x31f5('0x5e')],_0x53c45d);})['then'](function(_0x43d432){_0x709dce=_0x43d432;logger[_0x31f5('0x29')]('The\x20Enduser\x20ID\x20is:',_0x709dce['id']);return createTicket(_0x164c3a,_0x164a12,_0x709dce,_0x29e985,_0x56b8d0,_0x29e985[_0x31f5('0x4c')],_0x53c45d);})[_0x31f5('0x1f')](function(_0x5de601){_0x5e525d=_0x5de601;logger['info']('Ticket\x20created!');logger[_0x31f5('0x29')](_0x31f5('0x4d'),_0x5e525d['id']);})[_0x31f5('0x25')](function(_0x176708){logger[_0x31f5('0x24')]('['+_0x56b8d0[_0x31f5('0x5b')]['toUpperCase']()+']',JSON[_0x31f5('0x23')](_0x176708));});};