9efd8606dcdec8a4596bd9363e15de45e2ead397
[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 _0x7fb2=['lastIndexOf','outbound','contacts?$select=','&$filter=','%2b','contactid','nothing',']\x20Enduser\x20NOT\x20Created\x20-\x20ModuleCreate\x20=\x20Nothing',']\x20Enduser\x20created!','includes','string','nameField','customField','idField','indexOf','ticketType','incident','incidents','getString','Subjects','Descriptions','enduserId','/contacts(','Fields','phonecall','callDirection','queue','phonecalls','assign','headers','ticketId','format','%s/main.aspx?pagetype=entityrecord&etn=%s&id=%s','user:%s','emit','trigger:browser:url','stripTrailingSlash','serverUrl','unmanaged','modernAuthentication','%s/api/data/v9.0/','https://login.microsoftonline.com/%s/oauth2/token','uniqueid','[QUEUE]','recordingFormat','[OUTBOUND]','Recording\x20is\x20enabled!','recordingURL',']\x20The\x20User\x20ID\x20is:',']\x20The\x20User\x20email\x20is:','moduleCreate',']\x20The\x20Enduser\x20ID\x20is:',']\x20The\x20Enduser\x20phone\x20is:',']\x20The\x20%s\x20ID\x20is:\x20%s','openNewTab','name','Error\x20running\x20Dynamics365\x20integration','lodash','request-promise','util','./util','md5','ioredis','dynamics365','../../../../config/environment','defaults','redis','socket.io-emitter','remoteUri','CloudProvider','data7','tenantId','data2','POST','clientId','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','userEmail','email','length','internalemailaddress','info',']\x20Agent\x20found\x20','userId','systemuserid',']\x20Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!','toLowerCase','destcalleridnum','calleridnum','contacts','/systemusers(','body','odata-entityid','substring'];(function(_0x14add3,_0x13ce8d){var _0x4a174f=function(_0x2b3ed7){while(--_0x2b3ed7){_0x14add3['push'](_0x14add3['shift']());}};_0x4a174f(++_0x13ce8d);}(_0x7fb2,0x113));var _0x27fb=function(_0x594faf,_0x1a2209){_0x594faf=_0x594faf-0x0;var _0x5dfa74=_0x7fb2[_0x594faf];return _0x5dfa74;};'use strict';var _=require(_0x27fb('0x0'));var rp=require(_0x27fb('0x1'));var util=require(_0x27fb('0x2'));var intUtil=require(_0x27fb('0x3'));var md5=require(_0x27fb('0x4'));var Redis=require(_0x27fb('0x5'));var logger=require('../../../../config/logger')(_0x27fb('0x6'));var config=require(_0x27fb('0x7'));config['redis']=_[_0x27fb('0x8')](config[_0x27fb('0x9')],{'host':'localhost','port':0x18eb});var io=require(_0x27fb('0xa'))(new Redis(config[_0x27fb('0x9')]));function getAuthToken(_0x34547d,_0x55563e,_0x313957){if(_0x34547d['modernAuthentication']){_0x34547d[_0x27fb('0xb')]=_0x34547d[_0x27fb('0xc')][_0x27fb('0xd')];_0x34547d[_0x27fb('0xe')]=_0x34547d['CloudProvider'][_0x27fb('0xf')];return Promise['resolve'](_0x34547d['CloudProvider']['data5']);}var _0x3b53b0={'method':_0x27fb('0x10'),'uri':_0x55563e,'form':{'client_id':_0x34547d[_0x27fb('0x11')],'resource':_0x34547d[_0x27fb('0xb')],'username':_0x34547d[_0x27fb('0x12')],'password':_0x34547d[_0x27fb('0x13')],'grant_type':_0x27fb('0x13'),'client_secret':_0x34547d[_0x27fb('0x14')]},'json':!![]};return rp(_0x3b53b0)[_0x27fb('0x15')](function(_0x537106){return _0x537106[_0x27fb('0x16')];})[_0x27fb('0x17')](function(_0x2cb85e){logger[_0x27fb('0x18')]('['+[_0x313957[_0x27fb('0x19')]()]+_0x27fb('0x1a'));return _0x2cb85e[_0x27fb('0x1b')];});}function getUser(_0xca5c82,_0x382ad5,_0x2ed678,_0x3e4489,_0x5a3fe2,_0x379d0f){return new Promise(function(_0x337691,_0x2de28f){return getAuthToken(_0x382ad5,_0x3e4489,_0x379d0f)['then'](function(_0x44e8f3){var _0x240027={'method':_0x27fb('0x1c'),'uri':_0x5a3fe2+_0x27fb('0x1d'),'headers':{'Authorization':_0x27fb('0x1e')+_0x44e8f3,'OData-Version':_0x27fb('0x1f')},'json':!![]};return rp(_0x240027);})[_0x27fb('0x15')](function(_0x2acd3c){var _0x329f8d=_0x2acd3c[_0x27fb('0x20')];var _0x5f263d=![];var _0x53a265=null;if(!_[_0x27fb('0x21')](_0x2ed678)){if(intUtil[_0x27fb('0x22')](_0x2ed678['email'])){_0xca5c82[_0x27fb('0x23')]=_0x2ed678[_0x27fb('0x24')];for(_0x53a265=0x0;_0x53a265<_0x329f8d[_0x27fb('0x25')];_0x53a265++){if(_0x329f8d[_0x53a265][_0x27fb('0x26')]===_0x2ed678[_0x27fb('0x24')]&&!_0x329f8d[_0x53a265]['isdisabled']){_0x5f263d=!![];logger[_0x27fb('0x27')]('['+[_0x379d0f['toUpperCase']()]+_0x27fb('0x28')+_0x329f8d[_0x53a265]['systemuserid']+'!');_0xca5c82[_0x27fb('0x29')]=_0x329f8d[_0x53a265][_0x27fb('0x2a')];}}}}if(!_0x5f263d){logger[_0x27fb('0x27')]('['+[_0x379d0f[_0x27fb('0x19')]()]+_0x27fb('0x2b'));_0xca5c82[_0x27fb('0x23')]=_0x382ad5[_0x27fb('0x12')];for(_0x53a265=0x0;_0x53a265<_0x329f8d[_0x27fb('0x25')];_0x53a265++){if(_0x329f8d[_0x53a265][_0x27fb('0x26')]===_0x382ad5['username']){_0xca5c82[_0x27fb('0x29')]=_0x329f8d[_0x53a265]['systemuserid'];}}}_0x337691(_0xca5c82);})['catch'](function(_0x44f4bb){_0x2de28f(_0x44f4bb[_0x27fb('0x1b')]);});});}function createEndUser(_0x1f30a1,_0x5e8278,_0x10032e,_0x1267b3,_0x48c23f,_0x333122,_0x42b7b7){return new Promise(function(_0x58873d,_0x21aded){return getAuthToken(_0x5e8278,_0x10032e,_0x333122)[_0x27fb('0x15')](function(_0x2cf6c1){var _0x1336e6=_0x333122[_0x27fb('0x2c')]()==='outbound'?_0x48c23f[_0x27fb('0x2d')]:_0x48c23f[_0x27fb('0x2e')];var _0xbb5559={'method':_0x27fb('0x10'),'uri':_0x1267b3+_0x27fb('0x2f'),'body':{'fullname':_0x1336e6,'firstname':_0x1336e6,'lastname':'_','mobilephone':null,'address1_telephone1':null,'ownerid@odata.bind':_0x27fb('0x30')+_0x1f30a1['userId']+')'},'headers':{'Authorization':'Bearer\x20'+_0x2cf6c1,'OData-Version':_0x27fb('0x1f')},'json':!![],'resolveWithFullResponse':!![]};_0xbb5559[_0x27fb('0x31')][_0x42b7b7]=_0x1336e6;return rp(_0xbb5559);})[_0x27fb('0x15')](function(_0x25aab7){var _0x35e5dd=_0x25aab7['headers'][_0x27fb('0x32')];var _0x1caa5d=_0x35e5dd[_0x27fb('0x33')](_0x35e5dd[_0x27fb('0x34')]('(')+0x1,_0x35e5dd[_0x27fb('0x34')](')'));_0x1f30a1['enduserId']=_0x1caa5d;_0x58873d(_0x1f30a1);})[_0x27fb('0x17')](function(_0x3ed639){_0x21aded(_0x3ed639[_0x27fb('0x1b')]);});});}function getEndUser(_0x135246,_0xd6361a,_0x2940ea,_0x5ea6e6,_0x33afc4,_0x3e0fb4,_0x28d07f,_0x19aa3a){var _0x507d87=_0x3e0fb4['toLowerCase']()===_0x27fb('0x35')?_0x33afc4['destcalleridnum']:_0x33afc4[_0x27fb('0x2e')];return getAuthToken(_0xd6361a,_0x2940ea,_0x3e0fb4)['then'](function(_0x30872c){_0x19aa3a=_0x19aa3a?_0x19aa3a:'telephone1';var _0x51678f={'method':_0x27fb('0x1c'),'uri':_0x5ea6e6+_0x27fb('0x36')+_0x19aa3a+_0x27fb('0x37')+_0x19aa3a+'\x20eq\x20\x27'+_0x507d87['replace']('+',_0x27fb('0x38'))+'\x27','headers':{'Authorization':_0x27fb('0x1e')+_0x30872c,'If-None-Match':null,'OData-Version':_0x27fb('0x1f')},'json':!![]};return rp(_0x51678f);})[_0x27fb('0x15')](function(_0x45a06e){var _0x43b642=![];if(!_0x45a06e)return _0x43b642;var _0x226075=_0x45a06e[_0x27fb('0x20')];for(var _0x4f07ba=0x0;_0x4f07ba<_0x226075[_0x27fb('0x25')];_0x4f07ba++){if(_0x226075[_0x4f07ba][_0x19aa3a]===_0x507d87){_0x43b642=!![];_0x135246['enduserId']=_0x226075[_0x4f07ba][_0x27fb('0x39')];}}return _0x43b642;})['then'](function(_0x322d83){if(_0x322d83)return _0x135246;if(_0x28d07f===_0x27fb('0x3a')){logger[_0x27fb('0x27')]('['+[_0x3e0fb4[_0x27fb('0x19')]()]+_0x27fb('0x3b'));return _0x135246;}return createEndUser(_0x135246,_0xd6361a,_0x2940ea,_0x5ea6e6,_0x33afc4,_0x3e0fb4,_0x19aa3a)[_0x27fb('0x15')](function(_0x2a4596){logger['info']('['+[_0x3e0fb4[_0x27fb('0x19')]()]+_0x27fb('0x3c'),_0x2a4596['enduserId']);return _0x2a4596;});});}function getCustomFields(_0xa06340,_0x2bc971,_0x32b7de){var _0xe1b42b={};if(_0xa06340[_0x27fb('0x25')]>0x0){_['forEach'](_0xa06340,function(_0x2b156f){if(_[_0x27fb('0x3d')]([_0x27fb('0x3e'),'picklist'],_0x2b156f['type'])&&_0x2b156f[_0x27fb('0x3f')]&&!_0x2b156f[_0x27fb('0x40')]){var _0x44912=_0x2b156f[_0x27fb('0x41')][_0x27fb('0x2c')]();var _0x3d22d9=_0x44912[_0x27fb('0x33')](0x0,_0x44912['indexOf']('_'));var _0x289477=_0x44912[_0x27fb('0x33')](_0x44912[_0x27fb('0x42')]('_')+0x1);if(_0x3d22d9===_0x32b7de){_0xe1b42b[_0x289477]=_0x2b156f['content'];}}});}return _0xe1b42b;}function getTicket(_0x1e86c8,_0x317bba,_0x36e51c,_0x31f50f,_0x2fcc48,_0x2f11ec,_0x2d73ac){return new Promise(function(_0x316752,_0x3b0518){var _0xd5e3b8;return getAuthToken(_0x317bba,_0x36e51c,_0x2d73ac)[_0x27fb('0x15')](function(_0x4a340e){if(_0x2f11ec[_0x27fb('0x43')]===_0x27fb('0x44')){_0xd5e3b8={'method':_0x27fb('0x10'),'uri':_0x31f50f+_0x27fb('0x45'),'body':Object['assign']({},{'title':intUtil[_0x27fb('0x46')](_0x2f11ec[_0x27fb('0x47')],_0x2fcc48,'\x20'),'description':intUtil['getString'](_0x2f11ec[_0x27fb('0x48')],_0x2fcc48,'\x0a'),'customerid_contact@odata.bind':_0x1e86c8[_0x27fb('0x49')]?_0x27fb('0x4a')+_0x1e86c8[_0x27fb('0x49')]+')':undefined,'ownerid@odata.bind':'/systemusers('+_0x1e86c8[_0x27fb('0x29')]+')','caseorigincode':'1'},getCustomFields(_0x2f11ec[_0x27fb('0x4b')],_0x2fcc48,_0x2f11ec[_0x27fb('0x43')])),'headers':{'Authorization':_0x27fb('0x1e')+_0x4a340e,'OData-Version':_0x27fb('0x1f')},'json':!![],'resolveWithFullResponse':!![]};}if(_0x2f11ec['ticketType']===_0x27fb('0x4c')){var _0x26e1e4=_0x1e86c8[_0x27fb('0x49')]?[{'partyid_systemuser@odata.bind':_0x27fb('0x30')+_0x1e86c8[_0x27fb('0x29')]+')','participationtypemask':_0x1e86c8[_0x27fb('0x4d')]===_0x27fb('0x4e')?0x2:0x1},{'partyid_contact@odata.bind':_0x27fb('0x4a')+_0x1e86c8[_0x27fb('0x49')]+')','participationtypemask':_0x1e86c8[_0x27fb('0x4d')]===_0x27fb('0x4e')?0x1:0x2}]:[{'partyid_systemuser@odata.bind':'/systemusers('+_0x1e86c8[_0x27fb('0x29')]+')','participationtypemask':_0x1e86c8[_0x27fb('0x4d')]==='queue'?0x2:0x1}];_0xd5e3b8={'method':_0x27fb('0x10'),'uri':_0x31f50f+_0x27fb('0x4f'),'body':Object[_0x27fb('0x50')]({},{'subject':intUtil[_0x27fb('0x46')](_0x2f11ec['Subjects'],_0x2fcc48,'\x20'),'phonenumber':String(_0x2fcc48[_0x27fb('0x2e')]),'description':intUtil[_0x27fb('0x46')](_0x2f11ec[_0x27fb('0x48')],_0x2fcc48,'\x0a'),'regardingobjectid_contact@odata.bind':_0x1e86c8[_0x27fb('0x49')]?_0x27fb('0x4a')+_0x1e86c8['enduserId']+')':undefined,'ownerid@odata.bind':_0x27fb('0x30')+_0x1e86c8['userId']+')','directioncode':_0x1e86c8[_0x27fb('0x4d')]===_0x27fb('0x4e')?![]:!![],'phonecall_activity_parties':_0x26e1e4},getCustomFields(_0x2f11ec[_0x27fb('0x4b')],_0x2fcc48,_0x2f11ec[_0x27fb('0x43')])),'headers':{'Authorization':_0x27fb('0x1e')+_0x4a340e,'OData-Version':_0x27fb('0x1f')},'json':!![],'resolveWithFullResponse':!![]};}return rp(_0xd5e3b8);})[_0x27fb('0x15')](function(_0x1333c5){var _0x25749f=_0x1333c5[_0x27fb('0x51')][_0x27fb('0x32')];var _0x44c7cb=_0x25749f[_0x27fb('0x33')](_0x25749f[_0x27fb('0x34')]('(')+0x1,_0x25749f['lastIndexOf'](')'));_0x1e86c8[_0x27fb('0x52')]=_0x44c7cb;_0x316752(_0x1e86c8);})[_0x27fb('0x17')](function(_0x214528){_0x3b0518(_0x214528['message']);});});}function emit(_0x5a80e4,_0x44c8a9,_0x436141,_0x34bce7){var _0x28b947={'uri':util[_0x27fb('0x53')](_0x27fb('0x54'),_0x5a80e4,_0x436141,_0x34bce7)};io['to'](util[_0x27fb('0x53')](_0x27fb('0x55'),_0x44c8a9))[_0x27fb('0x56')](_0x27fb('0x57'),_0x28b947);}exports['cs']=function(_0xbae9af,_0x1a1d4f,_0xd27891,_0x3bc1e4,_0x41ad53,_0x498c61,_0x592a58,_0x23281b){var _0x52f6df={};_0x498c61[_0x27fb('0xb')]=_0x498c61['modernAuthentication']?intUtil[_0x27fb('0x58')](_0x498c61['CloudProvider'][_0x27fb('0xd')]):intUtil[_0x27fb('0x58')](_0x498c61[_0x27fb('0xb')]);_0x498c61[_0x27fb('0x59')]=intUtil[_0x27fb('0x58')](_0x498c61[_0x27fb('0x59')]);_0x52f6df['callDirection']=_0x23281b===_0x27fb('0x4e')||_0x23281b===_0x27fb('0x5a')?_0x27fb('0x4e'):_0x27fb('0x35');var _0x133da1=_0x498c61[_0x27fb('0x5b')]?util['format'](_0x27fb('0x5c'),_0x498c61[_0x27fb('0xc')][_0x27fb('0xd')]):util[_0x27fb('0x53')](_0x27fb('0x5c'),_0x498c61[_0x27fb('0xb')]);var _0x3a4423=util[_0x27fb('0x53')](_0x27fb('0x5d'),_0x498c61[_0x27fb('0xe')]);var _0x3014c8=util[_0x27fb('0x53')]('%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s',_0x498c61['serverUrl'],_0x1a1d4f[_0x27fb('0x5e')],md5(_0x1a1d4f[_0x27fb('0x5e')]));if(_0x41ad53&&_0x41ad53['monitor_format']){logger['info'](_0x27fb('0x5f'),'Recording\x20is\x20enabled!');_0x1a1d4f['recordingURL']=_0x3014c8;}else if(_0x592a58&&_0x592a58[_0x27fb('0x60')]&&_0x592a58['recordingFormat']!=='none'){logger[_0x27fb('0x27')](_0x27fb('0x61'),_0x27fb('0x62'));_0x1a1d4f[_0x27fb('0x63')]=_0x3014c8;}else{_0x1a1d4f[_0x27fb('0x63')]='';}return getUser(_0x52f6df,_0x498c61,_0x3bc1e4,_0x3a4423,_0x133da1,_0x23281b)[_0x27fb('0x15')](function(_0x17d32b){logger['info']('['+_0x23281b[_0x27fb('0x19')]()+_0x27fb('0x64'),_0x17d32b[_0x27fb('0x29')]);logger['info']('['+_0x23281b[_0x27fb('0x19')]()+_0x27fb('0x65'),_0x17d32b['userEmail']);return getEndUser(_0x17d32b,_0x498c61,_0x3a4423,_0x133da1,_0x1a1d4f,_0x23281b,_0xd27891[_0x27fb('0x66')],_0xd27891['additionalSearchContact']);})[_0x27fb('0x15')](function(_0x10d1fa){logger[_0x27fb('0x27')]('['+_0x23281b['toUpperCase']()+_0x27fb('0x67'),_0x10d1fa[_0x27fb('0x49')]);logger[_0x27fb('0x27')]('['+_0x23281b[_0x27fb('0x19')]()+_0x27fb('0x68'),_0x1a1d4f['calleridnum']);return getTicket(_0x10d1fa,_0x498c61,_0x3a4423,_0x133da1,_0x1a1d4f,_0xd27891,_0x23281b);})[_0x27fb('0x15')](function(_0x1f9c5a){logger[_0x27fb('0x27')](util['format']('['+_0x23281b['toUpperCase']()+_0x27fb('0x69'),_0xd27891[_0x27fb('0x43')],_0x1f9c5a[_0x27fb('0x52')]));logger['info'](util[_0x27fb('0x53')]('['+_0x23281b['toUpperCase']()+']\x20Call\x20direction:\x20%s',_0x23281b));if(_0x23281b!==_0x27fb('0x5a')&&_0xd27891[_0x27fb('0x6a')]){logger[_0x27fb('0x27')]('['+_0x23281b[_0x27fb('0x19')]()+']','Displaying\x20call\x20in\x20a\x20new\x20tab...');emit(_0x498c61[_0x27fb('0xb')],_0x3bc1e4[_0x27fb('0x6b')],_0xd27891['ticketType'],_0x1f9c5a[_0x27fb('0x52')]);}})[_0x27fb('0x17')](function(_0x3819fd){logger[_0x27fb('0x18')]('['+_0x23281b[_0x27fb('0x19')]()+']\x20Error\x20running\x20Dynamics365\x20integration',_0x3819fd[_0x27fb('0x1b')]);console[_0x27fb('0x18')](_0x27fb('0x6c'),_0x3819fd);});};