Built motion from commit (unavailable).|2.5.21
[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 _0x2f55=['getString','WhoId','Case','Subjects','Descriptions','ContactId','sobject','isNil','validateEmail','User','find','execute','then','length','Email','Agent\x20%s\x20found.','Name','info','identity','resolve','user_id','display_name','Searching\x20enduser\x20with\x20phone\x20%s.\x20Serching\x20method\x20used\x20%s.','moduleSearch','contact_lead','Contact','lead','Lead','contact','Phone','Enduser\x20with\x20phone\x20%s\x20found.','format','Caller\x20-\x20%s','moduleCreate','Enduser\x20with\x20phone\x20%s.\x20Creating\x20method\x20used\x20%s.','upperFirst','create','forEach','type','string','idField','toString','variable','variableName','customVariable','Variable','toLowerCase','name','concat','securityToken','Connection','remoteUri','https://login.salesforce.com','clientId','clientSecret','login','username','Connection\x20established.','queue','serverUrl','stripTrailingSlash','monitor_format','[QUEUE]','Recording\x20is\x20enabled!','recordingURL','uniqueid','email','calleridname','calleridnum','Fields','[QUEUE]\x20Ticket\x20%s\x20created','[QUEUE]\x20Displaying\x20ticket\x20%s\x20to\x20agent\x20%s.\x20Integration\x20account\x20type\x20%s.','integrationTab','user:%s','trigger:salesforce:display','trigger:browser:url','%s/%s/e','error','[QUEUE]\x20Salesforce\x20account\x20type\x20not\x20recognized.','catch','stringify','outbound','recordingFormat','none','[OUTBOUND]','destcalleridname','[OUTBOUND]\x20Ticket\x20%s\x20created.','[OUTBOUND]\x20Displaying\x20ticket\x20%s\x20to\x20agent\x20%s.\x20Integration\x20account\x20type\x20%s.','newTab','instanceUrl','Salesforce\x20account\x20type\x20not\x20recognized','unmanaged','lastevent','toUpperCase','%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s',']\x20Ticket\x20%s\x20created.',']\x20Integration\x20account\x20type\x20%s.','lodash','request-promise','moment','bluebird','md5','jsforce','util','ioredis','./util','../../../../config/environment','salesforce','redis','defaults','localhost','emit','ticketType','Task','assign','utc'];(function(_0x535e44,_0x1b5b76){var _0x188605=function(_0x291742){while(--_0x291742){_0x535e44['push'](_0x535e44['shift']());}};_0x188605(++_0x1b5b76);}(_0x2f55,0xd5));var _0x52f5=function(_0x233c30,_0x17daca){_0x233c30=_0x233c30-0x0;var _0x1ffe54=_0x2f55[_0x233c30];return _0x1ffe54;};'use strict';var _=require(_0x52f5('0x0'));var rp=require(_0x52f5('0x1'));var moment=require(_0x52f5('0x2'));var BPromise=require(_0x52f5('0x3'));var md5=require(_0x52f5('0x4'));var jsforce=require(_0x52f5('0x5'));var util=require(_0x52f5('0x6'));var Redis=require(_0x52f5('0x7'));var intUtil=require(_0x52f5('0x8'));var config=require(_0x52f5('0x9'));var logger=require('../../../../config/logger')(_0x52f5('0xa'));config[_0x52f5('0xb')]=_[_0x52f5('0xc')](config[_0x52f5('0xb')],{'host':_0x52f5('0xd'),'port':0x18eb});var io=require('socket.io-emitter')(new Redis(config[_0x52f5('0xb')]));function emit(_0x76f994,_0x30f382,_0x3620ff){io['to'](_0x76f994)[_0x52f5('0xe')](_0x30f382,_0x3620ff);}function getTicket(_0x35ad48,_0x4309cf,_0x450f3a,_0x5c5604,_0x4faaac,_0x482f36,_0x50ddb5,_0x4defe5){var _0x1a8fa1={};if(_0x5c5604[_0x52f5('0xf')]===_0x52f5('0x10')){_0x1a8fa1=Object[_0x52f5('0x11')]({},{'OwnerId':_0x4309cf['id'],'ActivityDate':moment[_0x52f5('0x12')](),'IsReminderSet':![],'IsRecurrence':![],'Subject':intUtil[_0x52f5('0x13')](_0x5c5604['Subjects'],_0x4faaac,'\x20'),'Description':intUtil['getString'](_0x5c5604['Descriptions'],_0x4faaac,'\x0a')},getCustomFields(_0x482f36,_0x4faaac));if(_0x450f3a){_0x1a8fa1[_0x52f5('0x14')]=_0x450f3a['id'];}}else if(_0x5c5604[_0x52f5('0xf')]===_0x52f5('0x15')){_0x1a8fa1=Object[_0x52f5('0x11')]({},{'OwnerId':_0x4309cf['id'],'Subject':intUtil[_0x52f5('0x13')](_0x5c5604[_0x52f5('0x16')],_0x4faaac,'\x20'),'Description':intUtil[_0x52f5('0x13')](_0x5c5604[_0x52f5('0x17')],_0x4faaac,'\x0a')},getCustomFields(_0x482f36,_0x4faaac));if(_0x450f3a){_0x1a8fa1[_0x52f5('0x18')]=_0x450f3a['id'];}}return _0x50ddb5[_0x52f5('0x19')](_0x5c5604[_0x52f5('0xf')])['create'](_0x1a8fa1);}function getUser(_0x534aae,_0x981e,_0x17a28f){if(!_[_0x52f5('0x1a')](_0x981e)&&intUtil[_0x52f5('0x1b')](_0x981e)){var _0x1ae6f0={'Email':_0x981e};var _0xc75d37={'Id':!![],'Username':!![],'Name':!![],'Email':!![]};return _0x17a28f[_0x52f5('0x19')](_0x52f5('0x1c'))[_0x52f5('0x1d')](_0x1ae6f0,_0xc75d37)[_0x52f5('0x1e')]()[_0x52f5('0x1f')](function(_0x20938d){if(_0x20938d[_0x52f5('0x20')]){var _0x25d490=_[_0x52f5('0x1d')](_0x20938d,function(_0x40d6bc){return _0x40d6bc[_0x52f5('0x21')]===_0x981e;});if(_0x25d490){logger['info'](_0x52f5('0x22'),_0x981e);return BPromise['resolve']({'id':_0x25d490['Id'],'name':_0x25d490[_0x52f5('0x23')]});}}else{logger[_0x52f5('0x24')]('Agent\x20%s\x20not\x20found.\x20Switch\x20to\x20the\x20admin\x20account.',_0x981e);return _0x17a28f[_0x52f5('0x25')]()[_0x52f5('0x1f')](function(_0x581c65){return BPromise[_0x52f5('0x26')]({'id':_0x581c65[_0x52f5('0x27')],'name':_0x581c65[_0x52f5('0x28')]});});}});}else{logger[_0x52f5('0x24')]('Agent\x20%s\x20not\x20found.\x20Switch\x20to\x20the\x20admin\x20account.',_0x981e);return _0x17a28f[_0x52f5('0x25')]()[_0x52f5('0x1f')](function(_0x452545){return BPromise[_0x52f5('0x26')]({'id':_0x452545[_0x52f5('0x27')],'name':_0x452545['display_name']});});}}function getEndUser(_0x1f08e5,_0x32592d,_0x146208,_0x41d9b9,_0x5e610d){var _0x361e2e={'Phone':_0x41d9b9};var _0x55453e={'Id':!![],'Name':!![],'Phone':!![]};return BPromise['resolve']()[_0x52f5('0x1f')](function(){if(_0x32592d[_0x52f5('0xf')]===_0x52f5('0x10')){logger[_0x52f5('0x24')](_0x52f5('0x29'),_0x41d9b9,_0x32592d[_0x52f5('0x2a')]);switch(_0x32592d[_0x52f5('0x2a')]){case _0x52f5('0x2b'):return _0x5e610d['sobject'](_0x52f5('0x2c'))['find'](_0x361e2e,_0x55453e)['execute']()[_0x52f5('0x1f')](function(_0x5cae1e){if(_0x5cae1e&&_0x5cae1e[_0x52f5('0x20')]){return _0x5cae1e;}else{return _0x5e610d[_0x52f5('0x19')]('Lead')[_0x52f5('0x1d')](_0x361e2e,_0x55453e);}});case'contact':return _0x5e610d['sobject'](_0x52f5('0x2c'))['find'](_0x361e2e,_0x55453e)['execute']();case _0x52f5('0x2d'):return _0x5e610d['sobject'](_0x52f5('0x2e'))[_0x52f5('0x1d')](_0x361e2e,_0x55453e);default:return null;}}else if(_0x32592d[_0x52f5('0xf')]===_0x52f5('0x15')){logger[_0x52f5('0x24')](_0x52f5('0x29'),_0x41d9b9,_0x52f5('0x2f'));return _0x5e610d[_0x52f5('0x19')]('Contact')[_0x52f5('0x1d')](_0x361e2e,_0x55453e)[_0x52f5('0x1e')]();}})[_0x52f5('0x1f')](function(_0x20dbf7){if(_0x20dbf7&&_0x20dbf7['length']){var _0x191854=_[_0x52f5('0x1d')](_0x20dbf7,function(_0x4e3bef){return _0x4e3bef[_0x52f5('0x30')]===_0x41d9b9;});if(_0x191854){return BPromise[_0x52f5('0x26')]({'id':_0x191854['Id'],'name':_0x191854[_0x52f5('0x23')],'phone':_0x191854['Phone']});}}})[_0x52f5('0x1f')](function(_0x1c71c2){if(_0x1c71c2){logger[_0x52f5('0x24')](_0x52f5('0x31'),_0x41d9b9);return BPromise[_0x52f5('0x26')](_0x1c71c2);}else{logger['info']('Enduser\x20with\x20phone\x20%s\x20not\x20found.',_0x41d9b9);var _0x406561={'LastName':_0x146208!=='<unknown>'?_0x146208:util[_0x52f5('0x32')](_0x52f5('0x33'),_0x41d9b9),'Phone':_0x41d9b9};if(_0x32592d[_0x52f5('0x34')]){if(_0x32592d['moduleCreate']===_0x52f5('0x2f')){logger[_0x52f5('0x24')]('Enduser\x20with\x20phone\x20%s.\x20Creating\x20method\x20used\x20%s.',_0x41d9b9,_0x32592d[_0x52f5('0x34')]);}else if(_0x32592d[_0x52f5('0x34')]===_0x52f5('0x2d')&&_0x32592d['ticketType']===_0x52f5('0x10')){logger[_0x52f5('0x24')](_0x52f5('0x35'),_0x41d9b9,_0x32592d[_0x52f5('0x34')]);_0x406561['Company']='No\x20Company';}else{return null;}}return _0x5e610d[_0x52f5('0x19')](_[_0x52f5('0x36')](_0x32592d[_0x52f5('0x34')]))[_0x52f5('0x37')](_0x406561);}});}function getCustomFields(_0x4fa04a,_0x115e78){var _0x85c58c={};if(_0x4fa04a['length']>0x0){_[_0x52f5('0x38')](_0x4fa04a,function(_0x2d6558){if(_0x2d6558[_0x52f5('0x39')]===_0x52f5('0x3a')&&_0x2d6558[_0x52f5('0x3b')]){_0x85c58c[_0x2d6558[_0x52f5('0x3b')]]=_0x2d6558['content'][_0x52f5('0x3c')]();}else if(_0x2d6558['type']===_0x52f5('0x3d')&&_0x2d6558['variableName']&&_0x2d6558[_0x52f5('0x3b')]){_0x115e78[_0x2d6558[_0x52f5('0x3e')]]&&(_0x85c58c[_0x2d6558[_0x52f5('0x3b')]]=_0x115e78[_0x2d6558['variableName']][_0x52f5('0x3c')]());}else if(_0x2d6558['type']===_0x52f5('0x3f')&&_0x2d6558[_0x52f5('0x40')]&&_0x2d6558['Variable']['name']&&_0x2d6558[_0x52f5('0x3b')]){_0x115e78[_0x2d6558['Variable']['name'][_0x52f5('0x41')]()]&&(_0x85c58c[_0x2d6558[_0x52f5('0x3b')]]=_0x115e78[_0x2d6558['Variable'][_0x52f5('0x42')][_0x52f5('0x41')]()][_0x52f5('0x3c')]());}});}return _0x85c58c;}function getConnection(_0x121e36){var _0x525c5f=_0x121e36['password'][_0x52f5('0x43')](_0x121e36[_0x52f5('0x44')]);var _0x402668=new jsforce[(_0x52f5('0x45'))]({'oauth2':{'loginUrl':_0x121e36[_0x52f5('0x46')]||_0x52f5('0x47'),'clientId':_0x121e36[_0x52f5('0x48')],'clientSecret':_0x121e36[_0x52f5('0x49')]}});return _0x402668[_0x52f5('0x4a')](_0x121e36[_0x52f5('0x4b')],_0x525c5f)['then'](function(){logger[_0x52f5('0x24')](_0x52f5('0x4c'));return _0x402668;});}exports[_0x52f5('0x4d')]=function(_0x54bc70,_0x4d1b2e,_0x13de5d,_0x3c7887,_0x2fa56c,_0x5d577e){var _0x538251,_0x327d44,_0x603ea2,_0xa5c655;_0x5d577e[_0x52f5('0x4e')]=intUtil[_0x52f5('0x4f')](_0x5d577e[_0x52f5('0x4e')]);_0x5d577e[_0x52f5('0x46')]=intUtil[_0x52f5('0x4f')](_0x5d577e[_0x52f5('0x46')]);if(_0x2fa56c&&_0x2fa56c[_0x52f5('0x50')]){logger[_0x52f5('0x24')](_0x52f5('0x51'),_0x52f5('0x52'));_0x4d1b2e[_0x52f5('0x53')]=util['format']('%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s',_0x5d577e['serverUrl'],_0x4d1b2e[_0x52f5('0x54')],md5(_0x4d1b2e[_0x52f5('0x54')]));}else{_0x4d1b2e[_0x52f5('0x53')]='';}return getConnection(_0x5d577e)['then'](function(_0x3b98fe){_0xa5c655=_0x3b98fe;return getUser(_0x5d577e,_0x3c7887[_0x52f5('0x55')],_0xa5c655);})[_0x52f5('0x1f')](function(_0x31daef){_0x538251=_0x31daef;return getEndUser(_0x5d577e,_0x13de5d,_0x4d1b2e[_0x52f5('0x56')],_0x4d1b2e[_0x52f5('0x57')],_0xa5c655);})[_0x52f5('0x1f')](function(_0x416cb0){_0x327d44=_0x416cb0;return getTicket(_0x5d577e,_0x538251,_0x327d44,_0x13de5d,_0x4d1b2e,_0x13de5d[_0x52f5('0x58')],_0xa5c655,_0x52f5('0x4d'));})[_0x52f5('0x1f')](function(_0x355589){_0x603ea2=_0x355589;logger['info'](_0x52f5('0x59'),_0x603ea2['id']);logger[_0x52f5('0x24')](_0x52f5('0x5a'),_0x603ea2['id'],_0x3c7887[_0x52f5('0x42')],_0x5d577e[_0x52f5('0x39')]);switch(_0x5d577e[_0x52f5('0x39')]){case _0x52f5('0x5b'):emit(util[_0x52f5('0x32')](_0x52f5('0x5c'),_0x3c7887[_0x52f5('0x42')]),_0x52f5('0x5d'),{'recordId':_0x603ea2['id']});break;case'newTab':emit(util[_0x52f5('0x32')](_0x52f5('0x5c'),_0x3c7887[_0x52f5('0x42')]),_0x52f5('0x5e'),{'uri':util[_0x52f5('0x32')](_0x52f5('0x5f'),_0xa5c655['instanceUrl'],_0x603ea2['id'])});break;default:logger[_0x52f5('0x60')](_0x52f5('0x61'));}})[_0x52f5('0x62')](function(_0x120bdb){logger['error'](_0x52f5('0x51'),JSON[_0x52f5('0x63')](_0x120bdb));});};exports[_0x52f5('0x64')]=function(_0x343172,_0x399552,_0x8b0ce4,_0x5c76c7,_0x117d73,_0x46a4a2,_0x172a0e){var _0x1dacb4,_0x25ccf6,_0x3a44f0,_0x52bdc8;_0x46a4a2[_0x52f5('0x4e')]=intUtil['stripTrailingSlash'](_0x46a4a2[_0x52f5('0x4e')]);_0x46a4a2[_0x52f5('0x46')]=intUtil[_0x52f5('0x4f')](_0x46a4a2[_0x52f5('0x46')]);if(_0x172a0e&&_0x172a0e[_0x52f5('0x65')]&&_0x172a0e[_0x52f5('0x65')]!==_0x52f5('0x66')){logger[_0x52f5('0x24')](_0x52f5('0x67'),_0x52f5('0x52'));_0x399552[_0x52f5('0x53')]=util[_0x52f5('0x32')]('%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s',_0x46a4a2['serverUrl'],_0x399552[_0x52f5('0x54')],md5(_0x399552[_0x52f5('0x54')]));}return getConnection(_0x46a4a2)[_0x52f5('0x1f')](function(_0x58a56f){_0x52bdc8=_0x58a56f;return getUser(_0x46a4a2,_0x5c76c7['email'],_0x52bdc8);})['then'](function(_0x2a10ac){_0x1dacb4=_0x2a10ac;return getEndUser(_0x46a4a2,_0x8b0ce4,_0x399552[_0x52f5('0x68')],_0x399552['destcalleridnum'],_0x52bdc8);})[_0x52f5('0x1f')](function(_0x33bfcd){_0x25ccf6=_0x33bfcd;return getTicket(_0x46a4a2,_0x1dacb4,_0x25ccf6,_0x8b0ce4,_0x399552,_0x8b0ce4[_0x52f5('0x58')],_0x52bdc8,_0x52f5('0x64'));})[_0x52f5('0x1f')](function(_0x1b1452){_0x3a44f0=_0x1b1452;logger[_0x52f5('0x24')](_0x52f5('0x69'),_0x3a44f0['id']);logger['info'](_0x52f5('0x6a'),_0x3a44f0['id'],_0x5c76c7[_0x52f5('0x42')],_0x46a4a2[_0x52f5('0x39')]);switch(_0x46a4a2[_0x52f5('0x39')]){case'integrationTab':emit(util['format'](_0x52f5('0x5c'),_0x5c76c7[_0x52f5('0x42')]),'trigger:salesforce:display',{'recordId':_0x3a44f0['id']});break;case _0x52f5('0x6b'):emit(util[_0x52f5('0x32')]('user:%s',_0x5c76c7[_0x52f5('0x42')]),_0x52f5('0x5e'),{'uri':util[_0x52f5('0x32')]('%s/%s/e',_0x52bdc8[_0x52f5('0x6c')],_0x3a44f0['id'])});break;default:logger[_0x52f5('0x60')](_0x52f5('0x67'),_0x52f5('0x6d'));}})[_0x52f5('0x62')](function(_0xfe4f1d){logger['error'](_0x52f5('0x67'),JSON[_0x52f5('0x63')](_0xfe4f1d));});};exports[_0x52f5('0x6e')]=function(_0x5f730a,_0x231023,_0x575777,_0x3ca962,_0x2d6390,_0x14fabd){var _0x1d2300,_0x340552,_0x312550,_0x3f2e6b;_0x14fabd['serverUrl']=intUtil[_0x52f5('0x4f')](_0x14fabd['serverUrl']);_0x14fabd[_0x52f5('0x46')]=intUtil[_0x52f5('0x4f')](_0x14fabd[_0x52f5('0x46')]);if(_0x2d6390&&_0x2d6390['monitor_format']){logger[_0x52f5('0x24')]('['+_0x231023[_0x52f5('0x6f')][_0x52f5('0x70')]()+']','Recording\x20is\x20enabled!');_0x231023[_0x52f5('0x53')]=util[_0x52f5('0x32')](_0x52f5('0x71'),_0x14fabd[_0x52f5('0x4e')],_0x231023[_0x52f5('0x54')],md5(_0x231023['uniqueid']));}else{_0x231023[_0x52f5('0x53')]='';}return getConnection(_0x14fabd)[_0x52f5('0x1f')](function(_0x2f4766){_0x3f2e6b=_0x2f4766;if(!_[_0x52f5('0x1a')](_0x3ca962)){return getUser(_0x14fabd,_0x3ca962['name'],_0x3f2e6b);}else{return getUser(_0x14fabd,null,_0x3f2e6b);}})[_0x52f5('0x1f')](function(_0x5d517f){_0x1d2300=_0x5d517f;return getEndUser(_0x14fabd,_0x575777,_0x231023[_0x52f5('0x56')],_0x231023[_0x52f5('0x57')],_0x3f2e6b);})[_0x52f5('0x1f')](function(_0x3139f7){_0x340552=_0x3139f7;return getTicket(_0x14fabd,_0x1d2300,_0x340552,_0x575777,_0x231023,_0x575777[_0x52f5('0x58')],_0x3f2e6b);})[_0x52f5('0x1f')](function(_0x1c259b){_0x312550=_0x1c259b;logger[_0x52f5('0x24')]('['+_0x231023[_0x52f5('0x6f')]['toUpperCase']()+_0x52f5('0x72'),_0x312550['id']);logger['info']('['+_0x231023['lastevent'][_0x52f5('0x70')]()+_0x52f5('0x73'),_0x14fabd[_0x52f5('0x39')]);})[_0x52f5('0x62')](function(_0x3ae171){logger[_0x52f5('0x60')]('['+_0x231023[_0x52f5('0x6f')][_0x52f5('0x70')]()+']',JSON[_0x52f5('0x63')](_0x3ae171));});};