Built motion from commit ffdca78c.|2.6.24
[motion2.git] / server / services / ami / trigger / integration / freshsales.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 _0x309e=['&entities=contact','find','contacts','format','contactNumber','mobile_number','endUserId','Contact\x20found\x20mobile_number:','Contact\x20created:','POST','/api/contacts','YYYY-MM-DDTHH:mm:ss','Contact\x20not\x20found,\x20creating\x20a\x20new\x20contact','contact','error','Creating\x20new\x20task','/api/tasks','Subjects','Descriptions','Contact','add','task','Task\x20created:','ticketId','/contacts/','user:%s','emit','Token\x20token=','remoteUri','stripTrailingSlash','username','toLowerCase','outbound','destcalleridnum','callDirection','queue','unmanaged','%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s','uniqueid','monitor_format','[QUEUE]','Recording\x20is\x20enabled!','recordingURL','recordingFormat','none','The\x20ticket\x20ID\x20is:\x20%s','name',']\x20Error\x20running\x20Freshsales\x20integration','inspect','lodash','request-promise','md5','util','bluebird','./util','../../../../config/environment','../../../../config/logger','freshsales','redis','socket.io-emitter','GET','/api/selector/owners','headers','info','then','adminEmail','Admin\x20found:','adminId','Requested\x20Resource\x20not\x20Found','catch','toUpperCase','message','Getting\x20user\x20list','agentEmail','Getting\x20user\x20with\x20email','userEmail','email','User\x20found:','userId','Admin\x20selected','/api/lookup?q=','&f='];(function(_0x4cf98e,_0xba4b79){var _0x1623ac=function(_0x459bc0){while(--_0x459bc0){_0x4cf98e['push'](_0x4cf98e['shift']());}};_0x1623ac(++_0xba4b79);}(_0x309e,0xd5));var _0xe309=function(_0x42cae2,_0x284ef8){_0x42cae2=_0x42cae2-0x0;var _0x5f256f=_0x309e[_0x42cae2];return _0x5f256f;};'use strict';var _=require(_0xe309('0x0'));var rp=require(_0xe309('0x1'));var md5=require(_0xe309('0x2'));var util=require(_0xe309('0x3'));var moment=require('moment');var Promise=require(_0xe309('0x4'));var Redis=require('ioredis');var intUtil=require(_0xe309('0x5'));var config=require(_0xe309('0x6'));var logger=require(_0xe309('0x7'))(_0xe309('0x8'));config[_0xe309('0x9')]=_['defaults'](config['redis'],{'host':'localhost','port':0x18eb});var io=require(_0xe309('0xa'))(new Redis(config['redis']));function getAdminId(_0x5a65e2,_0x596956,_0x4c8a36,_0x462c9a){return new Promise(function(_0x5bb24f,_0x4bae5d){var _0x883daa=![];var _0x4ab095={'method':_0xe309('0xb'),'uri':_0x462c9a+_0xe309('0xc'),'json':!![]};_0x4ab095[_0xe309('0xd')]=_0x4c8a36;logger[_0xe309('0xe')]('Getting\x20admin\x20information');return rp(_0x4ab095)[_0xe309('0xf')](function(_0x1bd765){if(_0x1bd765){var _0x3de681=_0x1bd765['users'];var _0x535e47=null;for(_0x535e47=0x0;_0x535e47<_0x3de681['length'];_0x535e47++){if(_0x3de681[_0x535e47]['email']===_0x5a65e2[_0xe309('0x10')]){_0x5a65e2['adminId']=_0x3de681[_0x535e47]['id'];_0x883daa=!![];}}if(_0x883daa){logger[_0xe309('0xe')](_0xe309('0x11'),_0x5a65e2[_0xe309('0x12')]);_0x5bb24f(_0x5a65e2);}else{_0x4bae5d({'statusCode':0x194,'message':'Requested\x20Resource\x20not\x20Found'});}}else{_0x4bae5d({'statusCode':0x194,'message':_0xe309('0x13')});}})[_0xe309('0x14')](function(_0x3ab56d){logger['error']('['+[_0x596956[_0xe309('0x15')]()]+']',_0x3ab56d[_0xe309('0x16')]);_0x4bae5d(_0x3ab56d);});});}function getUser(_0x221546,_0x23353f,_0x78cee3){return function(_0x5985fc){return new Promise(function(_0x7c0b11,_0x2a918f){var _0x38ab19={'method':_0xe309('0xb'),'uri':_0x78cee3+_0xe309('0xc'),'json':!![]};_0x38ab19['headers']=_0x23353f;logger['info'](_0xe309('0x17'));return rp(_0x38ab19)[_0xe309('0xf')](function(_0x4d9555){if(_0x4d9555){var _0x1649be=_0x4d9555['users'];var _0x1c7ed2=![];var _0xcddb52=null;if(_0x5985fc[_0xe309('0x18')]!==null){logger[_0xe309('0xe')](_0xe309('0x19'),_0x5985fc[_0xe309('0x18')]);_0x5985fc[_0xe309('0x1a')]=_0x5985fc[_0xe309('0x18')];for(_0xcddb52=0x0;_0xcddb52<_0x1649be['length'];_0xcddb52++){if(_0x1649be[_0xcddb52][_0xe309('0x1b')]===_0x5985fc[_0xe309('0x18')]){_0x1c7ed2=!![];_0x5985fc['userId']=_0x1649be[_0xcddb52]['id'];logger[_0xe309('0xe')](_0xe309('0x1c'),_0x5985fc[_0xe309('0x1d')]);}}}if(!_0x1c7ed2){logger[_0xe309('0xe')](_0xe309('0x1e'));_0x5985fc[_0xe309('0x1a')]=_0x5985fc[_0xe309('0x10')];_0x5985fc[_0xe309('0x1d')]=_0x5985fc['adminId'];_0x1c7ed2=!![];}if(_0x1c7ed2){_0x7c0b11(_0x5985fc);}else{_0x2a918f({'statusCode':0x194,'message':_0xe309('0x13')});}}else{_0x2a918f({'statusCode':0x194,'message':_0xe309('0x13')});}})[_0xe309('0x14')](function(_0x1798d7){logger['error']('['+[_0x221546[_0xe309('0x15')]()]+']',_0x1798d7[_0xe309('0x16')]);_0x2a918f(_0x1798d7);});});};}function searchContact(_0x42df87,_0x51a5fd,_0x3c7685,_0x3e1a54){return new Promise(function(_0x315518,_0x42a1f1){var _0x24043f={'method':'GET','headers':_0x51a5fd,'uri':_0x42df87+_0xe309('0x1f')+_0x3e1a54+_0xe309('0x20')+_0x3c7685+_0xe309('0x21'),'json':!![]};rp(_0x24043f)[_0xe309('0xf')](function(_0x5b7026){var _0x5d4aa8=_[_0xe309('0x22')](_0x5b7026[_0xe309('0x23')]['contacts'],[_0x3c7685,_0x3e1a54]);_0x315518(_0x5d4aa8);})['catch'](function(_0xce4ebf){_0x42a1f1(_0xce4ebf);});});}function getEndUser(_0x19aa09,_0x3b4b72,_0x2cd0b9){return function(_0x100cf7){return new Promise(function(_0x2d527a,_0x5c1566){logger[_0xe309('0xe')](util[_0xe309('0x24')]('Searching\x20contact\x20with\x20phone\x20%s',_0x100cf7[_0xe309('0x25')]));searchContact(_0x2cd0b9,_0x3b4b72,_0xe309('0x26'),_0x100cf7[_0xe309('0x25')])[_0xe309('0xf')](function(_0x5e69a7){if(_0x5e69a7){_0x100cf7[_0xe309('0x27')]=_0x5e69a7['id'];logger[_0xe309('0xe')](_0xe309('0x28'),_0x100cf7[_0xe309('0x27')]);return _0x2d527a(_0x100cf7);}searchContact(_0x2cd0b9,_0x3b4b72,'work_number',_0x100cf7[_0xe309('0x25')])[_0xe309('0xf')](function(_0x1efa2b){if(_0x1efa2b){_0x100cf7['endUserId']=_0x1efa2b['id'];logger[_0xe309('0xe')]('Contact\x20found\x20work_number:',_0x100cf7[_0xe309('0x27')]);return _0x2d527a(_0x100cf7);}createEndUser(_0x100cf7,_0x19aa09,_0x3b4b72,_0x2cd0b9)[_0xe309('0xf')](function(_0x4b7c45){logger[_0xe309('0xe')](_0xe309('0x29'),_0x4b7c45['endUserId']);return _0x2d527a(_0x4b7c45);});});})['catch'](function(_0x860199){_0x5c1566(_0x860199);});});};}function createEndUser(_0xc27526,_0x4a87cf,_0x30862f,_0xfb8dab){return new Promise(function(_0x22d099,_0x2e2986){var _0x25da23=moment();var _0x4eabc6={'method':_0xe309('0x2a'),'uri':_0xfb8dab+_0xe309('0x2b'),'body':{'contact':{'last_name':_0xc27526[_0xe309('0x25')],'work_number':_0xc27526[_0xe309('0x25')],'mobile_number':_0xc27526['contactNumber'],'owner_id':_0xc27526[_0xe309('0x1d')],'created_at':_0x25da23[_0xe309('0x24')](_0xe309('0x2c'))}},'json':!![]};_0x4eabc6[_0xe309('0xd')]=_0x30862f;logger['info'](_0xe309('0x2d'));return rp(_0x4eabc6)[_0xe309('0xf')](function(_0x371994){_0xc27526[_0xe309('0x27')]=_0x371994[_0xe309('0x2e')]['id'];_0x22d099(_0xc27526);})[_0xe309('0x14')](function(_0x2c8d13){logger[_0xe309('0x2f')]('['+[_0x4a87cf[_0xe309('0x15')]()]+']',_0x2c8d13[_0xe309('0x16')]);_0x2e2986(_0x2c8d13['message']);});});}function getTicket(_0x496cb2,_0x5672a9,_0x509268,_0x1a631b,_0x38bf8e){return function(_0x2bee54){logger[_0xe309('0xe')](_0xe309('0x30'));return new Promise(function(_0x1ab2fd,_0x480492){var _0x3910c5=moment();var _0x15a546={'method':'POST','uri':_0x509268+_0xe309('0x31'),'body':{'title':intUtil['getString'](_0x1a631b[_0xe309('0x32')],_0x38bf8e,'\x20'),'description':intUtil['getString'](_0x1a631b[_0xe309('0x33')],_0x38bf8e,'\x0a'),'owner_id':_0x2bee54[_0xe309('0x1d')],'creater_id':_0x2bee54[_0xe309('0x12')],'targetable_id':_0x2bee54[_0xe309('0x27')],'targetable_type':_0xe309('0x34'),'created_at':_0x3910c5[_0xe309('0x24')](_0xe309('0x2c')),'due_date':_0x3910c5[_0xe309('0x35')](0x1e,'m')[_0xe309('0x24')](_0xe309('0x2c'))},'json':!![]};_0x15a546[_0xe309('0xd')]=_0x5672a9;return rp(_0x15a546)[_0xe309('0xf')](function(_0x40ccd2){_0x2bee54['ticketId']=_0x40ccd2[_0xe309('0x36')]['id'];logger[_0xe309('0xe')](_0xe309('0x37'),_0x2bee54[_0xe309('0x38')]);_0x1ab2fd(_0x2bee54);})['catch'](function(_0x2e0a07){logger[_0xe309('0x2f')]('['+[_0x496cb2['toUpperCase']()]+']',_0x2e0a07[_0xe309('0x16')]);_0x480492(_0x2e0a07[_0xe309('0x16')]);});});};}function emit(_0x3cc3f5,_0x325208,_0x25db24){var _0x45bb4b={'uri':_0x3cc3f5+_0xe309('0x39')+_0x25db24};io['to'](util[_0xe309('0x24')](_0xe309('0x3a'),_0x325208))[_0xe309('0x3b')]('trigger:browser:url',_0x45bb4b);}exports['cs']=function(_0x3a3c25,_0x475799,_0x23186d,_0x30383b,_0x4c6719,_0x4fe69,_0x1bba32,_0x56b3c0){var _0xdf0871={};var _0x438093={'Authorization':_0xe309('0x3c')+_0x4fe69['apiKey']};_0x4fe69[_0xe309('0x3d')]=intUtil[_0xe309('0x3e')](_0x4fe69['remoteUri']);_0x4fe69['serverUrl']=intUtil['stripTrailingSlash'](_0x4fe69['serverUrl']);var _0x116ae6=_0x4fe69[_0xe309('0x3d')];_0xdf0871[_0xe309('0x10')]=_0x4fe69[_0xe309('0x3f')];_0xdf0871[_0xe309('0x25')]=_0x56b3c0[_0xe309('0x40')]()===_0xe309('0x41')?_0x475799[_0xe309('0x42')]:_0x475799['calleridnum'];_0xdf0871[_0xe309('0x43')]=_0x56b3c0===_0xe309('0x44')||_0x56b3c0===_0xe309('0x45')?_0xe309('0x44'):_0xe309('0x41');if(_0x30383b){_0xdf0871[_0xe309('0x18')]=_0x30383b[_0xe309('0x1b')];}var _0x1af942=util[_0xe309('0x24')](_0xe309('0x46'),_0x4fe69['serverUrl'],_0x475799[_0xe309('0x47')],md5(_0x475799[_0xe309('0x47')]));if(_0x4c6719&&_0x4c6719[_0xe309('0x48')]){logger[_0xe309('0xe')](_0xe309('0x49'),_0xe309('0x4a'));_0x475799[_0xe309('0x4b')]=_0x1af942;}else if(_0x1bba32&&_0x1bba32[_0xe309('0x4c')]&&_0x1bba32[_0xe309('0x4c')]!==_0xe309('0x4d')){logger[_0xe309('0xe')]('[OUTBOUND]','Recording\x20is\x20enabled!');_0x475799['recordingURL']=_0x1af942;}else{_0x475799[_0xe309('0x4b')]='';}return getAdminId(_0xdf0871,_0x56b3c0,_0x438093,_0x116ae6)[_0xe309('0xf')](getUser(_0x56b3c0,_0x438093,_0x116ae6))[_0xe309('0xf')](getEndUser(_0x56b3c0,_0x438093,_0x116ae6))[_0xe309('0xf')](getTicket(_0x56b3c0,_0x438093,_0x116ae6,_0x23186d,_0x475799))['then'](function(_0x4a85f9){logger[_0xe309('0xe')](util[_0xe309('0x24')](_0xe309('0x4e'),_0x4a85f9['ticketId']));if(_0x56b3c0!=='unmanaged'){logger[_0xe309('0xe')]('['+[_0x56b3c0[_0xe309('0x15')]()]+']','Displaying\x20task\x20in\x20a\x20new\x20tab...');emit(_0x116ae6,_0x30383b[_0xe309('0x4f')],_0x4a85f9[_0xe309('0x27')]);}})['catch'](function(_0x3e7ba7){logger['error']('['+[_0x56b3c0[_0xe309('0x15')]()]+_0xe309('0x50'),util[_0xe309('0x51')](_0x3e7ba7,{'showHidden':!![],'depth':null}));});};