Built motion from commit (unavailable).|2.5.25
[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 _0xddb3=['defaults','localhost','socket.io-emitter','application/json','validateEmail','Getting\x20agent\x20with\x20email','%s/users','then','total_entries','find','_embedded','info','Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!','format','%s/users/me','remoteUri','Invalid\x20email\x20format,\x20using\x20the\x20admin\x20account!','debug','[HTTP]','stringify','GET','basic','username','password','authType','token','%s/token:%s','toString','base64','%s/customers/search','Enduser\x20found!','%s/customers','POST','length','map','forEach','includes','string','picklist','customField','content','type','idField','custom_fields','variable','variableName','Variable','name','toLowerCase','priority','merge','Subjects','phone','new','xCALLY','CTI','concat','Tags','Descriptions','/api/v2/users/%d','getString','Fields','queue','serverUrl','stripTrailingSlash','monitor_format','[QUEUE]','Recording\x20is\x20enabled!','recordingURL','%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s','uniqueid','Authorization','email','The\x20User\x20ID\x20is:','The\x20Enduser\x20ID\x20is:','Ticket\x20created!','The\x20Ticket\x20ID\x20is:','integrationTab','user:%s','trigger:desk:display','newTab','Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...','Desk\x20account\x20type\x20not\x20recognized','catch','outbound','recordingFormat','none','[OUTBOUND]','destcalleridname','destcalleridnum','Displaying\x20ticket\x20to\x20agent...','%s/agent/tickets/%s','unmanaged','toUpperCase','calleridname','calleridnum','error','lastevent','lodash','request-promise','md5','util','ioredis','./util','../../../../config/environment','../../../../config/logger','redis'];(function(_0x2cb8d5,_0x2c8864){var _0x355821=function(_0x4f6cf5){while(--_0x4f6cf5){_0x2cb8d5['push'](_0x2cb8d5['shift']());}};_0x355821(++_0x2c8864);}(_0xddb3,0x1a3));var _0x3ddb=function(_0x3c15b2,_0x3e8ac8){_0x3c15b2=_0x3c15b2-0x0;var _0x4f0492=_0xddb3[_0x3c15b2];return _0x4f0492;};'use strict';var _=require(_0x3ddb('0x0'));var rp=require(_0x3ddb('0x1'));var md5=require(_0x3ddb('0x2'));var util=require(_0x3ddb('0x3'));var Redis=require(_0x3ddb('0x4'));var intUtil=require(_0x3ddb('0x5'));var config=require(_0x3ddb('0x6'));var logger=require(_0x3ddb('0x7'))('desk');config[_0x3ddb('0x8')]=_[_0x3ddb('0x9')](config[_0x3ddb('0x8')],{'host':_0x3ddb('0xa'),'port':0x18eb});var io=require(_0x3ddb('0xb'))(new Redis(config['redis']));function emit(_0x3e1225,_0x2afa6a,_0x44db88){io['to'](_0x3e1225)['emit'](_0x2afa6a,_0x44db88);}var headers={'Content-Type':_0x3ddb('0xc'),'Accept':_0x3ddb('0xc')};function getUser(_0x2d2bb8,_0x556d7d){if(intUtil[_0x3ddb('0xd')](_0x556d7d)===!![]){logger['info'](_0x3ddb('0xe'),_0x556d7d);return request({'uri':util['format'](_0x3ddb('0xf'),_0x2d2bb8['remoteUri'])})[_0x3ddb('0x10')](function(_0x1743bf){if(_0x1743bf[_0x3ddb('0x11')]){return _[_0x3ddb('0x12')](_0x1743bf[_0x3ddb('0x13')]['entries'],{'email':_0x556d7d});}})[_0x3ddb('0x10')](function(_0x18f0e1){if(_0x18f0e1){logger['info']('Agent\x20found!');return _0x18f0e1;}else{logger[_0x3ddb('0x14')](_0x3ddb('0x15'));return request({'uri':util[_0x3ddb('0x16')](_0x3ddb('0x17'),_0x2d2bb8[_0x3ddb('0x18')])});}});}else{logger[_0x3ddb('0x14')](_0x3ddb('0x19'));return request({'uri':util[_0x3ddb('0x16')](_0x3ddb('0x17'),_0x2d2bb8[_0x3ddb('0x18')])});}}function request(_0x2fe423){logger[_0x3ddb('0x1a')](_0x3ddb('0x1b'),JSON[_0x3ddb('0x1c')](_0x2fe423));return rp(_[_0x3ddb('0x9')](_0x2fe423,{'method':_0x3ddb('0x1d'),'headers':headers,'json':!![]}));}function getAuth(_0x36e618){var _0x1893c1;if(_0x36e618['authType']===_0x3ddb('0x1e')){_0x1893c1=util[_0x3ddb('0x16')]('%s:%s',_0x36e618[_0x3ddb('0x1f')],_0x36e618[_0x3ddb('0x20')]);}else if(_0x36e618[_0x3ddb('0x21')]===_0x3ddb('0x22')){_0x1893c1=util[_0x3ddb('0x16')](_0x3ddb('0x23'),_0x36e618[_0x3ddb('0x1f')],_0x36e618[_0x3ddb('0x22')]);}return util['format']('Basic\x20%s',new Buffer(_0x1893c1)[_0x3ddb('0x24')](_0x3ddb('0x25')));}function getEndUser(_0x2bf924,_0x5e5055,_0x364af5){logger[_0x3ddb('0x14')]('Searching\x20enduser\x20with\x20phone',_0x364af5);return request({'uri':util[_0x3ddb('0x16')](_0x3ddb('0x26'),_0x2bf924['remoteUri']),'qs':{'phone':_0x364af5}})['then'](function(_0x4fef25){if(_0x4fef25[_0x3ddb('0x11')]){logger[_0x3ddb('0x14')](_0x3ddb('0x27'));return _0x4fef25[_0x3ddb('0x13')]['entries'][0x0];}else{logger[_0x3ddb('0x14')]('Enduser\x20not\x20found,\x20creating\x20a\x20new\x20enduser!');return request({'uri':util[_0x3ddb('0x16')](_0x3ddb('0x28'),_0x2bf924['remoteUri']),'method':_0x3ddb('0x29'),'body':{'first_name':_0x5e5055,'title':_0x5e5055,'phone_numbers':[{'type':'work','value':_0x364af5}]}});}});}function getTags(_0x4e1a06,_0x2f1be9){var _0x30c0b4=[];if(_0x4e1a06&&_0x4e1a06[_0x3ddb('0x2a')]>0x0){_0x30c0b4=_[_0x3ddb('0x2b')](_0x4e1a06,_0x2f1be9);}return _0x30c0b4;}function getCustomFields(_0x483539,_0x9b98ab){var _0x40f132=[];var _0x416bbb={'custom_fields':{}};if(_0x483539[_0x3ddb('0x2a')]>0x0){_[_0x3ddb('0x2c')](_0x483539,function(_0x49cb30){if(_[_0x3ddb('0x2d')]([_0x3ddb('0x2e'),_0x3ddb('0x2f')],_0x49cb30['type'])&&_0x49cb30['nameField']&&!_0x49cb30[_0x3ddb('0x30')]){_0x416bbb[_0x49cb30['nameField']['toLowerCase']()]=_0x49cb30[_0x3ddb('0x31')][_0x3ddb('0x24')]();}else if(_[_0x3ddb('0x2d')]([_0x3ddb('0x2e'),_0x3ddb('0x2f')],_0x49cb30[_0x3ddb('0x32')])&&_0x49cb30[_0x3ddb('0x33')]&&_0x49cb30[_0x3ddb('0x30')]){_0x416bbb[_0x3ddb('0x34')][_0x49cb30[_0x3ddb('0x33')]]=_0x49cb30[_0x3ddb('0x31')][_0x3ddb('0x24')]();}else if(_0x49cb30[_0x3ddb('0x32')]===_0x3ddb('0x35')&&_0x49cb30['variableName']&&_0x49cb30['idField']&&_0x49cb30['customField']){_0x9b98ab[_0x49cb30[_0x3ddb('0x36')]]&&(_0x416bbb[_0x3ddb('0x34')][_0x49cb30['idField']]=_0x9b98ab[_0x49cb30[_0x3ddb('0x36')]][_0x3ddb('0x24')]());}else if(_0x49cb30[_0x3ddb('0x32')]==='customVariable'&&_0x49cb30[_0x3ddb('0x37')]&&_0x49cb30[_0x3ddb('0x37')]['name']&&_0x49cb30[_0x3ddb('0x33')]&&_0x49cb30[_0x3ddb('0x30')]){_0x9b98ab[_0x49cb30[_0x3ddb('0x37')][_0x3ddb('0x38')][_0x3ddb('0x39')]()]&&(_0x416bbb[_0x3ddb('0x34')][_0x49cb30[_0x3ddb('0x33')]]=_0x9b98ab[_0x49cb30[_0x3ddb('0x37')][_0x3ddb('0x38')]['toLowerCase']()][_0x3ddb('0x24')]());}});}if(_0x416bbb[_0x3ddb('0x3a')]){_0x416bbb['priority']=Number(_0x416bbb[_0x3ddb('0x3a')]);}return _0x416bbb;}function getTicket(_0x2a53d9,_0x49db02,_0x33f3db,_0x20fd9b,_0x532f2b){logger[_0x3ddb('0x14')]('Creating\x20new\x20ticket...');var _0x164335=_[_0x3ddb('0x3b')]({'subject':intUtil['getString'](_0x20fd9b[_0x3ddb('0x3c')],_0x532f2b,'\x20'),'type':_0x3ddb('0x3d'),'status':_0x3ddb('0x3e'),'labels':[_0x3ddb('0x3f'),'Motion',_0x3ddb('0x40')][_0x3ddb('0x41')](getTags(_0x20fd9b[_0x3ddb('0x42')],_0x3ddb('0x38'))),'description':intUtil['getString'](_0x20fd9b[_0x3ddb('0x43')],_0x532f2b,'\x0a'),'_links':{'assigned_user':{'href':util[_0x3ddb('0x16')](_0x3ddb('0x44'),_0x49db02['id']),'class':'user'}},'message':{'direction':'in','body':intUtil[_0x3ddb('0x45')](_0x20fd9b['Descriptions'],_0x532f2b,'\x0a')}},getCustomFields(_0x20fd9b[_0x3ddb('0x46')],_0x532f2b));return request({'uri':util[_0x3ddb('0x16')]('%s/customers/%d/cases',_0x2a53d9[_0x3ddb('0x18')],_0x33f3db['id']),'method':_0x3ddb('0x29'),'body':_0x164335});}exports[_0x3ddb('0x47')]=function(_0x3b5150,_0x4cafac,_0x4898fa,_0x12f4c9,_0x2b2666,_0x15e439){var _0x2ac5f4,_0x1e625d;_0x15e439[_0x3ddb('0x18')]=intUtil['stripTrailingSlash'](_0x15e439['remoteUri']);_0x15e439[_0x3ddb('0x48')]=intUtil[_0x3ddb('0x49')](_0x15e439['serverUrl']);if(_0x2b2666&&_0x2b2666[_0x3ddb('0x4a')]){logger['info'](_0x3ddb('0x4b'),_0x3ddb('0x4c'));_0x4cafac[_0x3ddb('0x4d')]=util[_0x3ddb('0x16')](_0x3ddb('0x4e'),_0x15e439[_0x3ddb('0x48')],_0x4cafac[_0x3ddb('0x4f')],md5(_0x4cafac[_0x3ddb('0x4f')]));}else{_0x4cafac['recordingURL']='';}headers[_0x3ddb('0x50')]=getAuth(_0x15e439);return getUser(_0x15e439,_0x12f4c9[_0x3ddb('0x51')])['then'](function(_0x30cbe5){_0x2ac5f4=_0x30cbe5;logger[_0x3ddb('0x14')](_0x3ddb('0x52'),_0x2ac5f4['id']);return getEndUser(_0x15e439,_0x4cafac['calleridname'],_0x4cafac['calleridnum']);})['then'](function(_0x20b496){_0x1e625d=_0x20b496;logger[_0x3ddb('0x14')](_0x3ddb('0x53'),_0x1e625d['id']);return getTicket(_0x15e439,_0x2ac5f4,_0x1e625d,_0x4898fa,_0x4cafac);})['then'](function(_0x5cb7d0){if(_0x5cb7d0){logger[_0x3ddb('0x14')](_0x3ddb('0x54'));logger[_0x3ddb('0x14')](_0x3ddb('0x55'),_0x5cb7d0['id']);switch(_0x15e439[_0x3ddb('0x32')]){case _0x3ddb('0x56'):logger[_0x3ddb('0x14')](_0x3ddb('0x4b'),'Displaying\x20ticket\x20to\x20agent...');emit(util[_0x3ddb('0x16')](_0x3ddb('0x57'),_0x12f4c9['name']),_0x3ddb('0x58'),{'recordId':_0x5cb7d0['id']});break;case _0x3ddb('0x59'):logger[_0x3ddb('0x14')](_0x3ddb('0x4b'),_0x3ddb('0x5a'));emit(util[_0x3ddb('0x16')](_0x3ddb('0x57'),_0x12f4c9['name']),'trigger:browser:url',{'uri':util[_0x3ddb('0x16')]('https://xcally.desk.com/web/agent/case/%d',_0x5cb7d0['id'])});break;default:logger['error'](_0x3ddb('0x4b'),_0x3ddb('0x5b'));}}})[_0x3ddb('0x5c')](function(_0x5ef77f){console['error'](_0x5ef77f);logger['error'](_0x3ddb('0x4b'),JSON['stringify'](_0x5ef77f));});};exports[_0x3ddb('0x5d')]=function(_0x1f0396,_0x4de1f2,_0x1b910c,_0x55a051,_0x2a7aef,_0x13ab77,_0xd6d082){var _0xb906db,_0x343d03;_0x13ab77[_0x3ddb('0x18')]=intUtil['stripTrailingSlash'](_0x13ab77[_0x3ddb('0x18')]);_0x13ab77[_0x3ddb('0x48')]=intUtil[_0x3ddb('0x49')](_0x13ab77[_0x3ddb('0x48')]);if(_0xd6d082&&_0xd6d082[_0x3ddb('0x5e')]&&_0xd6d082[_0x3ddb('0x5e')]!==_0x3ddb('0x5f')){logger[_0x3ddb('0x14')](_0x3ddb('0x60'),_0x3ddb('0x4c'));_0x4de1f2[_0x3ddb('0x4d')]=util[_0x3ddb('0x16')]('%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s',_0x13ab77[_0x3ddb('0x48')],_0x4de1f2[_0x3ddb('0x4f')],md5(_0x4de1f2['uniqueid']));}headers['Authorization']=getAuth(_0x13ab77);return getUser(_0x13ab77,_0x55a051[_0x3ddb('0x51')])['then'](function(_0x5e3eeb){_0xb906db=_0x5e3eeb;logger[_0x3ddb('0x14')](_0x3ddb('0x52'),_0xb906db['id']);return getEndUser(_0x13ab77,_0x4de1f2[_0x3ddb('0x61')],_0x4de1f2[_0x3ddb('0x62')]);})[_0x3ddb('0x10')](function(_0x18a374){_0x343d03=_0x18a374;logger[_0x3ddb('0x14')](_0x3ddb('0x53'),_0x343d03['id']);return getTicket(_0x13ab77,_0xb906db,_0x343d03,_0x1b910c,_0x4de1f2);})['then'](function(_0x3442aa){if(_0x3442aa){logger[_0x3ddb('0x14')]('Ticket\x20created!');logger[_0x3ddb('0x14')](_0x3ddb('0x55'),_0x3442aa['id']);switch(_0x13ab77['type']){case _0x3ddb('0x56'):logger['info'](_0x3ddb('0x4b'),_0x3ddb('0x63'));emit(util[_0x3ddb('0x16')](_0x3ddb('0x57'),_0x55a051[_0x3ddb('0x38')]),_0x3ddb('0x58'),{'recordId':_0x3442aa['id']});break;case _0x3ddb('0x59'):logger[_0x3ddb('0x14')](_0x3ddb('0x4b'),_0x3ddb('0x5a'));emit(util[_0x3ddb('0x16')]('user:%s',_0x55a051[_0x3ddb('0x38')]),'trigger:browser:url',{'uri':util[_0x3ddb('0x16')](_0x3ddb('0x64'),_0x13ab77['remoteUri'],_0x3442aa['id'])});break;default:logger['error']('[OUTBOUND]','Desk\x20account\x20type\x20not\x20recognized');}}})['catch'](function(_0x433327){logger['error']('[OUTBOUND]',JSON[_0x3ddb('0x1c')](_0x433327));});};exports[_0x3ddb('0x65')]=function(_0x3c7371,_0x1a3c43,_0x10525f,_0x1444cf,_0x1db446,_0x3e46fa){var _0x3d6424,_0xa85957;_0x3e46fa[_0x3ddb('0x18')]=intUtil[_0x3ddb('0x49')](_0x3e46fa[_0x3ddb('0x18')]);_0x3e46fa[_0x3ddb('0x48')]=intUtil[_0x3ddb('0x49')](_0x3e46fa['serverUrl']);if(_0x1db446&&_0x1db446[_0x3ddb('0x4a')]){logger[_0x3ddb('0x14')]('['+_0x1a3c43['lastevent'][_0x3ddb('0x66')]()+']',_0x3ddb('0x4c'));_0x1a3c43[_0x3ddb('0x4d')]=util[_0x3ddb('0x16')](_0x3ddb('0x4e'),_0x3e46fa[_0x3ddb('0x48')],_0x1a3c43[_0x3ddb('0x4f')],md5(_0x1a3c43[_0x3ddb('0x4f')]));}else{_0x1a3c43['recordingURL']='';}headers[_0x3ddb('0x50')]=getAuth(_0x3e46fa);return getUser(_0x3e46fa)[_0x3ddb('0x10')](function(_0x5381c1){_0x3d6424=_0x5381c1;logger[_0x3ddb('0x14')](_0x3ddb('0x52'),_0x3d6424['id']);return getEndUser(_0x3e46fa,_0x1a3c43[_0x3ddb('0x67')],_0x1a3c43[_0x3ddb('0x68')]);})[_0x3ddb('0x10')](function(_0x17c705){_0xa85957=_0x17c705;logger['info'](_0x3ddb('0x53'),_0xa85957['id']);return getTicket(_0x3e46fa,_0x3d6424,_0xa85957,_0x10525f,_0x1a3c43);})['then'](function(_0xc4c578){if(_0xc4c578){logger[_0x3ddb('0x14')](_0x3ddb('0x54'));logger['info'](_0x3ddb('0x55'),_0xc4c578['id']);}})[_0x3ddb('0x5c')](function(_0x213028){logger[_0x3ddb('0x69')]('['+_0x1a3c43[_0x3ddb('0x6a')][_0x3ddb('0x66')]()+']',JSON['stringify'](_0x213028));});};