6929cb2884111d3a59c4023a95eb6f74360447e1
[motion2.git] / server / services / ami / trigger / integration / freshdesk.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 _0x6c0c=['custom_fields','value','format','%s/api/v2/tickets','remoteUri','Ticket\x20created!','validateEmail','%s/api/v2/agents','then','length','Agent\x20found!','find','contact','email','%s/api/v2/agents/me','Invalid\x20email\x20format,\x20using\x20the\x20admin\x20account!','Admin\x20account\x20selected.','Searching\x20enduser\x20with\x20phone','Enduser\x20found!','Enduser\x20not\x20found,\x20creating\x20a\x20new\x20enduser!','Enduser\x20created.','Basic\x20%s','apiKey','base64','debug','[HTTP]','stringify','GET','map','forEach','includes','string','picklist','nameField','toLowerCase','content','idField','customField','push','toString','type','variable','variableName','Variable','queue','stripTrailingSlash','serverUrl','monitor_format','Recording\x20is\x20enabled!','recordingURL','%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s','uniqueid','The\x20User\x20ID\x20is:','calleridname','The\x20Enduser\x20ID\x20is:','The\x20Ticket\x20ID\x20is:','[QUEUE]','Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...','user:%s','trigger:browser:url','%s/helpdesk/tickets/%d','catch','error','recordingFormat','[OUTBOUND]','Authorization','destcalleridname','destcalleridnum','unmanaged','lastevent','toUpperCase','lodash','request-promise','md5','ioredis','./util','../../../../config/logger','redis','defaults','socket.io-emitter','application/json','emit','info','Creating\x20new\x20ticket...','merge','Caller\x20-\x20%s','calleridnum','getString','Descriptions','<br>','Tags','name','Fields'];(function(_0x2c91e9,_0x2fb965){var _0x13f0ff=function(_0xb39edf){while(--_0xb39edf){_0x2c91e9['push'](_0x2c91e9['shift']());}};_0x13f0ff(++_0x2fb965);}(_0x6c0c,0x15e));var _0xc6c0=function(_0x5b78ff,_0x237a80){_0x5b78ff=_0x5b78ff-0x0;var _0x4d9233=_0x6c0c[_0x5b78ff];return _0x4d9233;};'use strict';var _=require(_0xc6c0('0x0'));var rp=require(_0xc6c0('0x1'));var md5=require(_0xc6c0('0x2'));var util=require('util');var Redis=require(_0xc6c0('0x3'));var intUtil=require(_0xc6c0('0x4'));var config=require('../../../../config/environment');var logger=require(_0xc6c0('0x5'))('freshdesk');config[_0xc6c0('0x6')]=_[_0xc6c0('0x7')](config[_0xc6c0('0x6')],{'host':'localhost','port':0x18eb});var io=require(_0xc6c0('0x8'))(new Redis(config[_0xc6c0('0x6')]));var headers={'Content-Type':_0xc6c0('0x9'),'Accept':_0xc6c0('0x9')};function emit(_0x3f652c,_0x4b0caa,_0x15c98c){io['to'](_0x3f652c)[_0xc6c0('0xa')](_0x4b0caa,_0x15c98c);}function getTicket(_0x3d9e0b,_0x93c1ca,_0x306df2,_0x4fe83d,_0x542ee3){logger[_0xc6c0('0xb')](_0xc6c0('0xc'));var _0x586c54=_[_0xc6c0('0xd')]({'responder_id':_0x93c1ca['id'],'requester_id':_0x306df2['id'],'name':util['format'](_0xc6c0('0xe'),_0x542ee3['calleridname']),'phone':_0x542ee3[_0xc6c0('0xf')],'source':0x3,'priority':0x1,'status':0x2,'subject':intUtil['getString'](_0x4fe83d['Subjects'],_0x542ee3,'\x20'),'description':intUtil[_0xc6c0('0x10')](_0x4fe83d[_0xc6c0('0x11')],_0x542ee3,_0xc6c0('0x12')),'tags':getTags(_0x4fe83d[_0xc6c0('0x13')],_0xc6c0('0x14'))},getCustomFields(_0x4fe83d[_0xc6c0('0x15')],_0x542ee3));_0x586c54[_0xc6c0('0x16')]=_['mapValues'](_['keyBy'](_0x586c54[_0xc6c0('0x16')],'id'),_0xc6c0('0x17'));return request({'uri':util[_0xc6c0('0x18')](_0xc6c0('0x19'),_0x3d9e0b[_0xc6c0('0x1a')]),'method':'POST','body':_0x586c54})['then'](function(_0x125aa3){logger[_0xc6c0('0xb')](_0xc6c0('0x1b'));return _0x125aa3;});}function getUser(_0x511c15,_0x169868){if(intUtil[_0xc6c0('0x1c')](_0x169868)){return request({'uri':util[_0xc6c0('0x18')](_0xc6c0('0x1d'),_0x511c15['remoteUri']),'qs':{'email':_0x169868}})[_0xc6c0('0x1e')](function(_0x11010a){if(_0x11010a[_0xc6c0('0x1f')]){logger['info'](_0xc6c0('0x20'));return _[_0xc6c0('0x21')](_0x11010a,function(_0x319da1){return _0x319da1[_0xc6c0('0x22')][_0xc6c0('0x23')]===_0x169868;});}else{logger[_0xc6c0('0xb')]('Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!');return request({'uri':util['format'](_0xc6c0('0x24'),_0x511c15[_0xc6c0('0x1a')])})[_0xc6c0('0x1e')](function(_0x416558){logger[_0xc6c0('0xb')]('Admin\x20account\x20selected.');return _0x416558;});}});}else{logger[_0xc6c0('0xb')](_0xc6c0('0x25'));return request({'uri':util[_0xc6c0('0x18')](_0xc6c0('0x24'),_0x511c15[_0xc6c0('0x1a')])})['then'](function(_0x5f46c6){logger['info'](_0xc6c0('0x26'));return _0x5f46c6;});}}function getEndUser(_0x462f84,_0x25c17e,_0x3e73c1){logger[_0xc6c0('0xb')](_0xc6c0('0x27'),_0x3e73c1);return request({'uri':util[_0xc6c0('0x18')]('%s/api/v2/contacts',_0x462f84['remoteUri']),'qs':{'phone':_0x3e73c1}})['then'](function(_0x181d98){if(_0x181d98['length']){logger[_0xc6c0('0xb')](_0xc6c0('0x28'));return _[_0xc6c0('0x21')](_0x181d98,{'phone':_0x3e73c1});}else{logger[_0xc6c0('0xb')](_0xc6c0('0x29'));return request({'uri':util[_0xc6c0('0x18')]('%s/api/v2/contacts',_0x462f84[_0xc6c0('0x1a')]),'method':'POST','body':{'name':_0x25c17e,'phone':_0x3e73c1}})['then'](function(_0x2f8364){logger[_0xc6c0('0xb')](_0xc6c0('0x2a'));return _0x2f8364;});}});}function getAuth(_0x3c0fd7){return util[_0xc6c0('0x18')](_0xc6c0('0x2b'),new Buffer(_0x3c0fd7[_0xc6c0('0x2c')]+':X')['toString'](_0xc6c0('0x2d')));}function request(_0x5129c6){logger[_0xc6c0('0x2e')](_0xc6c0('0x2f'),JSON[_0xc6c0('0x30')](_0x5129c6));return rp(_[_0xc6c0('0x7')](_0x5129c6,{'method':_0xc6c0('0x31'),'headers':headers,'json':!![]}));}function getTags(_0x2be9b2,_0x5030f0){var _0x31d60c=[];if(_0x2be9b2&&_0x2be9b2[_0xc6c0('0x1f')]>0x0){_0x31d60c=_[_0xc6c0('0x32')](_0x2be9b2,_0x5030f0);}return _0x31d60c;}function getCustomFields(_0x59eb8b,_0x559659){var _0x57b681=[];var _0x25c282={'custom_fields':[]};if(_0x59eb8b[_0xc6c0('0x1f')]>0x0){_[_0xc6c0('0x33')](_0x59eb8b,function(_0x2deec4){if(_[_0xc6c0('0x34')]([_0xc6c0('0x35'),_0xc6c0('0x36')],_0x2deec4['type'])&&_0x2deec4['nameField']&&!_0x2deec4['customField']){_0x25c282[_0x2deec4[_0xc6c0('0x37')][_0xc6c0('0x38')]()]=_0x2deec4[_0xc6c0('0x39')]['toString']();}else if(_['includes'](['string','picklist'],_0x2deec4['type'])&&_0x2deec4[_0xc6c0('0x3a')]&&_0x2deec4[_0xc6c0('0x3b')]){_0x25c282[_0xc6c0('0x16')][_0xc6c0('0x3c')]({'id':_0x2deec4['idField'],'value':_0x2deec4[_0xc6c0('0x39')][_0xc6c0('0x3d')]()});}else if(_0x2deec4[_0xc6c0('0x3e')]===_0xc6c0('0x3f')&&_0x2deec4[_0xc6c0('0x40')]&&_0x2deec4[_0xc6c0('0x3a')]&&_0x2deec4[_0xc6c0('0x3b')]){_0x559659[_0x2deec4['variableName']]&&_0x25c282[_0xc6c0('0x16')][_0xc6c0('0x3c')]({'id':_0x2deec4['idField'],'value':_0x559659[_0x2deec4[_0xc6c0('0x40')]][_0xc6c0('0x3d')]()});}else if(_0x2deec4[_0xc6c0('0x3e')]==='customVariable'&&_0x2deec4[_0xc6c0('0x41')]&&_0x2deec4[_0xc6c0('0x41')][_0xc6c0('0x14')]&&_0x2deec4['idField']&&_0x2deec4[_0xc6c0('0x3b')]){_0x559659[_0x2deec4[_0xc6c0('0x41')][_0xc6c0('0x14')][_0xc6c0('0x38')]()]&&_0x25c282['custom_fields'][_0xc6c0('0x3c')]({'id':_0x2deec4[_0xc6c0('0x3a')],'value':_0x559659[_0x2deec4[_0xc6c0('0x41')][_0xc6c0('0x14')]['toLowerCase']()][_0xc6c0('0x3d')]()});}});}return _0x25c282;}exports[_0xc6c0('0x42')]=function(_0x3af08e,_0x5114b1,_0xc042b8,_0x4390e9,_0x11a536,_0xf86d85){var _0x55dc88,_0x474f6c;_0xf86d85[_0xc6c0('0x1a')]=intUtil[_0xc6c0('0x43')](_0xf86d85['remoteUri']);_0xf86d85[_0xc6c0('0x44')]=intUtil[_0xc6c0('0x43')](_0xf86d85[_0xc6c0('0x44')]);if(_0x11a536&&_0x11a536[_0xc6c0('0x45')]){logger['info']('[QUEUE]',_0xc6c0('0x46'));_0x5114b1[_0xc6c0('0x47')]=util[_0xc6c0('0x18')](_0xc6c0('0x48'),_0xf86d85['serverUrl'],_0x5114b1[_0xc6c0('0x49')],md5(_0x5114b1[_0xc6c0('0x49')]));}else{_0x5114b1[_0xc6c0('0x47')]='';}headers['Authorization']=getAuth(_0xf86d85);return getUser(_0xf86d85,_0x4390e9[_0xc6c0('0x23')])[_0xc6c0('0x1e')](function(_0xad970){_0x55dc88=_0xad970;logger['info'](_0xc6c0('0x4a'),_0x55dc88['id']);return getEndUser(_0xf86d85,_0x5114b1[_0xc6c0('0x4b')],_0x5114b1['calleridnum']);})[_0xc6c0('0x1e')](function(_0x3cc031){_0x474f6c=_0x3cc031;logger[_0xc6c0('0xb')](_0xc6c0('0x4c'),_0x474f6c['id']);return getTicket(_0xf86d85,_0x55dc88,_0x474f6c,_0xc042b8,_0x5114b1);})[_0xc6c0('0x1e')](function(_0x302027){if(_0x302027&&_0x302027['id']){logger[_0xc6c0('0xb')](_0xc6c0('0x1b'));logger[_0xc6c0('0xb')](_0xc6c0('0x4d'),_0x302027['id']);logger[_0xc6c0('0xb')](_0xc6c0('0x4e'),_0xc6c0('0x4f'));emit(util[_0xc6c0('0x18')](_0xc6c0('0x50'),_0x4390e9[_0xc6c0('0x14')]),_0xc6c0('0x51'),{'uri':util[_0xc6c0('0x18')](_0xc6c0('0x52'),_0xf86d85[_0xc6c0('0x1a')],_0x302027['id'])});}})[_0xc6c0('0x53')](function(_0x398f80){logger[_0xc6c0('0x54')](_0xc6c0('0x4e'),JSON[_0xc6c0('0x30')](_0x398f80));});};exports['outbound']=function(_0x45b4a7,_0x3ffa59,_0x26e974,_0x1914d3,_0x679159,_0x42cec0,_0x375407){var _0x222b97,_0x2a5aaf;_0x42cec0['remoteUri']=intUtil[_0xc6c0('0x43')](_0x42cec0[_0xc6c0('0x1a')]);_0x42cec0[_0xc6c0('0x44')]=intUtil[_0xc6c0('0x43')](_0x42cec0['serverUrl']);if(_0x375407&&_0x375407['recordingFormat']&&_0x375407[_0xc6c0('0x55')]!=='none'){logger[_0xc6c0('0xb')](_0xc6c0('0x56'),'Recording\x20is\x20enabled!');_0x3ffa59['recordingURL']=util[_0xc6c0('0x18')](_0xc6c0('0x48'),_0x42cec0['serverUrl'],_0x3ffa59[_0xc6c0('0x49')],md5(_0x3ffa59[_0xc6c0('0x49')]));}headers[_0xc6c0('0x57')]=getAuth(_0x42cec0);return getUser(_0x42cec0,_0x1914d3[_0xc6c0('0x23')])[_0xc6c0('0x1e')](function(_0x342b03){_0x222b97=_0x342b03;logger['info']('The\x20User\x20ID\x20is:',_0x222b97['id']);return getEndUser(_0x42cec0,_0x3ffa59[_0xc6c0('0x58')],_0x3ffa59[_0xc6c0('0x59')]);})[_0xc6c0('0x1e')](function(_0x665905){_0x2a5aaf=_0x665905;logger[_0xc6c0('0xb')](_0xc6c0('0x4c'),_0x2a5aaf['id']);return getTicket(_0x42cec0,_0x222b97,_0x2a5aaf,_0x26e974,_0x3ffa59);})[_0xc6c0('0x1e')](function(_0x22db9e){if(_0x22db9e&&_0x22db9e['id']){logger['info'](_0xc6c0('0x1b'));logger[_0xc6c0('0xb')](_0xc6c0('0x4d'),_0x22db9e['id']);logger[_0xc6c0('0xb')](_0xc6c0('0x56'),_0xc6c0('0x4f'));emit(util[_0xc6c0('0x18')]('user:%s',_0x1914d3[_0xc6c0('0x14')]),_0xc6c0('0x51'),{'uri':util['format'](_0xc6c0('0x52'),_0x42cec0[_0xc6c0('0x1a')],_0x22db9e['id'])});}})[_0xc6c0('0x53')](function(_0x226b3d){logger[_0xc6c0('0x54')]('[OUTBOUND]',JSON[_0xc6c0('0x30')](_0x226b3d));});};exports[_0xc6c0('0x5a')]=function(_0x155c6c,_0x4c7f57,_0x2d938f,_0x2ab371,_0x3906a8,_0x44c300){var _0x3dbc7d,_0x3a02dc;_0x44c300[_0xc6c0('0x1a')]=intUtil['stripTrailingSlash'](_0x44c300[_0xc6c0('0x1a')]);_0x44c300[_0xc6c0('0x44')]=intUtil['stripTrailingSlash'](_0x44c300['serverUrl']);if(_0x3906a8&&_0x3906a8[_0xc6c0('0x45')]){logger[_0xc6c0('0xb')]('['+_0x4c7f57[_0xc6c0('0x5b')][_0xc6c0('0x5c')]()+']',_0xc6c0('0x46'));_0x4c7f57['recordingURL']=util[_0xc6c0('0x18')](_0xc6c0('0x48'),_0x44c300[_0xc6c0('0x44')],_0x4c7f57[_0xc6c0('0x49')],md5(_0x4c7f57[_0xc6c0('0x49')]));}else{_0x4c7f57['recordingURL']='';}headers[_0xc6c0('0x57')]=getAuth(_0x44c300);return getUser(_0x44c300,'')['then'](function(_0x540357){_0x3dbc7d=_0x540357;logger['info']('The\x20User\x20ID\x20is:',_0x3dbc7d['id']);return getEndUser(_0x44c300,_0x4c7f57[_0xc6c0('0x4b')],_0x4c7f57[_0xc6c0('0xf')]);})['then'](function(_0x3b941a){_0x3a02dc=_0x3b941a;logger[_0xc6c0('0xb')](_0xc6c0('0x4c'),_0x3a02dc['id']);return getTicket(_0x44c300,_0x3dbc7d,_0x3a02dc,_0x2d938f,_0x4c7f57);})[_0xc6c0('0x1e')](function(_0x34085f){if(_0x34085f){logger[_0xc6c0('0xb')](_0xc6c0('0x1b'));logger[_0xc6c0('0xb')](_0xc6c0('0x4d'),_0x34085f['id']);}})[_0xc6c0('0x53')](function(_0x4ca9ef){logger[_0xc6c0('0x54')]('['+_0x4c7f57['lastevent'][_0xc6c0('0x5c')]()+']',JSON[_0xc6c0('0x30')](_0x4ca9ef));});};