0d821a920e3c76c8df685e1cfab43262b4e1312f
[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 _0xbc78=['POST','/api/contacts','format','Contact\x20not\x20found,\x20creating\x20a\x20new\x20contact','Creating\x20new\x20task','/api/tasks','getString','Subjects','Descriptions','Contact','add','ticketId','task','Task\x20created:','emit','trigger:browser:url','apiKey','remoteUri','stripTrailingSlash','serverUrl','username','outbound','unmanaged','%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s','uniqueid','[QUEUE]','Recording\x20is\x20enabled!','recordingURL','recordingFormat','none','[OUTBOUND]','The\x20ticket\x20ID\x20is:\x20%s','Displaying\x20task\x20in\x20a\x20new\x20tab...','name',']\x20Error\x20running\x20Freshsales\x20integration','inspect','md5','util','moment','bluebird','ioredis','./util','../../../../config/environment','../../../../config/logger','freshsales','redis','socket.io-emitter','GET','/api/selector/owners','headers','Getting\x20admin\x20information','then','users','length','email','adminId','Requested\x20Resource\x20not\x20Found','error','toUpperCase','message','agentEmail','info','userEmail','userId','Admin\x20selected','adminEmail','catch','/api/lookup?q=','&entities=contact','find','contacts','Searching\x20contact\x20with\x20phone\x20%s','contactNumber','endUserId','Contact\x20found\x20mobile_number:','Contact\x20found\x20work_number:','Contact\x20created:'];(function(_0x1fc122,_0x5e2c17){var _0xa2cb13=function(_0x263b13){while(--_0x263b13){_0x1fc122['push'](_0x1fc122['shift']());}};_0xa2cb13(++_0x5e2c17);}(_0xbc78,0x1a5));var _0x8bc7=function(_0x5927f3,_0x3a6e74){_0x5927f3=_0x5927f3-0x0;var _0x10f5ab=_0xbc78[_0x5927f3];return _0x10f5ab;};'use strict';var _=require('lodash');var rp=require('request-promise');var md5=require(_0x8bc7('0x0'));var util=require(_0x8bc7('0x1'));var moment=require(_0x8bc7('0x2'));var Promise=require(_0x8bc7('0x3'));var Redis=require(_0x8bc7('0x4'));var intUtil=require(_0x8bc7('0x5'));var config=require(_0x8bc7('0x6'));var logger=require(_0x8bc7('0x7'))(_0x8bc7('0x8'));config[_0x8bc7('0x9')]=_['defaults'](config[_0x8bc7('0x9')],{'host':'localhost','port':0x18eb});var io=require(_0x8bc7('0xa'))(new Redis(config[_0x8bc7('0x9')]));function getAdminId(_0x5c3538,_0x568e4b,_0x3f1ef7,_0x385d90){return new Promise(function(_0x29cd06,_0x2293ef){var _0x49f552=![];var _0x42dcd6={'method':_0x8bc7('0xb'),'uri':_0x385d90+_0x8bc7('0xc'),'json':!![]};_0x42dcd6[_0x8bc7('0xd')]=_0x3f1ef7;logger['info'](_0x8bc7('0xe'));return rp(_0x42dcd6)[_0x8bc7('0xf')](function(_0x2ca53d){if(_0x2ca53d){var _0x26b297=_0x2ca53d[_0x8bc7('0x10')];var _0x32e10a=null;for(_0x32e10a=0x0;_0x32e10a<_0x26b297[_0x8bc7('0x11')];_0x32e10a++){if(_0x26b297[_0x32e10a][_0x8bc7('0x12')]===_0x5c3538['adminEmail']){_0x5c3538[_0x8bc7('0x13')]=_0x26b297[_0x32e10a]['id'];_0x49f552=!![];}}if(_0x49f552){logger['info']('Admin\x20found:',_0x5c3538[_0x8bc7('0x13')]);_0x29cd06(_0x5c3538);}else{_0x2293ef({'statusCode':0x194,'message':_0x8bc7('0x14')});}}else{_0x2293ef({'statusCode':0x194,'message':_0x8bc7('0x14')});}})['catch'](function(_0x336642){logger[_0x8bc7('0x15')]('['+[_0x568e4b[_0x8bc7('0x16')]()]+']',_0x336642[_0x8bc7('0x17')]);_0x2293ef(_0x336642);});});}function getUser(_0x56f2dc,_0x5aa77e,_0x3bf1ba){return function(_0x35e189){return new Promise(function(_0x492320,_0x5096c7){var _0x30fe5f={'method':_0x8bc7('0xb'),'uri':_0x3bf1ba+_0x8bc7('0xc'),'json':!![]};_0x30fe5f['headers']=_0x5aa77e;logger['info']('Getting\x20user\x20list');return rp(_0x30fe5f)['then'](function(_0x9bad4c){if(_0x9bad4c){var _0x2bf3c8=_0x9bad4c[_0x8bc7('0x10')];var _0x171fec=![];var _0x55269a=null;if(_0x35e189[_0x8bc7('0x18')]!==null){logger[_0x8bc7('0x19')]('Getting\x20user\x20with\x20email',_0x35e189[_0x8bc7('0x18')]);_0x35e189[_0x8bc7('0x1a')]=_0x35e189[_0x8bc7('0x18')];for(_0x55269a=0x0;_0x55269a<_0x2bf3c8[_0x8bc7('0x11')];_0x55269a++){if(_0x2bf3c8[_0x55269a][_0x8bc7('0x12')]===_0x35e189[_0x8bc7('0x18')]){_0x171fec=!![];_0x35e189[_0x8bc7('0x1b')]=_0x2bf3c8[_0x55269a]['id'];logger[_0x8bc7('0x19')]('User\x20found:',_0x35e189[_0x8bc7('0x1b')]);}}}if(!_0x171fec){logger[_0x8bc7('0x19')](_0x8bc7('0x1c'));_0x35e189[_0x8bc7('0x1a')]=_0x35e189[_0x8bc7('0x1d')];_0x35e189['userId']=_0x35e189[_0x8bc7('0x13')];_0x171fec=!![];}if(_0x171fec){_0x492320(_0x35e189);}else{_0x5096c7({'statusCode':0x194,'message':_0x8bc7('0x14')});}}else{_0x5096c7({'statusCode':0x194,'message':_0x8bc7('0x14')});}})[_0x8bc7('0x1e')](function(_0x5f2820){logger[_0x8bc7('0x15')]('['+[_0x56f2dc[_0x8bc7('0x16')]()]+']',_0x5f2820[_0x8bc7('0x17')]);_0x5096c7(_0x5f2820);});});};}function searchContact(_0x72fda1,_0x3f68c7,_0x1c95b7,_0x43c74e){return new Promise(function(_0x1c97bd,_0x4a9005){var _0xd931f0={'method':'GET','headers':_0x3f68c7,'uri':_0x72fda1+_0x8bc7('0x1f')+_0x43c74e+'&f='+_0x1c95b7+_0x8bc7('0x20'),'json':!![]};rp(_0xd931f0)[_0x8bc7('0xf')](function(_0x59c39f){var _0x4acd84=_[_0x8bc7('0x21')](_0x59c39f[_0x8bc7('0x22')][_0x8bc7('0x22')],[_0x1c95b7,_0x43c74e]);_0x1c97bd(_0x4acd84);})[_0x8bc7('0x1e')](function(_0x46dc83){_0x4a9005(_0x46dc83);});});}function getEndUser(_0x4e1492,_0x3a6498,_0xacdd9c){return function(_0x381713){return new Promise(function(_0x134dcc,_0x36a58a){logger[_0x8bc7('0x19')](util['format'](_0x8bc7('0x23'),_0x381713['contactNumber']));searchContact(_0xacdd9c,_0x3a6498,'mobile_number',_0x381713[_0x8bc7('0x24')])[_0x8bc7('0xf')](function(_0x19bed5){if(_0x19bed5){_0x381713[_0x8bc7('0x25')]=_0x19bed5['id'];logger[_0x8bc7('0x19')](_0x8bc7('0x26'),_0x381713[_0x8bc7('0x25')]);return _0x134dcc(_0x381713);}searchContact(_0xacdd9c,_0x3a6498,'work_number',_0x381713[_0x8bc7('0x24')])[_0x8bc7('0xf')](function(_0x4a2d41){if(_0x4a2d41){_0x381713[_0x8bc7('0x25')]=_0x4a2d41['id'];logger[_0x8bc7('0x19')](_0x8bc7('0x27'),_0x381713['endUserId']);return _0x134dcc(_0x381713);}createEndUser(_0x381713,_0x4e1492,_0x3a6498,_0xacdd9c)[_0x8bc7('0xf')](function(_0x24f297){logger[_0x8bc7('0x19')](_0x8bc7('0x28'),_0x24f297['endUserId']);return _0x134dcc(_0x24f297);});});})[_0x8bc7('0x1e')](function(_0x14774e){_0x36a58a(_0x14774e);});});};}function createEndUser(_0xa93107,_0x54a795,_0x70555e,_0x593fbd){return new Promise(function(_0x1db648,_0x1d283d){var _0x2925fe=moment();var _0x35f46c={'method':_0x8bc7('0x29'),'uri':_0x593fbd+_0x8bc7('0x2a'),'body':{'contact':{'last_name':_0xa93107[_0x8bc7('0x24')],'work_number':_0xa93107[_0x8bc7('0x24')],'mobile_number':_0xa93107[_0x8bc7('0x24')],'owner_id':_0xa93107[_0x8bc7('0x1b')],'created_at':_0x2925fe[_0x8bc7('0x2b')]('YYYY-MM-DDTHH:mm:ss')}},'json':!![]};_0x35f46c['headers']=_0x70555e;logger[_0x8bc7('0x19')](_0x8bc7('0x2c'));return rp(_0x35f46c)[_0x8bc7('0xf')](function(_0x397454){_0xa93107[_0x8bc7('0x25')]=_0x397454['contact']['id'];_0x1db648(_0xa93107);})[_0x8bc7('0x1e')](function(_0x3c23d2){logger[_0x8bc7('0x15')]('['+[_0x54a795['toUpperCase']()]+']',_0x3c23d2[_0x8bc7('0x17')]);_0x1d283d(_0x3c23d2[_0x8bc7('0x17')]);});});}function getTicket(_0x5213f4,_0x53762e,_0x4dd2d5,_0x340f09,_0x4ede9d){return function(_0x307b51){logger[_0x8bc7('0x19')](_0x8bc7('0x2d'));return new Promise(function(_0x31acd1,_0x1bb855){var _0x2989f2=moment();var _0x4edb74={'method':'POST','uri':_0x4dd2d5+_0x8bc7('0x2e'),'body':{'title':intUtil[_0x8bc7('0x2f')](_0x340f09[_0x8bc7('0x30')],_0x4ede9d,'\x20'),'description':intUtil[_0x8bc7('0x2f')](_0x340f09[_0x8bc7('0x31')],_0x4ede9d,'\x0a'),'owner_id':_0x307b51[_0x8bc7('0x1b')],'creater_id':_0x307b51[_0x8bc7('0x13')],'targetable_id':_0x307b51[_0x8bc7('0x25')],'targetable_type':_0x8bc7('0x32'),'created_at':_0x2989f2['format']('YYYY-MM-DDTHH:mm:ss'),'due_date':_0x2989f2[_0x8bc7('0x33')](0x1e,'m')[_0x8bc7('0x2b')]('YYYY-MM-DDTHH:mm:ss')},'json':!![]};_0x4edb74[_0x8bc7('0xd')]=_0x53762e;return rp(_0x4edb74)[_0x8bc7('0xf')](function(_0x32f495){_0x307b51[_0x8bc7('0x34')]=_0x32f495[_0x8bc7('0x35')]['id'];logger[_0x8bc7('0x19')](_0x8bc7('0x36'),_0x307b51[_0x8bc7('0x34')]);_0x31acd1(_0x307b51);})['catch'](function(_0x4e3f33){logger['error']('['+[_0x5213f4[_0x8bc7('0x16')]()]+']',_0x4e3f33[_0x8bc7('0x17')]);_0x1bb855(_0x4e3f33[_0x8bc7('0x17')]);});});};}function emit(_0x224221,_0x38ab04,_0x26a46e){var _0xe28d68={'uri':_0x224221+'/contacts/'+_0x26a46e};io['to'](util[_0x8bc7('0x2b')]('user:%s',_0x38ab04))[_0x8bc7('0x37')](_0x8bc7('0x38'),_0xe28d68);}exports['cs']=function(_0x5902aa,_0x3927d1,_0x1989af,_0x383fc4,_0x55c829,_0x33a662,_0x176ed3,_0x563ad1){var _0x66a701={};var _0x4e5efa={'Authorization':'Token\x20token='+_0x33a662[_0x8bc7('0x39')]};_0x33a662[_0x8bc7('0x3a')]=intUtil[_0x8bc7('0x3b')](_0x33a662[_0x8bc7('0x3a')]);_0x33a662[_0x8bc7('0x3c')]=intUtil[_0x8bc7('0x3b')](_0x33a662[_0x8bc7('0x3c')]);var _0x1007b5=_0x33a662[_0x8bc7('0x3a')];_0x66a701[_0x8bc7('0x1d')]=_0x33a662[_0x8bc7('0x3d')];_0x66a701['contactNumber']=_0x563ad1['toLowerCase']()===_0x8bc7('0x3e')?_0x3927d1['destcalleridnum']:_0x3927d1['calleridnum'];_0x66a701['callDirection']=_0x563ad1==='queue'||_0x563ad1===_0x8bc7('0x3f')?'queue':_0x8bc7('0x3e');if(_0x383fc4){_0x66a701[_0x8bc7('0x18')]=_0x383fc4[_0x8bc7('0x12')];}var _0x41f7d5=util[_0x8bc7('0x2b')](_0x8bc7('0x40'),_0x33a662[_0x8bc7('0x3c')],_0x3927d1[_0x8bc7('0x41')],md5(_0x3927d1[_0x8bc7('0x41')]));if(_0x55c829&&_0x55c829['monitor_format']){logger['info'](_0x8bc7('0x42'),_0x8bc7('0x43'));_0x3927d1[_0x8bc7('0x44')]=_0x41f7d5;}else if(_0x176ed3&&_0x176ed3[_0x8bc7('0x45')]&&_0x176ed3['recordingFormat']!==_0x8bc7('0x46')){logger[_0x8bc7('0x19')](_0x8bc7('0x47'),_0x8bc7('0x43'));_0x3927d1[_0x8bc7('0x44')]=_0x41f7d5;}else{_0x3927d1['recordingURL']='';}return getAdminId(_0x66a701,_0x563ad1,_0x4e5efa,_0x1007b5)[_0x8bc7('0xf')](getUser(_0x563ad1,_0x4e5efa,_0x1007b5))['then'](getEndUser(_0x563ad1,_0x4e5efa,_0x1007b5))[_0x8bc7('0xf')](getTicket(_0x563ad1,_0x4e5efa,_0x1007b5,_0x1989af,_0x3927d1))[_0x8bc7('0xf')](function(_0x279d12){logger[_0x8bc7('0x19')](util[_0x8bc7('0x2b')](_0x8bc7('0x48'),_0x279d12['ticketId']));if(_0x563ad1!=='unmanaged'){logger[_0x8bc7('0x19')]('['+[_0x563ad1[_0x8bc7('0x16')]()]+']',_0x8bc7('0x49'));emit(_0x1007b5,_0x383fc4[_0x8bc7('0x4a')],_0x279d12[_0x8bc7('0x25')]);}})[_0x8bc7('0x1e')](function(_0x4f8d7e){logger[_0x8bc7('0x15')]('['+[_0x563ad1[_0x8bc7('0x16')]()]+_0x8bc7('0x4b'),util[_0x8bc7('0x4c')](_0x4f8d7e,{'showHidden':!![],'depth':null}));});};