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 _0xf569=['unmanaged','lastevent','toUpperCase',']\x20Integration\x20account\x20type\x20%s.','lodash','request-promise','moment','bluebird','md5','ioredis','./util','../../../../config/environment','../../../../config/logger','salesforce','redis','defaults','socket.io-emitter','emit','ticketType','Task','assign','utc','getString','Descriptions','WhoId','create','isNil','validateEmail','sobject','User','length','find','info','Agent\x20%s\x20found.','Agent\x20%s\x20not\x20found.\x20Switch\x20to\x20the\x20admin\x20account.','identity','resolve','user_id','display_name','then','Searching\x20enduser\x20with\x20phone\x20%s.\x20Serching\x20method\x20used\x20%s.','moduleSearch','Contact','execute','contact','Lead','Case','Phone','Name','<unknown>','format','Caller\x20-\x20%s','Enduser\x20with\x20phone\x20%s.\x20Creating\x20method\x20used\x20%s.','moduleCreate','lead','Company','No\x20Company','forEach','string','idField','type','variable','variableName','customVariable','Variable','name','toLowerCase','toString','password','concat','Connection','remoteUri','https://login.salesforce.com','clientSecret','username','Connection\x20established.','serverUrl','stripTrailingSlash','[QUEUE]','Recording\x20is\x20enabled!','recordingURL','%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s','uniqueid','calleridname','calleridnum','Fields','[QUEUE]\x20Ticket\x20%s\x20created','[QUEUE]\x20Displaying\x20ticket\x20%s\x20to\x20agent\x20%s.\x20Integration\x20account\x20type\x20%s.','integrationTab','newTab','trigger:browser:url','%s/%s/e','instanceUrl','catch','error','stringify','outbound','recordingFormat','none','[OUTBOUND]','destcalleridname','destcalleridnum','[OUTBOUND]\x20Ticket\x20%s\x20created.','user:%s','Salesforce\x20account\x20type\x20not\x20recognized'];(function(_0x7888bd,_0x53f6f3){var _0x25e3c0=function(_0x59c177){while(--_0x59c177){_0x7888bd['push'](_0x7888bd['shift']());}};_0x25e3c0(++_0x53f6f3);}(_0xf569,0x6d));var _0x9f56=function(_0x2b9b7f,_0x4d47df){_0x2b9b7f=_0x2b9b7f-0x0;var _0x4ed19a=_0xf569[_0x2b9b7f];return _0x4ed19a;};'use strict';var _=require(_0x9f56('0x0'));var rp=require(_0x9f56('0x1'));var moment=require(_0x9f56('0x2'));var BPromise=require(_0x9f56('0x3'));var md5=require(_0x9f56('0x4'));var jsforce=require('jsforce');var util=require('util');var Redis=require(_0x9f56('0x5'));var intUtil=require(_0x9f56('0x6'));var config=require(_0x9f56('0x7'));var logger=require(_0x9f56('0x8'))(_0x9f56('0x9'));config[_0x9f56('0xa')]=_[_0x9f56('0xb')](config[_0x9f56('0xa')],{'host':'localhost','port':0x18eb});var io=require(_0x9f56('0xc'))(new Redis(config[_0x9f56('0xa')]));function emit(_0x1da049,_0x29ff3e,_0x32f375){io['to'](_0x1da049)[_0x9f56('0xd')](_0x29ff3e,_0x32f375);}function getTicket(_0x267295,_0x4b2737,_0x1c1ae4,_0x47dcb6,_0xd13f5c,_0x40ff45,_0x119241,_0x45fd13){var _0x136b1b={};if(_0x47dcb6[_0x9f56('0xe')]===_0x9f56('0xf')){_0x136b1b=Object[_0x9f56('0x10')]({},{'OwnerId':_0x4b2737['id'],'ActivityDate':moment[_0x9f56('0x11')](),'IsReminderSet':![],'IsRecurrence':![],'Subject':intUtil[_0x9f56('0x12')](_0x47dcb6['Subjects'],_0xd13f5c,'\x20'),'Description':intUtil['getString'](_0x47dcb6[_0x9f56('0x13')],_0xd13f5c,'\x0a')},getCustomFields(_0x40ff45,_0xd13f5c));if(_0x1c1ae4){_0x136b1b[_0x9f56('0x14')]=_0x1c1ae4['id'];}}else if(_0x47dcb6[_0x9f56('0xe')]==='Case'){_0x136b1b=Object[_0x9f56('0x10')]({},{'OwnerId':_0x4b2737['id'],'Subject':intUtil[_0x9f56('0x12')](_0x47dcb6['Subjects'],_0xd13f5c,'\x20'),'Description':intUtil[_0x9f56('0x12')](_0x47dcb6[_0x9f56('0x13')],_0xd13f5c,'\x0a')},getCustomFields(_0x40ff45,_0xd13f5c));if(_0x1c1ae4){_0x136b1b['ContactId']=_0x1c1ae4['id'];}}return _0x119241['sobject'](_0x47dcb6[_0x9f56('0xe')])[_0x9f56('0x15')](_0x136b1b);}function getUser(_0xf973d9,_0x4e9778,_0x5105e3){if(!_[_0x9f56('0x16')](_0x4e9778)&&intUtil[_0x9f56('0x17')](_0x4e9778)){var _0x492174={'Email':_0x4e9778};var _0x596002={'Id':!![],'Username':!![],'Name':!![],'Email':!![]};return _0x5105e3[_0x9f56('0x18')](_0x9f56('0x19'))['find'](_0x492174,_0x596002)['execute']()['then'](function(_0x2ece56){if(_0x2ece56[_0x9f56('0x1a')]){var _0x3b0d3f=_[_0x9f56('0x1b')](_0x2ece56,function(_0x4ac478){return _0x4ac478['Email']===_0x4e9778;});if(_0x3b0d3f){logger[_0x9f56('0x1c')](_0x9f56('0x1d'),_0x4e9778);return BPromise['resolve']({'id':_0x3b0d3f['Id'],'name':_0x3b0d3f['Name']});}}else{logger['info'](_0x9f56('0x1e'),_0x4e9778);return _0x5105e3[_0x9f56('0x1f')]()['then'](function(_0x9cbd50){return BPromise[_0x9f56('0x20')]({'id':_0x9cbd50[_0x9f56('0x21')],'name':_0x9cbd50[_0x9f56('0x22')]});});}});}else{logger[_0x9f56('0x1c')](_0x9f56('0x1e'),_0x4e9778);return _0x5105e3[_0x9f56('0x1f')]()['then'](function(_0x52af97){return BPromise[_0x9f56('0x20')]({'id':_0x52af97['user_id'],'name':_0x52af97[_0x9f56('0x22')]});});}}function getEndUser(_0x1f18db,_0x42e4a4,_0x235474,_0x43925c,_0x476db4){var _0x4de52e={'Phone':_0x43925c};var _0x202b12={'Id':!![],'Name':!![],'Phone':!![]};return BPromise[_0x9f56('0x20')]()[_0x9f56('0x23')](function(){if(_0x42e4a4['ticketType']===_0x9f56('0xf')){logger[_0x9f56('0x1c')](_0x9f56('0x24'),_0x43925c,_0x42e4a4[_0x9f56('0x25')]);switch(_0x42e4a4[_0x9f56('0x25')]){case'contact_lead':return _0x476db4[_0x9f56('0x18')](_0x9f56('0x26'))['find'](_0x4de52e,_0x202b12)[_0x9f56('0x27')]()[_0x9f56('0x23')](function(_0xa54579){if(_0xa54579&&_0xa54579[_0x9f56('0x1a')]){return _0xa54579;}else{return _0x476db4[_0x9f56('0x18')]('Lead')[_0x9f56('0x1b')](_0x4de52e,_0x202b12);}});case _0x9f56('0x28'):return _0x476db4[_0x9f56('0x18')](_0x9f56('0x26'))['find'](_0x4de52e,_0x202b12)[_0x9f56('0x27')]();case'lead':return _0x476db4[_0x9f56('0x18')](_0x9f56('0x29'))[_0x9f56('0x1b')](_0x4de52e,_0x202b12);default:return null;}}else if(_0x42e4a4[_0x9f56('0xe')]===_0x9f56('0x2a')){logger['info']('Searching\x20enduser\x20with\x20phone\x20%s.\x20Serching\x20method\x20used\x20%s.',_0x43925c,_0x9f56('0x28'));return _0x476db4[_0x9f56('0x18')](_0x9f56('0x26'))['find'](_0x4de52e,_0x202b12)[_0x9f56('0x27')]();}})[_0x9f56('0x23')](function(_0x5f0172){if(_0x5f0172&&_0x5f0172[_0x9f56('0x1a')]){var _0x3ca997=_[_0x9f56('0x1b')](_0x5f0172,function(_0x393cbf){return _0x393cbf[_0x9f56('0x2b')]===_0x43925c;});if(_0x3ca997){return BPromise['resolve']({'id':_0x3ca997['Id'],'name':_0x3ca997[_0x9f56('0x2c')],'phone':_0x3ca997['Phone']});}}})[_0x9f56('0x23')](function(_0x5c4bc4){if(_0x5c4bc4){logger[_0x9f56('0x1c')]('Enduser\x20with\x20phone\x20%s\x20found.',_0x43925c);return BPromise[_0x9f56('0x20')](_0x5c4bc4);}else{logger['info']('Enduser\x20with\x20phone\x20%s\x20not\x20found.',_0x43925c);var _0x3bf743={'LastName':_0x235474!==_0x9f56('0x2d')?_0x235474:util[_0x9f56('0x2e')](_0x9f56('0x2f'),_0x43925c),'Phone':_0x43925c};if(_0x42e4a4['moduleCreate']){if(_0x42e4a4['moduleCreate']==='contact'){logger[_0x9f56('0x1c')](_0x9f56('0x30'),_0x43925c,_0x42e4a4['moduleCreate']);}else if(_0x42e4a4[_0x9f56('0x31')]===_0x9f56('0x32')&&_0x42e4a4[_0x9f56('0xe')]===_0x9f56('0xf')){logger[_0x9f56('0x1c')]('Enduser\x20with\x20phone\x20%s.\x20Creating\x20method\x20used\x20%s.',_0x43925c,_0x42e4a4[_0x9f56('0x31')]);_0x3bf743[_0x9f56('0x33')]=_0x9f56('0x34');}else{return null;}}return _0x476db4['sobject'](_['upperFirst'](_0x42e4a4[_0x9f56('0x31')]))[_0x9f56('0x15')](_0x3bf743);}});}function getCustomFields(_0x355cde,_0x3b0bd8){var _0x4c88be={};if(_0x355cde[_0x9f56('0x1a')]>0x0){_[_0x9f56('0x35')](_0x355cde,function(_0x18e572){if(_0x18e572['type']===_0x9f56('0x36')&&_0x18e572[_0x9f56('0x37')]){_0x4c88be[_0x18e572[_0x9f56('0x37')]]=_0x18e572['content']['toString']();}else if(_0x18e572[_0x9f56('0x38')]===_0x9f56('0x39')&&_0x18e572[_0x9f56('0x3a')]&&_0x18e572[_0x9f56('0x37')]){_0x3b0bd8[_0x18e572[_0x9f56('0x3a')]]&&(_0x4c88be[_0x18e572[_0x9f56('0x37')]]=_0x3b0bd8[_0x18e572[_0x9f56('0x3a')]]['toString']());}else if(_0x18e572[_0x9f56('0x38')]===_0x9f56('0x3b')&&_0x18e572['Variable']&&_0x18e572[_0x9f56('0x3c')][_0x9f56('0x3d')]&&_0x18e572[_0x9f56('0x37')]){_0x3b0bd8[_0x18e572['Variable'][_0x9f56('0x3d')][_0x9f56('0x3e')]()]&&(_0x4c88be[_0x18e572[_0x9f56('0x37')]]=_0x3b0bd8[_0x18e572[_0x9f56('0x3c')]['name'][_0x9f56('0x3e')]()][_0x9f56('0x3f')]());}});}return _0x4c88be;}function getConnection(_0x3a20c5){var _0x4de274=_0x3a20c5[_0x9f56('0x40')][_0x9f56('0x41')](_0x3a20c5['securityToken']);var _0x3e1fe0=new jsforce[(_0x9f56('0x42'))]({'oauth2':{'loginUrl':_0x3a20c5[_0x9f56('0x43')]||_0x9f56('0x44'),'clientId':_0x3a20c5['clientId'],'clientSecret':_0x3a20c5[_0x9f56('0x45')]}});return _0x3e1fe0['login'](_0x3a20c5[_0x9f56('0x46')],_0x4de274)[_0x9f56('0x23')](function(){logger[_0x9f56('0x1c')](_0x9f56('0x47'));return _0x3e1fe0;});}exports['queue']=function(_0x1f3d2b,_0x307ed1,_0x14a2c5,_0x5b384b,_0x55dde4,_0x244d51){var _0x3ae426,_0x4f1b4e,_0x2d109e,_0x40404a;_0x244d51[_0x9f56('0x48')]=intUtil[_0x9f56('0x49')](_0x244d51['serverUrl']);_0x244d51['remoteUri']=intUtil['stripTrailingSlash'](_0x244d51[_0x9f56('0x43')]);if(_0x55dde4&&_0x55dde4['monitor_format']){logger['info'](_0x9f56('0x4a'),_0x9f56('0x4b'));_0x307ed1[_0x9f56('0x4c')]=util[_0x9f56('0x2e')](_0x9f56('0x4d'),_0x244d51[_0x9f56('0x48')],_0x307ed1['uniqueid'],md5(_0x307ed1[_0x9f56('0x4e')]));}else{_0x307ed1[_0x9f56('0x4c')]='';}return getConnection(_0x244d51)[_0x9f56('0x23')](function(_0xf2fe09){_0x40404a=_0xf2fe09;return getUser(_0x244d51,_0x5b384b['email'],_0x40404a);})['then'](function(_0x333123){_0x3ae426=_0x333123;return getEndUser(_0x244d51,_0x14a2c5,_0x307ed1[_0x9f56('0x4f')],_0x307ed1[_0x9f56('0x50')],_0x40404a);})['then'](function(_0x3ba3b5){_0x4f1b4e=_0x3ba3b5;return getTicket(_0x244d51,_0x3ae426,_0x4f1b4e,_0x14a2c5,_0x307ed1,_0x14a2c5[_0x9f56('0x51')],_0x40404a,'queue');})[_0x9f56('0x23')](function(_0x538c6a){_0x2d109e=_0x538c6a;logger[_0x9f56('0x1c')](_0x9f56('0x52'),_0x2d109e['id']);logger['info'](_0x9f56('0x53'),_0x2d109e['id'],_0x5b384b[_0x9f56('0x3d')],_0x244d51['type']);switch(_0x244d51[_0x9f56('0x38')]){case _0x9f56('0x54'):emit(util[_0x9f56('0x2e')]('user:%s',_0x5b384b['name']),'trigger:salesforce:display',{'recordId':_0x2d109e['id']});break;case _0x9f56('0x55'):emit(util[_0x9f56('0x2e')]('user:%s',_0x5b384b['name']),_0x9f56('0x56'),{'uri':util['format'](_0x9f56('0x57'),_0x40404a[_0x9f56('0x58')],_0x2d109e['id'])});break;default:logger['error']('[QUEUE]\x20Salesforce\x20account\x20type\x20not\x20recognized.');}})[_0x9f56('0x59')](function(_0xccb675){logger[_0x9f56('0x5a')](_0x9f56('0x4a'),JSON[_0x9f56('0x5b')](_0xccb675));});};exports[_0x9f56('0x5c')]=function(_0x5817d6,_0x39f6e5,_0x4a39f6,_0x131857,_0x703335,_0x4b9ab9,_0x5e39f1){var _0x5d63d9,_0x231387,_0x20f030,_0x1f5cec;_0x4b9ab9[_0x9f56('0x48')]=intUtil[_0x9f56('0x49')](_0x4b9ab9[_0x9f56('0x48')]);_0x4b9ab9['remoteUri']=intUtil[_0x9f56('0x49')](_0x4b9ab9[_0x9f56('0x43')]);if(_0x5e39f1&&_0x5e39f1['recordingFormat']&&_0x5e39f1[_0x9f56('0x5d')]!==_0x9f56('0x5e')){logger[_0x9f56('0x1c')](_0x9f56('0x5f'),_0x9f56('0x4b'));_0x39f6e5['recordingURL']=util[_0x9f56('0x2e')](_0x9f56('0x4d'),_0x4b9ab9['serverUrl'],_0x39f6e5[_0x9f56('0x4e')],md5(_0x39f6e5['uniqueid']));}return getConnection(_0x4b9ab9)['then'](function(_0x3d3969){_0x1f5cec=_0x3d3969;return getUser(_0x4b9ab9,_0x131857['email'],_0x1f5cec);})[_0x9f56('0x23')](function(_0x5e6452){_0x5d63d9=_0x5e6452;return getEndUser(_0x4b9ab9,_0x4a39f6,_0x39f6e5[_0x9f56('0x60')],_0x39f6e5[_0x9f56('0x61')],_0x1f5cec);})['then'](function(_0xa36489){_0x231387=_0xa36489;return getTicket(_0x4b9ab9,_0x5d63d9,_0x231387,_0x4a39f6,_0x39f6e5,_0x4a39f6[_0x9f56('0x51')],_0x1f5cec,'outbound');})[_0x9f56('0x23')](function(_0x4f6d63){_0x20f030=_0x4f6d63;logger['info'](_0x9f56('0x62'),_0x20f030['id']);logger['info']('[OUTBOUND]\x20Displaying\x20ticket\x20%s\x20to\x20agent\x20%s.\x20Integration\x20account\x20type\x20%s.',_0x20f030['id'],_0x131857[_0x9f56('0x3d')],_0x4b9ab9[_0x9f56('0x38')]);switch(_0x4b9ab9['type']){case _0x9f56('0x54'):emit(util['format']('user:%s',_0x131857['name']),'trigger:salesforce:display',{'recordId':_0x20f030['id']});break;case _0x9f56('0x55'):emit(util[_0x9f56('0x2e')](_0x9f56('0x63'),_0x131857[_0x9f56('0x3d')]),_0x9f56('0x56'),{'uri':util[_0x9f56('0x2e')](_0x9f56('0x57'),_0x1f5cec['instanceUrl'],_0x20f030['id'])});break;default:logger[_0x9f56('0x5a')](_0x9f56('0x5f'),_0x9f56('0x64'));}})[_0x9f56('0x59')](function(_0x236438){logger[_0x9f56('0x5a')](_0x9f56('0x5f'),JSON[_0x9f56('0x5b')](_0x236438));});};exports[_0x9f56('0x65')]=function(_0x10ca1d,_0x218a22,_0x5acb0c,_0x157dcd,_0x1b7d7f,_0x22f54e){var _0x127812,_0x1ed436,_0x160dcc,_0x39857d;_0x22f54e[_0x9f56('0x48')]=intUtil['stripTrailingSlash'](_0x22f54e[_0x9f56('0x48')]);_0x22f54e['remoteUri']=intUtil[_0x9f56('0x49')](_0x22f54e[_0x9f56('0x43')]);if(_0x1b7d7f&&_0x1b7d7f['monitor_format']){logger[_0x9f56('0x1c')]('['+_0x218a22[_0x9f56('0x66')][_0x9f56('0x67')]()+']','Recording\x20is\x20enabled!');_0x218a22['recordingURL']=util[_0x9f56('0x2e')](_0x9f56('0x4d'),_0x22f54e[_0x9f56('0x48')],_0x218a22[_0x9f56('0x4e')],md5(_0x218a22[_0x9f56('0x4e')]));}else{_0x218a22[_0x9f56('0x4c')]='';}return getConnection(_0x22f54e)[_0x9f56('0x23')](function(_0x2ed536){_0x39857d=_0x2ed536;if(!_[_0x9f56('0x16')](_0x157dcd)){return getUser(_0x22f54e,_0x157dcd[_0x9f56('0x3d')],_0x39857d);}else{return getUser(_0x22f54e,null,_0x39857d);}})[_0x9f56('0x23')](function(_0x25267e){_0x127812=_0x25267e;return getEndUser(_0x22f54e,_0x5acb0c,_0x218a22[_0x9f56('0x4f')],_0x218a22['calleridnum'],_0x39857d);})['then'](function(_0x5012fb){_0x1ed436=_0x5012fb;return getTicket(_0x22f54e,_0x127812,_0x1ed436,_0x5acb0c,_0x218a22,_0x5acb0c[_0x9f56('0x51')],_0x39857d);})[_0x9f56('0x23')](function(_0x104196){_0x160dcc=_0x104196;logger['info']('['+_0x218a22[_0x9f56('0x66')]['toUpperCase']()+']\x20Ticket\x20%s\x20created.',_0x160dcc['id']);logger['info']('['+_0x218a22['lastevent'][_0x9f56('0x67')]()+_0x9f56('0x68'),_0x22f54e['type']);})[_0x9f56('0x59')](function(_0x437b2e){logger[_0x9f56('0x5a')]('['+_0x218a22[_0x9f56('0x66')][_0x9f56('0x67')]()+']',JSON[_0x9f56('0x5b')](_0x437b2e));});};