Built motion from commit (unavailable).|2.4.4
[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 _0x9f98=['username','password','clientSecret','then','access_token','catch','error','toUpperCase',']\x20Wrong\x20credentials!','message','GET','systemusers?$select=internalemailaddress,isdisabled,systemuserid','Bearer\x20','4.0','value','isNil','validateEmail','email','info',']\x20Agent\x20found\x20','systemuserid','userId','length','toLowerCase','outbound','destcalleridnum','calleridnum','contacts','/systemusers(','headers','odata-entityid','substring','lastIndexOf','enduserId','contacts?$select=telephone1','contactid',']\x20Enduser\x20created!','forEach','type','customField','idField','indexOf','content','ticketType','incident','getString','Subjects','Descriptions','Fields','phonecall','assign','/contacts(','callDirection','queue','format','%s/main.aspx?pagetype=entityrecord&etn=%s&id=%s','user:%s','trigger:browser:url','remoteUri','stripTrailingSlash','serverUrl','uniqueid','monitor_format','[QUEUE]','Recording\x20is\x20enabled!','recordingURL','recordingFormat','none','[OUTBOUND]','userEmail',']\x20The\x20Enduser\x20ID\x20is:',']\x20The\x20%s\x20ID\x20is:\x20%s','ticketId',']\x20Call\x20direction:\x20%s','abandoned','Displaying\x20call\x20in\x20a\x20new\x20tab...','name',']\x20Error\x20running\x20Dynamics365\x20integration','inspect','lodash','bluebird','request-promise','util','md5','ioredis','dynamics365','../../../../config/environment','defaults','redis','localhost','socket.io-emitter','POST','clientId'];(function(_0x14acb1,_0x717428){var _0x47a9be=function(_0x42c46b){while(--_0x42c46b){_0x14acb1['push'](_0x14acb1['shift']());}};_0x47a9be(++_0x717428);}(_0x9f98,0xac));var _0x89f9=function(_0x4b369d,_0x265eb8){_0x4b369d=_0x4b369d-0x0;var _0x571a98=_0x9f98[_0x4b369d];return _0x571a98;};'use strict';var _=require(_0x89f9('0x0'));var BPromise=require(_0x89f9('0x1'));var rp=require(_0x89f9('0x2'));var util=require(_0x89f9('0x3'));var intUtil=require('./util');var md5=require(_0x89f9('0x4'));var Redis=require(_0x89f9('0x5'));var logger=require('../../../../config/logger')(_0x89f9('0x6'));var config=require(_0x89f9('0x7'));config['redis']=_[_0x89f9('0x8')](config[_0x89f9('0x9')],{'host':_0x89f9('0xa'),'port':0x18eb});var io=require(_0x89f9('0xb'))(new Redis(config[_0x89f9('0x9')]));function getAuthToken(_0x18dc85,_0x401975,_0x57cab8){var _0x4aba2f={'method':_0x89f9('0xc'),'uri':_0x401975,'form':{'client_id':_0x18dc85[_0x89f9('0xd')],'resource':_0x18dc85['remoteUri'],'username':_0x18dc85[_0x89f9('0xe')],'password':_0x18dc85[_0x89f9('0xf')],'grant_type':_0x89f9('0xf'),'client_secret':_0x18dc85[_0x89f9('0x10')]},'json':!![]};return rp(_0x4aba2f)[_0x89f9('0x11')](function(_0x51e394){return _0x51e394[_0x89f9('0x12')];})[_0x89f9('0x13')](function(_0x421c2c){logger[_0x89f9('0x14')]('['+[_0x57cab8[_0x89f9('0x15')]()]+_0x89f9('0x16'));return _0x421c2c[_0x89f9('0x17')];});}function getUser(_0x2efac6,_0x18ae09,_0x1b8db4,_0x119a2d,_0x571cae,_0x23c957){return new BPromise(function(_0x2bc0b1,_0x32152f){return getAuthToken(_0x18ae09,_0x119a2d,_0x23c957)[_0x89f9('0x11')](function(_0x3a0fab){var _0x175621={'method':_0x89f9('0x18'),'uri':_0x571cae+_0x89f9('0x19'),'headers':{'Authorization':_0x89f9('0x1a')+_0x3a0fab,'OData-Version':_0x89f9('0x1b')},'json':!![]};return rp(_0x175621);})[_0x89f9('0x11')](function(_0x20c073){var _0x1ea16e=_0x20c073[_0x89f9('0x1c')];var _0x232aee=![];var _0x5a31ee=null;if(!_[_0x89f9('0x1d')](_0x1b8db4)){if(intUtil[_0x89f9('0x1e')](_0x1b8db4[_0x89f9('0x1f')])){_0x2efac6['userEmail']=_0x1b8db4[_0x89f9('0x1f')];for(_0x5a31ee=0x0;_0x5a31ee<_0x1ea16e['length'];_0x5a31ee++){if(_0x1ea16e[_0x5a31ee]['internalemailaddress']===_0x1b8db4[_0x89f9('0x1f')]&&!_0x1ea16e[_0x5a31ee]['isdisabled']){_0x232aee=!![];logger[_0x89f9('0x20')]('['+[_0x23c957[_0x89f9('0x15')]()]+_0x89f9('0x21')+_0x1ea16e[_0x5a31ee][_0x89f9('0x22')]+'!');_0x2efac6[_0x89f9('0x23')]=_0x1ea16e[_0x5a31ee][_0x89f9('0x22')];}}}}if(!_0x232aee){logger['info']('['+[_0x23c957[_0x89f9('0x15')]()]+']\x20Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!');_0x2efac6['userEmail']=_0x18ae09[_0x89f9('0xe')];for(_0x5a31ee=0x0;_0x5a31ee<_0x1ea16e[_0x89f9('0x24')];_0x5a31ee++){if(_0x1ea16e[_0x5a31ee]['internalemailaddress']===_0x18ae09[_0x89f9('0xe')]){_0x2efac6[_0x89f9('0x23')]=_0x1ea16e[_0x5a31ee][_0x89f9('0x22')];}}}_0x2bc0b1(_0x2efac6);})[_0x89f9('0x13')](function(_0x5bf173){_0x32152f(_0x5bf173[_0x89f9('0x17')]);});});}function createEndUser(_0x26c02e,_0x17668b,_0x4e571a,_0x538fb5,_0x1b43cc,_0x3346cc){return new BPromise(function(_0x5ba90b,_0x28904e){return getAuthToken(_0x17668b,_0x4e571a,_0x3346cc)[_0x89f9('0x11')](function(_0x3f5be1){var _0x5d5abe=_0x3346cc[_0x89f9('0x25')]()===_0x89f9('0x26')?_0x1b43cc[_0x89f9('0x27')]:_0x1b43cc[_0x89f9('0x28')];var _0x101e8b={'method':_0x89f9('0xc'),'uri':_0x538fb5+_0x89f9('0x29'),'body':{'telephone1':_0x5d5abe,'fullname':_0x5d5abe,'firstname':_0x5d5abe,'lastname':'_','mobilephone':null,'address1_telephone1':null,'ownerid@odata.bind':_0x89f9('0x2a')+_0x26c02e['userId']+')'},'headers':{'Authorization':'Bearer\x20'+_0x3f5be1,'OData-Version':_0x89f9('0x1b')},'json':!![],'resolveWithFullResponse':!![]};return rp(_0x101e8b);})['then'](function(_0x5a352b){var _0x390b8b=_0x5a352b[_0x89f9('0x2b')][_0x89f9('0x2c')];var _0x52dca9=_0x390b8b[_0x89f9('0x2d')](_0x390b8b['lastIndexOf']('(')+0x1,_0x390b8b[_0x89f9('0x2e')](')'));_0x26c02e[_0x89f9('0x2f')]=_0x52dca9;_0x5ba90b(_0x26c02e);})[_0x89f9('0x13')](function(_0x213ad5){_0x28904e(_0x213ad5[_0x89f9('0x17')]);});});}function getEndUser(_0x2344a0,_0x4e5653,_0x223a6a,_0x49772a,_0x40705f,_0x35496d){return new BPromise(function(_0x154b1d,_0x18bcaf){return getAuthToken(_0x4e5653,_0x223a6a,_0x35496d)['then'](function(_0x1e52f3){var _0x1beb6e={'method':_0x89f9('0x18'),'uri':_0x49772a+_0x89f9('0x30'),'headers':{'Authorization':_0x89f9('0x1a')+_0x1e52f3,'If-None-Match':null,'OData-Version':_0x89f9('0x1b')},'json':!![]};return rp(_0x1beb6e);})[_0x89f9('0x11')](function(_0x4e8042){var _0xab4082=null;if(_0x4e8042){var _0x48495e=_0x4e8042[_0x89f9('0x1c')];var _0x521ce2=![];for(_0xab4082=0x0;_0xab4082<_0x48495e[_0x89f9('0x24')];_0xab4082++){var _0x3367cd=_0x35496d[_0x89f9('0x25')]()===_0x89f9('0x26')?_0x40705f[_0x89f9('0x27')]:_0x40705f[_0x89f9('0x28')];if(_0x48495e[_0xab4082]['telephone1']===_0x3367cd){_0x521ce2=!![];_0x2344a0[_0x89f9('0x2f')]=_0x48495e[_0xab4082][_0x89f9('0x31')];}}if(_0x521ce2){_0x154b1d(_0x2344a0);}else{return createEndUser(_0x2344a0,_0x4e5653,_0x223a6a,_0x49772a,_0x40705f,_0x35496d)[_0x89f9('0x11')](function(_0x4f7b70){logger[_0x89f9('0x20')]('['+[_0x35496d[_0x89f9('0x15')]()]+_0x89f9('0x32'),_0x4f7b70[_0x89f9('0x2f')]);_0x154b1d(_0x4f7b70);});}}else{return createEndUser(_0x2344a0,_0x4e5653,_0x223a6a,_0x49772a,_0x40705f,_0x35496d)[_0x89f9('0x11')](function(_0x1a0411){logger[_0x89f9('0x20')]('['+[_0x35496d[_0x89f9('0x15')]()]+']\x20Enduser\x20Created!',_0x1a0411[_0x89f9('0x2f')]);_0x154b1d(_0x1a0411);});}});});}function getCustomFields(_0x6ba3bf,_0x9bfece,_0x2cdb75){var _0x48a26b={};if(_0x6ba3bf[_0x89f9('0x24')]>0x0){_[_0x89f9('0x33')](_0x6ba3bf,function(_0x4cdbee){if(_0x4cdbee[_0x89f9('0x34')]==='string'&&_0x4cdbee['nameField']&&!_0x4cdbee[_0x89f9('0x35')]){var _0x221600=_0x4cdbee[_0x89f9('0x36')][_0x89f9('0x25')]();var _0x19422d=_0x221600[_0x89f9('0x2d')](0x0,_0x221600[_0x89f9('0x37')]('_'));var _0x2d916c=_0x221600['substring'](_0x221600['indexOf']('_')+0x1);if(_0x19422d===_0x2cdb75){_0x48a26b[_0x2d916c]=_0x4cdbee[_0x89f9('0x38')];}}});}return _0x48a26b;}function getTicket(_0x9d15cf,_0x4c33f5,_0x24172c,_0x5e717d,_0x3b1fa9,_0x3bb90d,_0x13370c){return new BPromise(function(_0x131222,_0x3264b3){var _0x2c2137;return getAuthToken(_0x4c33f5,_0x24172c,_0x13370c)['then'](function(_0x45f31b){if(_0x3bb90d[_0x89f9('0x39')]===_0x89f9('0x3a')){_0x2c2137={'method':'POST','uri':_0x5e717d+'incidents','body':Object['assign']({},{'title':intUtil[_0x89f9('0x3b')](_0x3bb90d[_0x89f9('0x3c')],_0x3b1fa9,'\x20'),'description':intUtil[_0x89f9('0x3b')](_0x3bb90d[_0x89f9('0x3d')],_0x3b1fa9,'\x0a'),'customerid_contact@odata.bind':'/contacts('+_0x9d15cf['enduserId']+')','ownerid@odata.bind':_0x89f9('0x2a')+_0x9d15cf[_0x89f9('0x23')]+')','caseorigincode':'1'},getCustomFields(_0x3bb90d[_0x89f9('0x3e')],_0x3b1fa9,_0x3bb90d['ticketType'])),'headers':{'Authorization':_0x89f9('0x1a')+_0x45f31b,'OData-Version':_0x89f9('0x1b')},'json':!![],'resolveWithFullResponse':!![]};}if(_0x3bb90d[_0x89f9('0x39')]===_0x89f9('0x3f')){_0x2c2137={'method':_0x89f9('0xc'),'uri':_0x5e717d+'phonecalls','body':Object[_0x89f9('0x40')]({},{'subject':intUtil[_0x89f9('0x3b')](_0x3bb90d[_0x89f9('0x3c')],_0x3b1fa9,'\x20'),'phonenumber':String(_0x3b1fa9['calleridnum']),'description':intUtil[_0x89f9('0x3b')](_0x3bb90d['Descriptions'],_0x3b1fa9,'\x0a'),'regardingobjectid_contact@odata.bind':_0x89f9('0x41')+_0x9d15cf[_0x89f9('0x2f')]+')','ownerid@odata.bind':_0x89f9('0x2a')+_0x9d15cf['userId']+')','directioncode':_0x9d15cf[_0x89f9('0x42')]==='queue'?![]:!![],'phonecall_activity_parties':[{'partyid_systemuser@odata.bind':_0x89f9('0x2a')+_0x9d15cf[_0x89f9('0x23')]+')','participationtypemask':_0x9d15cf['callDirection']===_0x89f9('0x43')?0x2:0x1},{'partyid_contact@odata.bind':_0x89f9('0x41')+_0x9d15cf['enduserId']+')','participationtypemask':_0x9d15cf[_0x89f9('0x42')]==='queue'?0x1:0x2}]},getCustomFields(_0x3bb90d[_0x89f9('0x3e')],_0x3b1fa9,_0x3bb90d[_0x89f9('0x39')])),'headers':{'Authorization':'Bearer\x20'+_0x45f31b,'OData-Version':_0x89f9('0x1b')},'json':!![],'resolveWithFullResponse':!![]};}return rp(_0x2c2137);})[_0x89f9('0x11')](function(_0x4c528d){var _0x898fd5=_0x4c528d[_0x89f9('0x2b')][_0x89f9('0x2c')];var _0x2f551a=_0x898fd5[_0x89f9('0x2d')](_0x898fd5[_0x89f9('0x2e')]('(')+0x1,_0x898fd5[_0x89f9('0x2e')](')'));_0x9d15cf['ticketId']=_0x2f551a;_0x131222(_0x9d15cf);})['catch'](function(_0x49dcaa){_0x3264b3(_0x49dcaa['message']);});});}function emit(_0x315793,_0x42ecdf,_0x32f354,_0x52df43){var _0x2de437={'uri':util[_0x89f9('0x44')](_0x89f9('0x45'),_0x315793,_0x32f354,_0x52df43)};io['to'](util['format'](_0x89f9('0x46'),_0x42ecdf))['emit'](_0x89f9('0x47'),_0x2de437);}exports['cs']=function(_0x48ec7c,_0x2a7ecb,_0x5aa433,_0x83a95e,_0x124d7b,_0x26b4cd,_0x171451,_0x440e62){var _0x1ac7b9={};_0x26b4cd[_0x89f9('0x48')]=intUtil[_0x89f9('0x49')](_0x26b4cd[_0x89f9('0x48')]);_0x26b4cd[_0x89f9('0x4a')]=intUtil[_0x89f9('0x49')](_0x26b4cd['serverUrl']);_0x1ac7b9[_0x89f9('0x42')]=_0x440e62===_0x89f9('0x43')||_0x440e62==='abandoned'?_0x89f9('0x43'):_0x89f9('0x26');var _0x3945a6=util[_0x89f9('0x44')]('%s/api/data/v9.0/',_0x26b4cd[_0x89f9('0x48')]);var _0xd4079a=util[_0x89f9('0x44')]('https://login.microsoftonline.com/%s/oauth2/token',_0x26b4cd['tenantId']);var _0x2db937=util['format']('%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s',_0x26b4cd['serverUrl'],_0x2a7ecb[_0x89f9('0x4b')],md5(_0x2a7ecb[_0x89f9('0x4b')]));if(_0x124d7b&&_0x124d7b[_0x89f9('0x4c')]){logger[_0x89f9('0x20')](_0x89f9('0x4d'),_0x89f9('0x4e'));_0x2a7ecb[_0x89f9('0x4f')]=_0x2db937;}else if(_0x171451&&_0x171451[_0x89f9('0x50')]&&_0x171451['recordingFormat']!==_0x89f9('0x51')){logger[_0x89f9('0x20')](_0x89f9('0x52'),'Recording\x20is\x20enabled!');_0x2a7ecb[_0x89f9('0x4f')]=_0x2db937;}else{_0x2a7ecb['recordingURL']='';}return getUser(_0x1ac7b9,_0x26b4cd,_0x83a95e,_0xd4079a,_0x3945a6,_0x440e62)['then'](function(_0x3a4545){logger[_0x89f9('0x20')]('['+_0x440e62['toUpperCase']()+']\x20The\x20User\x20ID\x20is:',_0x3a4545[_0x89f9('0x23')]);logger[_0x89f9('0x20')]('['+_0x440e62[_0x89f9('0x15')]()+']\x20The\x20User\x20email\x20is:',_0x3a4545[_0x89f9('0x53')]);return getEndUser(_0x3a4545,_0x26b4cd,_0xd4079a,_0x3945a6,_0x2a7ecb,_0x440e62);})[_0x89f9('0x11')](function(_0x562a1f){logger[_0x89f9('0x20')]('['+_0x440e62['toUpperCase']()+_0x89f9('0x54'),_0x562a1f[_0x89f9('0x2f')]);logger['info']('['+_0x440e62['toUpperCase']()+']\x20The\x20Enduser\x20phone\x20is:',_0x2a7ecb['calleridnum']);return getTicket(_0x562a1f,_0x26b4cd,_0xd4079a,_0x3945a6,_0x2a7ecb,_0x5aa433,_0x440e62);})[_0x89f9('0x11')](function(_0x567033){logger['info'](util[_0x89f9('0x44')]('['+_0x440e62[_0x89f9('0x15')]()+_0x89f9('0x55'),_0x5aa433[_0x89f9('0x39')],_0x567033[_0x89f9('0x56')]));logger[_0x89f9('0x20')](util[_0x89f9('0x44')]('['+_0x440e62[_0x89f9('0x15')]()+_0x89f9('0x57'),_0x440e62));if(_0x440e62!==_0x89f9('0x58')){logger['info']('['+_0x440e62[_0x89f9('0x15')]()+']',_0x89f9('0x59'));emit(_0x26b4cd[_0x89f9('0x48')],_0x83a95e[_0x89f9('0x5a')],_0x5aa433['ticketType'],_0x567033['ticketId']);}})[_0x89f9('0x13')](function(_0x1f92dc){logger['error']('['+_0x440e62[_0x89f9('0x15')]()+_0x89f9('0x5b'),util[_0x89f9('0x5c')](_0x1f92dc,{'showHidden':!![],'depth':null}));});};