2dfe32a9d54fab70faf0703eebbb7ae37fe2d834
[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 _0xdcc4=['/api/lookup?q=','&f=','&entities=contact','find','contacts','format','Searching\x20contact\x20with\x20phone\x20%s','contactNumber','mobile_number','endUserId','Contact\x20found\x20mobile_number:','work_number','Contact\x20created:','POST','contact','toUpperCase','/api/tasks','getString','Subjects','Descriptions','Contact','YYYY-MM-DDTHH:mm:ss','add','ticketId','task','Task\x20created:','emit','trigger:browser:url','stripTrailingSlash','remoteUri','serverUrl','username','toLowerCase','outbound','destcalleridnum','calleridnum','callDirection','queue','unmanaged','%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s','monitor_format','[QUEUE]','Recording\x20is\x20enabled!','recordingURL','recordingFormat','none','The\x20ticket\x20ID\x20is:\x20%s','name',']\x20Error\x20running\x20Freshsales\x20integration','lodash','request-promise','md5','util','../../../../config/environment','../../../../config/logger','freshsales','redis','defaults','GET','/api/selector/owners','headers','info','Getting\x20admin\x20information','then','users','length','email','adminEmail','adminId','Admin\x20found:','error','message','Getting\x20user\x20list','agentEmail','Getting\x20user\x20with\x20email','userEmail','userId','Requested\x20Resource\x20not\x20Found','catch'];(function(_0x447b23,_0x49c577){var _0x2f37c7=function(_0x19d049){while(--_0x19d049){_0x447b23['push'](_0x447b23['shift']());}};_0x2f37c7(++_0x49c577);}(_0xdcc4,0x16d));var _0x4dcc=function(_0x2fbc55,_0x537fd0){_0x2fbc55=_0x2fbc55-0x0;var _0x1c82ec=_0xdcc4[_0x2fbc55];return _0x1c82ec;};'use strict';var _=require(_0x4dcc('0x0'));var rp=require(_0x4dcc('0x1'));var md5=require(_0x4dcc('0x2'));var util=require(_0x4dcc('0x3'));var moment=require('moment');var Promise=require('bluebird');var Redis=require('ioredis');var intUtil=require('./util');var config=require(_0x4dcc('0x4'));var logger=require(_0x4dcc('0x5'))(_0x4dcc('0x6'));config[_0x4dcc('0x7')]=_[_0x4dcc('0x8')](config[_0x4dcc('0x7')],{'host':'localhost','port':0x18eb});var io=require('socket.io-emitter')(new Redis(config[_0x4dcc('0x7')]));function getAdminId(_0x2f4641,_0x55937c,_0x38e286,_0xee92a3){return new Promise(function(_0x43b7c2,_0x4459ea){var _0x25bb1b=![];var _0xa7a05c={'method':_0x4dcc('0x9'),'uri':_0xee92a3+_0x4dcc('0xa'),'json':!![]};_0xa7a05c[_0x4dcc('0xb')]=_0x38e286;logger[_0x4dcc('0xc')](_0x4dcc('0xd'));return rp(_0xa7a05c)[_0x4dcc('0xe')](function(_0x19156b){if(_0x19156b){var _0x4ae4a0=_0x19156b[_0x4dcc('0xf')];var _0x2d97da=null;for(_0x2d97da=0x0;_0x2d97da<_0x4ae4a0[_0x4dcc('0x10')];_0x2d97da++){if(_0x4ae4a0[_0x2d97da][_0x4dcc('0x11')]===_0x2f4641[_0x4dcc('0x12')]){_0x2f4641[_0x4dcc('0x13')]=_0x4ae4a0[_0x2d97da]['id'];_0x25bb1b=!![];}}if(_0x25bb1b){logger['info'](_0x4dcc('0x14'),_0x2f4641[_0x4dcc('0x13')]);_0x43b7c2(_0x2f4641);}else{_0x4459ea({'statusCode':0x194,'message':'Requested\x20Resource\x20not\x20Found'});}}else{_0x4459ea({'statusCode':0x194,'message':'Requested\x20Resource\x20not\x20Found'});}})['catch'](function(_0xf3bfe4){logger[_0x4dcc('0x15')]('['+[_0x55937c['toUpperCase']()]+']',_0xf3bfe4[_0x4dcc('0x16')]);_0x4459ea(_0xf3bfe4);});});}function getUser(_0x54946c,_0x4b420e,_0x142a33){return function(_0xb8ae2f){return new Promise(function(_0x57a9a7,_0x3f7832){var _0x3672e2={'method':'GET','uri':_0x142a33+'/api/selector/owners','json':!![]};_0x3672e2['headers']=_0x4b420e;logger[_0x4dcc('0xc')](_0x4dcc('0x17'));return rp(_0x3672e2)[_0x4dcc('0xe')](function(_0xd51e6b){if(_0xd51e6b){var _0x51d316=_0xd51e6b['users'];var _0x57a2d1=![];var _0x388883=null;if(_0xb8ae2f[_0x4dcc('0x18')]!==null){logger['info'](_0x4dcc('0x19'),_0xb8ae2f[_0x4dcc('0x18')]);_0xb8ae2f[_0x4dcc('0x1a')]=_0xb8ae2f[_0x4dcc('0x18')];for(_0x388883=0x0;_0x388883<_0x51d316[_0x4dcc('0x10')];_0x388883++){if(_0x51d316[_0x388883][_0x4dcc('0x11')]===_0xb8ae2f[_0x4dcc('0x18')]){_0x57a2d1=!![];_0xb8ae2f['userId']=_0x51d316[_0x388883]['id'];logger[_0x4dcc('0xc')]('User\x20found:',_0xb8ae2f['userId']);}}}if(!_0x57a2d1){logger['info']('Admin\x20selected');_0xb8ae2f['userEmail']=_0xb8ae2f[_0x4dcc('0x12')];_0xb8ae2f[_0x4dcc('0x1b')]=_0xb8ae2f[_0x4dcc('0x13')];_0x57a2d1=!![];}if(_0x57a2d1){_0x57a9a7(_0xb8ae2f);}else{_0x3f7832({'statusCode':0x194,'message':_0x4dcc('0x1c')});}}else{_0x3f7832({'statusCode':0x194,'message':_0x4dcc('0x1c')});}})[_0x4dcc('0x1d')](function(_0xa2c663){logger[_0x4dcc('0x15')]('['+[_0x54946c['toUpperCase']()]+']',_0xa2c663[_0x4dcc('0x16')]);_0x3f7832(_0xa2c663);});});};}function searchContact(_0x27a37f,_0x2b4c4c,_0x1e1553,_0x253fec){return new Promise(function(_0xc83a19,_0x16181c){var _0xbb1a50={'method':_0x4dcc('0x9'),'headers':_0x2b4c4c,'uri':_0x27a37f+_0x4dcc('0x1e')+_0x253fec+_0x4dcc('0x1f')+_0x1e1553+_0x4dcc('0x20'),'json':!![]};rp(_0xbb1a50)[_0x4dcc('0xe')](function(_0xb5880b){var _0x42f0b2=_[_0x4dcc('0x21')](_0xb5880b[_0x4dcc('0x22')][_0x4dcc('0x22')],[_0x1e1553,_0x253fec]);_0xc83a19(_0x42f0b2);})[_0x4dcc('0x1d')](function(_0x16733d){_0x16181c(_0x16733d);});});}function getEndUser(_0x5acd7a,_0x4b00e9,_0x14ae35){return function(_0x2626a4){return new Promise(function(_0x1728dc,_0x3fee7d){logger[_0x4dcc('0xc')](util[_0x4dcc('0x23')](_0x4dcc('0x24'),_0x2626a4[_0x4dcc('0x25')]));searchContact(_0x14ae35,_0x4b00e9,_0x4dcc('0x26'),_0x2626a4['contactNumber'])[_0x4dcc('0xe')](function(_0x2eb722){if(_0x2eb722){_0x2626a4[_0x4dcc('0x27')]=_0x2eb722['id'];logger['info'](_0x4dcc('0x28'),_0x2626a4[_0x4dcc('0x27')]);return _0x1728dc(_0x2626a4);}searchContact(_0x14ae35,_0x4b00e9,_0x4dcc('0x29'),_0x2626a4[_0x4dcc('0x25')])[_0x4dcc('0xe')](function(_0x2b334c){if(_0x2b334c){_0x2626a4[_0x4dcc('0x27')]=_0x2b334c['id'];logger[_0x4dcc('0xc')]('Contact\x20found\x20work_number:',_0x2626a4['endUserId']);return _0x1728dc(_0x2626a4);}createEndUser(_0x2626a4,_0x5acd7a,_0x4b00e9,_0x14ae35)[_0x4dcc('0xe')](function(_0x14e1f3){logger[_0x4dcc('0xc')](_0x4dcc('0x2a'),_0x14e1f3['endUserId']);return _0x1728dc(_0x14e1f3);});});})[_0x4dcc('0x1d')](function(_0x487bbd){_0x3fee7d(_0x487bbd);});});};}function createEndUser(_0x35367d,_0x42b434,_0x39bb01,_0x3953cd){return new Promise(function(_0x3a7305,_0x23a192){var _0x425869=moment();var _0x41bbe5={'method':_0x4dcc('0x2b'),'uri':_0x3953cd+'/api/contacts','body':{'contact':{'last_name':_0x35367d[_0x4dcc('0x25')],'work_number':_0x35367d[_0x4dcc('0x25')],'mobile_number':_0x35367d['contactNumber'],'owner_id':_0x35367d[_0x4dcc('0x1b')],'created_at':_0x425869[_0x4dcc('0x23')]('YYYY-MM-DDTHH:mm:ss')}},'json':!![]};_0x41bbe5[_0x4dcc('0xb')]=_0x39bb01;logger[_0x4dcc('0xc')]('Contact\x20not\x20found,\x20creating\x20a\x20new\x20contact');return rp(_0x41bbe5)[_0x4dcc('0xe')](function(_0x4f8d5d){_0x35367d[_0x4dcc('0x27')]=_0x4f8d5d[_0x4dcc('0x2c')]['id'];_0x3a7305(_0x35367d);})[_0x4dcc('0x1d')](function(_0x8ee28c){logger[_0x4dcc('0x15')]('['+[_0x42b434[_0x4dcc('0x2d')]()]+']',_0x8ee28c['message']);_0x23a192(_0x8ee28c['message']);});});}function getTicket(_0x56a4e8,_0x59ebda,_0x5569f1,_0x27a5af,_0x3b2a45){return function(_0x1a6e03){logger[_0x4dcc('0xc')]('Creating\x20new\x20task');return new Promise(function(_0x1b0cb4,_0xa2666d){var _0x11fae7=moment();var _0xc6872c={'method':_0x4dcc('0x2b'),'uri':_0x5569f1+_0x4dcc('0x2e'),'body':{'title':intUtil[_0x4dcc('0x2f')](_0x27a5af[_0x4dcc('0x30')],_0x3b2a45,'\x20'),'description':intUtil[_0x4dcc('0x2f')](_0x27a5af[_0x4dcc('0x31')],_0x3b2a45,'\x0a'),'owner_id':_0x1a6e03['userId'],'creater_id':_0x1a6e03['adminId'],'targetable_id':_0x1a6e03[_0x4dcc('0x27')],'targetable_type':_0x4dcc('0x32'),'created_at':_0x11fae7[_0x4dcc('0x23')](_0x4dcc('0x33')),'due_date':_0x11fae7[_0x4dcc('0x34')](0x1e,'m')[_0x4dcc('0x23')](_0x4dcc('0x33'))},'json':!![]};_0xc6872c[_0x4dcc('0xb')]=_0x59ebda;return rp(_0xc6872c)['then'](function(_0x17f183){_0x1a6e03[_0x4dcc('0x35')]=_0x17f183[_0x4dcc('0x36')]['id'];logger[_0x4dcc('0xc')](_0x4dcc('0x37'),_0x1a6e03['ticketId']);_0x1b0cb4(_0x1a6e03);})['catch'](function(_0x4ee9ad){logger['error']('['+[_0x56a4e8[_0x4dcc('0x2d')]()]+']',_0x4ee9ad[_0x4dcc('0x16')]);_0xa2666d(_0x4ee9ad[_0x4dcc('0x16')]);});});};}function emit(_0x2b3186,_0x52cf17,_0x41f07c){var _0x52f87f={'uri':_0x2b3186+'/contacts/'+_0x41f07c};io['to'](util['format']('user:%s',_0x52cf17))[_0x4dcc('0x38')](_0x4dcc('0x39'),_0x52f87f);}exports['cs']=function(_0x1dc187,_0x4e88bc,_0x36f57a,_0x5df79b,_0x559168,_0x50842a,_0x3eef3f,_0x523298){var _0x413e50={};var _0x399072={'Authorization':'Token\x20token='+_0x50842a['apiKey']};_0x50842a['remoteUri']=intUtil[_0x4dcc('0x3a')](_0x50842a[_0x4dcc('0x3b')]);_0x50842a[_0x4dcc('0x3c')]=intUtil['stripTrailingSlash'](_0x50842a[_0x4dcc('0x3c')]);var _0x50b930=_0x50842a[_0x4dcc('0x3b')];_0x413e50['adminEmail']=_0x50842a[_0x4dcc('0x3d')];_0x413e50[_0x4dcc('0x25')]=_0x523298[_0x4dcc('0x3e')]()===_0x4dcc('0x3f')?_0x4e88bc[_0x4dcc('0x40')]:_0x4e88bc[_0x4dcc('0x41')];_0x413e50[_0x4dcc('0x42')]=_0x523298===_0x4dcc('0x43')||_0x523298===_0x4dcc('0x44')?_0x4dcc('0x43'):_0x4dcc('0x3f');if(_0x5df79b){_0x413e50[_0x4dcc('0x18')]=_0x5df79b[_0x4dcc('0x11')];}var _0x5eeafe=util['format'](_0x4dcc('0x45'),_0x50842a[_0x4dcc('0x3c')],_0x4e88bc['uniqueid'],md5(_0x4e88bc['uniqueid']));if(_0x559168&&_0x559168[_0x4dcc('0x46')]){logger['info'](_0x4dcc('0x47'),_0x4dcc('0x48'));_0x4e88bc[_0x4dcc('0x49')]=_0x5eeafe;}else if(_0x3eef3f&&_0x3eef3f[_0x4dcc('0x4a')]&&_0x3eef3f[_0x4dcc('0x4a')]!==_0x4dcc('0x4b')){logger[_0x4dcc('0xc')]('[OUTBOUND]',_0x4dcc('0x48'));_0x4e88bc[_0x4dcc('0x49')]=_0x5eeafe;}else{_0x4e88bc['recordingURL']='';}return getAdminId(_0x413e50,_0x523298,_0x399072,_0x50b930)[_0x4dcc('0xe')](getUser(_0x523298,_0x399072,_0x50b930))[_0x4dcc('0xe')](getEndUser(_0x523298,_0x399072,_0x50b930))[_0x4dcc('0xe')](getTicket(_0x523298,_0x399072,_0x50b930,_0x36f57a,_0x4e88bc))[_0x4dcc('0xe')](function(_0x463d81){logger[_0x4dcc('0xc')](util['format'](_0x4dcc('0x4c'),_0x463d81[_0x4dcc('0x35')]));if(_0x523298!==_0x4dcc('0x44')){logger[_0x4dcc('0xc')]('['+[_0x523298[_0x4dcc('0x2d')]()]+']','Displaying\x20task\x20in\x20a\x20new\x20tab...');emit(_0x50b930,_0x5df79b[_0x4dcc('0x4d')],_0x463d81['endUserId']);}})[_0x4dcc('0x1d')](function(_0x4a1cc0){logger[_0x4dcc('0x15')]('['+[_0x523298[_0x4dcc('0x2d')]()]+_0x4dcc('0x4e'),util['inspect'](_0x4a1cc0,{'showHidden':!![],'depth':null}));});};