Built motion from commit ecdc8e66766c75b1b08288b0ea13eb12d42606b4. Version 3.0.0...
[motion-next.git] / server / services / ami / trigger / integration / dynamics365.js
1 const a1136_0x1a9a=['dynamics365','incidents','content','callDirection','userEmail','toUpperCase','Recording\x20is\x20enabled!',']\x20The\x20Enduser\x20phone\x20is:','clientId','contactid','enduserId','username','getString','remoteUri','phonecalls','isNil','unmanaged','lodash','tenantId','ticketId','%s/main.aspx?pagetype=entityrecord&etn=%s&id=%s','incident','destcalleridnum','trigger:browser:url','recordingURL','request-promise-native','name','error','md5','outbound','info','telephone1','%s/api/data/v9.0/','substring','message','then','toLowerCase','validateEmail','value','clientSecret',']\x20Error\x20running\x20Dynamics365\x20integration','length','type','string','lastIndexOf','inspect','Descriptions',']\x20Agent\x20found\x20','../../../../config/logger','isdisabled','forEach','/systemusers(','Subjects','Bearer\x20',']\x20Enduser\x20Created!','assign','nameField','userId','contacts','/contacts(','emit','4.0','[QUEUE]','serverUrl','systemuserid','headers','stripTrailingSlash','calleridnum',']\x20The\x20User\x20ID\x20is:',']\x20The\x20User\x20email\x20is:','./util','phonecall','indexOf','uniqueid','ticketType','GET','format','internalemailaddress','queue','%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s',']\x20Wrong\x20credentials!','util','Fields','odata-entityid','[OUTBOUND]','email','user:%s','POST','catch',']\x20The\x20%s\x20ID\x20is:\x20%s','monitor_format'];(function(_0x1ea216,_0x1a9a2d){const _0xb01d03=function(_0x5019fd){while(--_0x5019fd){_0x1ea216['push'](_0x1ea216['shift']());}};_0xb01d03(++_0x1a9a2d);}(a1136_0x1a9a,0x7d));const a1136_0xb01d=function(_0x1ea216,_0x1a9a2d){_0x1ea216=_0x1ea216-0x0;let _0xb01d03=a1136_0x1a9a[_0x1ea216];return _0xb01d03;};const _0x3e3beb=a1136_0xb01d;'use strict';const _=require(_0x3e3beb('0x4a')),rp=require(_0x3e3beb('0x52')),util=require(_0x3e3beb('0x2f')),intUtil=require(_0x3e3beb('0x24')),md5=require(_0x3e3beb('0x55')),logger=require(_0x3e3beb('0xe'))(_0x3e3beb('0x39')),{getSocket}=require('../../../../rediscache'),io=getSocket();function getAuthToken(_0x5019fd,_0x886724,_0x29f66c){const _0x5f0d1c=_0x3e3beb,_0x2e0c2f={'method':_0x5f0d1c('0x35'),'uri':_0x886724,'form':{'client_id':_0x5019fd[_0x5f0d1c('0x41')],'resource':_0x5019fd[_0x5f0d1c('0x46')],'username':_0x5019fd[_0x5f0d1c('0x44')],'password':_0x5019fd['password'],'grant_type':'password','client_secret':_0x5019fd[_0x5f0d1c('0x5')]},'json':!![]};return rp(_0x2e0c2f)['then'](function(_0x4fed90){return _0x4fed90['access_token'];})[_0x5f0d1c('0x36')](function(_0x353339){const _0x57ce0d=_0x5f0d1c;return logger[_0x57ce0d('0x54')]('['+[_0x29f66c[_0x57ce0d('0x3e')]()]+_0x57ce0d('0x2e')),_0x353339['message'];});}function getUser(_0x5cdbd5,_0x5b77be,_0x4ae428,_0x1881a0,_0x4dc8f1,_0x27771c){return new Promise(function(_0x11b683,_0x3194d2){const _0x2db2a5=a1136_0xb01d;return getAuthToken(_0x5b77be,_0x1881a0,_0x27771c)[_0x2db2a5('0x1')](function(_0x5992a5){const _0x56bdab=_0x2db2a5,_0x2443d2={'method':_0x56bdab('0x29'),'uri':_0x4dc8f1+'systemusers?$select=internalemailaddress,isdisabled,systemuserid','headers':{'Authorization':_0x56bdab('0x13')+_0x5992a5,'OData-Version':_0x56bdab('0x1b')},'json':!![]};return rp(_0x2443d2);})[_0x2db2a5('0x1')](function(_0x27c352){const _0x3f02f4=_0x2db2a5,_0x373beb=_0x27c352[_0x3f02f4('0x4')];let _0x15ea42=![],_0x3b0968=null;if(!_[_0x3f02f4('0x48')](_0x4ae428)){if(intUtil[_0x3f02f4('0x3')](_0x4ae428[_0x3f02f4('0x33')])){_0x5cdbd5[_0x3f02f4('0x3d')]=_0x4ae428[_0x3f02f4('0x33')];for(_0x3b0968=0x0;_0x3b0968<_0x373beb[_0x3f02f4('0x7')];_0x3b0968++){_0x373beb[_0x3b0968]['internalemailaddress']===_0x4ae428['email']&&!_0x373beb[_0x3b0968][_0x3f02f4('0xf')]&&(_0x15ea42=!![],logger[_0x3f02f4('0x57')]('['+[_0x27771c[_0x3f02f4('0x3e')]()]+_0x3f02f4('0xd')+_0x373beb[_0x3b0968][_0x3f02f4('0x1e')]+'!'),_0x5cdbd5[_0x3f02f4('0x17')]=_0x373beb[_0x3b0968][_0x3f02f4('0x1e')]);}}}if(!_0x15ea42){logger['info']('['+[_0x27771c[_0x3f02f4('0x3e')]()]+']\x20Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!'),_0x5cdbd5[_0x3f02f4('0x3d')]=_0x5b77be[_0x3f02f4('0x44')];for(_0x3b0968=0x0;_0x3b0968<_0x373beb['length'];_0x3b0968++){_0x373beb[_0x3b0968][_0x3f02f4('0x2b')]===_0x5b77be['username']&&(_0x5cdbd5[_0x3f02f4('0x17')]=_0x373beb[_0x3b0968][_0x3f02f4('0x1e')]);}}_0x11b683(_0x5cdbd5);})['catch'](function(_0x5c4870){_0x3194d2(_0x5c4870['message']);});});}function createEndUser(_0x4974d5,_0x27bb45,_0x7ca2dc,_0x311dac,_0x992ceb,_0x49f495){return new Promise(function(_0x468c3e,_0x209c20){const _0xec5830=a1136_0xb01d;return getAuthToken(_0x27bb45,_0x7ca2dc,_0x49f495)[_0xec5830('0x1')](function(_0x4eeb6d){const _0x14819e=_0xec5830,_0x32d29e=_0x49f495[_0x14819e('0x2')]()==='outbound'?_0x992ceb[_0x14819e('0x4f')]:_0x992ceb[_0x14819e('0x21')],_0x568d4a={'method':'POST','uri':_0x311dac+_0x14819e('0x18'),'body':{'telephone1':_0x32d29e,'fullname':_0x32d29e,'firstname':_0x32d29e,'lastname':'_','mobilephone':null,'address1_telephone1':null,'ownerid@odata.bind':_0x14819e('0x11')+_0x4974d5[_0x14819e('0x17')]+')'},'headers':{'Authorization':_0x14819e('0x13')+_0x4eeb6d,'OData-Version':'4.0'},'json':!![],'resolveWithFullResponse':!![]};return rp(_0x568d4a);})[_0xec5830('0x1')](function(_0x1b81a3){const _0x3e8591=_0xec5830,_0x390850=_0x1b81a3['headers'][_0x3e8591('0x31')],_0x400c05=_0x390850[_0x3e8591('0x5a')](_0x390850[_0x3e8591('0xa')]('(')+0x1,_0x390850[_0x3e8591('0xa')](')'));_0x4974d5[_0x3e8591('0x43')]=_0x400c05,_0x468c3e(_0x4974d5);})[_0xec5830('0x36')](function(_0x187a2c){const _0xb9b7a5=_0xec5830;_0x209c20(_0x187a2c[_0xb9b7a5('0x0')]);});});}function getEndUser(_0x225292,_0x4c24f7,_0x11129e,_0x26078d,_0x485ca0,_0x265ee8){return new Promise(function(_0xcecb70){const _0x408332=a1136_0xb01d;return getAuthToken(_0x4c24f7,_0x11129e,_0x265ee8)[_0x408332('0x1')](function(_0x102ef5){const _0x6c3bd=_0x408332,_0x43237a={'method':_0x6c3bd('0x29'),'uri':_0x26078d+'contacts?$select=telephone1','headers':{'Authorization':_0x6c3bd('0x13')+_0x102ef5,'If-None-Match':null,'OData-Version':_0x6c3bd('0x1b')},'json':!![]};return rp(_0x43237a);})['then'](function(_0x4ead1a){const _0x49ecc5=_0x408332;let _0x207945=null;if(_0x4ead1a){const _0x274652=_0x4ead1a[_0x49ecc5('0x4')];let _0x386ca7=![];for(_0x207945=0x0;_0x207945<_0x274652[_0x49ecc5('0x7')];_0x207945++){const _0x387c32=_0x265ee8[_0x49ecc5('0x2')]()===_0x49ecc5('0x56')?_0x485ca0['destcalleridnum']:_0x485ca0['calleridnum'];_0x274652[_0x207945][_0x49ecc5('0x58')]===_0x387c32&&(_0x386ca7=!![],_0x225292[_0x49ecc5('0x43')]=_0x274652[_0x207945][_0x49ecc5('0x42')]);}if(_0x386ca7)_0xcecb70(_0x225292);else return createEndUser(_0x225292,_0x4c24f7,_0x11129e,_0x26078d,_0x485ca0,_0x265ee8)[_0x49ecc5('0x1')](function(_0x4b67b1){const _0x1b24b6=_0x49ecc5;logger[_0x1b24b6('0x57')]('['+[_0x265ee8['toUpperCase']()]+']\x20Enduser\x20created!',_0x4b67b1['enduserId']),_0xcecb70(_0x4b67b1);});}else return createEndUser(_0x225292,_0x4c24f7,_0x11129e,_0x26078d,_0x485ca0,_0x265ee8)[_0x49ecc5('0x1')](function(_0xfefd03){const _0x4226d3=_0x49ecc5;logger[_0x4226d3('0x57')]('['+[_0x265ee8['toUpperCase']()]+_0x4226d3('0x14'),_0xfefd03[_0x4226d3('0x43')]),_0xcecb70(_0xfefd03);});});});}function getCustomFields(_0x82c444,_0x5c4c60,_0x193b40){const _0x2d14df=_0x3e3beb,_0x3d93aa={};return _0x82c444[_0x2d14df('0x7')]>0x0&&_[_0x2d14df('0x10')](_0x82c444,function(_0x5e3854){const _0x2d7749=_0x2d14df;if(_0x5e3854[_0x2d7749('0x8')]===_0x2d7749('0x9')&&_0x5e3854[_0x2d7749('0x16')]&&!_0x5e3854['customField']){const _0x11e011=_0x5e3854['idField'][_0x2d7749('0x2')](),_0x498d81=_0x11e011['substring'](0x0,_0x11e011[_0x2d7749('0x26')]('_')),_0x4439a1=_0x11e011[_0x2d7749('0x5a')](_0x11e011[_0x2d7749('0x26')]('_')+0x1);_0x498d81===_0x193b40&&(_0x3d93aa[_0x4439a1]=_0x5e3854[_0x2d7749('0x3b')]);}}),_0x3d93aa;}function getTicket(_0x30b6f2,_0x17be0a,_0x2eaf53,_0x3cbdc5,_0x130191,_0x2d66ef,_0x1a7bd3){return new Promise(function(_0x3aa445,_0x157ef5){const _0x25171b=a1136_0xb01d;let _0x40e436;return getAuthToken(_0x17be0a,_0x2eaf53,_0x1a7bd3)['then'](function(_0x20c851){const _0x4cccfe=a1136_0xb01d;return _0x2d66ef[_0x4cccfe('0x28')]===_0x4cccfe('0x4e')&&(_0x40e436={'method':_0x4cccfe('0x35'),'uri':_0x3cbdc5+_0x4cccfe('0x3a'),'body':Object[_0x4cccfe('0x15')]({},{'title':intUtil[_0x4cccfe('0x45')](_0x2d66ef['Subjects'],_0x130191,'\x20'),'description':intUtil[_0x4cccfe('0x45')](_0x2d66ef[_0x4cccfe('0xc')],_0x130191,'\x0a'),'customerid_contact@odata.bind':_0x4cccfe('0x19')+_0x30b6f2['enduserId']+')','ownerid@odata.bind':'/systemusers('+_0x30b6f2['userId']+')','caseorigincode':'1'},getCustomFields(_0x2d66ef[_0x4cccfe('0x30')],_0x130191,_0x2d66ef[_0x4cccfe('0x28')])),'headers':{'Authorization':_0x4cccfe('0x13')+_0x20c851,'OData-Version':_0x4cccfe('0x1b')},'json':!![],'resolveWithFullResponse':!![]}),_0x2d66ef[_0x4cccfe('0x28')]===_0x4cccfe('0x25')&&(_0x40e436={'method':_0x4cccfe('0x35'),'uri':_0x3cbdc5+_0x4cccfe('0x47'),'body':Object['assign']({},{'subject':intUtil['getString'](_0x2d66ef[_0x4cccfe('0x12')],_0x130191,'\x20'),'phonenumber':String(_0x130191[_0x4cccfe('0x21')]),'description':intUtil['getString'](_0x2d66ef['Descriptions'],_0x130191,'\x0a'),'regardingobjectid_contact@odata.bind':_0x4cccfe('0x19')+_0x30b6f2[_0x4cccfe('0x43')]+')','ownerid@odata.bind':'/systemusers('+_0x30b6f2['userId']+')','directioncode':_0x30b6f2['callDirection']==='queue'?![]:!![],'phonecall_activity_parties':[{'partyid_systemuser@odata.bind':_0x4cccfe('0x11')+_0x30b6f2['userId']+')','participationtypemask':_0x30b6f2['callDirection']===_0x4cccfe('0x2c')?0x2:0x1},{'partyid_contact@odata.bind':_0x4cccfe('0x19')+_0x30b6f2[_0x4cccfe('0x43')]+')','participationtypemask':_0x30b6f2[_0x4cccfe('0x3c')]===_0x4cccfe('0x2c')?0x1:0x2}]},getCustomFields(_0x2d66ef[_0x4cccfe('0x30')],_0x130191,_0x2d66ef[_0x4cccfe('0x28')])),'headers':{'Authorization':_0x4cccfe('0x13')+_0x20c851,'OData-Version':_0x4cccfe('0x1b')},'json':!![],'resolveWithFullResponse':!![]}),rp(_0x40e436);})[_0x25171b('0x1')](function(_0x240035){const _0x24aff9=_0x25171b,_0x3f743e=_0x240035[_0x24aff9('0x1f')]['odata-entityid'],_0x1ab249=_0x3f743e[_0x24aff9('0x5a')](_0x3f743e[_0x24aff9('0xa')]('(')+0x1,_0x3f743e['lastIndexOf'](')'));_0x30b6f2[_0x24aff9('0x4c')]=_0x1ab249,_0x3aa445(_0x30b6f2);})[_0x25171b('0x36')](function(_0x264e52){const _0x598e0a=_0x25171b;_0x157ef5(_0x264e52[_0x598e0a('0x0')]);});});}function emit(_0x4aa004,_0x2bd069,_0x374a41,_0x3e5913){const _0x587959=_0x3e3beb,_0x5c226d={'uri':util[_0x587959('0x2a')](_0x587959('0x4d'),_0x4aa004,_0x374a41,_0x3e5913)};io['to'](util[_0x587959('0x2a')](_0x587959('0x34'),_0x2bd069))[_0x587959('0x1a')](_0x587959('0x50'),_0x5c226d);}exports['cs']=function(_0xfecc9,_0x1542c6,_0x953aca,_0x132a25,_0x3a919e,_0x55744a,_0x58087b,_0x781459){const _0x3d68bf=_0x3e3beb,_0x2e9480={};_0x55744a[_0x3d68bf('0x46')]=intUtil[_0x3d68bf('0x20')](_0x55744a[_0x3d68bf('0x46')]),_0x55744a['serverUrl']=intUtil[_0x3d68bf('0x20')](_0x55744a[_0x3d68bf('0x1d')]),_0x2e9480[_0x3d68bf('0x3c')]=_0x781459==='queue'||_0x781459===_0x3d68bf('0x49')?_0x3d68bf('0x2c'):_0x3d68bf('0x56');const _0x2f0238=util[_0x3d68bf('0x2a')](_0x3d68bf('0x59'),_0x55744a[_0x3d68bf('0x46')]),_0x120ab1=util[_0x3d68bf('0x2a')]('https://login.microsoftonline.com/%s/oauth2/token',_0x55744a[_0x3d68bf('0x4b')]),_0x15e54c=util[_0x3d68bf('0x2a')](_0x3d68bf('0x2d'),_0x55744a[_0x3d68bf('0x1d')],_0x1542c6[_0x3d68bf('0x27')],md5(_0x1542c6['uniqueid']));if(_0x3a919e&&_0x3a919e[_0x3d68bf('0x38')])logger['info'](_0x3d68bf('0x1c'),_0x3d68bf('0x3f')),_0x1542c6[_0x3d68bf('0x51')]=_0x15e54c;else _0x58087b&&_0x58087b['recordingFormat']&&_0x58087b['recordingFormat']!=='none'?(logger[_0x3d68bf('0x57')](_0x3d68bf('0x32'),'Recording\x20is\x20enabled!'),_0x1542c6[_0x3d68bf('0x51')]=_0x15e54c):_0x1542c6['recordingURL']='';return getUser(_0x2e9480,_0x55744a,_0x132a25,_0x120ab1,_0x2f0238,_0x781459)[_0x3d68bf('0x1')](function(_0x5cc592){const _0x542b11=_0x3d68bf;return logger[_0x542b11('0x57')]('['+_0x781459[_0x542b11('0x3e')]()+_0x542b11('0x22'),_0x5cc592[_0x542b11('0x17')]),logger[_0x542b11('0x57')]('['+_0x781459[_0x542b11('0x3e')]()+_0x542b11('0x23'),_0x5cc592[_0x542b11('0x3d')]),getEndUser(_0x5cc592,_0x55744a,_0x120ab1,_0x2f0238,_0x1542c6,_0x781459);})[_0x3d68bf('0x1')](function(_0x5f4cae){const _0x5767f9=_0x3d68bf;return logger['info']('['+_0x781459['toUpperCase']()+']\x20The\x20Enduser\x20ID\x20is:',_0x5f4cae[_0x5767f9('0x43')]),logger[_0x5767f9('0x57')]('['+_0x781459[_0x5767f9('0x3e')]()+_0x5767f9('0x40'),_0x1542c6[_0x5767f9('0x21')]),getTicket(_0x5f4cae,_0x55744a,_0x120ab1,_0x2f0238,_0x1542c6,_0x953aca,_0x781459);})['then'](function(_0x3a20b7){const _0x463227=_0x3d68bf;logger[_0x463227('0x57')](util[_0x463227('0x2a')]('['+_0x781459['toUpperCase']()+_0x463227('0x37'),_0x953aca[_0x463227('0x28')],_0x3a20b7[_0x463227('0x4c')])),logger['info'](util[_0x463227('0x2a')]('['+_0x781459['toUpperCase']()+']\x20Call\x20direction:\x20%s',_0x781459)),_0x781459!==_0x463227('0x49')&&(logger[_0x463227('0x57')]('['+_0x781459['toUpperCase']()+']','Displaying\x20call\x20in\x20a\x20new\x20tab...'),emit(_0x55744a[_0x463227('0x46')],_0x132a25[_0x463227('0x53')],_0x953aca[_0x463227('0x28')],_0x3a20b7[_0x463227('0x4c')]));})[_0x3d68bf('0x36')](function(_0x3732d1){const _0x45aeb7=_0x3d68bf;logger[_0x45aeb7('0x54')]('['+_0x781459['toUpperCase']()+_0x45aeb7('0x6'),util[_0x45aeb7('0xb')](_0x3732d1,{'showHidden':!![],'depth':null}));});};