e7f81f16e29c7719ed96cf7914f4941af0b9154b
[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 _0x2bc4=['serverUrl','unmanaged','%s/api/data/v9.0/','https://login.microsoftonline.com/%s/oauth2/token','tenantId','%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s','uniqueid','monitor_format','[QUEUE]','Recording\x20is\x20enabled!','recordingURL','recordingFormat','none','[OUTBOUND]',']\x20The\x20User\x20email\x20is:',']\x20The\x20Enduser\x20ID\x20is:',']\x20The\x20Enduser\x20phone\x20is:','ticketId','Displaying\x20call\x20in\x20a\x20new\x20tab...','name',']\x20Error\x20running\x20Dynamics365\x20integration','inspect','lodash','bluebird','util','./util','md5','ioredis','dynamics365','../../../../config/environment','redis','defaults','localhost','POST','clientId','remoteUri','password','clientSecret','then','catch','error','toUpperCase',']\x20Wrong\x20credentials!','message','GET','systemusers?$select=internalemailaddress,isdisabled,systemuserid','Bearer\x20','4.0','value','validateEmail','email','internalemailaddress','info',']\x20Agent\x20found\x20','userId','systemuserid','userEmail','username','length','outbound','calleridnum','contacts','/systemusers(','headers','odata-entityid','substring','lastIndexOf','contacts?$select=telephone1','toLowerCase','destcalleridnum','enduserId',']\x20Enduser\x20created!',']\x20Enduser\x20Created!','forEach','type','string','nameField','customField','idField','indexOf','content','incident','assign','getString','Subjects','Descriptions','/contacts(','Fields','ticketType','phonecall','phonecalls','callDirection','queue','format','user:%s','trigger:browser:url','stripTrailingSlash'];(function(_0x4fde0e,_0x42909e){var _0x359da0=function(_0xa21082){while(--_0xa21082){_0x4fde0e['push'](_0x4fde0e['shift']());}};_0x359da0(++_0x42909e);}(_0x2bc4,0x139));var _0x42bc=function(_0x57b54c,_0x54ce0d){_0x57b54c=_0x57b54c-0x0;var _0x59cc19=_0x2bc4[_0x57b54c];return _0x59cc19;};'use strict';var _=require(_0x42bc('0x0'));var BPromise=require(_0x42bc('0x1'));var rp=require('request-promise');var util=require(_0x42bc('0x2'));var intUtil=require(_0x42bc('0x3'));var md5=require(_0x42bc('0x4'));var Redis=require(_0x42bc('0x5'));var logger=require('../../../../config/logger')(_0x42bc('0x6'));var config=require(_0x42bc('0x7'));config[_0x42bc('0x8')]=_[_0x42bc('0x9')](config[_0x42bc('0x8')],{'host':_0x42bc('0xa'),'port':0x18eb});var io=require('socket.io-emitter')(new Redis(config[_0x42bc('0x8')]));function getAuthToken(_0x4ec8be,_0x3bbfff,_0x3ab83f){var _0x566fb2={'method':_0x42bc('0xb'),'uri':_0x3bbfff,'form':{'client_id':_0x4ec8be[_0x42bc('0xc')],'resource':_0x4ec8be[_0x42bc('0xd')],'username':_0x4ec8be['username'],'password':_0x4ec8be[_0x42bc('0xe')],'grant_type':_0x42bc('0xe'),'client_secret':_0x4ec8be[_0x42bc('0xf')]},'json':!![]};return rp(_0x566fb2)[_0x42bc('0x10')](function(_0x1a6b05){return _0x1a6b05['access_token'];})[_0x42bc('0x11')](function(_0x594d72){logger[_0x42bc('0x12')]('['+[_0x3ab83f[_0x42bc('0x13')]()]+_0x42bc('0x14'));return _0x594d72[_0x42bc('0x15')];});}function getUser(_0x43e59c,_0x1e9f2f,_0x29d5b5,_0x180990,_0x93666e,_0x4b275f){return new BPromise(function(_0x49baff,_0x49dbbd){return getAuthToken(_0x1e9f2f,_0x180990,_0x4b275f)[_0x42bc('0x10')](function(_0x53cb82){var _0x4116eb={'method':_0x42bc('0x16'),'uri':_0x93666e+_0x42bc('0x17'),'headers':{'Authorization':_0x42bc('0x18')+_0x53cb82,'OData-Version':_0x42bc('0x19')},'json':!![]};return rp(_0x4116eb);})[_0x42bc('0x10')](function(_0x4129a1){var _0x554870=_0x4129a1[_0x42bc('0x1a')];var _0x4bdd9e=![];var _0x58132c=null;if(!_['isNil'](_0x29d5b5)){if(intUtil[_0x42bc('0x1b')](_0x29d5b5[_0x42bc('0x1c')])){_0x43e59c['userEmail']=_0x29d5b5[_0x42bc('0x1c')];for(_0x58132c=0x0;_0x58132c<_0x554870['length'];_0x58132c++){if(_0x554870[_0x58132c][_0x42bc('0x1d')]===_0x29d5b5[_0x42bc('0x1c')]&&!_0x554870[_0x58132c]['isdisabled']){_0x4bdd9e=!![];logger[_0x42bc('0x1e')]('['+[_0x4b275f[_0x42bc('0x13')]()]+_0x42bc('0x1f')+_0x554870[_0x58132c]['systemuserid']+'!');_0x43e59c[_0x42bc('0x20')]=_0x554870[_0x58132c][_0x42bc('0x21')];}}}}if(!_0x4bdd9e){logger['info']('['+[_0x4b275f[_0x42bc('0x13')]()]+']\x20Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!');_0x43e59c[_0x42bc('0x22')]=_0x1e9f2f[_0x42bc('0x23')];for(_0x58132c=0x0;_0x58132c<_0x554870[_0x42bc('0x24')];_0x58132c++){if(_0x554870[_0x58132c][_0x42bc('0x1d')]===_0x1e9f2f[_0x42bc('0x23')]){_0x43e59c[_0x42bc('0x20')]=_0x554870[_0x58132c][_0x42bc('0x21')];}}}_0x49baff(_0x43e59c);})['catch'](function(_0x3abbdc){_0x49dbbd(_0x3abbdc['message']);});});}function createEndUser(_0xff86b2,_0x132364,_0x400d72,_0x1a2e51,_0x12e206,_0x1f2dac){return new BPromise(function(_0x399c0a,_0x1766ae){return getAuthToken(_0x132364,_0x400d72,_0x1f2dac)['then'](function(_0x45057d){var _0x3b7922=_0x1f2dac['toLowerCase']()===_0x42bc('0x25')?_0x12e206['destcalleridnum']:_0x12e206[_0x42bc('0x26')];var _0x389b3b={'method':'POST','uri':_0x1a2e51+_0x42bc('0x27'),'body':{'telephone1':_0x3b7922,'fullname':_0x3b7922,'firstname':_0x3b7922,'lastname':'_','mobilephone':null,'address1_telephone1':null,'ownerid@odata.bind':_0x42bc('0x28')+_0xff86b2[_0x42bc('0x20')]+')'},'headers':{'Authorization':'Bearer\x20'+_0x45057d,'OData-Version':_0x42bc('0x19')},'json':!![],'resolveWithFullResponse':!![]};return rp(_0x389b3b);})[_0x42bc('0x10')](function(_0x23ac49){var _0x3b042e=_0x23ac49[_0x42bc('0x29')][_0x42bc('0x2a')];var _0x918575=_0x3b042e[_0x42bc('0x2b')](_0x3b042e[_0x42bc('0x2c')]('(')+0x1,_0x3b042e['lastIndexOf'](')'));_0xff86b2['enduserId']=_0x918575;_0x399c0a(_0xff86b2);})[_0x42bc('0x11')](function(_0x584f0c){_0x1766ae(_0x584f0c[_0x42bc('0x15')]);});});}function getEndUser(_0x170faa,_0x38fcfc,_0x8d037,_0x23ecdb,_0x15db41,_0x316ccc){return new BPromise(function(_0x13ef11,_0x4cde9c){return getAuthToken(_0x38fcfc,_0x8d037,_0x316ccc)[_0x42bc('0x10')](function(_0x459038){var _0x344835={'method':'GET','uri':_0x23ecdb+_0x42bc('0x2d'),'headers':{'Authorization':_0x42bc('0x18')+_0x459038,'If-None-Match':null,'OData-Version':_0x42bc('0x19')},'json':!![]};return rp(_0x344835);})[_0x42bc('0x10')](function(_0x248dbd){var _0x785556=null;if(_0x248dbd){var _0x167083=_0x248dbd[_0x42bc('0x1a')];var _0x45a97d=![];for(_0x785556=0x0;_0x785556<_0x167083['length'];_0x785556++){var _0x59b782=_0x316ccc[_0x42bc('0x2e')]()===_0x42bc('0x25')?_0x15db41[_0x42bc('0x2f')]:_0x15db41[_0x42bc('0x26')];if(_0x167083[_0x785556]['telephone1']===_0x59b782){_0x45a97d=!![];_0x170faa[_0x42bc('0x30')]=_0x167083[_0x785556]['contactid'];}}if(_0x45a97d){_0x13ef11(_0x170faa);}else{return createEndUser(_0x170faa,_0x38fcfc,_0x8d037,_0x23ecdb,_0x15db41,_0x316ccc)[_0x42bc('0x10')](function(_0x4186cb){logger['info']('['+[_0x316ccc[_0x42bc('0x13')]()]+_0x42bc('0x31'),_0x4186cb[_0x42bc('0x30')]);_0x13ef11(_0x4186cb);});}}else{return createEndUser(_0x170faa,_0x38fcfc,_0x8d037,_0x23ecdb,_0x15db41,_0x316ccc)['then'](function(_0x49fdbd){logger['info']('['+[_0x316ccc[_0x42bc('0x13')]()]+_0x42bc('0x32'),_0x49fdbd[_0x42bc('0x30')]);_0x13ef11(_0x49fdbd);});}});});}function getCustomFields(_0x7238c8,_0x4c0b38,_0x4e8bd3){var _0x567820={};if(_0x7238c8[_0x42bc('0x24')]>0x0){_[_0x42bc('0x33')](_0x7238c8,function(_0xfe8091){if(_0xfe8091[_0x42bc('0x34')]===_0x42bc('0x35')&&_0xfe8091[_0x42bc('0x36')]&&!_0xfe8091[_0x42bc('0x37')]){var _0x42f81a=_0xfe8091[_0x42bc('0x38')][_0x42bc('0x2e')]();var _0x1e2e42=_0x42f81a['substring'](0x0,_0x42f81a[_0x42bc('0x39')]('_'));var _0x8e5935=_0x42f81a[_0x42bc('0x2b')](_0x42f81a[_0x42bc('0x39')]('_')+0x1);if(_0x1e2e42===_0x4e8bd3){_0x567820[_0x8e5935]=_0xfe8091[_0x42bc('0x3a')];}}});}return _0x567820;}function getTicket(_0x399ce6,_0x17497a,_0x3686b9,_0x786664,_0x533b5b,_0x224ac3,_0x1aa378){return new BPromise(function(_0x3cbf63,_0x1cc820){var _0x559ae1;return getAuthToken(_0x17497a,_0x3686b9,_0x1aa378)[_0x42bc('0x10')](function(_0x4493e7){if(_0x224ac3['ticketType']===_0x42bc('0x3b')){_0x559ae1={'method':_0x42bc('0xb'),'uri':_0x786664+'incidents','body':Object[_0x42bc('0x3c')]({},{'title':intUtil[_0x42bc('0x3d')](_0x224ac3[_0x42bc('0x3e')],_0x533b5b,'\x20'),'description':intUtil[_0x42bc('0x3d')](_0x224ac3[_0x42bc('0x3f')],_0x533b5b,'\x0a'),'customerid_contact@odata.bind':_0x42bc('0x40')+_0x399ce6['enduserId']+')','ownerid@odata.bind':_0x42bc('0x28')+_0x399ce6[_0x42bc('0x20')]+')','caseorigincode':'1'},getCustomFields(_0x224ac3[_0x42bc('0x41')],_0x533b5b,_0x224ac3[_0x42bc('0x42')])),'headers':{'Authorization':_0x42bc('0x18')+_0x4493e7,'OData-Version':_0x42bc('0x19')},'json':!![],'resolveWithFullResponse':!![]};}if(_0x224ac3[_0x42bc('0x42')]===_0x42bc('0x43')){_0x559ae1={'method':'POST','uri':_0x786664+_0x42bc('0x44'),'body':Object['assign']({},{'subject':intUtil[_0x42bc('0x3d')](_0x224ac3[_0x42bc('0x3e')],_0x533b5b,'\x20'),'phonenumber':String(_0x533b5b[_0x42bc('0x26')]),'description':intUtil[_0x42bc('0x3d')](_0x224ac3[_0x42bc('0x3f')],_0x533b5b,'\x0a'),'regardingobjectid_contact@odata.bind':_0x42bc('0x40')+_0x399ce6[_0x42bc('0x30')]+')','ownerid@odata.bind':'/systemusers('+_0x399ce6['userId']+')','directioncode':_0x399ce6[_0x42bc('0x45')]===_0x42bc('0x46')?![]:!![],'phonecall_activity_parties':[{'partyid_systemuser@odata.bind':_0x42bc('0x28')+_0x399ce6[_0x42bc('0x20')]+')','participationtypemask':_0x399ce6[_0x42bc('0x45')]===_0x42bc('0x46')?0x2:0x1},{'partyid_contact@odata.bind':_0x42bc('0x40')+_0x399ce6[_0x42bc('0x30')]+')','participationtypemask':_0x399ce6[_0x42bc('0x45')]===_0x42bc('0x46')?0x1:0x2}]},getCustomFields(_0x224ac3[_0x42bc('0x41')],_0x533b5b,_0x224ac3[_0x42bc('0x42')])),'headers':{'Authorization':_0x42bc('0x18')+_0x4493e7,'OData-Version':'4.0'},'json':!![],'resolveWithFullResponse':!![]};}return rp(_0x559ae1);})[_0x42bc('0x10')](function(_0x2a271f){var _0x48f33b=_0x2a271f['headers'][_0x42bc('0x2a')];var _0x53f084=_0x48f33b[_0x42bc('0x2b')](_0x48f33b[_0x42bc('0x2c')]('(')+0x1,_0x48f33b[_0x42bc('0x2c')](')'));_0x399ce6['ticketId']=_0x53f084;_0x3cbf63(_0x399ce6);})[_0x42bc('0x11')](function(_0x4164a0){_0x1cc820(_0x4164a0[_0x42bc('0x15')]);});});}function emit(_0x3446a3,_0xd18a63,_0x5bd7b3,_0x527348){var _0x591990={'uri':util[_0x42bc('0x47')]('%s/main.aspx?pagetype=entityrecord&etn=%s&id=%s',_0x3446a3,_0x5bd7b3,_0x527348)};io['to'](util[_0x42bc('0x47')](_0x42bc('0x48'),_0xd18a63))['emit'](_0x42bc('0x49'),_0x591990);}exports['cs']=function(_0x15a99d,_0x1e01fe,_0x2a0e91,_0x24f23f,_0x79e538,_0x28b5e9,_0x214908,_0x11bbc0){var _0x4c9e8a={};_0x28b5e9[_0x42bc('0xd')]=intUtil[_0x42bc('0x4a')](_0x28b5e9[_0x42bc('0xd')]);_0x28b5e9[_0x42bc('0x4b')]=intUtil[_0x42bc('0x4a')](_0x28b5e9[_0x42bc('0x4b')]);_0x4c9e8a[_0x42bc('0x45')]=_0x11bbc0==='queue'||_0x11bbc0===_0x42bc('0x4c')?_0x42bc('0x46'):_0x42bc('0x25');var _0x22c42c=util[_0x42bc('0x47')](_0x42bc('0x4d'),_0x28b5e9[_0x42bc('0xd')]);var _0x3ed374=util['format'](_0x42bc('0x4e'),_0x28b5e9[_0x42bc('0x4f')]);var _0x296a29=util['format'](_0x42bc('0x50'),_0x28b5e9[_0x42bc('0x4b')],_0x1e01fe[_0x42bc('0x51')],md5(_0x1e01fe[_0x42bc('0x51')]));if(_0x79e538&&_0x79e538[_0x42bc('0x52')]){logger['info'](_0x42bc('0x53'),_0x42bc('0x54'));_0x1e01fe[_0x42bc('0x55')]=_0x296a29;}else if(_0x214908&&_0x214908[_0x42bc('0x56')]&&_0x214908[_0x42bc('0x56')]!==_0x42bc('0x57')){logger[_0x42bc('0x1e')](_0x42bc('0x58'),'Recording\x20is\x20enabled!');_0x1e01fe[_0x42bc('0x55')]=_0x296a29;}else{_0x1e01fe[_0x42bc('0x55')]='';}return getUser(_0x4c9e8a,_0x28b5e9,_0x24f23f,_0x3ed374,_0x22c42c,_0x11bbc0)[_0x42bc('0x10')](function(_0x510a40){logger['info']('['+_0x11bbc0[_0x42bc('0x13')]()+']\x20The\x20User\x20ID\x20is:',_0x510a40[_0x42bc('0x20')]);logger['info']('['+_0x11bbc0[_0x42bc('0x13')]()+_0x42bc('0x59'),_0x510a40[_0x42bc('0x22')]);return getEndUser(_0x510a40,_0x28b5e9,_0x3ed374,_0x22c42c,_0x1e01fe,_0x11bbc0);})['then'](function(_0x5c9111){logger[_0x42bc('0x1e')]('['+_0x11bbc0['toUpperCase']()+_0x42bc('0x5a'),_0x5c9111[_0x42bc('0x30')]);logger['info']('['+_0x11bbc0['toUpperCase']()+_0x42bc('0x5b'),_0x1e01fe['calleridnum']);return getTicket(_0x5c9111,_0x28b5e9,_0x3ed374,_0x22c42c,_0x1e01fe,_0x2a0e91,_0x11bbc0);})[_0x42bc('0x10')](function(_0x5298f7){logger[_0x42bc('0x1e')](util['format']('['+_0x11bbc0[_0x42bc('0x13')]()+']\x20The\x20%s\x20ID\x20is:\x20%s',_0x2a0e91[_0x42bc('0x42')],_0x5298f7[_0x42bc('0x5c')]));logger[_0x42bc('0x1e')](util[_0x42bc('0x47')]('['+_0x11bbc0['toUpperCase']()+']\x20Call\x20direction:\x20%s',_0x11bbc0));if(_0x11bbc0!==_0x42bc('0x4c')){logger[_0x42bc('0x1e')]('['+_0x11bbc0[_0x42bc('0x13')]()+']',_0x42bc('0x5d'));emit(_0x28b5e9[_0x42bc('0xd')],_0x24f23f[_0x42bc('0x5e')],_0x2a0e91['ticketType'],_0x5298f7[_0x42bc('0x5c')]);}})['catch'](function(_0x29544d){logger['error']('['+_0x11bbc0[_0x42bc('0x13')]()+_0x42bc('0x5f'),util[_0x42bc('0x60')](_0x29544d,{'showHidden':!![],'depth':null}));});};