Built motion from commit (unavailable).|2.5.4
[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 _0x3999=['query','username','SELECT\x20first_name,\x20last_name,\x20email1\x20FROM\x20Users\x20WHERE\x20email1=\x27%s\x27;','Agent\x20found!','first_name','last_name','Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!','userId','admin','getUser','error','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;','success','firstname','lastname','Enduser\x20not\x20found,\x20creating\x20a\x20new\x20enduser!','Contacts','Enduser\x20created.','phone','getEndUser','accessKey','GET','getchallenge','login','token','Account\x20%d\x20is\x20not\x20valid','getConnection','queue','[QUEUE]','Recording\x20is\x20enabled!','recordingURL','uniqueid','email','The\x20User\x20ID\x20is:','calleridname','The\x20Enduser\x20ID\x20is:','Fields','Ticket\x20created!','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','stripTrailingSlash','serverUrl','recordingFormat','none','[OUTBOUND]','destcalleridname','destcalleridnum','split','unmanaged','monitor_format','lastevent','toUpperCase','%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s','calleridnum','lodash','request-promise','bluebird','md5','util','ioredis','../../../../config/environment','vTiger','redis','defaults','localhost','socket.io-emitter','emit','length','forEach','type','idField','toString','variableName','name','Variable','toLowerCase','Creating\x20new\x20ticket...','merge','Normal','Open','getString','Subjects','Descriptions','POST','format','%s/webservice.php','remoteUri','create','sessionName','HelpDesk','stringify','then','resolve','result','Ticket\x20could\x20not\x20be\x20created\x20due\x20to:\x20%s','catch','statusCode','[vTiger],\x20%s,\x20%s,\x20%s','validateEmail','info','Getting\x20agent\x20with\x20email'];(function(_0x1aab71,_0x1f6360){var _0x3018e2=function(_0x2003bd){while(--_0x2003bd){_0x1aab71['push'](_0x1aab71['shift']());}};_0x3018e2(++_0x1f6360);}(_0x3999,0x109));var _0x9399=function(_0x2105fc,_0x161754){_0x2105fc=_0x2105fc-0x0;var _0x2e1971=_0x3999[_0x2105fc];return _0x2e1971;};'use strict';var _=require(_0x9399('0x0'));var rp=require(_0x9399('0x1'));var BPromise=require(_0x9399('0x2'));var md5=require(_0x9399('0x3'));var util=require(_0x9399('0x4'));var Redis=require(_0x9399('0x5'));var intUtil=require('./util');var config=require(_0x9399('0x6'));var logger=require('../../../../config/logger')(_0x9399('0x7'));config[_0x9399('0x8')]=_[_0x9399('0x9')](config[_0x9399('0x8')],{'host':_0x9399('0xa'),'port':0x18eb});var io=require(_0x9399('0xb'))(new Redis(config['redis']));function emit(_0x100a3d,_0x30bf92,_0x11f03c){io['to'](_0x100a3d)[_0x9399('0xc')](_0x30bf92,_0x11f03c);}function getCustomFields(_0x3a3c9a,_0x49239e){var _0x594cc6={};if(_0x3a3c9a[_0x9399('0xd')]>0x0){_[_0x9399('0xe')](_0x3a3c9a,function(_0x59b20c){if(_0x59b20c[_0x9399('0xf')]==='string'&&_0x59b20c[_0x9399('0x10')]){_0x594cc6[_0x59b20c[_0x9399('0x10')]]=_0x59b20c['content'][_0x9399('0x11')]();}else if(_0x59b20c[_0x9399('0xf')]==='variable'&&_0x59b20c[_0x9399('0x12')]&&_0x59b20c[_0x9399('0x10')]&&_0x49239e[_0x59b20c[_0x9399('0x12')]]){_0x594cc6[_0x59b20c[_0x9399('0x10')]]=_0x49239e[_0x59b20c['variableName']][_0x9399('0x11')]();}else if(_0x59b20c['type']==='customVariable'&&_0x59b20c['Variable']&&_0x59b20c['Variable'][_0x9399('0x13')]&&_0x59b20c[_0x9399('0x10')]&&_0x49239e[_0x59b20c[_0x9399('0x14')]['name'][_0x9399('0x15')]()]){_0x594cc6[_0x59b20c[_0x9399('0x10')]]=_0x49239e[_0x59b20c['Variable'][_0x9399('0x13')][_0x9399('0x15')]()]['toString']();}});}return _0x594cc6;}function createTicket(_0x4ee48f,_0x488bd0,_0x1187a1,_0x68226d,_0x8d687b,_0x4fb0f9,_0x29a042){logger['info'](_0x9399('0x16'));var _0x46ebcf=_[_0x9399('0x17')]({'parent_id':'','assigned_user_id':_0x488bd0['id'],'ticketpriorities':_0x9399('0x18'),'ticketstatus':_0x9399('0x19'),'ticket_title':intUtil[_0x9399('0x1a')](_0x68226d[_0x9399('0x1b')],_0x8d687b,'\x20'),'description':intUtil[_0x9399('0x1a')](_0x68226d[_0x9399('0x1c')],_0x8d687b,'\x0a'),'contact_id':_0x1187a1['id']},getCustomFields(_0x4fb0f9,_0x8d687b));var _0xf2dd1b={'method':_0x9399('0x1d'),'uri':util[_0x9399('0x1e')](_0x9399('0x1f'),_0x4ee48f[_0x9399('0x20')]),'form':{'operation':_0x9399('0x21'),'username':_0x4ee48f['username'],'sessionName':_0x29a042[_0x9399('0x22')],'elementType':_0x9399('0x23'),'element':JSON[_0x9399('0x24')](_0x46ebcf)},'json':!![]};return request(_0xf2dd1b)[_0x9399('0x25')](function(_0x5cd9fc){if(_0x5cd9fc['success']){return BPromise[_0x9399('0x26')]({'id':_0x5cd9fc[_0x9399('0x27')]['id']});}else{throw new Error(util[_0x9399('0x1e')](_0x9399('0x28'),JSON[_0x9399('0x24')](_0x5cd9fc['error'])));}})[_0x9399('0x29')](function(_0x28406c){var _0x15f2d1=_0x28406c[_0x9399('0x2a')]||0x1f4;logger['error'](_0x9399('0x2b'),'createTicket',_0x15f2d1,JSON['stringify'](_0x28406c));});}function getUser(_0x1290fe,_0x2fcc15,_0x42b468){if(intUtil[_0x9399('0x2c')](_0x2fcc15)){logger[_0x9399('0x2d')](_0x9399('0x2e'),_0x2fcc15);return request({'method':'GET','uri':util['format'](_0x9399('0x1f'),_0x1290fe[_0x9399('0x20')]),'qs':{'operation':_0x9399('0x2f'),'username':_0x1290fe[_0x9399('0x30')],'sessionName':_0x42b468[_0x9399('0x22')],'query':util[_0x9399('0x1e')](_0x9399('0x31'),_0x2fcc15)},'json':!![]})['then'](function(_0x5927dc){if(_0x5927dc['success']&&_0x5927dc[_0x9399('0x27')][_0x9399('0xd')]){logger[_0x9399('0x2d')](_0x9399('0x32'));return BPromise[_0x9399('0x26')]({'id':_0x5927dc[_0x9399('0x27')][0x0]['id'],'email':_0x5927dc[_0x9399('0x27')][0x0]['email1'],'name':_0x5927dc['result'][0x0][_0x9399('0x33')]+'\x20'+_0x5927dc[_0x9399('0x27')][_0x9399('0x34')]});}else{logger[_0x9399('0x2d')](_0x9399('0x35'));return BPromise['resolve']({'id':_0x42b468[_0x9399('0x36')],'email':_0x1290fe[_0x9399('0x30')],'name':_0x9399('0x37')});}})[_0x9399('0x29')](function(_0x3d9dc9){var _0x5f1b91=_0x3d9dc9[_0x9399('0x2a')]||0x1f4;logger['error'](_0x9399('0x2b'),_0x9399('0x38'),_0x5f1b91,JSON['stringify'](_0x3d9dc9));});}else{logger[_0x9399('0x2d')](_0x9399('0x35'));return BPromise[_0x9399('0x26')]({'id':_0x42b468[_0x9399('0x36')],'email':_0x1290fe[_0x9399('0x30')],'name':_0x9399('0x37')})[_0x9399('0x29')](function(_0x5e1861){var _0xce2341=_0x5e1861['statusCode']||0x1f4;logger[_0x9399('0x39')](_0x9399('0x2b'),'getUser',_0xce2341,JSON[_0x9399('0x24')](_0x5e1861));});}}function getEndUser(_0x1b174f,_0x20c312,_0x2d1668,_0x3638ce){logger[_0x9399('0x2d')]('Searching\x20end\x20user\x20with\x20phone',_0x2d1668);return request({'method':'GET','uri':util[_0x9399('0x1e')]('%s/webservice.php',_0x1b174f[_0x9399('0x20')]),'qs':{'operation':_0x9399('0x2f'),'username':_0x1b174f[_0x9399('0x30')],'sessionName':_0x3638ce[_0x9399('0x22')],'query':util[_0x9399('0x1e')](_0x9399('0x3a'),_0x2d1668,_0x2d1668,_0x2d1668,_0x2d1668,_0x2d1668)},'json':!![]})[_0x9399('0x25')](function(_0x4cd012){if(_0x4cd012[_0x9399('0x3b')]&&_0x4cd012[_0x9399('0x27')][_0x9399('0xd')]){logger[_0x9399('0x2d')]('Enduser\x20found!');return BPromise[_0x9399('0x26')]({'id':_0x4cd012['result'][0x0]['id'],'name':_0x4cd012[_0x9399('0x27')][0x0][_0x9399('0x3c')]+'\x20'+_0x4cd012[_0x9399('0x27')][0x0][_0x9399('0x3d')],'phone':_0x2d1668});}else{logger['info'](_0x9399('0x3e'));return request({'method':'POST','uri':util[_0x9399('0x1e')](_0x9399('0x1f'),_0x1b174f['remoteUri']),'form':{'operation':_0x9399('0x21'),'sessionName':_0x3638ce[_0x9399('0x22')],'elementType':_0x9399('0x3f'),'element':JSON[_0x9399('0x24')]({'lastname':_0x20c312,'phone':_0x2d1668,'assigned_user_id':'\x20'})},'json':!![]})['then'](function(_0x5e2338){logger[_0x9399('0x2d')](_0x9399('0x40'));return BPromise[_0x9399('0x26')]({'id':_0x5e2338[_0x9399('0x27')]['id'],'name':_0x5e2338[_0x9399('0x27')][_0x9399('0x3d')],'phone':_0x5e2338['result'][_0x9399('0x41')]});})[_0x9399('0x29')](function(_0x468e32){var _0x475bf7=_0x468e32[_0x9399('0x2a')]||0x1f4;logger['error'](_0x9399('0x2b'),_0x9399('0x42'),_0x475bf7,JSON[_0x9399('0x24')](_0x468e32));});}});}function getConnection(_0x3a2496){var _0x361b10=_0x3a2496['username'];var _0xb60e73=_0x3a2496[_0x9399('0x43')];return request({'method':_0x9399('0x44'),'uri':util[_0x9399('0x1e')](_0x9399('0x1f'),_0x3a2496[_0x9399('0x20')]),'qs':{'operation':_0x9399('0x45'),'username':_0x361b10},'json':!![]})[_0x9399('0x25')](function(_0x4f14e6){if(_0x4f14e6[_0x9399('0x3b')]){return request({'method':'POST','uri':util['format'](_0x9399('0x1f'),_0x3a2496['remoteUri']),'form':{'operation':_0x9399('0x46'),'username':_0x361b10,'accessKey':md5(_0x4f14e6[_0x9399('0x27')][_0x9399('0x47')]+_0xb60e73)},'json':!![]});}})[_0x9399('0x25')](function(_0x30ad56){if(_0x30ad56['success']){return BPromise['resolve'](_0x30ad56[_0x9399('0x27')]);}else{throw new Error(util[_0x9399('0x1e')](_0x9399('0x48'),_0x3a2496['id']));}})[_0x9399('0x29')](function(_0x4acd77){var _0x5764dd=_0x4acd77[_0x9399('0x2a')]||0x1f4;logger[_0x9399('0x39')](_0x9399('0x2b'),_0x9399('0x49'),_0x5764dd,JSON[_0x9399('0x24')](_0x4acd77));});}function request(_0x6073d0){logger['info']('[HTTP]',JSON[_0x9399('0x24')](_0x6073d0));return rp(_0x6073d0);}exports[_0x9399('0x4a')]=function(_0x55f3d9,_0x141c0e,_0x3c2605,_0x184d15,_0x16e450,_0x2b044c){var _0xe5faae,_0x2eb190,_0x2d12a6,_0x5d352c;_0x2b044c[_0x9399('0x20')]=intUtil['stripTrailingSlash'](_0x2b044c[_0x9399('0x20')]);if(_0x16e450&&_0x16e450['monitor_format']){logger[_0x9399('0x2d')](_0x9399('0x4b'),_0x9399('0x4c'));_0x141c0e[_0x9399('0x4d')]=util[_0x9399('0x1e')]('%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s',_0x2b044c['serverUrl'],_0x141c0e['uniqueid'],md5(_0x141c0e[_0x9399('0x4e')]));}else{_0x141c0e[_0x9399('0x4d')]='';}return getConnection(_0x2b044c)[_0x9399('0x25')](function(_0x3a4bb0){_0x5d352c=_0x3a4bb0;return getUser(_0x2b044c,_0x184d15[_0x9399('0x4f')],_0x5d352c);})['then'](function(_0x475441){_0xe5faae=_0x475441;logger[_0x9399('0x2d')](_0x9399('0x50'),_0xe5faae['id']);return getEndUser(_0x2b044c,_0x141c0e[_0x9399('0x51')],_0x141c0e['calleridnum'],_0x5d352c);})[_0x9399('0x25')](function(_0x272054){_0x2eb190=_0x272054;logger['info'](_0x9399('0x52'),_0x2eb190['id']);return createTicket(_0x2b044c,_0xe5faae,_0x2eb190,_0x3c2605,_0x141c0e,_0x3c2605[_0x9399('0x53')],_0x5d352c);})[_0x9399('0x25')](function(_0x3ab33c){_0x2d12a6=_0x3ab33c;logger[_0x9399('0x2d')](_0x9399('0x54'));logger[_0x9399('0x2d')](_0x9399('0x55'),_0x2d12a6['id']);var _0x5dda2f=util[_0x9399('0x1e')](_0x9399('0x56'),_0x2b044c[_0x9399('0x20')],_['split'](_0x2d12a6['id'],'x')[0x1]);logger['info'](_0x9399('0x4b'),_0x9399('0x57'));emit(util[_0x9399('0x1e')](_0x9399('0x58'),_0x184d15[_0x9399('0x13')]),_0x9399('0x59'),{'uri':_0x5dda2f});})[_0x9399('0x29')](function(_0x2df132){logger[_0x9399('0x39')](_0x9399('0x4b'),JSON[_0x9399('0x24')](_0x2df132));});};exports['outbound']=function(_0x1b7db8,_0x51564f,_0x3175f6,_0xc30cea,_0x4e7a94,_0xec811e,_0x35676d){var _0x4e8b97,_0x333eeb,_0x497e83,_0x265db7;_0xec811e['remoteUri']=intUtil[_0x9399('0x5a')](_0xec811e[_0x9399('0x20')]);_0xec811e['serverUrl']=intUtil[_0x9399('0x5a')](_0xec811e[_0x9399('0x5b')]);if(_0x35676d&&_0x35676d[_0x9399('0x5c')]&&_0x35676d[_0x9399('0x5c')]!==_0x9399('0x5d')){logger[_0x9399('0x2d')](_0x9399('0x5e'),'Recording\x20is\x20enabled!');_0x51564f[_0x9399('0x4d')]=util[_0x9399('0x1e')]('%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s',_0xec811e[_0x9399('0x5b')],_0x51564f[_0x9399('0x4e')],md5(_0x51564f[_0x9399('0x4e')]));}return getConnection(_0xec811e)[_0x9399('0x25')](function(_0x3dd204){_0x265db7=_0x3dd204;return getUser(_0xec811e,_0xc30cea[_0x9399('0x4f')],_0x265db7);})[_0x9399('0x25')](function(_0x41beb3){_0x4e8b97=_0x41beb3;logger[_0x9399('0x2d')](_0x9399('0x50'),_0x4e8b97['id']);return getEndUser(_0xec811e,_0x51564f[_0x9399('0x5f')],_0x51564f[_0x9399('0x60')],_0x265db7);})['then'](function(_0x2735a2){_0x333eeb=_0x2735a2;logger['info'](_0x9399('0x52'),_0x333eeb['id']);return createTicket(_0xec811e,_0x4e8b97,_0x333eeb,_0x3175f6,_0x51564f,_0x3175f6[_0x9399('0x53')],_0x265db7);})[_0x9399('0x25')](function(_0x531d39){_0x497e83=_0x531d39;logger['info']('Ticket\x20created!');logger[_0x9399('0x2d')](_0x9399('0x55'),_0x497e83['id']);var _0x452e24=util['format'](_0x9399('0x56'),_0xec811e[_0x9399('0x20')],_[_0x9399('0x61')](_0x497e83['id'],'x')[0x1]);logger[_0x9399('0x2d')](_0x9399('0x5e'),_0x9399('0x57'));emit(util[_0x9399('0x1e')]('user:%s',_0xc30cea[_0x9399('0x13')]),_0x9399('0x59'),{'uri':_0x452e24});})[_0x9399('0x29')](function(_0x2a62c2){logger[_0x9399('0x39')](_0x9399('0x5e'),JSON['stringify'](_0x2a62c2));});};exports[_0x9399('0x62')]=function(_0x32b6f8,_0x9ae64c,_0x2ac1a0,_0xa70b93,_0x598c8c,_0x2c1316){var _0x3a2b27,_0x37f455,_0x3e6bcf,_0x463ec7;_0x2c1316[_0x9399('0x20')]=intUtil['stripTrailingSlash'](_0x2c1316[_0x9399('0x20')]);_0x2c1316['serverUrl']=intUtil[_0x9399('0x5a')](_0x2c1316[_0x9399('0x5b')]);if(_0x598c8c&&_0x598c8c[_0x9399('0x63')]){logger[_0x9399('0x2d')]('['+_0x9ae64c[_0x9399('0x64')][_0x9399('0x65')]()+']',_0x9399('0x4c'));_0x9ae64c[_0x9399('0x4d')]=util[_0x9399('0x1e')](_0x9399('0x66'),_0x2c1316[_0x9399('0x5b')],_0x9ae64c['uniqueid'],md5(_0x9ae64c[_0x9399('0x4e')]));}else{_0x9ae64c['recordingURL']='';}return getConnection(_0x2c1316)[_0x9399('0x25')](function(_0xf7caf7){_0x463ec7=_0xf7caf7;return getUser(_0x2c1316,'',_0x463ec7);})['then'](function(_0x56d4ee){_0x3a2b27=_0x56d4ee;logger[_0x9399('0x2d')](_0x9399('0x50'),_0x3a2b27['id']);return getEndUser(_0x2c1316,_0x9ae64c[_0x9399('0x51')],_0x9ae64c[_0x9399('0x67')],_0x463ec7);})['then'](function(_0x26aa35){_0x37f455=_0x26aa35;logger[_0x9399('0x2d')]('The\x20Enduser\x20ID\x20is:',_0x37f455['id']);return createTicket(_0x2c1316,_0x3a2b27,_0x37f455,_0x2ac1a0,_0x9ae64c,_0x2ac1a0[_0x9399('0x53')],_0x463ec7);})['then'](function(_0x5851e0){_0x3e6bcf=_0x5851e0;logger[_0x9399('0x2d')](_0x9399('0x54'));logger['info'](_0x9399('0x55'),_0x3e6bcf['id']);})[_0x9399('0x29')](function(_0x12e504){logger[_0x9399('0x39')]('['+_0x9ae64c[_0x9399('0x64')][_0x9399('0x65')]()+']',JSON[_0x9399('0x24')](_0x12e504));});};