ce0fde19b453adf4364ebb1cbd3cb5bf02028e9e
[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 _0x8bc7=['value','isNil','validateEmail','email','userEmail','length','internalemailaddress','isdisabled','info',']\x20Agent\x20found\x20','systemuserid','userId',']\x20Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!','username','toLowerCase','outbound','destcalleridnum','/systemusers(','body','headers','odata-entityid','substring','lastIndexOf','calleridnum','contacts?$select=','\x20eq\x20\x27','replace','enduserId','contactid','nothing',']\x20Enduser\x20created!','forEach','includes','string','type','idField','indexOf','content','ticketType','incidents','assign','getString','Subjects','Fields','phonecall','callDirection','/contacts(','queue','phonecalls','format','%s/main.aspx?pagetype=entityrecord&etn=%s&id=%s','emit','trigger:browser:url','stripTrailingSlash','data7','serverUrl','unmanaged','modernAuthentication','https://login.microsoftonline.com/%s/oauth2/token','tenantId','uniqueid','monitor_format','[QUEUE]','recordingURL','none','[OUTBOUND]',']\x20The\x20User\x20ID\x20is:',']\x20The\x20Enduser\x20ID\x20is:','ticketId',']\x20Call\x20direction:\x20%s','Displaying\x20call\x20in\x20a\x20new\x20tab...','name',']\x20Error\x20running\x20Dynamics365\x20integration','Error\x20running\x20Dynamics365\x20integration','util','./util','../../../../config/logger','../../../../config/environment','redis','defaults','localhost','socket.io-emitter','remoteUri','CloudProvider','data2','resolve','data5','POST','clientId','password','clientSecret','then','access_token','catch','error','toUpperCase',']\x20Wrong\x20credentials!','message','GET','systemusers?$select=internalemailaddress,isdisabled,systemuserid','Bearer\x20','4.0'];(function(_0x3070dc,_0x40cf11){var _0x431489=function(_0x49bc2a){while(--_0x49bc2a){_0x3070dc['push'](_0x3070dc['shift']());}};_0x431489(++_0x40cf11);}(_0x8bc7,0x116));var _0x78bc=function(_0x366e5c,_0x57d4bb){_0x366e5c=_0x366e5c-0x0;var _0x4b99dc=_0x8bc7[_0x366e5c];return _0x4b99dc;};'use strict';var _=require('lodash');var rp=require('request-promise');var util=require(_0x78bc('0x0'));var intUtil=require(_0x78bc('0x1'));var md5=require('md5');var Redis=require('ioredis');var logger=require(_0x78bc('0x2'))('dynamics365');var config=require(_0x78bc('0x3'));config[_0x78bc('0x4')]=_[_0x78bc('0x5')](config[_0x78bc('0x4')],{'host':_0x78bc('0x6'),'port':0x18eb});var io=require(_0x78bc('0x7'))(new Redis(config[_0x78bc('0x4')]));function getAuthToken(_0xa6775c,_0x20241a,_0x4d55d8){if(_0xa6775c['modernAuthentication']){_0xa6775c[_0x78bc('0x8')]=_0xa6775c[_0x78bc('0x9')]['data7'];_0xa6775c['tenantId']=_0xa6775c[_0x78bc('0x9')][_0x78bc('0xa')];return Promise[_0x78bc('0xb')](_0xa6775c['CloudProvider'][_0x78bc('0xc')]);}var _0x162067={'method':_0x78bc('0xd'),'uri':_0x20241a,'form':{'client_id':_0xa6775c[_0x78bc('0xe')],'resource':_0xa6775c[_0x78bc('0x8')],'username':_0xa6775c['username'],'password':_0xa6775c[_0x78bc('0xf')],'grant_type':'password','client_secret':_0xa6775c[_0x78bc('0x10')]},'json':!![]};return rp(_0x162067)[_0x78bc('0x11')](function(_0x2ecb42){return _0x2ecb42[_0x78bc('0x12')];})[_0x78bc('0x13')](function(_0x15e5b1){logger[_0x78bc('0x14')]('['+[_0x4d55d8[_0x78bc('0x15')]()]+_0x78bc('0x16'));return _0x15e5b1[_0x78bc('0x17')];});}function getUser(_0x25f833,_0xbc95a8,_0x1b952c,_0x14b8ce,_0x43679e,_0x1199e7){return new Promise(function(_0x3d6640,_0x1ec345){return getAuthToken(_0xbc95a8,_0x14b8ce,_0x1199e7)[_0x78bc('0x11')](function(_0x4643c8){var _0x14deb4={'method':_0x78bc('0x18'),'uri':_0x43679e+_0x78bc('0x19'),'headers':{'Authorization':_0x78bc('0x1a')+_0x4643c8,'OData-Version':_0x78bc('0x1b')},'json':!![]};return rp(_0x14deb4);})['then'](function(_0x5d84fd){var _0x4d591c=_0x5d84fd[_0x78bc('0x1c')];var _0x52ca6b=![];var _0x70ccde=null;if(!_[_0x78bc('0x1d')](_0x1b952c)){if(intUtil[_0x78bc('0x1e')](_0x1b952c[_0x78bc('0x1f')])){_0x25f833[_0x78bc('0x20')]=_0x1b952c[_0x78bc('0x1f')];for(_0x70ccde=0x0;_0x70ccde<_0x4d591c[_0x78bc('0x21')];_0x70ccde++){if(_0x4d591c[_0x70ccde][_0x78bc('0x22')]===_0x1b952c['email']&&!_0x4d591c[_0x70ccde][_0x78bc('0x23')]){_0x52ca6b=!![];logger[_0x78bc('0x24')]('['+[_0x1199e7[_0x78bc('0x15')]()]+_0x78bc('0x25')+_0x4d591c[_0x70ccde][_0x78bc('0x26')]+'!');_0x25f833[_0x78bc('0x27')]=_0x4d591c[_0x70ccde][_0x78bc('0x26')];}}}}if(!_0x52ca6b){logger['info']('['+[_0x1199e7['toUpperCase']()]+_0x78bc('0x28'));_0x25f833[_0x78bc('0x20')]=_0xbc95a8[_0x78bc('0x29')];for(_0x70ccde=0x0;_0x70ccde<_0x4d591c[_0x78bc('0x21')];_0x70ccde++){if(_0x4d591c[_0x70ccde]['internalemailaddress']===_0xbc95a8['username']){_0x25f833[_0x78bc('0x27')]=_0x4d591c[_0x70ccde]['systemuserid'];}}}_0x3d6640(_0x25f833);})['catch'](function(_0x4410bd){_0x1ec345(_0x4410bd[_0x78bc('0x17')]);});});}function createEndUser(_0x1aba1d,_0x325105,_0x5b7ec4,_0x4edd9c,_0x3cb28e,_0x2890e9,_0x1338d1){return new Promise(function(_0xdaeaa8,_0x39f239){return getAuthToken(_0x325105,_0x5b7ec4,_0x2890e9)[_0x78bc('0x11')](function(_0xf9414c){var _0x4e50b2=_0x2890e9[_0x78bc('0x2a')]()===_0x78bc('0x2b')?_0x3cb28e[_0x78bc('0x2c')]:_0x3cb28e['calleridnum'];var _0xe4ee8f={'method':_0x78bc('0xd'),'uri':_0x4edd9c+'contacts','body':{'fullname':_0x4e50b2,'firstname':_0x4e50b2,'lastname':'_','mobilephone':null,'address1_telephone1':null,'ownerid@odata.bind':_0x78bc('0x2d')+_0x1aba1d['userId']+')'},'headers':{'Authorization':_0x78bc('0x1a')+_0xf9414c,'OData-Version':_0x78bc('0x1b')},'json':!![],'resolveWithFullResponse':!![]};_0xe4ee8f[_0x78bc('0x2e')][_0x1338d1]=_0x4e50b2;return rp(_0xe4ee8f);})[_0x78bc('0x11')](function(_0x3fca05){var _0x2c2499=_0x3fca05[_0x78bc('0x2f')][_0x78bc('0x30')];var _0x383ada=_0x2c2499[_0x78bc('0x31')](_0x2c2499[_0x78bc('0x32')]('(')+0x1,_0x2c2499[_0x78bc('0x32')](')'));_0x1aba1d['enduserId']=_0x383ada;_0xdaeaa8(_0x1aba1d);})['catch'](function(_0x157f2f){_0x39f239(_0x157f2f[_0x78bc('0x17')]);});});}function getEndUser(_0x4d0d3d,_0x238526,_0x248084,_0x2a8068,_0x29c374,_0x373df2,_0x4bf438,_0x16d694){var _0x5ed138=_0x373df2[_0x78bc('0x2a')]()===_0x78bc('0x2b')?_0x29c374['destcalleridnum']:_0x29c374[_0x78bc('0x33')];return getAuthToken(_0x238526,_0x248084,_0x373df2)[_0x78bc('0x11')](function(_0x525125){_0x16d694=_0x16d694?_0x16d694:'telephone1';var _0x1a20f9={'method':_0x78bc('0x18'),'uri':_0x2a8068+_0x78bc('0x34')+_0x16d694+'&$filter='+_0x16d694+_0x78bc('0x35')+_0x5ed138[_0x78bc('0x36')]('+','%2b')+'\x27','headers':{'Authorization':_0x78bc('0x1a')+_0x525125,'If-None-Match':null,'OData-Version':'4.0'},'json':!![]};return rp(_0x1a20f9);})[_0x78bc('0x11')](function(_0x5653cc){var _0x337f6c=![];if(!_0x5653cc)return _0x337f6c;var _0x3ef38f=_0x5653cc[_0x78bc('0x1c')];for(var _0x4bfd04=0x0;_0x4bfd04<_0x3ef38f[_0x78bc('0x21')];_0x4bfd04++){if(_0x3ef38f[_0x4bfd04][_0x16d694]===_0x5ed138){_0x337f6c=!![];_0x4d0d3d[_0x78bc('0x37')]=_0x3ef38f[_0x4bfd04][_0x78bc('0x38')];}}return _0x337f6c;})[_0x78bc('0x11')](function(_0x325758){if(_0x325758)return _0x4d0d3d;if(_0x4bf438===_0x78bc('0x39')){logger[_0x78bc('0x24')]('['+[_0x373df2[_0x78bc('0x15')]()]+']\x20Enduser\x20NOT\x20Created\x20-\x20ModuleCreate\x20=\x20Nothing');return _0x4d0d3d;}return createEndUser(_0x4d0d3d,_0x238526,_0x248084,_0x2a8068,_0x29c374,_0x373df2,_0x16d694)['then'](function(_0x542533){logger[_0x78bc('0x24')]('['+[_0x373df2[_0x78bc('0x15')]()]+_0x78bc('0x3a'),_0x542533[_0x78bc('0x37')]);return _0x542533;});});}function getCustomFields(_0x39580f,_0x1854a3,_0x4232f1){var _0x5cf5a5={};if(_0x39580f[_0x78bc('0x21')]>0x0){_[_0x78bc('0x3b')](_0x39580f,function(_0x32526f){if(_[_0x78bc('0x3c')]([_0x78bc('0x3d'),'picklist'],_0x32526f[_0x78bc('0x3e')])&&_0x32526f['nameField']&&!_0x32526f['customField']){var _0x5c151f=_0x32526f[_0x78bc('0x3f')][_0x78bc('0x2a')]();var _0x1e1a7d=_0x5c151f[_0x78bc('0x31')](0x0,_0x5c151f['indexOf']('_'));var _0xedfc5c=_0x5c151f[_0x78bc('0x31')](_0x5c151f[_0x78bc('0x40')]('_')+0x1);if(_0x1e1a7d===_0x4232f1){_0x5cf5a5[_0xedfc5c]=_0x32526f[_0x78bc('0x41')];}}});}return _0x5cf5a5;}function getTicket(_0x5c1942,_0x47631c,_0x499e0d,_0x9cb176,_0x25f725,_0x25f03e,_0x29ddff){return new Promise(function(_0x81ffc0,_0x420c5e){var _0x2594fc;return getAuthToken(_0x47631c,_0x499e0d,_0x29ddff)[_0x78bc('0x11')](function(_0x4791b4){if(_0x25f03e[_0x78bc('0x42')]==='incident'){_0x2594fc={'method':_0x78bc('0xd'),'uri':_0x9cb176+_0x78bc('0x43'),'body':Object[_0x78bc('0x44')]({},{'title':intUtil[_0x78bc('0x45')](_0x25f03e[_0x78bc('0x46')],_0x25f725,'\x20'),'description':intUtil[_0x78bc('0x45')](_0x25f03e['Descriptions'],_0x25f725,'\x0a'),'customerid_contact@odata.bind':_0x5c1942[_0x78bc('0x37')]?'/contacts('+_0x5c1942[_0x78bc('0x37')]+')':undefined,'ownerid@odata.bind':_0x78bc('0x2d')+_0x5c1942[_0x78bc('0x27')]+')','caseorigincode':'1'},getCustomFields(_0x25f03e[_0x78bc('0x47')],_0x25f725,_0x25f03e[_0x78bc('0x42')])),'headers':{'Authorization':_0x78bc('0x1a')+_0x4791b4,'OData-Version':_0x78bc('0x1b')},'json':!![],'resolveWithFullResponse':!![]};}if(_0x25f03e['ticketType']===_0x78bc('0x48')){var _0x310e95=_0x5c1942[_0x78bc('0x37')]?[{'partyid_systemuser@odata.bind':_0x78bc('0x2d')+_0x5c1942[_0x78bc('0x27')]+')','participationtypemask':_0x5c1942[_0x78bc('0x49')]==='queue'?0x2:0x1},{'partyid_contact@odata.bind':_0x78bc('0x4a')+_0x5c1942['enduserId']+')','participationtypemask':_0x5c1942['callDirection']===_0x78bc('0x4b')?0x1:0x2}]:[{'partyid_systemuser@odata.bind':_0x78bc('0x2d')+_0x5c1942['userId']+')','participationtypemask':_0x5c1942['callDirection']===_0x78bc('0x4b')?0x2:0x1}];_0x2594fc={'method':'POST','uri':_0x9cb176+_0x78bc('0x4c'),'body':Object[_0x78bc('0x44')]({},{'subject':intUtil[_0x78bc('0x45')](_0x25f03e[_0x78bc('0x46')],_0x25f725,'\x20'),'phonenumber':String(_0x25f725[_0x78bc('0x33')]),'description':intUtil[_0x78bc('0x45')](_0x25f03e['Descriptions'],_0x25f725,'\x0a'),'regardingobjectid_contact@odata.bind':_0x5c1942['enduserId']?'/contacts('+_0x5c1942[_0x78bc('0x37')]+')':undefined,'ownerid@odata.bind':'/systemusers('+_0x5c1942['userId']+')','directioncode':_0x5c1942[_0x78bc('0x49')]==='queue'?![]:!![],'phonecall_activity_parties':_0x310e95},getCustomFields(_0x25f03e[_0x78bc('0x47')],_0x25f725,_0x25f03e[_0x78bc('0x42')])),'headers':{'Authorization':_0x78bc('0x1a')+_0x4791b4,'OData-Version':_0x78bc('0x1b')},'json':!![],'resolveWithFullResponse':!![]};}return rp(_0x2594fc);})[_0x78bc('0x11')](function(_0x4d3480){var _0x5d51b8=_0x4d3480[_0x78bc('0x2f')][_0x78bc('0x30')];var _0x4bbabc=_0x5d51b8['substring'](_0x5d51b8['lastIndexOf']('(')+0x1,_0x5d51b8[_0x78bc('0x32')](')'));_0x5c1942['ticketId']=_0x4bbabc;_0x81ffc0(_0x5c1942);})[_0x78bc('0x13')](function(_0x45a198){_0x420c5e(_0x45a198['message']);});});}function emit(_0x2b693e,_0x5b8439,_0x224a26,_0x5168ea){var _0x108760={'uri':util[_0x78bc('0x4d')](_0x78bc('0x4e'),_0x2b693e,_0x224a26,_0x5168ea)};io['to'](util['format']('user:%s',_0x5b8439))[_0x78bc('0x4f')](_0x78bc('0x50'),_0x108760);}exports['cs']=function(_0x5031b0,_0x2b280e,_0x536757,_0x29c218,_0x1b2bdb,_0x2a6922,_0x2d46ee,_0x2f0ec3){var _0x1fb50={};_0x2a6922[_0x78bc('0x8')]=_0x2a6922['modernAuthentication']?intUtil[_0x78bc('0x51')](_0x2a6922[_0x78bc('0x9')][_0x78bc('0x52')]):intUtil[_0x78bc('0x51')](_0x2a6922[_0x78bc('0x8')]);_0x2a6922[_0x78bc('0x53')]=intUtil[_0x78bc('0x51')](_0x2a6922[_0x78bc('0x53')]);_0x1fb50[_0x78bc('0x49')]=_0x2f0ec3===_0x78bc('0x4b')||_0x2f0ec3===_0x78bc('0x54')?_0x78bc('0x4b'):_0x78bc('0x2b');var _0x4c4662=_0x2a6922[_0x78bc('0x55')]?util[_0x78bc('0x4d')]('%s/api/data/v9.0/',_0x2a6922['CloudProvider']['data7']):util[_0x78bc('0x4d')]('%s/api/data/v9.0/',_0x2a6922[_0x78bc('0x8')]);var _0x394410=util[_0x78bc('0x4d')](_0x78bc('0x56'),_0x2a6922[_0x78bc('0x57')]);var _0x21e78f=util[_0x78bc('0x4d')]('%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s',_0x2a6922['serverUrl'],_0x2b280e[_0x78bc('0x58')],md5(_0x2b280e[_0x78bc('0x58')]));if(_0x1b2bdb&&_0x1b2bdb[_0x78bc('0x59')]){logger[_0x78bc('0x24')](_0x78bc('0x5a'),'Recording\x20is\x20enabled!');_0x2b280e[_0x78bc('0x5b')]=_0x21e78f;}else if(_0x2d46ee&&_0x2d46ee['recordingFormat']&&_0x2d46ee['recordingFormat']!==_0x78bc('0x5c')){logger[_0x78bc('0x24')](_0x78bc('0x5d'),'Recording\x20is\x20enabled!');_0x2b280e[_0x78bc('0x5b')]=_0x21e78f;}else{_0x2b280e['recordingURL']='';}return getUser(_0x1fb50,_0x2a6922,_0x29c218,_0x394410,_0x4c4662,_0x2f0ec3)[_0x78bc('0x11')](function(_0x3b2e4f){logger['info']('['+_0x2f0ec3['toUpperCase']()+_0x78bc('0x5e'),_0x3b2e4f[_0x78bc('0x27')]);logger[_0x78bc('0x24')]('['+_0x2f0ec3[_0x78bc('0x15')]()+']\x20The\x20User\x20email\x20is:',_0x3b2e4f[_0x78bc('0x20')]);return getEndUser(_0x3b2e4f,_0x2a6922,_0x394410,_0x4c4662,_0x2b280e,_0x2f0ec3,_0x536757['moduleCreate'],_0x536757['additionalSearchContact']);})[_0x78bc('0x11')](function(_0x149b4f){logger[_0x78bc('0x24')]('['+_0x2f0ec3['toUpperCase']()+_0x78bc('0x5f'),_0x149b4f[_0x78bc('0x37')]);logger['info']('['+_0x2f0ec3['toUpperCase']()+']\x20The\x20Enduser\x20phone\x20is:',_0x2b280e['calleridnum']);return getTicket(_0x149b4f,_0x2a6922,_0x394410,_0x4c4662,_0x2b280e,_0x536757,_0x2f0ec3);})[_0x78bc('0x11')](function(_0x4056ca){logger['info'](util['format']('['+_0x2f0ec3[_0x78bc('0x15')]()+']\x20The\x20%s\x20ID\x20is:\x20%s',_0x536757[_0x78bc('0x42')],_0x4056ca[_0x78bc('0x60')]));logger[_0x78bc('0x24')](util[_0x78bc('0x4d')]('['+_0x2f0ec3[_0x78bc('0x15')]()+_0x78bc('0x61'),_0x2f0ec3));if(_0x2f0ec3!==_0x78bc('0x54')&&_0x536757['openNewTab']){logger[_0x78bc('0x24')]('['+_0x2f0ec3[_0x78bc('0x15')]()+']',_0x78bc('0x62'));emit(_0x2a6922[_0x78bc('0x8')],_0x29c218[_0x78bc('0x63')],_0x536757['ticketType'],_0x4056ca[_0x78bc('0x60')]);}})[_0x78bc('0x13')](function(_0x270dcf){logger[_0x78bc('0x14')]('['+_0x2f0ec3[_0x78bc('0x15')]()+_0x78bc('0x64'),_0x270dcf[_0x78bc('0x17')]);console[_0x78bc('0x14')](_0x78bc('0x65'),_0x270dcf);});};