7f5115ced3c8c179419ec1f9a39a0b998b803b56
[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 _0xc95b=['type','string','nameField','customField','idField','substring','indexOf','content','incident','assign','getString','Subjects','Descriptions','/contacts(','/systemusers(','Fields','ticketType','phonecall','queue','callDirection','odata-entityid','ticketId','%s/main.aspx?pagetype=entityrecord&etn=%s&id=%s','format','user:%s','emit','trigger:browser:url','stripTrailingSlash','serverUrl','tenantId','%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s','uniqueid','monitor_format','Recording\x20is\x20enabled!','recordingFormat','none','[OUTBOUND]','recordingURL',']\x20The\x20User\x20ID\x20is:',']\x20The\x20User\x20email\x20is:',']\x20The\x20Enduser\x20ID\x20is:',']\x20The\x20Enduser\x20phone\x20is:',']\x20Call\x20direction:\x20%s','Displaying\x20call\x20in\x20a\x20new\x20tab...','error','inspect','lodash','bluebird','request-promise','util','ioredis','../../../../config/logger','dynamics365','../../../../config/environment','redis','defaults','localhost','POST','clientId','remoteUri','username','password','then','access_token','catch','toUpperCase',']\x20Wrong\x20credentials!','message','GET','systemusers?$select=internalemailaddress,isdisabled,systemuserid','Bearer\x20','4.0','value','isNil','validateEmail','email','length','internalemailaddress','info',']\x20Agent\x20found\x20','systemuserid','userId',']\x20Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!','userEmail','toLowerCase','outbound','destcalleridnum','contacts','headers','lastIndexOf','contacts?$select=telephone1','calleridnum','telephone1','enduserId','contactid',']\x20Enduser\x20created!',']\x20Enduser\x20Created!','forEach'];(function(_0x4c708b,_0xd3a977){var _0x1f0fff=function(_0x4d2e61){while(--_0x4d2e61){_0x4c708b['push'](_0x4c708b['shift']());}};_0x1f0fff(++_0xd3a977);}(_0xc95b,0xf2));var _0xbc95=function(_0x162bc1,_0x5cd1c6){_0x162bc1=_0x162bc1-0x0;var _0x2dc459=_0xc95b[_0x162bc1];return _0x2dc459;};'use strict';var _=require(_0xbc95('0x0'));var BPromise=require(_0xbc95('0x1'));var rp=require(_0xbc95('0x2'));var util=require(_0xbc95('0x3'));var intUtil=require('./util');var md5=require('md5');var Redis=require(_0xbc95('0x4'));var logger=require(_0xbc95('0x5'))(_0xbc95('0x6'));var config=require(_0xbc95('0x7'));config[_0xbc95('0x8')]=_[_0xbc95('0x9')](config[_0xbc95('0x8')],{'host':_0xbc95('0xa'),'port':0x18eb});var io=require('socket.io-emitter')(new Redis(config[_0xbc95('0x8')]));function getAuthToken(_0x30fa0d,_0x3340cc,_0x3b9394){var _0x425aaf={'method':_0xbc95('0xb'),'uri':_0x3340cc,'form':{'client_id':_0x30fa0d[_0xbc95('0xc')],'resource':_0x30fa0d[_0xbc95('0xd')],'username':_0x30fa0d[_0xbc95('0xe')],'password':_0x30fa0d[_0xbc95('0xf')],'grant_type':_0xbc95('0xf'),'client_secret':_0x30fa0d['clientSecret']},'json':!![]};return rp(_0x425aaf)[_0xbc95('0x10')](function(_0x24fc5f){return _0x24fc5f[_0xbc95('0x11')];})[_0xbc95('0x12')](function(_0x3a11b2){logger['error']('['+[_0x3b9394[_0xbc95('0x13')]()]+_0xbc95('0x14'));return _0x3a11b2[_0xbc95('0x15')];});}function getUser(_0x2edbe9,_0x1b2b35,_0x50cac5,_0x306879,_0x3f454c,_0x38c402){return new BPromise(function(_0x3821f9,_0x2ed7c6){return getAuthToken(_0x1b2b35,_0x306879,_0x38c402)[_0xbc95('0x10')](function(_0xcaffd7){var _0x3abbc4={'method':_0xbc95('0x16'),'uri':_0x3f454c+_0xbc95('0x17'),'headers':{'Authorization':_0xbc95('0x18')+_0xcaffd7,'OData-Version':_0xbc95('0x19')},'json':!![]};return rp(_0x3abbc4);})[_0xbc95('0x10')](function(_0x402fb2){var _0x16cd52=_0x402fb2[_0xbc95('0x1a')];var _0x1126ed=![];var _0xe23c0c=null;if(!_[_0xbc95('0x1b')](_0x50cac5)){if(intUtil[_0xbc95('0x1c')](_0x50cac5['email'])){_0x2edbe9['userEmail']=_0x50cac5[_0xbc95('0x1d')];for(_0xe23c0c=0x0;_0xe23c0c<_0x16cd52[_0xbc95('0x1e')];_0xe23c0c++){if(_0x16cd52[_0xe23c0c][_0xbc95('0x1f')]===_0x50cac5[_0xbc95('0x1d')]&&!_0x16cd52[_0xe23c0c]['isdisabled']){_0x1126ed=!![];logger[_0xbc95('0x20')]('['+[_0x38c402[_0xbc95('0x13')]()]+_0xbc95('0x21')+_0x16cd52[_0xe23c0c][_0xbc95('0x22')]+'!');_0x2edbe9[_0xbc95('0x23')]=_0x16cd52[_0xe23c0c][_0xbc95('0x22')];}}}}if(!_0x1126ed){logger[_0xbc95('0x20')]('['+[_0x38c402[_0xbc95('0x13')]()]+_0xbc95('0x24'));_0x2edbe9[_0xbc95('0x25')]=_0x1b2b35[_0xbc95('0xe')];for(_0xe23c0c=0x0;_0xe23c0c<_0x16cd52[_0xbc95('0x1e')];_0xe23c0c++){if(_0x16cd52[_0xe23c0c][_0xbc95('0x1f')]===_0x1b2b35['username']){_0x2edbe9[_0xbc95('0x23')]=_0x16cd52[_0xe23c0c][_0xbc95('0x22')];}}}_0x3821f9(_0x2edbe9);})[_0xbc95('0x12')](function(_0x2a57fc){_0x2ed7c6(_0x2a57fc[_0xbc95('0x15')]);});});}function createEndUser(_0x1ea0f0,_0x53374b,_0x4dfa3e,_0x107f88,_0xd0bd9b,_0x1d0027){return new BPromise(function(_0x4b55e4,_0x31fb06){return getAuthToken(_0x53374b,_0x4dfa3e,_0x1d0027)[_0xbc95('0x10')](function(_0x31a3bc){var _0x3aefcb=_0x1d0027[_0xbc95('0x26')]()===_0xbc95('0x27')?_0xd0bd9b[_0xbc95('0x28')]:_0xd0bd9b['calleridnum'];var _0x219ba2={'method':_0xbc95('0xb'),'uri':_0x107f88+_0xbc95('0x29'),'body':{'telephone1':_0x3aefcb,'fullname':_0x3aefcb,'firstname':_0x3aefcb,'lastname':'_','mobilephone':null,'address1_telephone1':null,'ownerid@odata.bind':'/systemusers('+_0x1ea0f0[_0xbc95('0x23')]+')'},'headers':{'Authorization':'Bearer\x20'+_0x31a3bc,'OData-Version':_0xbc95('0x19')},'json':!![],'resolveWithFullResponse':!![]};return rp(_0x219ba2);})[_0xbc95('0x10')](function(_0xef15e4){var _0x5383df=_0xef15e4[_0xbc95('0x2a')]['odata-entityid'];var _0x45d896=_0x5383df['substring'](_0x5383df[_0xbc95('0x2b')]('(')+0x1,_0x5383df[_0xbc95('0x2b')](')'));_0x1ea0f0['enduserId']=_0x45d896;_0x4b55e4(_0x1ea0f0);})[_0xbc95('0x12')](function(_0xaf3363){_0x31fb06(_0xaf3363[_0xbc95('0x15')]);});});}function getEndUser(_0x1a2f58,_0x21466d,_0x23ca00,_0x3982a2,_0xbc1b28,_0x50dee6){return new BPromise(function(_0xe774c3,_0x3e66d2){return getAuthToken(_0x21466d,_0x23ca00,_0x50dee6)[_0xbc95('0x10')](function(_0x10998d){var _0xe220a1={'method':_0xbc95('0x16'),'uri':_0x3982a2+_0xbc95('0x2c'),'headers':{'Authorization':'Bearer\x20'+_0x10998d,'If-None-Match':null,'OData-Version':'4.0'},'json':!![]};return rp(_0xe220a1);})['then'](function(_0x51a437){var _0x4bc601=null;if(_0x51a437){var _0x4038e3=_0x51a437[_0xbc95('0x1a')];var _0x4140d9=![];for(_0x4bc601=0x0;_0x4bc601<_0x4038e3[_0xbc95('0x1e')];_0x4bc601++){var _0x32c750=_0x50dee6[_0xbc95('0x26')]()===_0xbc95('0x27')?_0xbc1b28[_0xbc95('0x28')]:_0xbc1b28[_0xbc95('0x2d')];if(_0x4038e3[_0x4bc601][_0xbc95('0x2e')]===_0x32c750){_0x4140d9=!![];_0x1a2f58[_0xbc95('0x2f')]=_0x4038e3[_0x4bc601][_0xbc95('0x30')];}}if(_0x4140d9){_0xe774c3(_0x1a2f58);}else{return createEndUser(_0x1a2f58,_0x21466d,_0x23ca00,_0x3982a2,_0xbc1b28,_0x50dee6)[_0xbc95('0x10')](function(_0x1fbc80){logger['info']('['+[_0x50dee6[_0xbc95('0x13')]()]+_0xbc95('0x31'),_0x1fbc80[_0xbc95('0x2f')]);_0xe774c3(_0x1fbc80);});}}else{return createEndUser(_0x1a2f58,_0x21466d,_0x23ca00,_0x3982a2,_0xbc1b28,_0x50dee6)[_0xbc95('0x10')](function(_0x250c44){logger[_0xbc95('0x20')]('['+[_0x50dee6[_0xbc95('0x13')]()]+_0xbc95('0x32'),_0x250c44[_0xbc95('0x2f')]);_0xe774c3(_0x250c44);});}});});}function getCustomFields(_0x5a38cc,_0x485036,_0x2e3b30){var _0x3ca535={};if(_0x5a38cc[_0xbc95('0x1e')]>0x0){_[_0xbc95('0x33')](_0x5a38cc,function(_0x15aa73){if(_0x15aa73[_0xbc95('0x34')]===_0xbc95('0x35')&&_0x15aa73[_0xbc95('0x36')]&&!_0x15aa73[_0xbc95('0x37')]){var _0x5e33b8=_0x15aa73[_0xbc95('0x38')]['toLowerCase']();var _0x3e5e98=_0x5e33b8[_0xbc95('0x39')](0x0,_0x5e33b8[_0xbc95('0x3a')]('_'));var _0x2a1f7f=_0x5e33b8[_0xbc95('0x39')](_0x5e33b8[_0xbc95('0x3a')]('_')+0x1);if(_0x3e5e98===_0x2e3b30){_0x3ca535[_0x2a1f7f]=_0x15aa73[_0xbc95('0x3b')];}}});}return _0x3ca535;}function getTicket(_0x2d510a,_0xca384a,_0x1a0c69,_0xe049c1,_0x17e122,_0xc8c00b,_0x41dec7){return new BPromise(function(_0x30ce1a,_0x1f437c){var _0x5434d6;return getAuthToken(_0xca384a,_0x1a0c69,_0x41dec7)[_0xbc95('0x10')](function(_0x138f8a){if(_0xc8c00b['ticketType']===_0xbc95('0x3c')){_0x5434d6={'method':_0xbc95('0xb'),'uri':_0xe049c1+'incidents','body':Object[_0xbc95('0x3d')]({},{'title':intUtil[_0xbc95('0x3e')](_0xc8c00b[_0xbc95('0x3f')],_0x17e122,'\x20'),'description':intUtil['getString'](_0xc8c00b[_0xbc95('0x40')],_0x17e122,'\x0a'),'customerid_contact@odata.bind':_0xbc95('0x41')+_0x2d510a['enduserId']+')','ownerid@odata.bind':_0xbc95('0x42')+_0x2d510a['userId']+')','caseorigincode':'1'},getCustomFields(_0xc8c00b[_0xbc95('0x43')],_0x17e122,_0xc8c00b[_0xbc95('0x44')])),'headers':{'Authorization':_0xbc95('0x18')+_0x138f8a,'OData-Version':_0xbc95('0x19')},'json':!![],'resolveWithFullResponse':!![]};}if(_0xc8c00b[_0xbc95('0x44')]===_0xbc95('0x45')){_0x5434d6={'method':_0xbc95('0xb'),'uri':_0xe049c1+'phonecalls','body':Object[_0xbc95('0x3d')]({},{'subject':intUtil[_0xbc95('0x3e')](_0xc8c00b[_0xbc95('0x3f')],_0x17e122,'\x20'),'phonenumber':String(_0x17e122[_0xbc95('0x2d')]),'description':intUtil[_0xbc95('0x3e')](_0xc8c00b[_0xbc95('0x40')],_0x17e122,'\x0a'),'regardingobjectid_contact@odata.bind':'/contacts('+_0x2d510a[_0xbc95('0x2f')]+')','ownerid@odata.bind':_0xbc95('0x42')+_0x2d510a[_0xbc95('0x23')]+')','directioncode':_0x2d510a['callDirection']===_0xbc95('0x46')?![]:!![],'phonecall_activity_parties':[{'partyid_systemuser@odata.bind':_0xbc95('0x42')+_0x2d510a['userId']+')','participationtypemask':_0x2d510a[_0xbc95('0x47')]==='queue'?0x2:0x1},{'partyid_contact@odata.bind':_0xbc95('0x41')+_0x2d510a[_0xbc95('0x2f')]+')','participationtypemask':_0x2d510a[_0xbc95('0x47')]==='queue'?0x1:0x2}]},getCustomFields(_0xc8c00b['Fields'],_0x17e122,_0xc8c00b[_0xbc95('0x44')])),'headers':{'Authorization':_0xbc95('0x18')+_0x138f8a,'OData-Version':_0xbc95('0x19')},'json':!![],'resolveWithFullResponse':!![]};}return rp(_0x5434d6);})['then'](function(_0x30c15d){var _0x43ce20=_0x30c15d[_0xbc95('0x2a')][_0xbc95('0x48')];var _0xfe7975=_0x43ce20['substring'](_0x43ce20[_0xbc95('0x2b')]('(')+0x1,_0x43ce20[_0xbc95('0x2b')](')'));_0x2d510a[_0xbc95('0x49')]=_0xfe7975;_0x30ce1a(_0x2d510a);})[_0xbc95('0x12')](function(_0x92aba0){_0x1f437c(_0x92aba0[_0xbc95('0x15')]);});});}function emit(_0x259b4f,_0x2fcad1,_0x1580d6,_0x38f8f7){var _0x53eda0={'uri':util['format'](_0xbc95('0x4a'),_0x259b4f,_0x1580d6,_0x38f8f7)};io['to'](util[_0xbc95('0x4b')](_0xbc95('0x4c'),_0x2fcad1))[_0xbc95('0x4d')](_0xbc95('0x4e'),_0x53eda0);}exports['cs']=function(_0x192fab,_0x49f226,_0x38979e,_0x38c021,_0x136cfc,_0x5b8ffd,_0x60e518,_0x1a96d4){var _0x4d78f5={};_0x5b8ffd['remoteUri']=intUtil[_0xbc95('0x4f')](_0x5b8ffd[_0xbc95('0xd')]);_0x5b8ffd['serverUrl']=intUtil[_0xbc95('0x4f')](_0x5b8ffd[_0xbc95('0x50')]);_0x4d78f5[_0xbc95('0x47')]=_0x1a96d4==='queue'||_0x1a96d4==='unmanaged'?_0xbc95('0x46'):_0xbc95('0x27');var _0x544744=util[_0xbc95('0x4b')]('%s/api/data/v9.0/',_0x5b8ffd['remoteUri']);var _0x122ba6=util[_0xbc95('0x4b')]('https://login.microsoftonline.com/%s/oauth2/token',_0x5b8ffd[_0xbc95('0x51')]);var _0x2a8cc4=util[_0xbc95('0x4b')](_0xbc95('0x52'),_0x5b8ffd[_0xbc95('0x50')],_0x49f226['uniqueid'],md5(_0x49f226[_0xbc95('0x53')]));if(_0x136cfc&&_0x136cfc[_0xbc95('0x54')]){logger[_0xbc95('0x20')]('[QUEUE]',_0xbc95('0x55'));_0x49f226['recordingURL']=_0x2a8cc4;}else if(_0x60e518&&_0x60e518[_0xbc95('0x56')]&&_0x60e518['recordingFormat']!==_0xbc95('0x57')){logger[_0xbc95('0x20')](_0xbc95('0x58'),'Recording\x20is\x20enabled!');_0x49f226[_0xbc95('0x59')]=_0x2a8cc4;}else{_0x49f226[_0xbc95('0x59')]='';}return getUser(_0x4d78f5,_0x5b8ffd,_0x38c021,_0x122ba6,_0x544744,_0x1a96d4)[_0xbc95('0x10')](function(_0xa49108){logger[_0xbc95('0x20')]('['+_0x1a96d4[_0xbc95('0x13')]()+_0xbc95('0x5a'),_0xa49108['userId']);logger[_0xbc95('0x20')]('['+_0x1a96d4[_0xbc95('0x13')]()+_0xbc95('0x5b'),_0xa49108[_0xbc95('0x25')]);return getEndUser(_0xa49108,_0x5b8ffd,_0x122ba6,_0x544744,_0x49f226,_0x1a96d4);})[_0xbc95('0x10')](function(_0x23c572){logger['info']('['+_0x1a96d4['toUpperCase']()+_0xbc95('0x5c'),_0x23c572[_0xbc95('0x2f')]);logger[_0xbc95('0x20')]('['+_0x1a96d4[_0xbc95('0x13')]()+_0xbc95('0x5d'),_0x49f226[_0xbc95('0x2d')]);return getTicket(_0x23c572,_0x5b8ffd,_0x122ba6,_0x544744,_0x49f226,_0x38979e,_0x1a96d4);})[_0xbc95('0x10')](function(_0x23c78a){logger[_0xbc95('0x20')](util[_0xbc95('0x4b')]('['+_0x1a96d4[_0xbc95('0x13')]()+']\x20The\x20%s\x20ID\x20is:\x20%s',_0x38979e[_0xbc95('0x44')],_0x23c78a[_0xbc95('0x49')]));logger[_0xbc95('0x20')](util[_0xbc95('0x4b')]('['+_0x1a96d4[_0xbc95('0x13')]()+_0xbc95('0x5e'),_0x1a96d4));if(_0x1a96d4!=='unmanaged'){logger['info']('['+_0x1a96d4[_0xbc95('0x13')]()+']',_0xbc95('0x5f'));emit(_0x5b8ffd[_0xbc95('0xd')],_0x38c021['name'],_0x38979e[_0xbc95('0x44')],_0x23c78a['ticketId']);}})[_0xbc95('0x12')](function(_0x148eb9){logger[_0xbc95('0x60')]('['+_0x1a96d4[_0xbc95('0x13')]()+']\x20Error\x20running\x20Dynamics365\x20integration',util[_0xbc95('0x61')](_0x148eb9,{'showHidden':!![],'depth':null}));});};