6e518cce88b303bf4a63fa2aae5fd2659dffa9f1
[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 _0xf782=[']\x20The\x20Enduser\x20phone\x20is:','calleridnum',']\x20The\x20%s\x20ID\x20is:\x20%s',']\x20Call\x20direction:\x20%s','Displaying\x20call\x20in\x20a\x20new\x20tab...','name','error',']\x20Error\x20running\x20Dynamics365\x20integration','inspect','request-promise','util','./util','md5','ioredis','../../../../config/logger','../../../../config/environment','defaults','redis','localhost','socket.io-emitter','POST','clientId','remoteUri','username','password','clientSecret','then','access_token','catch','toUpperCase',']\x20Wrong\x20credentials!','message','systemusers?$select=internalemailaddress,isdisabled,systemuserid','Bearer\x20','4.0','validateEmail','email','userEmail','length','internalemailaddress','isdisabled',']\x20Agent\x20found\x20','systemuserid','userId','info',']\x20Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!','outbound','contacts','/systemusers(','headers','odata-entityid','substring','lastIndexOf','enduserId','contacts?$select=telephone1','value','destcalleridnum','telephone1',']\x20Enduser\x20created!','type','string','nameField','customField','idField','toLowerCase','indexOf','content','ticketType','incident','assign','getString','Descriptions','Fields','phonecall','phonecalls','Subjects','/contacts(','callDirection','queue','ticketId','format','%s/main.aspx?pagetype=entityrecord&etn=%s&id=%s','user:%s','trigger:browser:url','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','[QUEUE]','Recording\x20is\x20enabled!','recordingURL','none','[OUTBOUND]',']\x20The\x20User\x20ID\x20is:',']\x20The\x20User\x20email\x20is:'];(function(_0x444127,_0x27d1e9){var _0x5855c0=function(_0x143f95){while(--_0x143f95){_0x444127['push'](_0x444127['shift']());}};_0x5855c0(++_0x27d1e9);}(_0xf782,0xcd));var _0x2f78=function(_0x548627,_0x4ec392){_0x548627=_0x548627-0x0;var _0x844a84=_0xf782[_0x548627];return _0x844a84;};'use strict';var _=require('lodash');var BPromise=require('bluebird');var rp=require(_0x2f78('0x0'));var util=require(_0x2f78('0x1'));var intUtil=require(_0x2f78('0x2'));var md5=require(_0x2f78('0x3'));var Redis=require(_0x2f78('0x4'));var logger=require(_0x2f78('0x5'))('dynamics365');var config=require(_0x2f78('0x6'));config['redis']=_[_0x2f78('0x7')](config[_0x2f78('0x8')],{'host':_0x2f78('0x9'),'port':0x18eb});var io=require(_0x2f78('0xa'))(new Redis(config[_0x2f78('0x8')]));function getAuthToken(_0x490418,_0x180f85,_0x5e6ee3){var _0x4f0c71={'method':_0x2f78('0xb'),'uri':_0x180f85,'form':{'client_id':_0x490418[_0x2f78('0xc')],'resource':_0x490418[_0x2f78('0xd')],'username':_0x490418[_0x2f78('0xe')],'password':_0x490418[_0x2f78('0xf')],'grant_type':_0x2f78('0xf'),'client_secret':_0x490418[_0x2f78('0x10')]},'json':!![]};return rp(_0x4f0c71)[_0x2f78('0x11')](function(_0x34e86d){return _0x34e86d[_0x2f78('0x12')];})[_0x2f78('0x13')](function(_0x4d35b9){logger['error']('['+[_0x5e6ee3[_0x2f78('0x14')]()]+_0x2f78('0x15'));return _0x4d35b9[_0x2f78('0x16')];});}function getUser(_0x5df4c7,_0x1b061d,_0x2f841a,_0x55e78b,_0x585652,_0x173f1e){return new BPromise(function(_0x44a25f,_0x75b6b7){return getAuthToken(_0x1b061d,_0x55e78b,_0x173f1e)[_0x2f78('0x11')](function(_0x44328c){var _0x20d79d={'method':'GET','uri':_0x585652+_0x2f78('0x17'),'headers':{'Authorization':_0x2f78('0x18')+_0x44328c,'OData-Version':_0x2f78('0x19')},'json':!![]};return rp(_0x20d79d);})['then'](function(_0x23cc5d){var _0x151d38=_0x23cc5d['value'];var _0x5457e2=![];var _0x2ba2d5=null;if(!_['isNil'](_0x2f841a)){if(intUtil[_0x2f78('0x1a')](_0x2f841a[_0x2f78('0x1b')])){_0x5df4c7[_0x2f78('0x1c')]=_0x2f841a['email'];for(_0x2ba2d5=0x0;_0x2ba2d5<_0x151d38[_0x2f78('0x1d')];_0x2ba2d5++){if(_0x151d38[_0x2ba2d5][_0x2f78('0x1e')]===_0x2f841a['email']&&!_0x151d38[_0x2ba2d5][_0x2f78('0x1f')]){_0x5457e2=!![];logger['info']('['+[_0x173f1e['toUpperCase']()]+_0x2f78('0x20')+_0x151d38[_0x2ba2d5][_0x2f78('0x21')]+'!');_0x5df4c7[_0x2f78('0x22')]=_0x151d38[_0x2ba2d5][_0x2f78('0x21')];}}}}if(!_0x5457e2){logger[_0x2f78('0x23')]('['+[_0x173f1e[_0x2f78('0x14')]()]+_0x2f78('0x24'));_0x5df4c7['userEmail']=_0x1b061d['username'];for(_0x2ba2d5=0x0;_0x2ba2d5<_0x151d38[_0x2f78('0x1d')];_0x2ba2d5++){if(_0x151d38[_0x2ba2d5][_0x2f78('0x1e')]===_0x1b061d[_0x2f78('0xe')]){_0x5df4c7[_0x2f78('0x22')]=_0x151d38[_0x2ba2d5][_0x2f78('0x21')];}}}_0x44a25f(_0x5df4c7);})[_0x2f78('0x13')](function(_0x323e9d){_0x75b6b7(_0x323e9d[_0x2f78('0x16')]);});});}function createEndUser(_0x59872f,_0x278ee7,_0x3ed5e4,_0x489cea,_0x16b2a4,_0x33ecab){return new BPromise(function(_0x3e8f0d,_0x42fcde){return getAuthToken(_0x278ee7,_0x3ed5e4,_0x33ecab)[_0x2f78('0x11')](function(_0x56551f){var _0x284706=_0x33ecab['toLowerCase']()===_0x2f78('0x25')?_0x16b2a4['destcalleridnum']:_0x16b2a4['calleridnum'];var _0x5569c1={'method':_0x2f78('0xb'),'uri':_0x489cea+_0x2f78('0x26'),'body':{'telephone1':_0x284706,'fullname':_0x284706,'firstname':_0x284706,'lastname':'_','mobilephone':null,'address1_telephone1':null,'ownerid@odata.bind':_0x2f78('0x27')+_0x59872f[_0x2f78('0x22')]+')'},'headers':{'Authorization':_0x2f78('0x18')+_0x56551f,'OData-Version':_0x2f78('0x19')},'json':!![],'resolveWithFullResponse':!![]};return rp(_0x5569c1);})[_0x2f78('0x11')](function(_0x2b46cc){var _0x2ffaa1=_0x2b46cc[_0x2f78('0x28')][_0x2f78('0x29')];var _0x48a352=_0x2ffaa1[_0x2f78('0x2a')](_0x2ffaa1[_0x2f78('0x2b')]('(')+0x1,_0x2ffaa1[_0x2f78('0x2b')](')'));_0x59872f[_0x2f78('0x2c')]=_0x48a352;_0x3e8f0d(_0x59872f);})[_0x2f78('0x13')](function(_0x3b6f43){_0x42fcde(_0x3b6f43[_0x2f78('0x16')]);});});}function getEndUser(_0x178df4,_0xef6081,_0x2a65f0,_0xdc208f,_0x24fba0,_0x2fac1c){return new BPromise(function(_0x578946,_0x1fe053){return getAuthToken(_0xef6081,_0x2a65f0,_0x2fac1c)[_0x2f78('0x11')](function(_0x20c63f){var _0x32592b={'method':'GET','uri':_0xdc208f+_0x2f78('0x2d'),'headers':{'Authorization':_0x2f78('0x18')+_0x20c63f,'If-None-Match':null,'OData-Version':_0x2f78('0x19')},'json':!![]};return rp(_0x32592b);})[_0x2f78('0x11')](function(_0x10e110){var _0x473cc1=null;if(_0x10e110){var _0x2d3367=_0x10e110[_0x2f78('0x2e')];var _0x8d5c4=![];for(_0x473cc1=0x0;_0x473cc1<_0x2d3367['length'];_0x473cc1++){var _0x20c465=_0x2fac1c['toLowerCase']()==='outbound'?_0x24fba0[_0x2f78('0x2f')]:_0x24fba0['calleridnum'];if(_0x2d3367[_0x473cc1][_0x2f78('0x30')]===_0x20c465){_0x8d5c4=!![];_0x178df4[_0x2f78('0x2c')]=_0x2d3367[_0x473cc1]['contactid'];}}if(_0x8d5c4){_0x578946(_0x178df4);}else{return createEndUser(_0x178df4,_0xef6081,_0x2a65f0,_0xdc208f,_0x24fba0,_0x2fac1c)['then'](function(_0x32d971){logger[_0x2f78('0x23')]('['+[_0x2fac1c['toUpperCase']()]+_0x2f78('0x31'),_0x32d971[_0x2f78('0x2c')]);_0x578946(_0x32d971);});}}else{return createEndUser(_0x178df4,_0xef6081,_0x2a65f0,_0xdc208f,_0x24fba0,_0x2fac1c)['then'](function(_0x385b46){logger[_0x2f78('0x23')]('['+[_0x2fac1c['toUpperCase']()]+']\x20Enduser\x20Created!',_0x385b46['enduserId']);_0x578946(_0x385b46);});}});});}function getCustomFields(_0x58c3c6,_0x290568,_0xca860d){var _0x4099e3={};if(_0x58c3c6['length']>0x0){_['forEach'](_0x58c3c6,function(_0x292e06){if(_0x292e06[_0x2f78('0x32')]===_0x2f78('0x33')&&_0x292e06[_0x2f78('0x34')]&&!_0x292e06[_0x2f78('0x35')]){var _0x1628a2=_0x292e06[_0x2f78('0x36')][_0x2f78('0x37')]();var _0x2e58a9=_0x1628a2[_0x2f78('0x2a')](0x0,_0x1628a2[_0x2f78('0x38')]('_'));var _0x353fa4=_0x1628a2[_0x2f78('0x2a')](_0x1628a2[_0x2f78('0x38')]('_')+0x1);if(_0x2e58a9===_0xca860d){_0x4099e3[_0x353fa4]=_0x292e06[_0x2f78('0x39')];}}});}return _0x4099e3;}function getTicket(_0xd18186,_0x5a8b28,_0x1bc103,_0xadd27c,_0x92033d,_0x338438,_0x14aebf){return new BPromise(function(_0x54bd73,_0x6f0dcc){var _0x3350d2;return getAuthToken(_0x5a8b28,_0x1bc103,_0x14aebf)[_0x2f78('0x11')](function(_0x18d784){if(_0x338438[_0x2f78('0x3a')]===_0x2f78('0x3b')){_0x3350d2={'method':'POST','uri':_0xadd27c+'incidents','body':Object[_0x2f78('0x3c')]({},{'title':intUtil[_0x2f78('0x3d')](_0x338438['Subjects'],_0x92033d,'\x20'),'description':intUtil[_0x2f78('0x3d')](_0x338438[_0x2f78('0x3e')],_0x92033d,'\x0a'),'customerid_contact@odata.bind':'/contacts('+_0xd18186['enduserId']+')','ownerid@odata.bind':_0x2f78('0x27')+_0xd18186[_0x2f78('0x22')]+')','caseorigincode':'1'},getCustomFields(_0x338438[_0x2f78('0x3f')],_0x92033d,_0x338438[_0x2f78('0x3a')])),'headers':{'Authorization':_0x2f78('0x18')+_0x18d784,'OData-Version':_0x2f78('0x19')},'json':!![],'resolveWithFullResponse':!![]};}if(_0x338438[_0x2f78('0x3a')]===_0x2f78('0x40')){_0x3350d2={'method':'POST','uri':_0xadd27c+_0x2f78('0x41'),'body':Object[_0x2f78('0x3c')]({},{'subject':intUtil[_0x2f78('0x3d')](_0x338438[_0x2f78('0x42')],_0x92033d,'\x20'),'phonenumber':String(_0x92033d['calleridnum']),'description':intUtil[_0x2f78('0x3d')](_0x338438[_0x2f78('0x3e')],_0x92033d,'\x0a'),'regardingobjectid_contact@odata.bind':_0x2f78('0x43')+_0xd18186[_0x2f78('0x2c')]+')','ownerid@odata.bind':_0x2f78('0x27')+_0xd18186[_0x2f78('0x22')]+')','directioncode':_0xd18186[_0x2f78('0x44')]===_0x2f78('0x45')?![]:!![],'phonecall_activity_parties':[{'partyid_systemuser@odata.bind':_0x2f78('0x27')+_0xd18186[_0x2f78('0x22')]+')','participationtypemask':_0xd18186[_0x2f78('0x44')]==='queue'?0x2:0x1},{'partyid_contact@odata.bind':_0x2f78('0x43')+_0xd18186[_0x2f78('0x2c')]+')','participationtypemask':_0xd18186[_0x2f78('0x44')]===_0x2f78('0x45')?0x1:0x2}]},getCustomFields(_0x338438[_0x2f78('0x3f')],_0x92033d,_0x338438[_0x2f78('0x3a')])),'headers':{'Authorization':'Bearer\x20'+_0x18d784,'OData-Version':_0x2f78('0x19')},'json':!![],'resolveWithFullResponse':!![]};}return rp(_0x3350d2);})['then'](function(_0x300609){var _0x54c133=_0x300609[_0x2f78('0x28')][_0x2f78('0x29')];var _0x43abca=_0x54c133['substring'](_0x54c133['lastIndexOf']('(')+0x1,_0x54c133[_0x2f78('0x2b')](')'));_0xd18186[_0x2f78('0x46')]=_0x43abca;_0x54bd73(_0xd18186);})[_0x2f78('0x13')](function(_0x3d6794){_0x6f0dcc(_0x3d6794[_0x2f78('0x16')]);});});}function emit(_0x21540f,_0x233ef3,_0x2c275c,_0x3db8d6){var _0x2e2196={'uri':util[_0x2f78('0x47')](_0x2f78('0x48'),_0x21540f,_0x2c275c,_0x3db8d6)};io['to'](util[_0x2f78('0x47')](_0x2f78('0x49'),_0x233ef3))['emit'](_0x2f78('0x4a'),_0x2e2196);}exports['cs']=function(_0x9dfab3,_0x350cd7,_0xd5bd,_0x546cb8,_0x597b7a,_0x1e2123,_0x1322ad,_0x20ae84){var _0x3589c9={};_0x1e2123[_0x2f78('0xd')]=intUtil['stripTrailingSlash'](_0x1e2123[_0x2f78('0xd')]);_0x1e2123[_0x2f78('0x4b')]=intUtil['stripTrailingSlash'](_0x1e2123[_0x2f78('0x4b')]);_0x3589c9['callDirection']=_0x20ae84==='queue'||_0x20ae84===_0x2f78('0x4c')?_0x2f78('0x45'):_0x2f78('0x25');var _0x30a764=util[_0x2f78('0x47')](_0x2f78('0x4d'),_0x1e2123[_0x2f78('0xd')]);var _0x42d552=util[_0x2f78('0x47')](_0x2f78('0x4e'),_0x1e2123[_0x2f78('0x4f')]);var _0x1537d9=util['format'](_0x2f78('0x50'),_0x1e2123['serverUrl'],_0x350cd7[_0x2f78('0x51')],md5(_0x350cd7['uniqueid']));if(_0x597b7a&&_0x597b7a['monitor_format']){logger[_0x2f78('0x23')](_0x2f78('0x52'),_0x2f78('0x53'));_0x350cd7[_0x2f78('0x54')]=_0x1537d9;}else if(_0x1322ad&&_0x1322ad['recordingFormat']&&_0x1322ad['recordingFormat']!==_0x2f78('0x55')){logger[_0x2f78('0x23')](_0x2f78('0x56'),'Recording\x20is\x20enabled!');_0x350cd7[_0x2f78('0x54')]=_0x1537d9;}else{_0x350cd7[_0x2f78('0x54')]='';}return getUser(_0x3589c9,_0x1e2123,_0x546cb8,_0x42d552,_0x30a764,_0x20ae84)[_0x2f78('0x11')](function(_0x4b5ee5){logger[_0x2f78('0x23')]('['+_0x20ae84[_0x2f78('0x14')]()+_0x2f78('0x57'),_0x4b5ee5[_0x2f78('0x22')]);logger[_0x2f78('0x23')]('['+_0x20ae84['toUpperCase']()+_0x2f78('0x58'),_0x4b5ee5[_0x2f78('0x1c')]);return getEndUser(_0x4b5ee5,_0x1e2123,_0x42d552,_0x30a764,_0x350cd7,_0x20ae84);})[_0x2f78('0x11')](function(_0x538c18){logger[_0x2f78('0x23')]('['+_0x20ae84[_0x2f78('0x14')]()+']\x20The\x20Enduser\x20ID\x20is:',_0x538c18[_0x2f78('0x2c')]);logger[_0x2f78('0x23')]('['+_0x20ae84[_0x2f78('0x14')]()+_0x2f78('0x59'),_0x350cd7[_0x2f78('0x5a')]);return getTicket(_0x538c18,_0x1e2123,_0x42d552,_0x30a764,_0x350cd7,_0xd5bd,_0x20ae84);})[_0x2f78('0x11')](function(_0xdca5a9){logger['info'](util['format']('['+_0x20ae84[_0x2f78('0x14')]()+_0x2f78('0x5b'),_0xd5bd[_0x2f78('0x3a')],_0xdca5a9[_0x2f78('0x46')]));logger[_0x2f78('0x23')](util[_0x2f78('0x47')]('['+_0x20ae84['toUpperCase']()+_0x2f78('0x5c'),_0x20ae84));if(_0x20ae84!==_0x2f78('0x4c')){logger[_0x2f78('0x23')]('['+_0x20ae84['toUpperCase']()+']',_0x2f78('0x5d'));emit(_0x1e2123['remoteUri'],_0x546cb8[_0x2f78('0x5e')],_0xd5bd[_0x2f78('0x3a')],_0xdca5a9['ticketId']);}})['catch'](function(_0x4d15ad){logger[_0x2f78('0x5f')]('['+_0x20ae84['toUpperCase']()+_0x2f78('0x60'),util[_0x2f78('0x61')](_0x4d15ad,{'showHidden':!![],'depth':null}));});};