Built motion from commit (unavailable).|2.4.17
[motion2.git] / server / services / ami / trigger / integration / salesforce.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 _0xac6f=['[ABANDONED]\x20Ticket\x20%s\x20created.','[ABANDONED]\x20Integration\x20account\x20type\x20%s.','catch','[ABANDONED]','lodash','request-promise','moment','bluebird','jsforce','util','ioredis','./util','../../../../config/environment','../../../../config/logger','defaults','redis','socket.io-emitter','emit','Task','assign','utc','getString','Subjects','Descriptions','ticketType','Case','ContactId','create','isNil','validateEmail','sobject','User','find','then','info','Agent\x20%s\x20found.','resolve','Name','Agent\x20%s\x20not\x20found.\x20Switch\x20to\x20the\x20admin\x20account.','identity','user_id','display_name','moduleSearch','contact_lead','Contact','execute','length','Lead','contact','lead','Searching\x20enduser\x20with\x20phone\x20%s.\x20Serching\x20method\x20used\x20%s.','Phone','Enduser\x20with\x20phone\x20%s\x20found.','Enduser\x20with\x20phone\x20%s\x20not\x20found.','<unknown>','format','Caller\x20-\x20%s','moduleCreate','Enduser\x20with\x20phone\x20%s.\x20Creating\x20method\x20used\x20%s.','Company','No\x20Company','upperFirst','forEach','idField','content','toString','type','variableName','customVariable','Variable','toLowerCase','name','concat','securityToken','remoteUri','https://login.salesforce.com','clientId','clientSecret','login','username','Connection\x20established.','queue','serverUrl','stripTrailingSlash','monitor_format','Recording\x20is\x20enabled!','recordingURL','%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s','uniqueid','email','calleridnum','Fields','[QUEUE]\x20Ticket\x20%s\x20created','[QUEUE]\x20Displaying\x20ticket\x20%s\x20to\x20agent\x20%s.\x20Integration\x20account\x20type\x20%s.','integrationTab','trigger:salesforce:display','trigger:browser:url','%s/%s/e','instanceUrl','[QUEUE]\x20Salesforce\x20account\x20type\x20not\x20recognized.','[QUEUE]','stringify','outbound','none','[OUTBOUND]','destcalleridname','destcalleridnum','[OUTBOUND]\x20Ticket\x20%s\x20created.','user:%s','error','Salesforce\x20account\x20type\x20not\x20recognized','abandoned','calleridname'];(function(_0x13ea00,_0x12f610){var _0x1f7347=function(_0x34ae63){while(--_0x34ae63){_0x13ea00['push'](_0x13ea00['shift']());}};_0x1f7347(++_0x12f610);}(_0xac6f,0xe6));var _0xfac6=function(_0xa9fdc4,_0x3a59db){_0xa9fdc4=_0xa9fdc4-0x0;var _0x3874da=_0xac6f[_0xa9fdc4];return _0x3874da;};'use strict';var _=require(_0xfac6('0x0'));var rp=require(_0xfac6('0x1'));var moment=require(_0xfac6('0x2'));var BPromise=require(_0xfac6('0x3'));var md5=require('md5');var jsforce=require(_0xfac6('0x4'));var util=require(_0xfac6('0x5'));var Redis=require(_0xfac6('0x6'));var intUtil=require(_0xfac6('0x7'));var config=require(_0xfac6('0x8'));var logger=require(_0xfac6('0x9'))('salesforce');config['redis']=_[_0xfac6('0xa')](config[_0xfac6('0xb')],{'host':'localhost','port':0x18eb});var io=require(_0xfac6('0xc'))(new Redis(config[_0xfac6('0xb')]));function emit(_0x5f111a,_0x27ad1a,_0x2676d7){io['to'](_0x5f111a)[_0xfac6('0xd')](_0x27ad1a,_0x2676d7);}function getTicket(_0x34ba47,_0x62f30e,_0x5df967,_0x2c9e26,_0x18053e,_0x57faa1,_0x5cf876,_0x39ae62){var _0x2dc6a2={};if(_0x2c9e26['ticketType']===_0xfac6('0xe')){_0x2dc6a2=Object[_0xfac6('0xf')]({},{'OwnerId':_0x62f30e['id'],'ActivityDate':moment[_0xfac6('0x10')](),'IsReminderSet':![],'IsRecurrence':![],'Subject':intUtil[_0xfac6('0x11')](_0x2c9e26[_0xfac6('0x12')],_0x18053e,'\x20'),'Description':intUtil[_0xfac6('0x11')](_0x2c9e26[_0xfac6('0x13')],_0x18053e,'\x0a')},getCustomFields(_0x57faa1,_0x18053e));if(_0x5df967){_0x2dc6a2['WhoId']=_0x5df967['id'];}}else if(_0x2c9e26[_0xfac6('0x14')]===_0xfac6('0x15')){_0x2dc6a2=Object[_0xfac6('0xf')]({},{'OwnerId':_0x62f30e['id'],'Subject':intUtil[_0xfac6('0x11')](_0x2c9e26['Subjects'],_0x18053e,'\x20'),'Description':intUtil[_0xfac6('0x11')](_0x2c9e26[_0xfac6('0x13')],_0x18053e,'\x0a')},getCustomFields(_0x57faa1,_0x18053e));if(_0x5df967){_0x2dc6a2[_0xfac6('0x16')]=_0x5df967['id'];}}return _0x5cf876['sobject'](_0x2c9e26['ticketType'])[_0xfac6('0x17')](_0x2dc6a2);}function getUser(_0x1fc1da,_0x50b8f8,_0x358b77){if(!_[_0xfac6('0x18')](_0x50b8f8)&&intUtil[_0xfac6('0x19')](_0x50b8f8)){var _0x2ae838={'Email':_0x50b8f8};var _0x4d4161={'Id':!![],'Username':!![],'Name':!![],'Email':!![]};return _0x358b77[_0xfac6('0x1a')](_0xfac6('0x1b'))[_0xfac6('0x1c')](_0x2ae838,_0x4d4161)['execute']()[_0xfac6('0x1d')](function(_0x4843c8){if(_0x4843c8['length']){var _0x3ec09e=_[_0xfac6('0x1c')](_0x4843c8,function(_0x51e8a8){return _0x51e8a8['Email']===_0x50b8f8;});if(_0x3ec09e){logger[_0xfac6('0x1e')](_0xfac6('0x1f'),_0x50b8f8);return BPromise[_0xfac6('0x20')]({'id':_0x3ec09e['Id'],'name':_0x3ec09e[_0xfac6('0x21')]});}}else{logger[_0xfac6('0x1e')](_0xfac6('0x22'),_0x50b8f8);return _0x358b77[_0xfac6('0x23')]()[_0xfac6('0x1d')](function(_0x3db491){return BPromise[_0xfac6('0x20')]({'id':_0x3db491[_0xfac6('0x24')],'name':_0x3db491[_0xfac6('0x25')]});});}});}else{logger[_0xfac6('0x1e')](_0xfac6('0x22'),_0x50b8f8);return _0x358b77[_0xfac6('0x23')]()['then'](function(_0xb6c40f){return BPromise[_0xfac6('0x20')]({'id':_0xb6c40f[_0xfac6('0x24')],'name':_0xb6c40f['display_name']});});}}function getEndUser(_0x299d85,_0x2e9f72,_0x29b13d,_0x5222a1,_0x1f6d92){var _0x1dc434={'Phone':_0x5222a1};var _0xeeec0b={'Id':!![],'Name':!![],'Phone':!![]};return BPromise[_0xfac6('0x20')]()[_0xfac6('0x1d')](function(){if(_0x2e9f72['ticketType']==='Task'){logger[_0xfac6('0x1e')]('Searching\x20enduser\x20with\x20phone\x20%s.\x20Serching\x20method\x20used\x20%s.',_0x5222a1,_0x2e9f72[_0xfac6('0x26')]);switch(_0x2e9f72[_0xfac6('0x26')]){case _0xfac6('0x27'):return _0x1f6d92[_0xfac6('0x1a')](_0xfac6('0x28'))[_0xfac6('0x1c')](_0x1dc434,_0xeeec0b)[_0xfac6('0x29')]()[_0xfac6('0x1d')](function(_0x1fb937){if(_0x1fb937&&_0x1fb937[_0xfac6('0x2a')]){return _0x1fb937;}else{return _0x1f6d92[_0xfac6('0x1a')](_0xfac6('0x2b'))[_0xfac6('0x1c')](_0x1dc434,_0xeeec0b);}});case _0xfac6('0x2c'):return _0x1f6d92[_0xfac6('0x1a')]('Contact')[_0xfac6('0x1c')](_0x1dc434,_0xeeec0b)[_0xfac6('0x29')]();case _0xfac6('0x2d'):return _0x1f6d92[_0xfac6('0x1a')]('Lead')[_0xfac6('0x1c')](_0x1dc434,_0xeeec0b);default:return null;}}else if(_0x2e9f72['ticketType']===_0xfac6('0x15')){logger[_0xfac6('0x1e')](_0xfac6('0x2e'),_0x5222a1,_0xfac6('0x2c'));return _0x1f6d92['sobject'](_0xfac6('0x28'))['find'](_0x1dc434,_0xeeec0b)[_0xfac6('0x29')]();}})[_0xfac6('0x1d')](function(_0x5f0a6c){if(_0x5f0a6c&&_0x5f0a6c[_0xfac6('0x2a')]){var _0x44c4f4=_['find'](_0x5f0a6c,function(_0x2d8501){return _0x2d8501[_0xfac6('0x2f')]===_0x5222a1;});if(_0x44c4f4){return BPromise[_0xfac6('0x20')]({'id':_0x44c4f4['Id'],'name':_0x44c4f4['Name'],'phone':_0x44c4f4[_0xfac6('0x2f')]});}}})[_0xfac6('0x1d')](function(_0x3e7a2e){if(_0x3e7a2e){logger[_0xfac6('0x1e')](_0xfac6('0x30'),_0x5222a1);return BPromise[_0xfac6('0x20')](_0x3e7a2e);}else{logger[_0xfac6('0x1e')](_0xfac6('0x31'),_0x5222a1);var _0x4757dc={'LastName':_0x29b13d!==_0xfac6('0x32')?_0x29b13d:util[_0xfac6('0x33')](_0xfac6('0x34'),_0x5222a1),'Phone':_0x5222a1};if(_0x2e9f72[_0xfac6('0x35')]){if(_0x2e9f72[_0xfac6('0x35')]===_0xfac6('0x2c')){logger[_0xfac6('0x1e')](_0xfac6('0x36'),_0x5222a1,_0x2e9f72['moduleCreate']);}else if(_0x2e9f72[_0xfac6('0x35')]===_0xfac6('0x2d')&&_0x2e9f72['ticketType']===_0xfac6('0xe')){logger[_0xfac6('0x1e')](_0xfac6('0x36'),_0x5222a1,_0x2e9f72[_0xfac6('0x35')]);_0x4757dc[_0xfac6('0x37')]=_0xfac6('0x38');}else{return null;}}return _0x1f6d92['sobject'](_[_0xfac6('0x39')](_0x2e9f72['moduleCreate']))[_0xfac6('0x17')](_0x4757dc);}});}function getCustomFields(_0xc890dc,_0x438205){var _0xa617a0={};if(_0xc890dc[_0xfac6('0x2a')]>0x0){_[_0xfac6('0x3a')](_0xc890dc,function(_0xfbedb1){if(_0xfbedb1['type']==='string'&&_0xfbedb1[_0xfac6('0x3b')]){_0xa617a0[_0xfbedb1[_0xfac6('0x3b')]]=_0xfbedb1[_0xfac6('0x3c')][_0xfac6('0x3d')]();}else if(_0xfbedb1[_0xfac6('0x3e')]==='variable'&&_0xfbedb1['variableName']&&_0xfbedb1[_0xfac6('0x3b')]){_0x438205[_0xfbedb1[_0xfac6('0x3f')]]&&(_0xa617a0[_0xfbedb1['idField']]=_0x438205[_0xfbedb1[_0xfac6('0x3f')]][_0xfac6('0x3d')]());}else if(_0xfbedb1[_0xfac6('0x3e')]===_0xfac6('0x40')&&_0xfbedb1[_0xfac6('0x41')]&&_0xfbedb1[_0xfac6('0x41')]['name']&&_0xfbedb1['idField']){_0x438205[_0xfbedb1[_0xfac6('0x41')]['name'][_0xfac6('0x42')]()]&&(_0xa617a0[_0xfbedb1['idField']]=_0x438205[_0xfbedb1[_0xfac6('0x41')][_0xfac6('0x43')][_0xfac6('0x42')]()][_0xfac6('0x3d')]());}});}return _0xa617a0;}function getConnection(_0x36e2e4){var _0x1fabc1=_0x36e2e4['password'][_0xfac6('0x44')](_0x36e2e4[_0xfac6('0x45')]);var _0x5ec2d0=new jsforce['Connection']({'oauth2':{'loginUrl':_0x36e2e4[_0xfac6('0x46')]||_0xfac6('0x47'),'clientId':_0x36e2e4[_0xfac6('0x48')],'clientSecret':_0x36e2e4[_0xfac6('0x49')]}});return _0x5ec2d0[_0xfac6('0x4a')](_0x36e2e4[_0xfac6('0x4b')],_0x1fabc1)[_0xfac6('0x1d')](function(){logger[_0xfac6('0x1e')](_0xfac6('0x4c'));return _0x5ec2d0;});}exports[_0xfac6('0x4d')]=function(_0x27ec25,_0x31ac54,_0x54954a,_0x40cc55,_0xbe214,_0x4d5610){var _0x5612a3,_0x25d2ab,_0x346b98,_0x44f131;_0x4d5610[_0xfac6('0x4e')]=intUtil[_0xfac6('0x4f')](_0x4d5610[_0xfac6('0x4e')]);_0x4d5610['remoteUri']=intUtil['stripTrailingSlash'](_0x4d5610[_0xfac6('0x46')]);if(_0xbe214&&_0xbe214[_0xfac6('0x50')]){logger[_0xfac6('0x1e')]('[QUEUE]',_0xfac6('0x51'));_0x31ac54[_0xfac6('0x52')]=util[_0xfac6('0x33')](_0xfac6('0x53'),_0x4d5610[_0xfac6('0x4e')],_0x31ac54[_0xfac6('0x54')],md5(_0x31ac54['uniqueid']));}else{_0x31ac54[_0xfac6('0x52')]='';}return getConnection(_0x4d5610)['then'](function(_0x5420de){_0x44f131=_0x5420de;return getUser(_0x4d5610,_0x40cc55[_0xfac6('0x55')],_0x44f131);})['then'](function(_0x1f7962){_0x5612a3=_0x1f7962;return getEndUser(_0x4d5610,_0x54954a,_0x31ac54['calleridname'],_0x31ac54[_0xfac6('0x56')],_0x44f131);})[_0xfac6('0x1d')](function(_0x2ec345){_0x25d2ab=_0x2ec345;return getTicket(_0x4d5610,_0x5612a3,_0x25d2ab,_0x54954a,_0x31ac54,_0x54954a[_0xfac6('0x57')],_0x44f131,'queue');})[_0xfac6('0x1d')](function(_0x566c25){_0x346b98=_0x566c25;logger[_0xfac6('0x1e')](_0xfac6('0x58'),_0x346b98['id']);logger[_0xfac6('0x1e')](_0xfac6('0x59'),_0x346b98['id'],_0x40cc55[_0xfac6('0x43')],_0x4d5610[_0xfac6('0x3e')]);switch(_0x4d5610['type']){case _0xfac6('0x5a'):emit(util[_0xfac6('0x33')]('user:%s',_0x40cc55['name']),_0xfac6('0x5b'),{'recordId':_0x346b98['id']});break;case'newTab':emit(util['format']('user:%s',_0x40cc55[_0xfac6('0x43')]),_0xfac6('0x5c'),{'uri':util[_0xfac6('0x33')](_0xfac6('0x5d'),_0x44f131[_0xfac6('0x5e')],_0x346b98['id'])});break;default:logger['error'](_0xfac6('0x5f'));}})['catch'](function(_0x4a5785){logger['error'](_0xfac6('0x60'),JSON[_0xfac6('0x61')](_0x4a5785));});};exports[_0xfac6('0x62')]=function(_0x1abda5,_0x280650,_0x4c10d9,_0x1b9747,_0x406719,_0x24259a,_0x1dd747){var _0x1f1215,_0xb3937c,_0x2e8eab,_0x38c6a2;_0x24259a['serverUrl']=intUtil[_0xfac6('0x4f')](_0x24259a['serverUrl']);_0x24259a[_0xfac6('0x46')]=intUtil[_0xfac6('0x4f')](_0x24259a[_0xfac6('0x46')]);if(_0x1dd747&&_0x1dd747['recordingFormat']&&_0x1dd747['recordingFormat']!==_0xfac6('0x63')){logger[_0xfac6('0x1e')](_0xfac6('0x64'),'Recording\x20is\x20enabled!');_0x280650[_0xfac6('0x52')]=util[_0xfac6('0x33')]('%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s',_0x24259a[_0xfac6('0x4e')],_0x280650[_0xfac6('0x54')],md5(_0x280650[_0xfac6('0x54')]));}return getConnection(_0x24259a)[_0xfac6('0x1d')](function(_0x4ca8fd){_0x38c6a2=_0x4ca8fd;return getUser(_0x24259a,_0x1b9747[_0xfac6('0x55')],_0x38c6a2);})['then'](function(_0x54e469){_0x1f1215=_0x54e469;return getEndUser(_0x24259a,_0x4c10d9,_0x280650[_0xfac6('0x65')],_0x280650[_0xfac6('0x66')],_0x38c6a2);})[_0xfac6('0x1d')](function(_0x6ef247){_0xb3937c=_0x6ef247;return getTicket(_0x24259a,_0x1f1215,_0xb3937c,_0x4c10d9,_0x280650,_0x4c10d9[_0xfac6('0x57')],_0x38c6a2,'outbound');})[_0xfac6('0x1d')](function(_0x506147){_0x2e8eab=_0x506147;logger[_0xfac6('0x1e')](_0xfac6('0x67'),_0x2e8eab['id']);logger[_0xfac6('0x1e')]('[OUTBOUND]\x20Displaying\x20ticket\x20%s\x20to\x20agent\x20%s.\x20Integration\x20account\x20type\x20%s.',_0x2e8eab['id'],_0x1b9747[_0xfac6('0x43')],_0x24259a[_0xfac6('0x3e')]);switch(_0x24259a[_0xfac6('0x3e')]){case _0xfac6('0x5a'):emit(util[_0xfac6('0x33')](_0xfac6('0x68'),_0x1b9747[_0xfac6('0x43')]),_0xfac6('0x5b'),{'recordId':_0x2e8eab['id']});break;case'newTab':emit(util[_0xfac6('0x33')](_0xfac6('0x68'),_0x1b9747[_0xfac6('0x43')]),_0xfac6('0x5c'),{'uri':util[_0xfac6('0x33')](_0xfac6('0x5d'),_0x38c6a2['instanceUrl'],_0x2e8eab['id'])});break;default:logger[_0xfac6('0x69')](_0xfac6('0x64'),_0xfac6('0x6a'));}})['catch'](function(_0x23fac1){logger[_0xfac6('0x69')](_0xfac6('0x64'),JSON['stringify'](_0x23fac1));});};exports[_0xfac6('0x6b')]=function(_0x215be6,_0x4e2fcb,_0x220fcc,_0x4fdd05,_0x154209,_0x4eb93b){var _0x5ece96,_0x5e09e4,_0x55f3e1,_0x2b6193;_0x4eb93b['serverUrl']=intUtil[_0xfac6('0x4f')](_0x4eb93b[_0xfac6('0x4e')]);_0x4eb93b[_0xfac6('0x46')]=intUtil[_0xfac6('0x4f')](_0x4eb93b[_0xfac6('0x46')]);if(_0x154209&&_0x154209[_0xfac6('0x50')]){logger[_0xfac6('0x1e')]('[ABANDONED]',_0xfac6('0x51'));_0x4e2fcb[_0xfac6('0x52')]=util['format'](_0xfac6('0x53'),_0x4eb93b['serverUrl'],_0x4e2fcb[_0xfac6('0x54')],md5(_0x4e2fcb[_0xfac6('0x54')]));}else{_0x4e2fcb[_0xfac6('0x52')]='';}return getConnection(_0x4eb93b)[_0xfac6('0x1d')](function(_0x4a3a7d){_0x2b6193=_0x4a3a7d;if(!_[_0xfac6('0x18')](_0x4fdd05)){return getUser(_0x4eb93b,_0x4fdd05[_0xfac6('0x43')],_0x2b6193);}else{return getUser(_0x4eb93b,null,_0x2b6193);}})['then'](function(_0x3f3a69){_0x5ece96=_0x3f3a69;return getEndUser(_0x4eb93b,_0x220fcc,_0x4e2fcb[_0xfac6('0x6c')],_0x4e2fcb[_0xfac6('0x56')],_0x2b6193);})[_0xfac6('0x1d')](function(_0x298a7f){_0x5e09e4=_0x298a7f;return getTicket(_0x4eb93b,_0x5ece96,_0x5e09e4,_0x220fcc,_0x4e2fcb,_0x220fcc[_0xfac6('0x57')],_0x2b6193);})['then'](function(_0x1ad58c){_0x55f3e1=_0x1ad58c;logger['info'](_0xfac6('0x6d'),_0x55f3e1['id']);logger[_0xfac6('0x1e')](_0xfac6('0x6e'),_0x4eb93b[_0xfac6('0x3e')]);})[_0xfac6('0x6f')](function(_0x31d88f){logger[_0xfac6('0x69')](_0xfac6('0x70'),JSON[_0xfac6('0x61')](_0x31d88f));});};