Built motion from commit (unavailable).|2.5.17
[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 _0xf558=['SELECT\x20first_name,\x20last_name,\x20email1\x20FROM\x20Users\x20WHERE\x20email1=\x27%s\x27;','result','first_name','last_name','Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!','userId','[vTiger],\x20%s,\x20%s,\x20%s','getUser','Searching\x20end\x20user\x20with\x20phone','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!','lastname','Contacts','phone','accessKey','getchallenge','login','token','Account\x20%d\x20is\x20not\x20valid','getConnection','[HTTP]','stripTrailingSlash','monitor_format','[QUEUE]','Recording\x20is\x20enabled!','%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s','serverUrl','recordingURL','email','The\x20User\x20ID\x20is:','calleridnum','The\x20Enduser\x20ID\x20is:','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','trigger:browser:url','outbound','recordingFormat','none','uniqueid','destcalleridname','destcalleridnum','Ticket\x20created!','unmanaged','lastevent','toUpperCase','calleridname','lodash','bluebird','md5','util','ioredis','./util','vTiger','redis','defaults','localhost','socket.io-emitter','emit','length','forEach','type','string','content','toString','variable','variableName','idField','customVariable','Variable','toLowerCase','name','Creating\x20new\x20ticket...','Normal','Open','Subjects','Descriptions','POST','format','%s/webservice.php','remoteUri','create','username','sessionName','HelpDesk','stringify','then','success','resolve','Ticket\x20could\x20not\x20be\x20created\x20due\x20to:\x20%s','error','catch','statusCode','createTicket','info','Getting\x20agent\x20with\x20email','GET','query'];(function(_0x265b89,_0x3b9dde){var _0x3d972c=function(_0x4277f1){while(--_0x4277f1){_0x265b89['push'](_0x265b89['shift']());}};_0x3d972c(++_0x3b9dde);}(_0xf558,0x1c6));var _0x8f55=function(_0x13d5a3,_0x4fe4ec){_0x13d5a3=_0x13d5a3-0x0;var _0xf5e40d=_0xf558[_0x13d5a3];return _0xf5e40d;};'use strict';var _=require(_0x8f55('0x0'));var rp=require('request-promise');var BPromise=require(_0x8f55('0x1'));var md5=require(_0x8f55('0x2'));var util=require(_0x8f55('0x3'));var Redis=require(_0x8f55('0x4'));var intUtil=require(_0x8f55('0x5'));var config=require('../../../../config/environment');var logger=require('../../../../config/logger')(_0x8f55('0x6'));config[_0x8f55('0x7')]=_[_0x8f55('0x8')](config[_0x8f55('0x7')],{'host':_0x8f55('0x9'),'port':0x18eb});var io=require(_0x8f55('0xa'))(new Redis(config[_0x8f55('0x7')]));function emit(_0x50542a,_0x1f8952,_0x34df75){io['to'](_0x50542a)[_0x8f55('0xb')](_0x1f8952,_0x34df75);}function getCustomFields(_0x2210f6,_0x460ac4){var _0x30c13c={};if(_0x2210f6[_0x8f55('0xc')]>0x0){_[_0x8f55('0xd')](_0x2210f6,function(_0x206a77){if(_0x206a77[_0x8f55('0xe')]===_0x8f55('0xf')&&_0x206a77['idField']){_0x30c13c[_0x206a77['idField']]=_0x206a77[_0x8f55('0x10')][_0x8f55('0x11')]();}else if(_0x206a77[_0x8f55('0xe')]===_0x8f55('0x12')&&_0x206a77[_0x8f55('0x13')]&&_0x206a77[_0x8f55('0x14')]&&_0x460ac4[_0x206a77[_0x8f55('0x13')]]){_0x30c13c[_0x206a77[_0x8f55('0x14')]]=_0x460ac4[_0x206a77[_0x8f55('0x13')]][_0x8f55('0x11')]();}else if(_0x206a77[_0x8f55('0xe')]===_0x8f55('0x15')&&_0x206a77[_0x8f55('0x16')]&&_0x206a77['Variable']['name']&&_0x206a77[_0x8f55('0x14')]&&_0x460ac4[_0x206a77['Variable']['name'][_0x8f55('0x17')]()]){_0x30c13c[_0x206a77[_0x8f55('0x14')]]=_0x460ac4[_0x206a77['Variable'][_0x8f55('0x18')][_0x8f55('0x17')]()][_0x8f55('0x11')]();}});}return _0x30c13c;}function createTicket(_0x4c6b0,_0x18196b,_0x4909f8,_0x13e61a,_0xbd1559,_0x901183,_0x404087){logger['info'](_0x8f55('0x19'));var _0x2a1560=_['merge']({'parent_id':'','assigned_user_id':_0x18196b['id'],'ticketpriorities':_0x8f55('0x1a'),'ticketstatus':_0x8f55('0x1b'),'ticket_title':intUtil['getString'](_0x13e61a[_0x8f55('0x1c')],_0xbd1559,'\x20'),'description':intUtil['getString'](_0x13e61a[_0x8f55('0x1d')],_0xbd1559,'\x0a'),'contact_id':_0x4909f8['id']},getCustomFields(_0x901183,_0xbd1559));var _0x55782e={'method':_0x8f55('0x1e'),'uri':util[_0x8f55('0x1f')](_0x8f55('0x20'),_0x4c6b0[_0x8f55('0x21')]),'form':{'operation':_0x8f55('0x22'),'username':_0x4c6b0[_0x8f55('0x23')],'sessionName':_0x404087[_0x8f55('0x24')],'elementType':_0x8f55('0x25'),'element':JSON[_0x8f55('0x26')](_0x2a1560)},'json':!![]};return request(_0x55782e)[_0x8f55('0x27')](function(_0x322caa){if(_0x322caa[_0x8f55('0x28')]){return BPromise[_0x8f55('0x29')]({'id':_0x322caa['result']['id']});}else{throw new Error(util['format'](_0x8f55('0x2a'),JSON[_0x8f55('0x26')](_0x322caa[_0x8f55('0x2b')])));}})[_0x8f55('0x2c')](function(_0x207396){var _0x472760=_0x207396[_0x8f55('0x2d')]||0x1f4;logger[_0x8f55('0x2b')]('[vTiger],\x20%s,\x20%s,\x20%s',_0x8f55('0x2e'),_0x472760,JSON[_0x8f55('0x26')](_0x207396));});}function getUser(_0x345507,_0x286032,_0x5b4c90){if(intUtil['validateEmail'](_0x286032)){logger[_0x8f55('0x2f')](_0x8f55('0x30'),_0x286032);return request({'method':_0x8f55('0x31'),'uri':util['format'](_0x8f55('0x20'),_0x345507[_0x8f55('0x21')]),'qs':{'operation':_0x8f55('0x32'),'username':_0x345507[_0x8f55('0x23')],'sessionName':_0x5b4c90[_0x8f55('0x24')],'query':util[_0x8f55('0x1f')](_0x8f55('0x33'),_0x286032)},'json':!![]})['then'](function(_0x3e73a4){if(_0x3e73a4[_0x8f55('0x28')]&&_0x3e73a4['result'][_0x8f55('0xc')]){logger['info']('Agent\x20found!');return BPromise['resolve']({'id':_0x3e73a4['result'][0x0]['id'],'email':_0x3e73a4[_0x8f55('0x34')][0x0]['email1'],'name':_0x3e73a4[_0x8f55('0x34')][0x0][_0x8f55('0x35')]+'\x20'+_0x3e73a4['result'][_0x8f55('0x36')]});}else{logger[_0x8f55('0x2f')](_0x8f55('0x37'));return BPromise[_0x8f55('0x29')]({'id':_0x5b4c90[_0x8f55('0x38')],'email':_0x345507['username'],'name':'admin'});}})['catch'](function(_0xc2c205){var _0x4ceec2=_0xc2c205[_0x8f55('0x2d')]||0x1f4;logger[_0x8f55('0x2b')](_0x8f55('0x39'),'getUser',_0x4ceec2,JSON[_0x8f55('0x26')](_0xc2c205));});}else{logger['info'](_0x8f55('0x37'));return BPromise[_0x8f55('0x29')]({'id':_0x5b4c90['userId'],'email':_0x345507[_0x8f55('0x23')],'name':'admin'})[_0x8f55('0x2c')](function(_0x34fc44){var _0x3c3414=_0x34fc44['statusCode']||0x1f4;logger[_0x8f55('0x2b')](_0x8f55('0x39'),_0x8f55('0x3a'),_0x3c3414,JSON[_0x8f55('0x26')](_0x34fc44));});}}function getEndUser(_0x4104d6,_0xadb18f,_0x56aba6,_0x28984c){logger[_0x8f55('0x2f')](_0x8f55('0x3b'),_0x56aba6);return request({'method':_0x8f55('0x31'),'uri':util['format'](_0x8f55('0x20'),_0x4104d6['remoteUri']),'qs':{'operation':_0x8f55('0x32'),'username':_0x4104d6[_0x8f55('0x23')],'sessionName':_0x28984c[_0x8f55('0x24')],'query':util['format'](_0x8f55('0x3c'),_0x56aba6,_0x56aba6,_0x56aba6,_0x56aba6,_0x56aba6)},'json':!![]})[_0x8f55('0x27')](function(_0x2c5688){if(_0x2c5688[_0x8f55('0x28')]&&_0x2c5688[_0x8f55('0x34')][_0x8f55('0xc')]){logger['info'](_0x8f55('0x3d'));return BPromise[_0x8f55('0x29')]({'id':_0x2c5688[_0x8f55('0x34')][0x0]['id'],'name':_0x2c5688[_0x8f55('0x34')][0x0]['firstname']+'\x20'+_0x2c5688[_0x8f55('0x34')][0x0][_0x8f55('0x3e')],'phone':_0x56aba6});}else{logger[_0x8f55('0x2f')]('Enduser\x20not\x20found,\x20creating\x20a\x20new\x20enduser!');return request({'method':_0x8f55('0x1e'),'uri':util[_0x8f55('0x1f')](_0x8f55('0x20'),_0x4104d6[_0x8f55('0x21')]),'form':{'operation':_0x8f55('0x22'),'sessionName':_0x28984c['sessionName'],'elementType':_0x8f55('0x3f'),'element':JSON[_0x8f55('0x26')]({'lastname':_0xadb18f,'phone':_0x56aba6,'assigned_user_id':'\x20'})},'json':!![]})[_0x8f55('0x27')](function(_0x348850){logger[_0x8f55('0x2f')]('Enduser\x20created.');return BPromise[_0x8f55('0x29')]({'id':_0x348850[_0x8f55('0x34')]['id'],'name':_0x348850[_0x8f55('0x34')][_0x8f55('0x3e')],'phone':_0x348850[_0x8f55('0x34')][_0x8f55('0x40')]});})[_0x8f55('0x2c')](function(_0x59d52f){var _0x2d5a3e=_0x59d52f[_0x8f55('0x2d')]||0x1f4;logger[_0x8f55('0x2b')](_0x8f55('0x39'),'getEndUser',_0x2d5a3e,JSON['stringify'](_0x59d52f));});}});}function getConnection(_0x2fd565){var _0x4dc559=_0x2fd565[_0x8f55('0x23')];var _0x2cee97=_0x2fd565[_0x8f55('0x41')];return request({'method':_0x8f55('0x31'),'uri':util[_0x8f55('0x1f')](_0x8f55('0x20'),_0x2fd565[_0x8f55('0x21')]),'qs':{'operation':_0x8f55('0x42'),'username':_0x4dc559},'json':!![]})[_0x8f55('0x27')](function(_0xf635a6){if(_0xf635a6['success']){return request({'method':'POST','uri':util[_0x8f55('0x1f')]('%s/webservice.php',_0x2fd565[_0x8f55('0x21')]),'form':{'operation':_0x8f55('0x43'),'username':_0x4dc559,'accessKey':md5(_0xf635a6[_0x8f55('0x34')][_0x8f55('0x44')]+_0x2cee97)},'json':!![]});}})[_0x8f55('0x27')](function(_0x4bf4ca){if(_0x4bf4ca['success']){return BPromise[_0x8f55('0x29')](_0x4bf4ca['result']);}else{throw new Error(util[_0x8f55('0x1f')](_0x8f55('0x45'),_0x2fd565['id']));}})[_0x8f55('0x2c')](function(_0x1f6983){var _0x4f0236=_0x1f6983[_0x8f55('0x2d')]||0x1f4;logger[_0x8f55('0x2b')]('[vTiger],\x20%s,\x20%s,\x20%s',_0x8f55('0x46'),_0x4f0236,JSON[_0x8f55('0x26')](_0x1f6983));});}function request(_0x39aa0b){logger['info'](_0x8f55('0x47'),JSON[_0x8f55('0x26')](_0x39aa0b));return rp(_0x39aa0b);}exports['queue']=function(_0x30401e,_0x4da779,_0x57356b,_0x56b870,_0x17f05d,_0x481349){var _0x43362d,_0x5884e9,_0x45247f,_0x4c4674;_0x481349[_0x8f55('0x21')]=intUtil[_0x8f55('0x48')](_0x481349[_0x8f55('0x21')]);if(_0x17f05d&&_0x17f05d[_0x8f55('0x49')]){logger[_0x8f55('0x2f')](_0x8f55('0x4a'),_0x8f55('0x4b'));_0x4da779['recordingURL']=util[_0x8f55('0x1f')](_0x8f55('0x4c'),_0x481349[_0x8f55('0x4d')],_0x4da779['uniqueid'],md5(_0x4da779['uniqueid']));}else{_0x4da779[_0x8f55('0x4e')]='';}return getConnection(_0x481349)[_0x8f55('0x27')](function(_0x26024b){_0x4c4674=_0x26024b;return getUser(_0x481349,_0x56b870[_0x8f55('0x4f')],_0x4c4674);})[_0x8f55('0x27')](function(_0x534f1a){_0x43362d=_0x534f1a;logger[_0x8f55('0x2f')](_0x8f55('0x50'),_0x43362d['id']);return getEndUser(_0x481349,_0x4da779['calleridname'],_0x4da779[_0x8f55('0x51')],_0x4c4674);})['then'](function(_0x14ee3){_0x5884e9=_0x14ee3;logger['info'](_0x8f55('0x52'),_0x5884e9['id']);return createTicket(_0x481349,_0x43362d,_0x5884e9,_0x57356b,_0x4da779,_0x57356b[_0x8f55('0x53')],_0x4c4674);})[_0x8f55('0x27')](function(_0x1d9d9e){_0x45247f=_0x1d9d9e;logger[_0x8f55('0x2f')]('Ticket\x20created!');logger[_0x8f55('0x2f')](_0x8f55('0x54'),_0x45247f['id']);var _0x2c716e=util['format'](_0x8f55('0x55'),_0x481349[_0x8f55('0x21')],_[_0x8f55('0x56')](_0x45247f['id'],'x')[0x1]);logger['info'](_0x8f55('0x4a'),_0x8f55('0x57'));emit(util[_0x8f55('0x1f')](_0x8f55('0x58'),_0x56b870['name']),_0x8f55('0x59'),{'uri':_0x2c716e});})[_0x8f55('0x2c')](function(_0x3e884c){logger[_0x8f55('0x2b')](_0x8f55('0x4a'),JSON[_0x8f55('0x26')](_0x3e884c));});};exports[_0x8f55('0x5a')]=function(_0x1911d5,_0x2f5589,_0x1df98d,_0x1e8343,_0xf6acf5,_0x410502,_0x1f0211){var _0x12ae8c,_0x3deb9a,_0x43fc99,_0xd97124;_0x410502[_0x8f55('0x21')]=intUtil[_0x8f55('0x48')](_0x410502[_0x8f55('0x21')]);_0x410502[_0x8f55('0x4d')]=intUtil[_0x8f55('0x48')](_0x410502[_0x8f55('0x4d')]);if(_0x1f0211&&_0x1f0211[_0x8f55('0x5b')]&&_0x1f0211[_0x8f55('0x5b')]!==_0x8f55('0x5c')){logger[_0x8f55('0x2f')]('[OUTBOUND]',_0x8f55('0x4b'));_0x2f5589['recordingURL']=util['format']('%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s',_0x410502[_0x8f55('0x4d')],_0x2f5589[_0x8f55('0x5d')],md5(_0x2f5589[_0x8f55('0x5d')]));}return getConnection(_0x410502)[_0x8f55('0x27')](function(_0x50ddf0){_0xd97124=_0x50ddf0;return getUser(_0x410502,_0x1e8343[_0x8f55('0x4f')],_0xd97124);})['then'](function(_0x3025f0){_0x12ae8c=_0x3025f0;logger['info'](_0x8f55('0x50'),_0x12ae8c['id']);return getEndUser(_0x410502,_0x2f5589[_0x8f55('0x5e')],_0x2f5589[_0x8f55('0x5f')],_0xd97124);})[_0x8f55('0x27')](function(_0x3e2b1){_0x3deb9a=_0x3e2b1;logger[_0x8f55('0x2f')]('The\x20Enduser\x20ID\x20is:',_0x3deb9a['id']);return createTicket(_0x410502,_0x12ae8c,_0x3deb9a,_0x1df98d,_0x2f5589,_0x1df98d[_0x8f55('0x53')],_0xd97124);})[_0x8f55('0x27')](function(_0x5d62bd){_0x43fc99=_0x5d62bd;logger[_0x8f55('0x2f')](_0x8f55('0x60'));logger['info'](_0x8f55('0x54'),_0x43fc99['id']);var _0x30aa29=util[_0x8f55('0x1f')](_0x8f55('0x55'),_0x410502[_0x8f55('0x21')],_['split'](_0x43fc99['id'],'x')[0x1]);logger['info']('[OUTBOUND]',_0x8f55('0x57'));emit(util['format']('user:%s',_0x1e8343[_0x8f55('0x18')]),_0x8f55('0x59'),{'uri':_0x30aa29});})[_0x8f55('0x2c')](function(_0x108388){logger['error']('[OUTBOUND]',JSON[_0x8f55('0x26')](_0x108388));});};exports[_0x8f55('0x61')]=function(_0x53129c,_0x115839,_0x554ef8,_0x307e8d,_0x577783,_0x3b3aa2){var _0x51a648,_0x245beb,_0x26f49e,_0x8e1b8c;_0x3b3aa2[_0x8f55('0x21')]=intUtil[_0x8f55('0x48')](_0x3b3aa2[_0x8f55('0x21')]);_0x3b3aa2[_0x8f55('0x4d')]=intUtil[_0x8f55('0x48')](_0x3b3aa2['serverUrl']);if(_0x577783&&_0x577783[_0x8f55('0x49')]){logger[_0x8f55('0x2f')]('['+_0x115839[_0x8f55('0x62')][_0x8f55('0x63')]()+']',_0x8f55('0x4b'));_0x115839[_0x8f55('0x4e')]=util[_0x8f55('0x1f')](_0x8f55('0x4c'),_0x3b3aa2['serverUrl'],_0x115839[_0x8f55('0x5d')],md5(_0x115839[_0x8f55('0x5d')]));}else{_0x115839['recordingURL']='';}return getConnection(_0x3b3aa2)['then'](function(_0x5443cb){_0x8e1b8c=_0x5443cb;return getUser(_0x3b3aa2,'',_0x8e1b8c);})[_0x8f55('0x27')](function(_0xdaf66d){_0x51a648=_0xdaf66d;logger[_0x8f55('0x2f')]('The\x20User\x20ID\x20is:',_0x51a648['id']);return getEndUser(_0x3b3aa2,_0x115839[_0x8f55('0x64')],_0x115839[_0x8f55('0x51')],_0x8e1b8c);})['then'](function(_0x4c1a7f){_0x245beb=_0x4c1a7f;logger[_0x8f55('0x2f')](_0x8f55('0x52'),_0x245beb['id']);return createTicket(_0x3b3aa2,_0x51a648,_0x245beb,_0x554ef8,_0x115839,_0x554ef8[_0x8f55('0x53')],_0x8e1b8c);})[_0x8f55('0x27')](function(_0x25515e){_0x26f49e=_0x25515e;logger[_0x8f55('0x2f')]('Ticket\x20created!');logger['info'](_0x8f55('0x54'),_0x26f49e['id']);})[_0x8f55('0x2c')](function(_0x2e77be){logger['error']('['+_0x115839[_0x8f55('0x62')][_0x8f55('0x63')]()+']',JSON[_0x8f55('0x26')](_0x2e77be));});};