32175fb7c9818082380be18693de63a0bb54fe26
[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 _0x1bf7=['outbound','destcalleridnum','calleridnum','contacts','headers','odata-entityid','lastIndexOf','contacts?$select=telephone1','telephone1','enduserId',']\x20Enduser\x20Created!','forEach','type','string','nameField','idField','substring','indexOf','content','ticketType','incident','incidents','getString','Subjects','Descriptions','/contacts(','/systemusers(','Fields','phonecalls','assign','queue','callDirection','ticketId','format','%s/main.aspx?pagetype=entityrecord&etn=%s&id=%s','emit','remoteUri','serverUrl','stripTrailingSlash','tenantId','%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s','uniqueid','monitor_format','[QUEUE]','Recording\x20is\x20enabled!','recordingURL','recordingFormat','[OUTBOUND]',']\x20The\x20User\x20ID\x20is:',']\x20The\x20User\x20email\x20is:','userEmail',']\x20The\x20Enduser\x20ID\x20is:',']\x20The\x20Enduser\x20phone\x20is:',']\x20The\x20%s\x20ID\x20is:\x20%s',']\x20Call\x20direction:\x20%s','unmanaged','Displaying\x20call\x20in\x20a\x20new\x20tab...','name',']\x20Error\x20running\x20Dynamics365\x20integration','inspect','./util','ioredis','../../../../config/logger','dynamics365','../../../../config/environment','redis','defaults','localhost','socket.io-emitter','POST','username','clientSecret','then','access_token','catch','error','toUpperCase',']\x20Wrong\x20credentials!','message','GET','systemusers?$select=internalemailaddress,isdisabled,systemuserid','Bearer\x20','4.0','isNil','validateEmail','email','length','isdisabled',']\x20Agent\x20found\x20','systemuserid','info',']\x20Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!','internalemailaddress','userId'];(function(_0x29dbbd,_0x410267){var _0x1ea22f=function(_0x34fa26){while(--_0x34fa26){_0x29dbbd['push'](_0x29dbbd['shift']());}};_0x1ea22f(++_0x410267);}(_0x1bf7,0x1b4));var _0x71bf=function(_0x3d0e12,_0x41e165){_0x3d0e12=_0x3d0e12-0x0;var _0x22da51=_0x1bf7[_0x3d0e12];return _0x22da51;};'use strict';var _=require('lodash');var BPromise=require('bluebird');var rp=require('request-promise');var util=require('util');var intUtil=require(_0x71bf('0x0'));var md5=require('md5');var Redis=require(_0x71bf('0x1'));var logger=require(_0x71bf('0x2'))(_0x71bf('0x3'));var config=require(_0x71bf('0x4'));config[_0x71bf('0x5')]=_[_0x71bf('0x6')](config[_0x71bf('0x5')],{'host':_0x71bf('0x7'),'port':0x18eb});var io=require(_0x71bf('0x8'))(new Redis(config[_0x71bf('0x5')]));function getAuthToken(_0x5524e6,_0x58579b,_0x158f03){var _0x1bbbb3={'method':_0x71bf('0x9'),'uri':_0x58579b,'form':{'client_id':_0x5524e6['clientId'],'resource':_0x5524e6['remoteUri'],'username':_0x5524e6[_0x71bf('0xa')],'password':_0x5524e6['password'],'grant_type':'password','client_secret':_0x5524e6[_0x71bf('0xb')]},'json':!![]};return rp(_0x1bbbb3)[_0x71bf('0xc')](function(_0xa60440){return _0xa60440[_0x71bf('0xd')];})[_0x71bf('0xe')](function(_0x2e724b){logger[_0x71bf('0xf')]('['+[_0x158f03[_0x71bf('0x10')]()]+_0x71bf('0x11'));return _0x2e724b[_0x71bf('0x12')];});}function getUser(_0x4fb124,_0xdf8a03,_0x462c27,_0x22874c,_0x100f06,_0x5cf6e0){return new BPromise(function(_0x32cc47,_0x46179d){return getAuthToken(_0xdf8a03,_0x22874c,_0x5cf6e0)[_0x71bf('0xc')](function(_0x14f48a){var _0x4a7eb0={'method':_0x71bf('0x13'),'uri':_0x100f06+_0x71bf('0x14'),'headers':{'Authorization':_0x71bf('0x15')+_0x14f48a,'OData-Version':_0x71bf('0x16')},'json':!![]};return rp(_0x4a7eb0);})[_0x71bf('0xc')](function(_0x37e07e){var _0x21301c=_0x37e07e['value'];var _0xa4dfee=![];var _0x15841a=null;if(!_[_0x71bf('0x17')](_0x462c27)){if(intUtil[_0x71bf('0x18')](_0x462c27[_0x71bf('0x19')])){_0x4fb124['userEmail']=_0x462c27['email'];for(_0x15841a=0x0;_0x15841a<_0x21301c[_0x71bf('0x1a')];_0x15841a++){if(_0x21301c[_0x15841a]['internalemailaddress']===_0x462c27[_0x71bf('0x19')]&&!_0x21301c[_0x15841a][_0x71bf('0x1b')]){_0xa4dfee=!![];logger['info']('['+[_0x5cf6e0['toUpperCase']()]+_0x71bf('0x1c')+_0x21301c[_0x15841a]['systemuserid']+'!');_0x4fb124['userId']=_0x21301c[_0x15841a][_0x71bf('0x1d')];}}}}if(!_0xa4dfee){logger[_0x71bf('0x1e')]('['+[_0x5cf6e0[_0x71bf('0x10')]()]+_0x71bf('0x1f'));_0x4fb124['userEmail']=_0xdf8a03[_0x71bf('0xa')];for(_0x15841a=0x0;_0x15841a<_0x21301c[_0x71bf('0x1a')];_0x15841a++){if(_0x21301c[_0x15841a][_0x71bf('0x20')]===_0xdf8a03[_0x71bf('0xa')]){_0x4fb124[_0x71bf('0x21')]=_0x21301c[_0x15841a]['systemuserid'];}}}_0x32cc47(_0x4fb124);})['catch'](function(_0x5919a1){_0x46179d(_0x5919a1[_0x71bf('0x12')]);});});}function createEndUser(_0x38abb2,_0x5b3d29,_0xbf1ba4,_0x99f272,_0x501b67,_0x30d4b5){return new BPromise(function(_0x58cae5,_0x599d39){return getAuthToken(_0x5b3d29,_0xbf1ba4,_0x30d4b5)[_0x71bf('0xc')](function(_0x2a4823){var _0x18f41d=_0x30d4b5['toLowerCase']()===_0x71bf('0x22')?_0x501b67[_0x71bf('0x23')]:_0x501b67[_0x71bf('0x24')];var _0x45213a={'method':_0x71bf('0x9'),'uri':_0x99f272+_0x71bf('0x25'),'body':{'telephone1':_0x18f41d,'fullname':_0x18f41d,'firstname':_0x18f41d,'lastname':'_','mobilephone':null,'address1_telephone1':null,'ownerid@odata.bind':'/systemusers('+_0x38abb2['userId']+')'},'headers':{'Authorization':_0x71bf('0x15')+_0x2a4823,'OData-Version':'4.0'},'json':!![],'resolveWithFullResponse':!![]};return rp(_0x45213a);})[_0x71bf('0xc')](function(_0x12c0c8){var _0x4546e2=_0x12c0c8[_0x71bf('0x26')][_0x71bf('0x27')];var _0x37c8ea=_0x4546e2['substring'](_0x4546e2[_0x71bf('0x28')]('(')+0x1,_0x4546e2[_0x71bf('0x28')](')'));_0x38abb2['enduserId']=_0x37c8ea;_0x58cae5(_0x38abb2);})[_0x71bf('0xe')](function(_0x4779de){_0x599d39(_0x4779de[_0x71bf('0x12')]);});});}function getEndUser(_0x4f18d8,_0x315ae1,_0x3a5a4d,_0x41fee7,_0x10f06f,_0x286fc8){return new BPromise(function(_0x134f36,_0x20fcb4){return getAuthToken(_0x315ae1,_0x3a5a4d,_0x286fc8)[_0x71bf('0xc')](function(_0x13ffa2){var _0x380331={'method':_0x71bf('0x13'),'uri':_0x41fee7+_0x71bf('0x29'),'headers':{'Authorization':'Bearer\x20'+_0x13ffa2,'If-None-Match':null,'OData-Version':'4.0'},'json':!![]};return rp(_0x380331);})[_0x71bf('0xc')](function(_0x585615){var _0x5d32df=null;if(_0x585615){var _0x42c7a5=_0x585615['value'];var _0x4f6133=![];for(_0x5d32df=0x0;_0x5d32df<_0x42c7a5[_0x71bf('0x1a')];_0x5d32df++){var _0x36555c=_0x286fc8['toLowerCase']()===_0x71bf('0x22')?_0x10f06f['destcalleridnum']:_0x10f06f['calleridnum'];if(_0x42c7a5[_0x5d32df][_0x71bf('0x2a')]===_0x36555c){_0x4f6133=!![];_0x4f18d8[_0x71bf('0x2b')]=_0x42c7a5[_0x5d32df]['contactid'];}}if(_0x4f6133){_0x134f36(_0x4f18d8);}else{return createEndUser(_0x4f18d8,_0x315ae1,_0x3a5a4d,_0x41fee7,_0x10f06f,_0x286fc8)[_0x71bf('0xc')](function(_0x4e5788){logger[_0x71bf('0x1e')]('['+[_0x286fc8['toUpperCase']()]+']\x20Enduser\x20created!',_0x4e5788[_0x71bf('0x2b')]);_0x134f36(_0x4e5788);});}}else{return createEndUser(_0x4f18d8,_0x315ae1,_0x3a5a4d,_0x41fee7,_0x10f06f,_0x286fc8)[_0x71bf('0xc')](function(_0x54d3bf){logger[_0x71bf('0x1e')]('['+[_0x286fc8[_0x71bf('0x10')]()]+_0x71bf('0x2c'),_0x54d3bf[_0x71bf('0x2b')]);_0x134f36(_0x54d3bf);});}});});}function getCustomFields(_0x4f00d3,_0x339540,_0x2dfaae){var _0x4dbf78={};if(_0x4f00d3[_0x71bf('0x1a')]>0x0){_[_0x71bf('0x2d')](_0x4f00d3,function(_0x5dab67){if(_0x5dab67[_0x71bf('0x2e')]===_0x71bf('0x2f')&&_0x5dab67[_0x71bf('0x30')]&&!_0x5dab67['customField']){var _0xf58c43=_0x5dab67[_0x71bf('0x31')]['toLowerCase']();var _0x2b0eea=_0xf58c43[_0x71bf('0x32')](0x0,_0xf58c43[_0x71bf('0x33')]('_'));var _0x80df3a=_0xf58c43[_0x71bf('0x32')](_0xf58c43['indexOf']('_')+0x1);if(_0x2b0eea===_0x2dfaae){_0x4dbf78[_0x80df3a]=_0x5dab67[_0x71bf('0x34')];}}});}return _0x4dbf78;}function getTicket(_0x3ca51b,_0x3d8731,_0x2c4776,_0x1235c2,_0x26579c,_0x46f5a3,_0x47c8d9){return new BPromise(function(_0x1af5c5,_0x27dbfe){var _0x1355e9;return getAuthToken(_0x3d8731,_0x2c4776,_0x47c8d9)['then'](function(_0x458020){if(_0x46f5a3[_0x71bf('0x35')]===_0x71bf('0x36')){_0x1355e9={'method':_0x71bf('0x9'),'uri':_0x1235c2+_0x71bf('0x37'),'body':Object['assign']({},{'title':intUtil[_0x71bf('0x38')](_0x46f5a3[_0x71bf('0x39')],_0x26579c,'\x20'),'description':intUtil[_0x71bf('0x38')](_0x46f5a3[_0x71bf('0x3a')],_0x26579c,'\x0a'),'customerid_contact@odata.bind':_0x71bf('0x3b')+_0x3ca51b[_0x71bf('0x2b')]+')','ownerid@odata.bind':_0x71bf('0x3c')+_0x3ca51b['userId']+')','caseorigincode':'1'},getCustomFields(_0x46f5a3[_0x71bf('0x3d')],_0x26579c,_0x46f5a3[_0x71bf('0x35')])),'headers':{'Authorization':_0x71bf('0x15')+_0x458020,'OData-Version':_0x71bf('0x16')},'json':!![],'resolveWithFullResponse':!![]};}if(_0x46f5a3[_0x71bf('0x35')]==='phonecall'){_0x1355e9={'method':_0x71bf('0x9'),'uri':_0x1235c2+_0x71bf('0x3e'),'body':Object[_0x71bf('0x3f')]({},{'subject':intUtil[_0x71bf('0x38')](_0x46f5a3['Subjects'],_0x26579c,'\x20'),'phonenumber':String(_0x26579c['calleridnum']),'description':intUtil[_0x71bf('0x38')](_0x46f5a3[_0x71bf('0x3a')],_0x26579c,'\x0a'),'regardingobjectid_contact@odata.bind':_0x71bf('0x3b')+_0x3ca51b[_0x71bf('0x2b')]+')','ownerid@odata.bind':_0x71bf('0x3c')+_0x3ca51b[_0x71bf('0x21')]+')','directioncode':_0x3ca51b['callDirection']===_0x71bf('0x40')?![]:!![],'phonecall_activity_parties':[{'partyid_systemuser@odata.bind':_0x71bf('0x3c')+_0x3ca51b[_0x71bf('0x21')]+')','participationtypemask':_0x3ca51b[_0x71bf('0x41')]===_0x71bf('0x40')?0x2:0x1},{'partyid_contact@odata.bind':_0x71bf('0x3b')+_0x3ca51b[_0x71bf('0x2b')]+')','participationtypemask':_0x3ca51b[_0x71bf('0x41')]===_0x71bf('0x40')?0x1:0x2}]},getCustomFields(_0x46f5a3[_0x71bf('0x3d')],_0x26579c,_0x46f5a3[_0x71bf('0x35')])),'headers':{'Authorization':'Bearer\x20'+_0x458020,'OData-Version':_0x71bf('0x16')},'json':!![],'resolveWithFullResponse':!![]};}return rp(_0x1355e9);})[_0x71bf('0xc')](function(_0x470dc0){var _0x2ba32a=_0x470dc0[_0x71bf('0x26')][_0x71bf('0x27')];var _0x1aca9e=_0x2ba32a[_0x71bf('0x32')](_0x2ba32a['lastIndexOf']('(')+0x1,_0x2ba32a[_0x71bf('0x28')](')'));_0x3ca51b[_0x71bf('0x42')]=_0x1aca9e;_0x1af5c5(_0x3ca51b);})[_0x71bf('0xe')](function(_0x35da98){_0x27dbfe(_0x35da98[_0x71bf('0x12')]);});});}function emit(_0x564d49,_0x5c3617,_0xdac919,_0xb39077){var _0x569cd7={'uri':util[_0x71bf('0x43')](_0x71bf('0x44'),_0x564d49,_0xdac919,_0xb39077)};io['to'](util[_0x71bf('0x43')]('user:%s',_0x5c3617))[_0x71bf('0x45')]('trigger:browser:url',_0x569cd7);}exports['cs']=function(_0x20fc86,_0x49b648,_0x3a9c1e,_0x37acf0,_0x393eb3,_0x2f4d66,_0x5f397b,_0x4efb5f){var _0x2eed4f={};_0x2f4d66[_0x71bf('0x46')]=intUtil['stripTrailingSlash'](_0x2f4d66[_0x71bf('0x46')]);_0x2f4d66[_0x71bf('0x47')]=intUtil[_0x71bf('0x48')](_0x2f4d66['serverUrl']);_0x2eed4f[_0x71bf('0x41')]=_0x4efb5f===_0x71bf('0x40')||_0x4efb5f==='unmanaged'?_0x71bf('0x40'):_0x71bf('0x22');var _0x51c856=util[_0x71bf('0x43')]('%s/api/data/v9.0/',_0x2f4d66[_0x71bf('0x46')]);var _0x520c6f=util[_0x71bf('0x43')]('https://login.microsoftonline.com/%s/oauth2/token',_0x2f4d66[_0x71bf('0x49')]);var _0x542f50=util[_0x71bf('0x43')](_0x71bf('0x4a'),_0x2f4d66[_0x71bf('0x47')],_0x49b648['uniqueid'],md5(_0x49b648[_0x71bf('0x4b')]));if(_0x393eb3&&_0x393eb3[_0x71bf('0x4c')]){logger['info'](_0x71bf('0x4d'),_0x71bf('0x4e'));_0x49b648[_0x71bf('0x4f')]=_0x542f50;}else if(_0x5f397b&&_0x5f397b['recordingFormat']&&_0x5f397b[_0x71bf('0x50')]!=='none'){logger[_0x71bf('0x1e')](_0x71bf('0x51'),'Recording\x20is\x20enabled!');_0x49b648[_0x71bf('0x4f')]=_0x542f50;}else{_0x49b648[_0x71bf('0x4f')]='';}return getUser(_0x2eed4f,_0x2f4d66,_0x37acf0,_0x520c6f,_0x51c856,_0x4efb5f)[_0x71bf('0xc')](function(_0x3eff68){logger['info']('['+_0x4efb5f[_0x71bf('0x10')]()+_0x71bf('0x52'),_0x3eff68[_0x71bf('0x21')]);logger[_0x71bf('0x1e')]('['+_0x4efb5f[_0x71bf('0x10')]()+_0x71bf('0x53'),_0x3eff68[_0x71bf('0x54')]);return getEndUser(_0x3eff68,_0x2f4d66,_0x520c6f,_0x51c856,_0x49b648,_0x4efb5f);})[_0x71bf('0xc')](function(_0x26e860){logger['info']('['+_0x4efb5f['toUpperCase']()+_0x71bf('0x55'),_0x26e860[_0x71bf('0x2b')]);logger[_0x71bf('0x1e')]('['+_0x4efb5f[_0x71bf('0x10')]()+_0x71bf('0x56'),_0x49b648[_0x71bf('0x24')]);return getTicket(_0x26e860,_0x2f4d66,_0x520c6f,_0x51c856,_0x49b648,_0x3a9c1e,_0x4efb5f);})[_0x71bf('0xc')](function(_0x1e24e7){logger[_0x71bf('0x1e')](util[_0x71bf('0x43')]('['+_0x4efb5f[_0x71bf('0x10')]()+_0x71bf('0x57'),_0x3a9c1e[_0x71bf('0x35')],_0x1e24e7[_0x71bf('0x42')]));logger[_0x71bf('0x1e')](util['format']('['+_0x4efb5f['toUpperCase']()+_0x71bf('0x58'),_0x4efb5f));if(_0x4efb5f!==_0x71bf('0x59')){logger[_0x71bf('0x1e')]('['+_0x4efb5f['toUpperCase']()+']',_0x71bf('0x5a'));emit(_0x2f4d66[_0x71bf('0x46')],_0x37acf0[_0x71bf('0x5b')],_0x3a9c1e['ticketType'],_0x1e24e7['ticketId']);}})['catch'](function(_0x380cd1){logger[_0x71bf('0xf')]('['+_0x4efb5f[_0x71bf('0x10')]()+_0x71bf('0x5c'),util[_0x71bf('0x5d')](_0x380cd1,{'showHidden':!![],'depth':null}));});};