Built motion from commit (unavailable).|2.5.31
[motion2.git] / server / services / ami / trigger / integration / desk.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 _0xd5df=['Enduser\x20found!','_embedded','Enduser\x20not\x20found,\x20creating\x20a\x20new\x20enduser!','%s/customers','work','length','map','forEach','includes','picklist','type','nameField','content','string','idField','custom_fields','variable','variableName','customField','customVariable','Variable','name','toLowerCase','priority','Creating\x20new\x20ticket...','merge','getString','Subjects','new','Motion','CTI','concat','Tags','Descriptions','/api/v2/users/%d','Fields','%s/customers/%d/cases','POST','queue','stripTrailingSlash','serverUrl','monitor_format','Recording\x20is\x20enabled!','%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s','recordingURL','Authorization','email','The\x20User\x20ID\x20is:','The\x20Enduser\x20ID\x20is:','Ticket\x20created!','The\x20Ticket\x20ID\x20is:','integrationTab','[QUEUE]','Displaying\x20ticket\x20to\x20agent...','trigger:desk:display','newTab','Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...','user:%s','trigger:browser:url','https://xcally.desk.com/web/agent/case/%d','Desk\x20account\x20type\x20not\x20recognized','error','outbound','recordingFormat','none','[OUTBOUND]','uniqueid','destcalleridname','destcalleridnum','%s/agent/tickets/%s','catch','unmanaged','lastevent','toUpperCase','calleridname','lodash','request-promise','md5','ioredis','./util','../../../../config/environment','../../../../config/logger','desk','redis','socket.io-emitter','emit','application/json','validateEmail','%s/users','then','total_entries','find','entries','info','Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!','remoteUri','Invalid\x20email\x20format,\x20using\x20the\x20admin\x20account!','%s/users/me','debug','[HTTP]','stringify','defaults','GET','authType','format','%s:%s','username','password','%s/token:%s','token','Basic\x20%s','toString','Searching\x20enduser\x20with\x20phone','%s/customers/search'];(function(_0x871323,_0x124801){var _0x51e770=function(_0x1fe5d5){while(--_0x1fe5d5){_0x871323['push'](_0x871323['shift']());}};_0x51e770(++_0x124801);}(_0xd5df,0x1a1));var _0xfd5d=function(_0x2ac85e,_0x30bcef){_0x2ac85e=_0x2ac85e-0x0;var _0x47edfc=_0xd5df[_0x2ac85e];return _0x47edfc;};'use strict';var _=require(_0xfd5d('0x0'));var rp=require(_0xfd5d('0x1'));var md5=require(_0xfd5d('0x2'));var util=require('util');var Redis=require(_0xfd5d('0x3'));var intUtil=require(_0xfd5d('0x4'));var config=require(_0xfd5d('0x5'));var logger=require(_0xfd5d('0x6'))(_0xfd5d('0x7'));config['redis']=_['defaults'](config[_0xfd5d('0x8')],{'host':'localhost','port':0x18eb});var io=require(_0xfd5d('0x9'))(new Redis(config[_0xfd5d('0x8')]));function emit(_0x58f75b,_0x2c10d2,_0x5bafe0){io['to'](_0x58f75b)[_0xfd5d('0xa')](_0x2c10d2,_0x5bafe0);}var headers={'Content-Type':_0xfd5d('0xb'),'Accept':'application/json'};function getUser(_0x11ebf9,_0x19698c){if(intUtil[_0xfd5d('0xc')](_0x19698c)===!![]){logger['info']('Getting\x20agent\x20with\x20email',_0x19698c);return request({'uri':util['format'](_0xfd5d('0xd'),_0x11ebf9['remoteUri'])})[_0xfd5d('0xe')](function(_0x4d8072){if(_0x4d8072[_0xfd5d('0xf')]){return _[_0xfd5d('0x10')](_0x4d8072['_embedded'][_0xfd5d('0x11')],{'email':_0x19698c});}})['then'](function(_0x476949){if(_0x476949){logger[_0xfd5d('0x12')]('Agent\x20found!');return _0x476949;}else{logger[_0xfd5d('0x12')](_0xfd5d('0x13'));return request({'uri':util['format']('%s/users/me',_0x11ebf9[_0xfd5d('0x14')])});}});}else{logger[_0xfd5d('0x12')](_0xfd5d('0x15'));return request({'uri':util['format'](_0xfd5d('0x16'),_0x11ebf9[_0xfd5d('0x14')])});}}function request(_0x7b326e){logger[_0xfd5d('0x17')](_0xfd5d('0x18'),JSON[_0xfd5d('0x19')](_0x7b326e));return rp(_[_0xfd5d('0x1a')](_0x7b326e,{'method':_0xfd5d('0x1b'),'headers':headers,'json':!![]}));}function getAuth(_0x4c60a2){var _0x36a427;if(_0x4c60a2[_0xfd5d('0x1c')]==='basic'){_0x36a427=util[_0xfd5d('0x1d')](_0xfd5d('0x1e'),_0x4c60a2[_0xfd5d('0x1f')],_0x4c60a2[_0xfd5d('0x20')]);}else if(_0x4c60a2[_0xfd5d('0x1c')]==='token'){_0x36a427=util[_0xfd5d('0x1d')](_0xfd5d('0x21'),_0x4c60a2[_0xfd5d('0x1f')],_0x4c60a2[_0xfd5d('0x22')]);}return util[_0xfd5d('0x1d')](_0xfd5d('0x23'),new Buffer(_0x36a427)[_0xfd5d('0x24')]('base64'));}function getEndUser(_0x43d317,_0x4409ed,_0x268216){logger['info'](_0xfd5d('0x25'),_0x268216);return request({'uri':util['format'](_0xfd5d('0x26'),_0x43d317[_0xfd5d('0x14')]),'qs':{'phone':_0x268216}})[_0xfd5d('0xe')](function(_0x1786b7){if(_0x1786b7['total_entries']){logger[_0xfd5d('0x12')](_0xfd5d('0x27'));return _0x1786b7[_0xfd5d('0x28')][_0xfd5d('0x11')][0x0];}else{logger[_0xfd5d('0x12')](_0xfd5d('0x29'));return request({'uri':util[_0xfd5d('0x1d')](_0xfd5d('0x2a'),_0x43d317[_0xfd5d('0x14')]),'method':'POST','body':{'first_name':_0x4409ed,'title':_0x4409ed,'phone_numbers':[{'type':_0xfd5d('0x2b'),'value':_0x268216}]}});}});}function getTags(_0x1ad2db,_0x6a2dc7){var _0x57c407=[];if(_0x1ad2db&&_0x1ad2db[_0xfd5d('0x2c')]>0x0){_0x57c407=_[_0xfd5d('0x2d')](_0x1ad2db,_0x6a2dc7);}return _0x57c407;}function getCustomFields(_0xb44a9e,_0x36ad0e){var _0xaead7e=[];var _0xa86d9b={'custom_fields':{}};if(_0xb44a9e[_0xfd5d('0x2c')]>0x0){_[_0xfd5d('0x2e')](_0xb44a9e,function(_0x5cf76c){if(_[_0xfd5d('0x2f')](['string',_0xfd5d('0x30')],_0x5cf76c[_0xfd5d('0x31')])&&_0x5cf76c[_0xfd5d('0x32')]&&!_0x5cf76c['customField']){_0xa86d9b[_0x5cf76c[_0xfd5d('0x32')]['toLowerCase']()]=_0x5cf76c[_0xfd5d('0x33')][_0xfd5d('0x24')]();}else if(_[_0xfd5d('0x2f')]([_0xfd5d('0x34'),_0xfd5d('0x30')],_0x5cf76c[_0xfd5d('0x31')])&&_0x5cf76c[_0xfd5d('0x35')]&&_0x5cf76c['customField']){_0xa86d9b[_0xfd5d('0x36')][_0x5cf76c[_0xfd5d('0x35')]]=_0x5cf76c['content'][_0xfd5d('0x24')]();}else if(_0x5cf76c[_0xfd5d('0x31')]===_0xfd5d('0x37')&&_0x5cf76c[_0xfd5d('0x38')]&&_0x5cf76c['idField']&&_0x5cf76c[_0xfd5d('0x39')]){_0x36ad0e[_0x5cf76c[_0xfd5d('0x38')]]&&(_0xa86d9b['custom_fields'][_0x5cf76c[_0xfd5d('0x35')]]=_0x36ad0e[_0x5cf76c[_0xfd5d('0x38')]]['toString']());}else if(_0x5cf76c[_0xfd5d('0x31')]===_0xfd5d('0x3a')&&_0x5cf76c[_0xfd5d('0x3b')]&&_0x5cf76c[_0xfd5d('0x3b')][_0xfd5d('0x3c')]&&_0x5cf76c[_0xfd5d('0x35')]&&_0x5cf76c[_0xfd5d('0x39')]){_0x36ad0e[_0x5cf76c[_0xfd5d('0x3b')][_0xfd5d('0x3c')]['toLowerCase']()]&&(_0xa86d9b['custom_fields'][_0x5cf76c[_0xfd5d('0x35')]]=_0x36ad0e[_0x5cf76c['Variable']['name'][_0xfd5d('0x3d')]()]['toString']());}});}if(_0xa86d9b[_0xfd5d('0x3e')]){_0xa86d9b['priority']=Number(_0xa86d9b[_0xfd5d('0x3e')]);}return _0xa86d9b;}function getTicket(_0x585bfa,_0x312714,_0x2b3a61,_0xb759f6,_0x4b9d44){logger[_0xfd5d('0x12')](_0xfd5d('0x3f'));var _0x29e6d7=_[_0xfd5d('0x40')]({'subject':intUtil[_0xfd5d('0x41')](_0xb759f6[_0xfd5d('0x42')],_0x4b9d44,'\x20'),'type':'phone','status':_0xfd5d('0x43'),'labels':['xCALLY',_0xfd5d('0x44'),_0xfd5d('0x45')][_0xfd5d('0x46')](getTags(_0xb759f6[_0xfd5d('0x47')],_0xfd5d('0x3c'))),'description':intUtil[_0xfd5d('0x41')](_0xb759f6[_0xfd5d('0x48')],_0x4b9d44,'\x0a'),'_links':{'assigned_user':{'href':util[_0xfd5d('0x1d')](_0xfd5d('0x49'),_0x312714['id']),'class':'user'}},'message':{'direction':'in','body':intUtil[_0xfd5d('0x41')](_0xb759f6['Descriptions'],_0x4b9d44,'\x0a')}},getCustomFields(_0xb759f6[_0xfd5d('0x4a')],_0x4b9d44));return request({'uri':util[_0xfd5d('0x1d')](_0xfd5d('0x4b'),_0x585bfa[_0xfd5d('0x14')],_0x2b3a61['id']),'method':_0xfd5d('0x4c'),'body':_0x29e6d7});}exports[_0xfd5d('0x4d')]=function(_0x5bba0e,_0x170b3c,_0x34204e,_0x49a88e,_0x3ba3c4,_0x328051){var _0x3da20c,_0x486325;_0x328051[_0xfd5d('0x14')]=intUtil[_0xfd5d('0x4e')](_0x328051[_0xfd5d('0x14')]);_0x328051[_0xfd5d('0x4f')]=intUtil['stripTrailingSlash'](_0x328051[_0xfd5d('0x4f')]);if(_0x3ba3c4&&_0x3ba3c4[_0xfd5d('0x50')]){logger['info']('[QUEUE]',_0xfd5d('0x51'));_0x170b3c['recordingURL']=util[_0xfd5d('0x1d')](_0xfd5d('0x52'),_0x328051[_0xfd5d('0x4f')],_0x170b3c['uniqueid'],md5(_0x170b3c['uniqueid']));}else{_0x170b3c[_0xfd5d('0x53')]='';}headers[_0xfd5d('0x54')]=getAuth(_0x328051);return getUser(_0x328051,_0x49a88e[_0xfd5d('0x55')])['then'](function(_0x4bd5a9){_0x3da20c=_0x4bd5a9;logger[_0xfd5d('0x12')](_0xfd5d('0x56'),_0x3da20c['id']);return getEndUser(_0x328051,_0x170b3c['calleridname'],_0x170b3c['calleridnum']);})['then'](function(_0x3fd0a9){_0x486325=_0x3fd0a9;logger[_0xfd5d('0x12')](_0xfd5d('0x57'),_0x486325['id']);return getTicket(_0x328051,_0x3da20c,_0x486325,_0x34204e,_0x170b3c);})[_0xfd5d('0xe')](function(_0xc84653){if(_0xc84653){logger[_0xfd5d('0x12')](_0xfd5d('0x58'));logger[_0xfd5d('0x12')](_0xfd5d('0x59'),_0xc84653['id']);switch(_0x328051['type']){case _0xfd5d('0x5a'):logger['info'](_0xfd5d('0x5b'),_0xfd5d('0x5c'));emit(util[_0xfd5d('0x1d')]('user:%s',_0x49a88e[_0xfd5d('0x3c')]),_0xfd5d('0x5d'),{'recordId':_0xc84653['id']});break;case _0xfd5d('0x5e'):logger[_0xfd5d('0x12')](_0xfd5d('0x5b'),_0xfd5d('0x5f'));emit(util['format'](_0xfd5d('0x60'),_0x49a88e['name']),_0xfd5d('0x61'),{'uri':util[_0xfd5d('0x1d')](_0xfd5d('0x62'),_0xc84653['id'])});break;default:logger['error']('[QUEUE]',_0xfd5d('0x63'));}}})['catch'](function(_0x42b74d){console[_0xfd5d('0x64')](_0x42b74d);logger['error']('[QUEUE]',JSON[_0xfd5d('0x19')](_0x42b74d));});};exports[_0xfd5d('0x65')]=function(_0x446965,_0x318f83,_0x3ca666,_0x307064,_0x1cfa58,_0x3e00cc,_0x48f994){var _0x5c0af1,_0x195cbe;_0x3e00cc[_0xfd5d('0x14')]=intUtil[_0xfd5d('0x4e')](_0x3e00cc[_0xfd5d('0x14')]);_0x3e00cc[_0xfd5d('0x4f')]=intUtil['stripTrailingSlash'](_0x3e00cc[_0xfd5d('0x4f')]);if(_0x48f994&&_0x48f994['recordingFormat']&&_0x48f994[_0xfd5d('0x66')]!==_0xfd5d('0x67')){logger[_0xfd5d('0x12')](_0xfd5d('0x68'),_0xfd5d('0x51'));_0x318f83[_0xfd5d('0x53')]=util['format'](_0xfd5d('0x52'),_0x3e00cc[_0xfd5d('0x4f')],_0x318f83[_0xfd5d('0x69')],md5(_0x318f83[_0xfd5d('0x69')]));}headers[_0xfd5d('0x54')]=getAuth(_0x3e00cc);return getUser(_0x3e00cc,_0x307064[_0xfd5d('0x55')])[_0xfd5d('0xe')](function(_0x12292b){_0x5c0af1=_0x12292b;logger[_0xfd5d('0x12')](_0xfd5d('0x56'),_0x5c0af1['id']);return getEndUser(_0x3e00cc,_0x318f83[_0xfd5d('0x6a')],_0x318f83[_0xfd5d('0x6b')]);})[_0xfd5d('0xe')](function(_0x182cb5){_0x195cbe=_0x182cb5;logger['info']('The\x20Enduser\x20ID\x20is:',_0x195cbe['id']);return getTicket(_0x3e00cc,_0x5c0af1,_0x195cbe,_0x3ca666,_0x318f83);})[_0xfd5d('0xe')](function(_0x481ebd){if(_0x481ebd){logger[_0xfd5d('0x12')](_0xfd5d('0x58'));logger[_0xfd5d('0x12')](_0xfd5d('0x59'),_0x481ebd['id']);switch(_0x3e00cc['type']){case _0xfd5d('0x5a'):logger[_0xfd5d('0x12')](_0xfd5d('0x5b'),_0xfd5d('0x5c'));emit(util[_0xfd5d('0x1d')](_0xfd5d('0x60'),_0x307064[_0xfd5d('0x3c')]),'trigger:desk:display',{'recordId':_0x481ebd['id']});break;case _0xfd5d('0x5e'):logger[_0xfd5d('0x12')](_0xfd5d('0x5b'),_0xfd5d('0x5f'));emit(util['format'](_0xfd5d('0x60'),_0x307064[_0xfd5d('0x3c')]),_0xfd5d('0x61'),{'uri':util['format'](_0xfd5d('0x6c'),_0x3e00cc['remoteUri'],_0x481ebd['id'])});break;default:logger['error'](_0xfd5d('0x68'),_0xfd5d('0x63'));}}})[_0xfd5d('0x6d')](function(_0x145cae){logger[_0xfd5d('0x64')]('[OUTBOUND]',JSON[_0xfd5d('0x19')](_0x145cae));});};exports[_0xfd5d('0x6e')]=function(_0x12bc06,_0x91b883,_0x4edce6,_0xa4392c,_0x4616bf,_0x2ae225){var _0x2836ae,_0x3a66aa;_0x2ae225[_0xfd5d('0x14')]=intUtil[_0xfd5d('0x4e')](_0x2ae225[_0xfd5d('0x14')]);_0x2ae225[_0xfd5d('0x4f')]=intUtil['stripTrailingSlash'](_0x2ae225[_0xfd5d('0x4f')]);if(_0x4616bf&&_0x4616bf[_0xfd5d('0x50')]){logger[_0xfd5d('0x12')]('['+_0x91b883[_0xfd5d('0x6f')][_0xfd5d('0x70')]()+']',_0xfd5d('0x51'));_0x91b883[_0xfd5d('0x53')]=util[_0xfd5d('0x1d')](_0xfd5d('0x52'),_0x2ae225['serverUrl'],_0x91b883[_0xfd5d('0x69')],md5(_0x91b883['uniqueid']));}else{_0x91b883['recordingURL']='';}headers[_0xfd5d('0x54')]=getAuth(_0x2ae225);return getUser(_0x2ae225)[_0xfd5d('0xe')](function(_0x21aa01){_0x2836ae=_0x21aa01;logger['info']('The\x20User\x20ID\x20is:',_0x2836ae['id']);return getEndUser(_0x2ae225,_0x91b883[_0xfd5d('0x71')],_0x91b883['calleridnum']);})[_0xfd5d('0xe')](function(_0x277ce0){_0x3a66aa=_0x277ce0;logger[_0xfd5d('0x12')]('The\x20Enduser\x20ID\x20is:',_0x3a66aa['id']);return getTicket(_0x2ae225,_0x2836ae,_0x3a66aa,_0x4edce6,_0x91b883);})[_0xfd5d('0xe')](function(_0x474daa){if(_0x474daa){logger[_0xfd5d('0x12')](_0xfd5d('0x58'));logger[_0xfd5d('0x12')](_0xfd5d('0x59'),_0x474daa['id']);}})[_0xfd5d('0x6d')](function(_0x257215){logger['error']('['+_0x91b883[_0xfd5d('0x6f')]['toUpperCase']()+']',JSON[_0xfd5d('0x19')](_0x257215));});};