f3715582bb76033ee7232ef08486bd3d74099918
[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 _0xe953=['trigger:browser:url','Token\x20token=','remoteUri','stripTrailingSlash','serverUrl','username','toLowerCase','outbound','destcalleridnum','queue','unmanaged','uniqueid','Recording\x20is\x20enabled!','recordingURL','recordingFormat','none','The\x20ticket\x20ID\x20is:\x20%s','Displaying\x20task\x20in\x20a\x20new\x20tab...',']\x20Error\x20running\x20Freshsales\x20integration','inspect','bluebird','ioredis','../../../../config/environment','freshsales','defaults','redis','localhost','socket.io-emitter','GET','headers','Getting\x20admin\x20information','then','users','email','adminEmail','info','Admin\x20found:','adminId','Requested\x20Resource\x20not\x20Found','catch','toUpperCase','message','/api/selector/owners','Getting\x20user\x20list','Getting\x20user\x20with\x20email','agentEmail','length','userId','User\x20found:','Admin\x20selected','userEmail','error','/api/lookup?q=','&f=','contacts','format','Searching\x20contact\x20with\x20phone\x20%s','mobile_number','contactNumber','endUserId','Contact\x20created:','POST','YYYY-MM-DDTHH:mm:ss','Contact\x20not\x20found,\x20creating\x20a\x20new\x20contact','contact','Creating\x20new\x20task','/api/tasks','getString','Descriptions','add','task','Task\x20created:','ticketId','user:%s'];(function(_0x563e9b,_0x1b82f4){var _0x25dc70=function(_0x3cd05e){while(--_0x3cd05e){_0x563e9b['push'](_0x563e9b['shift']());}};_0x25dc70(++_0x1b82f4);}(_0xe953,0x1d0));var _0x3e95=function(_0x58547b,_0x4b89c2){_0x58547b=_0x58547b-0x0;var _0x1d3fd2=_0xe953[_0x58547b];return _0x1d3fd2;};'use strict';var _=require('lodash');var rp=require('request-promise');var md5=require('md5');var util=require('util');var moment=require('moment');var Promise=require(_0x3e95('0x0'));var Redis=require(_0x3e95('0x1'));var intUtil=require('./util');var config=require(_0x3e95('0x2'));var logger=require('../../../../config/logger')(_0x3e95('0x3'));config['redis']=_[_0x3e95('0x4')](config[_0x3e95('0x5')],{'host':_0x3e95('0x6'),'port':0x18eb});var io=require(_0x3e95('0x7'))(new Redis(config[_0x3e95('0x5')]));function getAdminId(_0x5bb380,_0x38c27b,_0x21ac04,_0x1b6ce7){return new Promise(function(_0x5ae40a,_0xf019c5){var _0x12a756=![];var _0x35ed18={'method':_0x3e95('0x8'),'uri':_0x1b6ce7+'/api/selector/owners','json':!![]};_0x35ed18[_0x3e95('0x9')]=_0x21ac04;logger['info'](_0x3e95('0xa'));return rp(_0x35ed18)[_0x3e95('0xb')](function(_0x2ae756){if(_0x2ae756){var _0x3b2f1a=_0x2ae756[_0x3e95('0xc')];var _0x152671=null;for(_0x152671=0x0;_0x152671<_0x3b2f1a['length'];_0x152671++){if(_0x3b2f1a[_0x152671][_0x3e95('0xd')]===_0x5bb380[_0x3e95('0xe')]){_0x5bb380['adminId']=_0x3b2f1a[_0x152671]['id'];_0x12a756=!![];}}if(_0x12a756){logger[_0x3e95('0xf')](_0x3e95('0x10'),_0x5bb380[_0x3e95('0x11')]);_0x5ae40a(_0x5bb380);}else{_0xf019c5({'statusCode':0x194,'message':_0x3e95('0x12')});}}else{_0xf019c5({'statusCode':0x194,'message':'Requested\x20Resource\x20not\x20Found'});}})[_0x3e95('0x13')](function(_0xd1d345){logger['error']('['+[_0x38c27b[_0x3e95('0x14')]()]+']',_0xd1d345[_0x3e95('0x15')]);_0xf019c5(_0xd1d345);});});}function getUser(_0x12dec2,_0xc3be41,_0x5c8896){return function(_0x1490ec){return new Promise(function(_0x4d44c3,_0x3ecd6f){var _0x2f6a8c={'method':'GET','uri':_0x5c8896+_0x3e95('0x16'),'json':!![]};_0x2f6a8c[_0x3e95('0x9')]=_0xc3be41;logger[_0x3e95('0xf')](_0x3e95('0x17'));return rp(_0x2f6a8c)[_0x3e95('0xb')](function(_0x1da3c7){if(_0x1da3c7){var _0x177818=_0x1da3c7[_0x3e95('0xc')];var _0x5c7479=![];var _0x18a811=null;if(_0x1490ec['agentEmail']!==null){logger[_0x3e95('0xf')](_0x3e95('0x18'),_0x1490ec[_0x3e95('0x19')]);_0x1490ec['userEmail']=_0x1490ec[_0x3e95('0x19')];for(_0x18a811=0x0;_0x18a811<_0x177818[_0x3e95('0x1a')];_0x18a811++){if(_0x177818[_0x18a811][_0x3e95('0xd')]===_0x1490ec[_0x3e95('0x19')]){_0x5c7479=!![];_0x1490ec[_0x3e95('0x1b')]=_0x177818[_0x18a811]['id'];logger[_0x3e95('0xf')](_0x3e95('0x1c'),_0x1490ec[_0x3e95('0x1b')]);}}}if(!_0x5c7479){logger[_0x3e95('0xf')](_0x3e95('0x1d'));_0x1490ec[_0x3e95('0x1e')]=_0x1490ec[_0x3e95('0xe')];_0x1490ec[_0x3e95('0x1b')]=_0x1490ec[_0x3e95('0x11')];_0x5c7479=!![];}if(_0x5c7479){_0x4d44c3(_0x1490ec);}else{_0x3ecd6f({'statusCode':0x194,'message':_0x3e95('0x12')});}}else{_0x3ecd6f({'statusCode':0x194,'message':_0x3e95('0x12')});}})[_0x3e95('0x13')](function(_0x3f29a3){logger[_0x3e95('0x1f')]('['+[_0x12dec2[_0x3e95('0x14')]()]+']',_0x3f29a3[_0x3e95('0x15')]);_0x3ecd6f(_0x3f29a3);});});};}function searchContact(_0x70753d,_0x203699,_0x348cbb,_0x19ea9f){return new Promise(function(_0x51595b,_0x761f8b){var _0x38c2f5={'method':_0x3e95('0x8'),'headers':_0x203699,'uri':_0x70753d+_0x3e95('0x20')+_0x19ea9f+_0x3e95('0x21')+_0x348cbb+'&entities=contact','json':!![]};rp(_0x38c2f5)[_0x3e95('0xb')](function(_0x3397b3){var _0x5e1e51=_['find'](_0x3397b3[_0x3e95('0x22')]['contacts'],[_0x348cbb,_0x19ea9f]);_0x51595b(_0x5e1e51);})[_0x3e95('0x13')](function(_0x3d2cdb){_0x761f8b(_0x3d2cdb);});});}function getEndUser(_0x4ee356,_0x17690c,_0x4849de){return function(_0x1d3970){return new Promise(function(_0x575fb5,_0x367da3){logger[_0x3e95('0xf')](util[_0x3e95('0x23')](_0x3e95('0x24'),_0x1d3970['contactNumber']));searchContact(_0x4849de,_0x17690c,_0x3e95('0x25'),_0x1d3970[_0x3e95('0x26')])['then'](function(_0x255ac4){if(_0x255ac4){_0x1d3970[_0x3e95('0x27')]=_0x255ac4['id'];logger[_0x3e95('0xf')]('Contact\x20found\x20mobile_number:',_0x1d3970[_0x3e95('0x27')]);return _0x575fb5(_0x1d3970);}searchContact(_0x4849de,_0x17690c,'work_number',_0x1d3970['contactNumber'])[_0x3e95('0xb')](function(_0x50b257){if(_0x50b257){_0x1d3970[_0x3e95('0x27')]=_0x50b257['id'];logger['info']('Contact\x20found\x20work_number:',_0x1d3970['endUserId']);return _0x575fb5(_0x1d3970);}createEndUser(_0x1d3970,_0x4ee356,_0x17690c,_0x4849de)[_0x3e95('0xb')](function(_0x279abc){logger[_0x3e95('0xf')](_0x3e95('0x28'),_0x279abc[_0x3e95('0x27')]);return _0x575fb5(_0x279abc);});});})[_0x3e95('0x13')](function(_0x8b2f21){_0x367da3(_0x8b2f21);});});};}function createEndUser(_0xdd7c15,_0x7bba56,_0x3ca817,_0x18ac71){return new Promise(function(_0x31b556,_0x1feb3a){var _0x3e5b0f=moment();var _0x397ab9={'method':_0x3e95('0x29'),'uri':_0x18ac71+'/api/contacts','body':{'contact':{'last_name':_0xdd7c15[_0x3e95('0x26')],'work_number':_0xdd7c15['contactNumber'],'mobile_number':_0xdd7c15[_0x3e95('0x26')],'owner_id':_0xdd7c15[_0x3e95('0x1b')],'created_at':_0x3e5b0f['format'](_0x3e95('0x2a'))}},'json':!![]};_0x397ab9[_0x3e95('0x9')]=_0x3ca817;logger['info'](_0x3e95('0x2b'));return rp(_0x397ab9)[_0x3e95('0xb')](function(_0x3789c2){_0xdd7c15[_0x3e95('0x27')]=_0x3789c2[_0x3e95('0x2c')]['id'];_0x31b556(_0xdd7c15);})[_0x3e95('0x13')](function(_0x3dcc58){logger[_0x3e95('0x1f')]('['+[_0x7bba56[_0x3e95('0x14')]()]+']',_0x3dcc58['message']);_0x1feb3a(_0x3dcc58[_0x3e95('0x15')]);});});}function getTicket(_0x21797b,_0x3865ec,_0xbe80fe,_0x2cd62d,_0x288f90){return function(_0x464de9){logger['info'](_0x3e95('0x2d'));return new Promise(function(_0x288826,_0x1f300c){var _0x2a2b02=moment();var _0xe379db={'method':_0x3e95('0x29'),'uri':_0xbe80fe+_0x3e95('0x2e'),'body':{'title':intUtil[_0x3e95('0x2f')](_0x2cd62d['Subjects'],_0x288f90,'\x20'),'description':intUtil[_0x3e95('0x2f')](_0x2cd62d[_0x3e95('0x30')],_0x288f90,'\x0a'),'owner_id':_0x464de9[_0x3e95('0x1b')],'creater_id':_0x464de9[_0x3e95('0x11')],'targetable_id':_0x464de9[_0x3e95('0x27')],'targetable_type':'Contact','created_at':_0x2a2b02['format']('YYYY-MM-DDTHH:mm:ss'),'due_date':_0x2a2b02[_0x3e95('0x31')](0x1e,'m')['format']('YYYY-MM-DDTHH:mm:ss')},'json':!![]};_0xe379db[_0x3e95('0x9')]=_0x3865ec;return rp(_0xe379db)[_0x3e95('0xb')](function(_0x8099de){_0x464de9['ticketId']=_0x8099de[_0x3e95('0x32')]['id'];logger['info'](_0x3e95('0x33'),_0x464de9[_0x3e95('0x34')]);_0x288826(_0x464de9);})[_0x3e95('0x13')](function(_0x153528){logger[_0x3e95('0x1f')]('['+[_0x21797b['toUpperCase']()]+']',_0x153528[_0x3e95('0x15')]);_0x1f300c(_0x153528[_0x3e95('0x15')]);});});};}function emit(_0x1d8862,_0xbca246,_0x3fd4d3){var _0x4eafe6={'uri':_0x1d8862+'/contacts/'+_0x3fd4d3};io['to'](util[_0x3e95('0x23')](_0x3e95('0x35'),_0xbca246))['emit'](_0x3e95('0x36'),_0x4eafe6);}exports['cs']=function(_0x439ace,_0x388718,_0x5e8177,_0x1e27f9,_0x1323fd,_0x5a693e,_0x255b1f,_0xe13a52){var _0x1e5627={};var _0x173a4e={'Authorization':_0x3e95('0x37')+_0x5a693e['apiKey']};_0x5a693e[_0x3e95('0x38')]=intUtil[_0x3e95('0x39')](_0x5a693e[_0x3e95('0x38')]);_0x5a693e[_0x3e95('0x3a')]=intUtil['stripTrailingSlash'](_0x5a693e[_0x3e95('0x3a')]);var _0x4e4d8b=_0x5a693e['remoteUri'];_0x1e5627[_0x3e95('0xe')]=_0x5a693e[_0x3e95('0x3b')];_0x1e5627['contactNumber']=_0xe13a52[_0x3e95('0x3c')]()===_0x3e95('0x3d')?_0x388718[_0x3e95('0x3e')]:_0x388718['calleridnum'];_0x1e5627['callDirection']=_0xe13a52===_0x3e95('0x3f')||_0xe13a52===_0x3e95('0x40')?_0x3e95('0x3f'):'outbound';if(_0x1e27f9){_0x1e5627['agentEmail']=_0x1e27f9[_0x3e95('0xd')];}var _0x426cf6=util[_0x3e95('0x23')]('%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s',_0x5a693e[_0x3e95('0x3a')],_0x388718[_0x3e95('0x41')],md5(_0x388718['uniqueid']));if(_0x1323fd&&_0x1323fd['monitor_format']){logger[_0x3e95('0xf')]('[QUEUE]',_0x3e95('0x42'));_0x388718[_0x3e95('0x43')]=_0x426cf6;}else if(_0x255b1f&&_0x255b1f[_0x3e95('0x44')]&&_0x255b1f[_0x3e95('0x44')]!==_0x3e95('0x45')){logger['info']('[OUTBOUND]',_0x3e95('0x42'));_0x388718[_0x3e95('0x43')]=_0x426cf6;}else{_0x388718[_0x3e95('0x43')]='';}return getAdminId(_0x1e5627,_0xe13a52,_0x173a4e,_0x4e4d8b)[_0x3e95('0xb')](getUser(_0xe13a52,_0x173a4e,_0x4e4d8b))[_0x3e95('0xb')](getEndUser(_0xe13a52,_0x173a4e,_0x4e4d8b))['then'](getTicket(_0xe13a52,_0x173a4e,_0x4e4d8b,_0x5e8177,_0x388718))['then'](function(_0x416707){logger[_0x3e95('0xf')](util[_0x3e95('0x23')](_0x3e95('0x46'),_0x416707['ticketId']));if(_0xe13a52!==_0x3e95('0x40')){logger[_0x3e95('0xf')]('['+[_0xe13a52[_0x3e95('0x14')]()]+']',_0x3e95('0x47'));emit(_0x4e4d8b,_0x1e27f9['name'],_0x416707[_0x3e95('0x27')]);}})[_0x3e95('0x13')](function(_0xbbd1c8){logger[_0x3e95('0x1f')]('['+[_0xe13a52[_0x3e95('0x14')]()]+_0x3e95('0x48'),util[_0x3e95('0x49')](_0xbbd1c8,{'showHidden':!![],'depth':null}));});};