9945280053207fad8233544e1bcde58d8eea96b7
[motion2.git] / server / services / ami / trigger / integration / dynamics365.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 _0x525f=['trigger:browser:url','remoteUri','modernAuthentication','stripTrailingSlash','unmanaged','%s/api/data/v9.0/','https://login.microsoftonline.com/%s/oauth2/token','%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s','serverUrl','uniqueid','monitor_format','[QUEUE]','Recording\x20is\x20enabled!','recordingFormat','none','recordingURL',']\x20The\x20User\x20ID\x20is:',']\x20The\x20User\x20email\x20is:','moduleCreate','additionalSearchContact',']\x20The\x20%s\x20ID\x20is:\x20%s','openNewTab','Displaying\x20call\x20in\x20a\x20new\x20tab...','name','error',']\x20Error\x20running\x20Dynamics365\x20integration','Error\x20running\x20Dynamics365\x20integration','lodash','request-promise','util','./util','ioredis','../../../../config/logger','dynamics365','redis','defaults','localhost','socket.io-emitter','data7','tenantId','CloudProvider','data2','POST','username','password','clientSecret','then','access_token','catch','toUpperCase','message','GET','systemusers?$select=internalemailaddress,isdisabled,systemuserid','Bearer\x20','4.0','isNil','validateEmail','email','length','internalemailaddress','info',']\x20Agent\x20found\x20','userId','systemuserid',']\x20Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!','userEmail','toLowerCase','outbound','destcalleridnum','calleridnum','contacts','/systemusers(','body','odata-entityid','substring','lastIndexOf','enduserId','telephone1','&$filter=','\x20eq\x20\x27','replace','value','contactid',']\x20Enduser\x20NOT\x20Created\x20-\x20ModuleCreate\x20=\x20Nothing','string','type','nameField','customField','idField','indexOf','content','incident','incidents','assign','getString','Subjects','Descriptions','/contacts(','Fields','ticketType','phonecall','callDirection','queue','phonecalls','headers','ticketId','format','%s/main.aspx?pagetype=entityrecord&etn=%s&id=%s','user:%s'];(function(_0x581340,_0x590b76){var _0x5bca27=function(_0x21fa2e){while(--_0x21fa2e){_0x581340['push'](_0x581340['shift']());}};_0x5bca27(++_0x590b76);}(_0x525f,0x162));var _0xf525=function(_0x297e92,_0x45d01d){_0x297e92=_0x297e92-0x0;var _0xbe3a5e=_0x525f[_0x297e92];return _0xbe3a5e;};'use strict';var _=require(_0xf525('0x0'));var rp=require(_0xf525('0x1'));var util=require(_0xf525('0x2'));var intUtil=require(_0xf525('0x3'));var md5=require('md5');var Redis=require(_0xf525('0x4'));var logger=require(_0xf525('0x5'))(_0xf525('0x6'));var config=require('../../../../config/environment');config[_0xf525('0x7')]=_[_0xf525('0x8')](config[_0xf525('0x7')],{'host':_0xf525('0x9'),'port':0x18eb});var io=require(_0xf525('0xa'))(new Redis(config[_0xf525('0x7')]));function getAuthToken(_0x4aadd7,_0x2ad4a9,_0x12adad){if(_0x4aadd7['modernAuthentication']){_0x4aadd7['remoteUri']=_0x4aadd7['CloudProvider'][_0xf525('0xb')];_0x4aadd7[_0xf525('0xc')]=_0x4aadd7[_0xf525('0xd')][_0xf525('0xe')];return Promise['resolve'](_0x4aadd7[_0xf525('0xd')]['data5']);}var _0x4841fb={'method':_0xf525('0xf'),'uri':_0x2ad4a9,'form':{'client_id':_0x4aadd7['clientId'],'resource':_0x4aadd7['remoteUri'],'username':_0x4aadd7[_0xf525('0x10')],'password':_0x4aadd7[_0xf525('0x11')],'grant_type':_0xf525('0x11'),'client_secret':_0x4aadd7[_0xf525('0x12')]},'json':!![]};return rp(_0x4841fb)[_0xf525('0x13')](function(_0x5bfb6b){return _0x5bfb6b[_0xf525('0x14')];})[_0xf525('0x15')](function(_0xec1f95){logger['error']('['+[_0x12adad[_0xf525('0x16')]()]+']\x20Wrong\x20credentials!');return _0xec1f95[_0xf525('0x17')];});}function getUser(_0x49fc6b,_0x4393fc,_0xb40668,_0x1531b6,_0x48988d,_0x4ab592){return new Promise(function(_0x5e1e96,_0x5e8a84){return getAuthToken(_0x4393fc,_0x1531b6,_0x4ab592)[_0xf525('0x13')](function(_0x8a548c){var _0x51453f={'method':_0xf525('0x18'),'uri':_0x48988d+_0xf525('0x19'),'headers':{'Authorization':_0xf525('0x1a')+_0x8a548c,'OData-Version':_0xf525('0x1b')},'json':!![]};return rp(_0x51453f);})[_0xf525('0x13')](function(_0x325b40){var _0x441422=_0x325b40['value'];var _0x256a91=![];var _0x3208b1=null;if(!_[_0xf525('0x1c')](_0xb40668)){if(intUtil[_0xf525('0x1d')](_0xb40668[_0xf525('0x1e')])){_0x49fc6b['userEmail']=_0xb40668[_0xf525('0x1e')];for(_0x3208b1=0x0;_0x3208b1<_0x441422[_0xf525('0x1f')];_0x3208b1++){if(_0x441422[_0x3208b1][_0xf525('0x20')]===_0xb40668[_0xf525('0x1e')]&&!_0x441422[_0x3208b1]['isdisabled']){_0x256a91=!![];logger[_0xf525('0x21')]('['+[_0x4ab592['toUpperCase']()]+_0xf525('0x22')+_0x441422[_0x3208b1]['systemuserid']+'!');_0x49fc6b[_0xf525('0x23')]=_0x441422[_0x3208b1][_0xf525('0x24')];}}}}if(!_0x256a91){logger[_0xf525('0x21')]('['+[_0x4ab592['toUpperCase']()]+_0xf525('0x25'));_0x49fc6b[_0xf525('0x26')]=_0x4393fc[_0xf525('0x10')];for(_0x3208b1=0x0;_0x3208b1<_0x441422[_0xf525('0x1f')];_0x3208b1++){if(_0x441422[_0x3208b1]['internalemailaddress']===_0x4393fc['username']){_0x49fc6b['userId']=_0x441422[_0x3208b1][_0xf525('0x24')];}}}_0x5e1e96(_0x49fc6b);})['catch'](function(_0x24ea20){_0x5e8a84(_0x24ea20['message']);});});}function createEndUser(_0x8b42c6,_0x573a26,_0x4908ac,_0x34ea70,_0x565cec,_0x3b8ecb,_0x2dfda7){return new Promise(function(_0x150300,_0x79d942){return getAuthToken(_0x573a26,_0x4908ac,_0x3b8ecb)[_0xf525('0x13')](function(_0x2c83fb){var _0x1d4067=_0x3b8ecb[_0xf525('0x27')]()===_0xf525('0x28')?_0x565cec[_0xf525('0x29')]:_0x565cec[_0xf525('0x2a')];var _0x44045f={'method':_0xf525('0xf'),'uri':_0x34ea70+_0xf525('0x2b'),'body':{'fullname':_0x1d4067,'firstname':_0x1d4067,'lastname':'_','mobilephone':null,'address1_telephone1':null,'ownerid@odata.bind':_0xf525('0x2c')+_0x8b42c6[_0xf525('0x23')]+')'},'headers':{'Authorization':_0xf525('0x1a')+_0x2c83fb,'OData-Version':_0xf525('0x1b')},'json':!![],'resolveWithFullResponse':!![]};_0x44045f[_0xf525('0x2d')][_0x2dfda7]=_0x1d4067;return rp(_0x44045f);})['then'](function(_0x1dae18){var _0x443cc9=_0x1dae18['headers'][_0xf525('0x2e')];var _0x133f48=_0x443cc9[_0xf525('0x2f')](_0x443cc9['lastIndexOf']('(')+0x1,_0x443cc9[_0xf525('0x30')](')'));_0x8b42c6[_0xf525('0x31')]=_0x133f48;_0x150300(_0x8b42c6);})[_0xf525('0x15')](function(_0x288cf7){_0x79d942(_0x288cf7['message']);});});}function getEndUser(_0x5f3252,_0x45e161,_0x4aebf4,_0x5529e6,_0x89c989,_0x377df3,_0x4a3ea3,_0x33c448){var _0x57c4af=_0x377df3[_0xf525('0x27')]()===_0xf525('0x28')?_0x89c989[_0xf525('0x29')]:_0x89c989['calleridnum'];return getAuthToken(_0x45e161,_0x4aebf4,_0x377df3)[_0xf525('0x13')](function(_0x205ee4){_0x33c448=_0x33c448?_0x33c448:_0xf525('0x32');var _0x1a76e6={'method':_0xf525('0x18'),'uri':_0x5529e6+'contacts?$select='+_0x33c448+_0xf525('0x33')+_0x33c448+_0xf525('0x34')+_0x57c4af[_0xf525('0x35')]('+','%2b')+'\x27','headers':{'Authorization':_0xf525('0x1a')+_0x205ee4,'If-None-Match':null,'OData-Version':_0xf525('0x1b')},'json':!![]};return rp(_0x1a76e6);})[_0xf525('0x13')](function(_0x2d441f){var _0x2be4bc=![];if(!_0x2d441f)return _0x2be4bc;var _0x3c40a8=_0x2d441f[_0xf525('0x36')];for(var _0x2e898b=0x0;_0x2e898b<_0x3c40a8['length'];_0x2e898b++){if(_0x3c40a8[_0x2e898b][_0x33c448]===_0x57c4af){_0x2be4bc=!![];_0x5f3252['enduserId']=_0x3c40a8[_0x2e898b][_0xf525('0x37')];}}return _0x2be4bc;})[_0xf525('0x13')](function(_0x744d8f){if(_0x744d8f)return _0x5f3252;if(_0x4a3ea3==='nothing'){logger['info']('['+[_0x377df3[_0xf525('0x16')]()]+_0xf525('0x38'));return _0x5f3252;}return createEndUser(_0x5f3252,_0x45e161,_0x4aebf4,_0x5529e6,_0x89c989,_0x377df3,_0x33c448)[_0xf525('0x13')](function(_0x3e059f){logger[_0xf525('0x21')]('['+[_0x377df3[_0xf525('0x16')]()]+']\x20Enduser\x20created!',_0x3e059f[_0xf525('0x31')]);return _0x3e059f;});});}function getCustomFields(_0x4c277c,_0x5404e3,_0x2f7771){var _0x1dc29e={};if(_0x4c277c[_0xf525('0x1f')]>0x0){_['forEach'](_0x4c277c,function(_0x5bf206){if(_['includes']([_0xf525('0x39'),'picklist'],_0x5bf206[_0xf525('0x3a')])&&_0x5bf206[_0xf525('0x3b')]&&!_0x5bf206[_0xf525('0x3c')]){var _0x38cf04=_0x5bf206[_0xf525('0x3d')][_0xf525('0x27')]();var _0x48dea6=_0x38cf04[_0xf525('0x2f')](0x0,_0x38cf04[_0xf525('0x3e')]('_'));var _0x2fd781=_0x38cf04[_0xf525('0x2f')](_0x38cf04[_0xf525('0x3e')]('_')+0x1);if(_0x48dea6===_0x2f7771){_0x1dc29e[_0x2fd781]=_0x5bf206[_0xf525('0x3f')];}}});}return _0x1dc29e;}function getTicket(_0x3c0b90,_0x56da59,_0x19d875,_0x4d3bb1,_0x5216c2,_0x402380,_0xe07dbb){return new Promise(function(_0x331241,_0xf60e4a){var _0x1e9917;return getAuthToken(_0x56da59,_0x19d875,_0xe07dbb)['then'](function(_0x37ae1a){if(_0x402380['ticketType']===_0xf525('0x40')){_0x1e9917={'method':_0xf525('0xf'),'uri':_0x4d3bb1+_0xf525('0x41'),'body':Object[_0xf525('0x42')]({},{'title':intUtil[_0xf525('0x43')](_0x402380[_0xf525('0x44')],_0x5216c2,'\x20'),'description':intUtil[_0xf525('0x43')](_0x402380[_0xf525('0x45')],_0x5216c2,'\x0a'),'customerid_contact@odata.bind':_0x3c0b90[_0xf525('0x31')]?_0xf525('0x46')+_0x3c0b90[_0xf525('0x31')]+')':undefined,'ownerid@odata.bind':_0xf525('0x2c')+_0x3c0b90[_0xf525('0x23')]+')','caseorigincode':'1'},getCustomFields(_0x402380[_0xf525('0x47')],_0x5216c2,_0x402380['ticketType'])),'headers':{'Authorization':_0xf525('0x1a')+_0x37ae1a,'OData-Version':'4.0'},'json':!![],'resolveWithFullResponse':!![]};}if(_0x402380[_0xf525('0x48')]===_0xf525('0x49')){var _0x1e7526=_0x3c0b90[_0xf525('0x31')]?[{'partyid_systemuser@odata.bind':'/systemusers('+_0x3c0b90['userId']+')','participationtypemask':_0x3c0b90[_0xf525('0x4a')]===_0xf525('0x4b')?0x2:0x1},{'partyid_contact@odata.bind':'/contacts('+_0x3c0b90['enduserId']+')','participationtypemask':_0x3c0b90[_0xf525('0x4a')]===_0xf525('0x4b')?0x1:0x2}]:[{'partyid_systemuser@odata.bind':_0xf525('0x2c')+_0x3c0b90[_0xf525('0x23')]+')','participationtypemask':_0x3c0b90[_0xf525('0x4a')]===_0xf525('0x4b')?0x2:0x1}];_0x1e9917={'method':_0xf525('0xf'),'uri':_0x4d3bb1+_0xf525('0x4c'),'body':Object[_0xf525('0x42')]({},{'subject':intUtil[_0xf525('0x43')](_0x402380[_0xf525('0x44')],_0x5216c2,'\x20'),'phonenumber':String(_0x5216c2[_0xf525('0x2a')]),'description':intUtil[_0xf525('0x43')](_0x402380[_0xf525('0x45')],_0x5216c2,'\x0a'),'regardingobjectid_contact@odata.bind':_0x3c0b90[_0xf525('0x31')]?_0xf525('0x46')+_0x3c0b90[_0xf525('0x31')]+')':undefined,'ownerid@odata.bind':_0xf525('0x2c')+_0x3c0b90[_0xf525('0x23')]+')','directioncode':_0x3c0b90['callDirection']==='queue'?![]:!![],'phonecall_activity_parties':_0x1e7526},getCustomFields(_0x402380[_0xf525('0x47')],_0x5216c2,_0x402380[_0xf525('0x48')])),'headers':{'Authorization':_0xf525('0x1a')+_0x37ae1a,'OData-Version':_0xf525('0x1b')},'json':!![],'resolveWithFullResponse':!![]};}return rp(_0x1e9917);})[_0xf525('0x13')](function(_0x4d12a0){var _0x3e546d=_0x4d12a0[_0xf525('0x4d')][_0xf525('0x2e')];var _0x34d32d=_0x3e546d[_0xf525('0x2f')](_0x3e546d[_0xf525('0x30')]('(')+0x1,_0x3e546d[_0xf525('0x30')](')'));_0x3c0b90[_0xf525('0x4e')]=_0x34d32d;_0x331241(_0x3c0b90);})[_0xf525('0x15')](function(_0x24af56){_0xf60e4a(_0x24af56['message']);});});}function emit(_0x265b44,_0x3bf4b3,_0x37f70d,_0x3391bb){var _0x1c5bc7={'uri':util[_0xf525('0x4f')](_0xf525('0x50'),_0x265b44,_0x37f70d,_0x3391bb)};io['to'](util['format'](_0xf525('0x51'),_0x3bf4b3))['emit'](_0xf525('0x52'),_0x1c5bc7);}exports['cs']=function(_0x55e424,_0x469b5a,_0x14f09a,_0x49c63c,_0x391aee,_0x1f8de2,_0x23f63f,_0x1d9efb){var _0x77762b={};_0x1f8de2[_0xf525('0x53')]=_0x1f8de2[_0xf525('0x54')]?intUtil[_0xf525('0x55')](_0x1f8de2[_0xf525('0xd')][_0xf525('0xb')]):intUtil['stripTrailingSlash'](_0x1f8de2[_0xf525('0x53')]);_0x1f8de2['serverUrl']=intUtil[_0xf525('0x55')](_0x1f8de2['serverUrl']);_0x77762b[_0xf525('0x4a')]=_0x1d9efb===_0xf525('0x4b')||_0x1d9efb===_0xf525('0x56')?_0xf525('0x4b'):_0xf525('0x28');var _0x2efb3a=_0x1f8de2[_0xf525('0x54')]?util[_0xf525('0x4f')]('%s/api/data/v9.0/',_0x1f8de2[_0xf525('0xd')][_0xf525('0xb')]):util[_0xf525('0x4f')](_0xf525('0x57'),_0x1f8de2[_0xf525('0x53')]);var _0x307959=util[_0xf525('0x4f')](_0xf525('0x58'),_0x1f8de2[_0xf525('0xc')]);var _0xb7ddf2=util[_0xf525('0x4f')](_0xf525('0x59'),_0x1f8de2[_0xf525('0x5a')],_0x469b5a[_0xf525('0x5b')],md5(_0x469b5a[_0xf525('0x5b')]));if(_0x391aee&&_0x391aee[_0xf525('0x5c')]){logger[_0xf525('0x21')](_0xf525('0x5d'),_0xf525('0x5e'));_0x469b5a['recordingURL']=_0xb7ddf2;}else if(_0x23f63f&&_0x23f63f[_0xf525('0x5f')]&&_0x23f63f[_0xf525('0x5f')]!==_0xf525('0x60')){logger[_0xf525('0x21')]('[OUTBOUND]',_0xf525('0x5e'));_0x469b5a['recordingURL']=_0xb7ddf2;}else{_0x469b5a[_0xf525('0x61')]='';}return getUser(_0x77762b,_0x1f8de2,_0x49c63c,_0x307959,_0x2efb3a,_0x1d9efb)['then'](function(_0x50465d){logger[_0xf525('0x21')]('['+_0x1d9efb[_0xf525('0x16')]()+_0xf525('0x62'),_0x50465d[_0xf525('0x23')]);logger[_0xf525('0x21')]('['+_0x1d9efb['toUpperCase']()+_0xf525('0x63'),_0x50465d[_0xf525('0x26')]);return getEndUser(_0x50465d,_0x1f8de2,_0x307959,_0x2efb3a,_0x469b5a,_0x1d9efb,_0x14f09a[_0xf525('0x64')],_0x14f09a[_0xf525('0x65')]);})[_0xf525('0x13')](function(_0x31c71d){logger[_0xf525('0x21')]('['+_0x1d9efb[_0xf525('0x16')]()+']\x20The\x20Enduser\x20ID\x20is:',_0x31c71d['enduserId']);logger[_0xf525('0x21')]('['+_0x1d9efb[_0xf525('0x16')]()+']\x20The\x20Enduser\x20phone\x20is:',_0x469b5a[_0xf525('0x2a')]);return getTicket(_0x31c71d,_0x1f8de2,_0x307959,_0x2efb3a,_0x469b5a,_0x14f09a,_0x1d9efb);})[_0xf525('0x13')](function(_0x57dd46){logger[_0xf525('0x21')](util[_0xf525('0x4f')]('['+_0x1d9efb[_0xf525('0x16')]()+_0xf525('0x66'),_0x14f09a['ticketType'],_0x57dd46[_0xf525('0x4e')]));logger[_0xf525('0x21')](util[_0xf525('0x4f')]('['+_0x1d9efb[_0xf525('0x16')]()+']\x20Call\x20direction:\x20%s',_0x1d9efb));if(_0x1d9efb!==_0xf525('0x56')&&_0x14f09a[_0xf525('0x67')]){logger[_0xf525('0x21')]('['+_0x1d9efb[_0xf525('0x16')]()+']',_0xf525('0x68'));emit(_0x1f8de2[_0xf525('0x53')],_0x49c63c[_0xf525('0x69')],_0x14f09a[_0xf525('0x48')],_0x57dd46[_0xf525('0x4e')]);}})['catch'](function(_0x52fe89){logger[_0xf525('0x6a')]('['+_0x1d9efb[_0xf525('0x16')]()+_0xf525('0x6b'),_0x52fe89[_0xf525('0x17')]);console[_0xf525('0x6a')](_0xf525('0x6c'),_0x52fe89);});};