d6857c1e5c16f98593f862b8e21b4563b015e567
[motion2.git] / server / api / intFreshdeskAccount / intFreshdeskAccount.controller.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 _0x4b55=['body','getConfigurations','findOne','FreshdeskConfiguration','order','hasOwnProperty','addConfiguration','AccountId','Subjects','getSubjects','Descriptions','channel','FreshdeskField','getFields','remoteUri','apiKey','slice','substring','GET','%s/%s','api/v2/ticket_fields','format','Basic\x20%s','%s:X','toString','requester','subject','agent','description','group','source','includes','default','label','choices','capitalize','test','statusCode','Wrong\x20credentials','html-pdf','eml-format','rimraf','request-promise','moment','bluebird','mustache','util','sox','to-csv','ejs','lodash','squel','crypto','jsforce','desk.js','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/advancedSearch','../../config/license/hardware','api','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','../../components/integrations/configuration','jayson/promise','client','http','request','then','info','debug','FreshdeskAccount,\x20%s,\x20%s,\x20%s','request\x20sent','stringify','error','FreshdeskAccount,\x20%s,\x20%s','message','result','catch','sendStatus','status','json','count','offset','limit','Content-Range','apply','reject','save','update','destroy','stack','name','send','index','map','FreshdeskAccount','fieldName','type','key','model','query','attributes','intersection','fields','length','nolimit','sort','where','pick','filters','filter','merge','VIRTUAL','options','includeAll','include','findAll','rows','show','params','keys','rawAttributes','find','create'];(function(_0x16ee2b,_0x3ecf1b){var _0x260c85=function(_0x474dfd){while(--_0x474dfd){_0x16ee2b['push'](_0x16ee2b['shift']());}};_0x260c85(++_0x3ecf1b);}(_0x4b55,0x127));var _0x54b5=function(_0x368992,_0x6a87e2){_0x368992=_0x368992-0x0;var _0x208c6e=_0x4b55[_0x368992];return _0x208c6e;};'use strict';var pdf=require(_0x54b5('0x0'));var emlformat=require(_0x54b5('0x1'));var rimraf=require(_0x54b5('0x2'));var zipdir=require('zip-dir');var jsonpatch=require('fast-json-patch');var rp=require(_0x54b5('0x3'));var moment=require(_0x54b5('0x4'));var BPromise=require(_0x54b5('0x5'));var Mustache=require(_0x54b5('0x6'));var util=require(_0x54b5('0x7'));var path=require('path');var sox=require(_0x54b5('0x8'));var csv=require(_0x54b5('0x9'));var ejs=require(_0x54b5('0xa'));var fs=require('fs');var _=require(_0x54b5('0xb'));var squel=require(_0x54b5('0xc'));var crypto=require(_0x54b5('0xd'));var jsforce=require(_0x54b5('0xe'));var deskjs=require(_0x54b5('0xf'));var toCsv=require('to-csv');var querystring=require(_0x54b5('0x10'));var Papa=require(_0x54b5('0x11'));var Redis=require(_0x54b5('0x12'));var authService=require(_0x54b5('0x13'));var qs=require('../../components/parsers/qs');var as=require(_0x54b5('0x14'));var hardwareService=require(_0x54b5('0x15'));var logger=require('../../config/logger')(_0x54b5('0x16'));var utils=require(_0x54b5('0x17'));var config=require(_0x54b5('0x18'));var licenseUtil=require(_0x54b5('0x19'));var db=require(_0x54b5('0x1a'))['db'];var integrations=require(_0x54b5('0x1b'));var jayson=require(_0x54b5('0x1c'));var client=jayson[_0x54b5('0x1d')][_0x54b5('0x1e')]({'port':0x232a});function respondWithRpcPromise(_0xf4c43e,_0x2d7265,_0x2e81b7,_0x372127){return new BPromise(function(_0x593ed4,_0x33e97a){var _0x5e8c87=_0x372127||client;return _0x5e8c87[_0x54b5('0x1f')](_0xf4c43e,_0x2e81b7)[_0x54b5('0x20')](function(_0x1c9dff){logger[_0x54b5('0x21')]('FreshdeskAccount,\x20%s,\x20%s',_0x2d7265,'request\x20sent');logger[_0x54b5('0x22')](_0x54b5('0x23'),_0x2d7265,_0x54b5('0x24'),JSON[_0x54b5('0x25')](_0x1c9dff));if(_0x1c9dff['error']){if(_0x1c9dff['error']['code']===0x1f4){logger[_0x54b5('0x26')](_0x54b5('0x27'),_0x2d7265,_0x1c9dff[_0x54b5('0x26')][_0x54b5('0x28')]);return _0x33e97a(_0x1c9dff[_0x54b5('0x26')][_0x54b5('0x28')]);}logger['error'](_0x54b5('0x27'),_0x2d7265,_0x1c9dff[_0x54b5('0x26')][_0x54b5('0x28')]);return _0x593ed4(_0x1c9dff['error'][_0x54b5('0x28')]);}else{logger[_0x54b5('0x21')](_0x54b5('0x27'),_0x2d7265,_0x54b5('0x24'));_0x593ed4(_0x1c9dff[_0x54b5('0x29')][_0x54b5('0x28')]);}})[_0x54b5('0x2a')](function(_0x153242){logger['error'](_0x54b5('0x27'),_0x2d7265,_0x153242);_0x33e97a(_0x153242);});});}function respondWithStatusCode(_0x5c3511,_0x501b7a){_0x501b7a=_0x501b7a||0xcc;return function(_0x11d09f){if(_0x11d09f){return _0x5c3511[_0x54b5('0x2b')](_0x501b7a);}return _0x5c3511[_0x54b5('0x2c')](_0x501b7a)['end']();};}function respondWithResult(_0x1e90fd,_0x382231){_0x382231=_0x382231||0xc8;return function(_0x103115){if(_0x103115){return _0x1e90fd['status'](_0x382231)[_0x54b5('0x2d')](_0x103115);}};}function respondWithFilteredResult(_0x4c29b9,_0x528df0){return function(_0x1057a4){if(_0x1057a4){var _0x47b626=_0x1057a4[_0x54b5('0x2e')],_0x4dff25=_0x528df0[_0x54b5('0x2f')],_0x509ea7=_0x528df0[_0x54b5('0x2f')]+_0x528df0[_0x54b5('0x30')],_0x312f52;if(_0x509ea7>=_0x47b626){_0x509ea7=_0x47b626;_0x312f52=0xc8;}else{_0x312f52=0xce;}_0x4c29b9[_0x54b5('0x2c')](_0x312f52);return _0x4c29b9['set'](_0x54b5('0x31'),_0x4dff25+'-'+_0x509ea7+'/'+_0x47b626)[_0x54b5('0x2d')](_0x1057a4);}return null;};}function patchUpdates(_0x304add){return function(_0x77b150){try{jsonpatch[_0x54b5('0x32')](_0x77b150,_0x304add,!![]);}catch(_0x1d92b1){return BPromise[_0x54b5('0x33')](_0x1d92b1);}return _0x77b150[_0x54b5('0x34')]();};}function saveUpdates(_0x2e4437,_0x104941){return function(_0x5677c2){if(_0x5677c2){return _0x5677c2[_0x54b5('0x35')](_0x2e4437)[_0x54b5('0x20')](function(_0x3be556){return _0x3be556;});}return null;};}function removeEntity(_0x36b118,_0x5d61fa){return function(_0x2b21c7){if(_0x2b21c7){return _0x2b21c7[_0x54b5('0x36')]()[_0x54b5('0x20')](function(){_0x36b118[_0x54b5('0x2c')](0xcc)['end']();});}};}function handleEntityNotFound(_0x59e98e,_0x45f272){return function(_0x55807a){if(!_0x55807a){_0x59e98e[_0x54b5('0x2b')](0x194);}return _0x55807a;};}function handleError(_0x19b7cd,_0x36b547){_0x36b547=_0x36b547||0x1f4;return function(_0x4fc328){logger['error'](_0x4fc328[_0x54b5('0x37')]);if(_0x4fc328['name']){delete _0x4fc328[_0x54b5('0x38')];}_0x19b7cd[_0x54b5('0x2c')](_0x36b547)[_0x54b5('0x39')](_0x4fc328);};}exports[_0x54b5('0x3a')]=function(_0x3689bd,_0x5b90eb){var _0x458d4c={},_0x1b5ce2={},_0x21ee7c={'count':0x0,'rows':[]};var _0x211ab1=_[_0x54b5('0x3b')](db[_0x54b5('0x3c')]['rawAttributes'],function(_0x34b667){return{'name':_0x34b667[_0x54b5('0x3d')],'type':_0x34b667[_0x54b5('0x3e')][_0x54b5('0x3f')]};});_0x1b5ce2[_0x54b5('0x40')]=_[_0x54b5('0x3b')](_0x211ab1,'name');_0x1b5ce2[_0x54b5('0x41')]=_['keys'](_0x3689bd[_0x54b5('0x41')]);_0x1b5ce2['filters']=_['intersection'](_0x1b5ce2[_0x54b5('0x40')],_0x1b5ce2[_0x54b5('0x41')]);_0x458d4c[_0x54b5('0x42')]=_[_0x54b5('0x43')](_0x1b5ce2['model'],qs[_0x54b5('0x44')](_0x3689bd[_0x54b5('0x41')][_0x54b5('0x44')]));_0x458d4c[_0x54b5('0x42')]=_0x458d4c[_0x54b5('0x42')][_0x54b5('0x45')]?_0x458d4c['attributes']:_0x1b5ce2['model'];if(!_0x3689bd[_0x54b5('0x41')]['hasOwnProperty'](_0x54b5('0x46'))){_0x458d4c[_0x54b5('0x30')]=qs['limit'](_0x3689bd[_0x54b5('0x41')][_0x54b5('0x30')]);_0x458d4c[_0x54b5('0x2f')]=qs['offset'](_0x3689bd[_0x54b5('0x41')][_0x54b5('0x2f')]);}_0x458d4c['order']=qs['sort'](_0x3689bd[_0x54b5('0x41')][_0x54b5('0x47')]);_0x458d4c[_0x54b5('0x48')]=qs['filters'](_[_0x54b5('0x49')](_0x3689bd[_0x54b5('0x41')],_0x1b5ce2[_0x54b5('0x4a')]),_0x211ab1);if(_0x3689bd[_0x54b5('0x41')][_0x54b5('0x4b')]){_0x458d4c[_0x54b5('0x48')]=_[_0x54b5('0x4c')](_0x458d4c[_0x54b5('0x48')],{'$or':_[_0x54b5('0x3b')](_0x211ab1,function(_0x3e0923){if(_0x3e0923[_0x54b5('0x3e')]!==_0x54b5('0x4d')){var _0x39565a={};_0x39565a[_0x3e0923[_0x54b5('0x38')]]={'$like':'%'+_0x3689bd[_0x54b5('0x41')]['filter']+'%'};return _0x39565a;}})});}_0x458d4c=_['merge']({},_0x458d4c,_0x3689bd[_0x54b5('0x4e')]);var _0xf1397e={'where':_0x458d4c['where']};return db[_0x54b5('0x3c')][_0x54b5('0x2e')](_0xf1397e)['then'](function(_0x44b82b){_0x21ee7c['count']=_0x44b82b;if(_0x3689bd[_0x54b5('0x41')][_0x54b5('0x4f')]){_0x458d4c[_0x54b5('0x50')]=[{'all':!![]}];}return db[_0x54b5('0x3c')][_0x54b5('0x51')](_0x458d4c);})[_0x54b5('0x20')](function(_0x369087){_0x21ee7c[_0x54b5('0x52')]=_0x369087;return _0x21ee7c;})['then'](respondWithFilteredResult(_0x5b90eb,_0x458d4c))[_0x54b5('0x2a')](handleError(_0x5b90eb,null));};exports[_0x54b5('0x53')]=function(_0x22ef16,_0x8a8bfb){var _0x57602f={'raw':![],'where':{'id':_0x22ef16[_0x54b5('0x54')]['id']}},_0x8b1940={};_0x8b1940[_0x54b5('0x40')]=_[_0x54b5('0x55')](db[_0x54b5('0x3c')][_0x54b5('0x56')]);_0x8b1940[_0x54b5('0x41')]=_[_0x54b5('0x55')](_0x22ef16[_0x54b5('0x41')]);_0x8b1940[_0x54b5('0x4a')]=_[_0x54b5('0x43')](_0x8b1940[_0x54b5('0x40')],_0x8b1940[_0x54b5('0x41')]);_0x57602f['attributes']=_[_0x54b5('0x43')](_0x8b1940[_0x54b5('0x40')],qs[_0x54b5('0x44')](_0x22ef16['query'][_0x54b5('0x44')]));_0x57602f['attributes']=_0x57602f[_0x54b5('0x42')]['length']?_0x57602f[_0x54b5('0x42')]:_0x8b1940[_0x54b5('0x40')];if(_0x22ef16[_0x54b5('0x41')]['includeAll']){_0x57602f['include']=[{'all':!![]}];}_0x57602f=_[_0x54b5('0x4c')]({},_0x57602f,_0x22ef16[_0x54b5('0x4e')]);return db[_0x54b5('0x3c')][_0x54b5('0x57')](_0x57602f)[_0x54b5('0x20')](handleEntityNotFound(_0x8a8bfb,null))[_0x54b5('0x20')](respondWithResult(_0x8a8bfb,null))['catch'](handleError(_0x8a8bfb,null));};exports[_0x54b5('0x58')]=function(_0x3ff31c,_0x1ce97f){return db['FreshdeskAccount']['create'](_0x3ff31c[_0x54b5('0x59')],{})[_0x54b5('0x20')](respondWithResult(_0x1ce97f,0xc9))['catch'](handleError(_0x1ce97f,null));};exports[_0x54b5('0x35')]=function(_0x2a3db9,_0x437d5c){if(_0x2a3db9['body']['id']){delete _0x2a3db9[_0x54b5('0x59')]['id'];}return db[_0x54b5('0x3c')][_0x54b5('0x57')]({'where':{'id':_0x2a3db9[_0x54b5('0x54')]['id']}})[_0x54b5('0x20')](handleEntityNotFound(_0x437d5c,null))[_0x54b5('0x20')](saveUpdates(_0x2a3db9[_0x54b5('0x59')],null))['then'](respondWithResult(_0x437d5c,null))[_0x54b5('0x2a')](handleError(_0x437d5c,null));};exports[_0x54b5('0x36')]=function(_0x1564d7,_0x407b78){return db[_0x54b5('0x3c')][_0x54b5('0x57')]({'where':{'id':_0x1564d7[_0x54b5('0x54')]['id']}})[_0x54b5('0x20')](handleEntityNotFound(_0x407b78,null))[_0x54b5('0x20')](removeEntity(_0x407b78,null))[_0x54b5('0x2a')](handleError(_0x407b78,null));};exports[_0x54b5('0x5a')]=function(_0x6107b4,_0x225531,_0x368ead){var _0x14850b={};var _0x301016={};var _0x216610;var _0x1c5090;return db[_0x54b5('0x3c')][_0x54b5('0x5b')]({'where':{'id':_0x6107b4['params']['id']}})['then'](handleEntityNotFound(_0x225531,null))['then'](function(_0x2aa8c0){if(_0x2aa8c0){_0x216610=_0x2aa8c0;_0x301016['model']=_[_0x54b5('0x55')](db[_0x54b5('0x5c')][_0x54b5('0x56')]);_0x301016[_0x54b5('0x41')]=_[_0x54b5('0x55')](_0x6107b4['query']);_0x301016['filters']=_[_0x54b5('0x43')](_0x301016[_0x54b5('0x40')],_0x301016[_0x54b5('0x41')]);_0x14850b['attributes']=_['intersection'](_0x301016[_0x54b5('0x40')],qs[_0x54b5('0x44')](_0x6107b4[_0x54b5('0x41')][_0x54b5('0x44')]));_0x14850b[_0x54b5('0x42')]=_0x14850b[_0x54b5('0x42')]['length']?_0x14850b[_0x54b5('0x42')]:_0x301016[_0x54b5('0x40')];_0x14850b[_0x54b5('0x5d')]=qs[_0x54b5('0x47')](_0x6107b4[_0x54b5('0x41')][_0x54b5('0x47')]);_0x14850b[_0x54b5('0x48')]=qs[_0x54b5('0x4a')](_[_0x54b5('0x49')](_0x6107b4[_0x54b5('0x41')],_0x301016[_0x54b5('0x4a')]));if(_0x6107b4['query'][_0x54b5('0x4b')]){_0x14850b[_0x54b5('0x48')]=_[_0x54b5('0x4c')](_0x14850b['where'],{'$or':_[_0x54b5('0x3b')](_0x14850b[_0x54b5('0x42')],function(_0x528bc7){var _0xe0d89c={};_0xe0d89c[_0x528bc7]={'$like':'%'+_0x6107b4[_0x54b5('0x41')]['filter']+'%'};return _0xe0d89c;})});}_0x14850b=_['merge']({},_0x14850b,_0x6107b4[_0x54b5('0x4e')]);return _0x216610['getConfigurations'](_0x14850b);}})[_0x54b5('0x20')](function(_0x2206c3){if(_0x2206c3){_0x1c5090=_0x2206c3[_0x54b5('0x45')];if(!_0x6107b4['query'][_0x54b5('0x5e')]('nolimit')){_0x14850b[_0x54b5('0x30')]=qs[_0x54b5('0x30')](_0x6107b4['query'][_0x54b5('0x30')]);_0x14850b[_0x54b5('0x2f')]=qs[_0x54b5('0x2f')](_0x6107b4[_0x54b5('0x41')]['offset']);}return _0x216610[_0x54b5('0x5a')](_0x14850b);}})[_0x54b5('0x20')](function(_0x5b7238){if(_0x5b7238){return _0x5b7238?{'count':_0x1c5090,'rows':_0x5b7238}:null;}})['then'](respondWithResult(_0x225531,null))[_0x54b5('0x2a')](handleError(_0x225531,null));};exports[_0x54b5('0x5f')]=function(_0x39690c,_0x15424b,_0x54dfd4){if(_0x39690c[_0x54b5('0x59')]['id']){delete _0x39690c[_0x54b5('0x59')]['id'];}return db[_0x54b5('0x3c')]['findOne']({'where':{'id':_0x39690c[_0x54b5('0x54')]['id']}})['then'](handleEntityNotFound(_0x15424b,null))[_0x54b5('0x20')](function(_0x194caf){if(_0x194caf){_0x39690c[_0x54b5('0x59')][_0x54b5('0x60')]=_0x194caf['id'];_0x39690c[_0x54b5('0x59')][_0x54b5('0x61')]=integrations[_0x54b5('0x62')](_0x39690c[_0x54b5('0x59')]['channel'],_0x39690c[_0x54b5('0x59')]['type']);_0x39690c[_0x54b5('0x59')][_0x54b5('0x63')]=integrations['getDescriptions'](_0x39690c['body'][_0x54b5('0x64')],_0x39690c[_0x54b5('0x59')][_0x54b5('0x3e')]);return db[_0x54b5('0x5c')][_0x54b5('0x58')](_0x39690c[_0x54b5('0x59')],{'include':[{'model':db['FreshdeskField'],'as':_0x54b5('0x61')},{'model':db[_0x54b5('0x65')],'as':'Descriptions'}]});}return null;})[_0x54b5('0x20')](respondWithResult(_0x15424b,null))[_0x54b5('0x2a')](handleError(_0x15424b,null));};exports[_0x54b5('0x66')]=function(_0x3bef27,_0x229d89,_0x1f873a){var _0x1e9b04='';return db[_0x54b5('0x3c')][_0x54b5('0x5b')]({'where':{'id':_0x3bef27[_0x54b5('0x54')]['id']},'attributes':['id',_0x54b5('0x67'),_0x54b5('0x68')]})[_0x54b5('0x20')](handleEntityNotFound(_0x229d89,null))['then'](function(_0x4bc05d){if(_0x4bc05d){_0x1e9b04=_0x4bc05d[_0x54b5('0x67')];var _0x4e62d7=_0x1e9b04[_0x54b5('0x69')](-0x1);if(_0x4e62d7==='/'){_0x1e9b04=_0x1e9b04[_0x54b5('0x6a')](0x0,_0x1e9b04['lastIndexOf'](_0x4e62d7));}return rp({'method':_0x54b5('0x6b'),'uri':util['format'](_0x54b5('0x6c'),_0x1e9b04,_0x54b5('0x6d')),'headers':{'Authorization':util[_0x54b5('0x6e')](_0x54b5('0x6f'),new Buffer(util[_0x54b5('0x6e')](_0x54b5('0x70'),_0x4bc05d[_0x54b5('0x68')]))[_0x54b5('0x71')]('base64'))},'json':!![]});}})[_0x54b5('0x20')](function(_0x3256e9){if(_0x3256e9){var _0x3df282=[_0x54b5('0x72'),_0x54b5('0x73'),_0x54b5('0x74'),_0x54b5('0x75'),_0x54b5('0x2c'),'priority',_0x54b5('0x76'),_0x54b5('0x77'),'ticket_type',_0x54b5('0x76'),'company'];_['remove'](_0x3256e9,function(_0x26a637){return _0x3df282[_0x54b5('0x78')](_0x26a637[_0x54b5('0x38')])||!_0x26a637[_0x54b5('0x79')]&&_0x26a637['type']!=='custom_text'&&_0x26a637[_0x54b5('0x3e')]!=='custom_dropdown';});return{'count':_0x3256e9['length'],'rows':_['map'](_0x3256e9,function(_0x4aadd7){return{'id':_0x4aadd7[_0x54b5('0x38')],'name':_0x4aadd7[_0x54b5('0x7a')],'custom':!_0x4aadd7[_0x54b5('0x79')],'options':_0x4aadd7['choices']?_[_0x54b5('0x3b')](_0x4aadd7[_0x54b5('0x7b')],function(_0xcfc87f){return{'name':_[_0x54b5('0x7c')](_0xcfc87f),'value':_0xcfc87f};}):[]};})};}})[_0x54b5('0x20')](respondWithResult(_0x229d89,null))[_0x54b5('0x2a')](function(_0x403842){var _0x49f1f6=_0x3bef27['query'][_0x54b5('0x7d')]?0x1f4:_0x403842[_0x54b5('0x7e')]||0x1f4;logger[_0x54b5('0x26')]('getFields,\x20%s,\x20%s,\x20%s',_0x54b5('0x66'),_0x49f1f6,JSON[_0x54b5('0x25')](_0x403842));delete _0x403842[_0x54b5('0x38')];if(_0x49f1f6===0x191){_0x49f1f6=0x190;}_0x229d89['status'](_0x49f1f6)[_0x54b5('0x39')](_0x3bef27['query'][_0x54b5('0x7d')]?{'message':_0x54b5('0x7f'),'statusCode':_0x403842[_0x54b5('0x7e')]}:_0x403842);});};