Built motion from commit (unavailable).|2.5.19
[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 _0xeb88=['slice','GET','format','%s/%s','api/v2/ticket_fields','Basic\x20%s','apiKey','toString','base64','subject','description','source','group','remove','custom_text','label','default','choices','statusCode','getFields,\x20%s,\x20%s,\x20%s','Wrong\x20credentials','html-pdf','request-promise','moment','util','path','sox','lodash','squel','crypto','jsforce','desk.js','to-csv','querystring','papaparse','ioredis','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','api','../../config/utils','../../config/license/util','../../mysqldb','../../components/integrations/configuration','client','http','request','then','info','FreshdeskAccount,\x20%s,\x20%s','request\x20sent','debug','FreshdeskAccount,\x20%s,\x20%s,\x20%s','stringify','error','message','result','catch','end','status','json','offset','limit','set','Content-Range','reject','save','destroy','sendStatus','name','send','map','FreshdeskAccount','rawAttributes','fieldName','key','model','query','filters','fields','attributes','length','hasOwnProperty','nolimit','order','sort','where','filter','merge','type','VIRTUAL','options','count','includeAll','include','findAll','rows','params','intersection','create','update','body','find','keys','getConfigurations','addConfiguration','findOne','AccountId','getSubjects','channel','getDescriptions','FreshdeskConfiguration','Subjects','Descriptions','getFields','remoteUri'];(function(_0x58e8b0,_0x431473){var _0x1aaf9b=function(_0x47505b){while(--_0x47505b){_0x58e8b0['push'](_0x58e8b0['shift']());}};_0x1aaf9b(++_0x431473);}(_0xeb88,0x171));var _0x8eb8=function(_0x2de408,_0x44b348){_0x2de408=_0x2de408-0x0;var _0x1914da=_0xeb88[_0x2de408];return _0x1914da;};'use strict';var pdf=require(_0x8eb8('0x0'));var emlformat=require('eml-format');var rimraf=require('rimraf');var zipdir=require('zip-dir');var jsonpatch=require('fast-json-patch');var rp=require(_0x8eb8('0x1'));var moment=require(_0x8eb8('0x2'));var BPromise=require('bluebird');var Mustache=require('mustache');var util=require(_0x8eb8('0x3'));var path=require(_0x8eb8('0x4'));var sox=require(_0x8eb8('0x5'));var csv=require('to-csv');var ejs=require('ejs');var fs=require('fs');var _=require(_0x8eb8('0x6'));var squel=require(_0x8eb8('0x7'));var crypto=require(_0x8eb8('0x8'));var jsforce=require(_0x8eb8('0x9'));var deskjs=require(_0x8eb8('0xa'));var toCsv=require(_0x8eb8('0xb'));var querystring=require(_0x8eb8('0xc'));var Papa=require(_0x8eb8('0xd'));var Redis=require(_0x8eb8('0xe'));var authService=require('../../components/auth/service');var qs=require(_0x8eb8('0xf'));var as=require(_0x8eb8('0x10'));var hardwareService=require(_0x8eb8('0x11'));var logger=require('../../config/logger')(_0x8eb8('0x12'));var utils=require(_0x8eb8('0x13'));var config=require('../../config/environment');var licenseUtil=require(_0x8eb8('0x14'));var db=require(_0x8eb8('0x15'))['db'];var integrations=require(_0x8eb8('0x16'));var jayson=require('jayson/promise');var client=jayson[_0x8eb8('0x17')][_0x8eb8('0x18')]({'port':0x232a});function respondWithRpcPromise(_0x342491,_0x56db29,_0x2ff7ed,_0x1db8dd){return new BPromise(function(_0x2cf20a,_0x466f8b){var _0x2a5f2c=_0x1db8dd||client;return _0x2a5f2c[_0x8eb8('0x19')](_0x342491,_0x2ff7ed)[_0x8eb8('0x1a')](function(_0x38066c){logger[_0x8eb8('0x1b')](_0x8eb8('0x1c'),_0x56db29,_0x8eb8('0x1d'));logger[_0x8eb8('0x1e')](_0x8eb8('0x1f'),_0x56db29,_0x8eb8('0x1d'),JSON[_0x8eb8('0x20')](_0x38066c));if(_0x38066c[_0x8eb8('0x21')]){if(_0x38066c[_0x8eb8('0x21')]['code']===0x1f4){logger[_0x8eb8('0x21')](_0x8eb8('0x1c'),_0x56db29,_0x38066c[_0x8eb8('0x21')][_0x8eb8('0x22')]);return _0x466f8b(_0x38066c[_0x8eb8('0x21')][_0x8eb8('0x22')]);}logger[_0x8eb8('0x21')]('FreshdeskAccount,\x20%s,\x20%s',_0x56db29,_0x38066c['error'][_0x8eb8('0x22')]);return _0x2cf20a(_0x38066c[_0x8eb8('0x21')][_0x8eb8('0x22')]);}else{logger[_0x8eb8('0x1b')](_0x8eb8('0x1c'),_0x56db29,_0x8eb8('0x1d'));_0x2cf20a(_0x38066c[_0x8eb8('0x23')]['message']);}})[_0x8eb8('0x24')](function(_0x53f520){logger[_0x8eb8('0x21')](_0x8eb8('0x1c'),_0x56db29,_0x53f520);_0x466f8b(_0x53f520);});});}function respondWithStatusCode(_0x2e161b,_0x54612f){_0x54612f=_0x54612f||0xcc;return function(_0x476ffa){if(_0x476ffa){return _0x2e161b['sendStatus'](_0x54612f);}return _0x2e161b['status'](_0x54612f)[_0x8eb8('0x25')]();};}function respondWithResult(_0x11d4c5,_0x21ffc2){_0x21ffc2=_0x21ffc2||0xc8;return function(_0x462bde){if(_0x462bde){return _0x11d4c5[_0x8eb8('0x26')](_0x21ffc2)[_0x8eb8('0x27')](_0x462bde);}};}function respondWithFilteredResult(_0x58344e,_0xe8d3a3){return function(_0x4e5d2c){if(_0x4e5d2c){var _0x5310f8=_0x4e5d2c['count'],_0x15dc78=_0xe8d3a3[_0x8eb8('0x28')],_0x42aa21=_0xe8d3a3[_0x8eb8('0x28')]+_0xe8d3a3[_0x8eb8('0x29')],_0x565e24;if(_0x42aa21>=_0x5310f8){_0x42aa21=_0x5310f8;_0x565e24=0xc8;}else{_0x565e24=0xce;}_0x58344e[_0x8eb8('0x26')](_0x565e24);return _0x58344e[_0x8eb8('0x2a')](_0x8eb8('0x2b'),_0x15dc78+'-'+_0x42aa21+'/'+_0x5310f8)['json'](_0x4e5d2c);}return null;};}function patchUpdates(_0x51d204){return function(_0x17ac37){try{jsonpatch['apply'](_0x17ac37,_0x51d204,!![]);}catch(_0x3be85c){return BPromise[_0x8eb8('0x2c')](_0x3be85c);}return _0x17ac37[_0x8eb8('0x2d')]();};}function saveUpdates(_0x323273,_0x18f0ff){return function(_0x4e282a){if(_0x4e282a){return _0x4e282a['update'](_0x323273)[_0x8eb8('0x1a')](function(_0x15f53d){return _0x15f53d;});}return null;};}function removeEntity(_0x311a14,_0x291273){return function(_0x5ce7d4){if(_0x5ce7d4){return _0x5ce7d4[_0x8eb8('0x2e')]()[_0x8eb8('0x1a')](function(){_0x311a14[_0x8eb8('0x26')](0xcc)[_0x8eb8('0x25')]();});}};}function handleEntityNotFound(_0x544fcc,_0xda8b67){return function(_0x4ea0a0){if(!_0x4ea0a0){_0x544fcc[_0x8eb8('0x2f')](0x194);}return _0x4ea0a0;};}function handleError(_0x21d069,_0x7818d5){_0x7818d5=_0x7818d5||0x1f4;return function(_0x487184){logger[_0x8eb8('0x21')](_0x487184['stack']);if(_0x487184[_0x8eb8('0x30')]){delete _0x487184[_0x8eb8('0x30')];}_0x21d069['status'](_0x7818d5)[_0x8eb8('0x31')](_0x487184);};}exports['index']=function(_0x4735c0,_0x5b9bf8){var _0x20abb3={},_0x41818e={},_0x471894={'count':0x0,'rows':[]};var _0x373f81=_[_0x8eb8('0x32')](db[_0x8eb8('0x33')][_0x8eb8('0x34')],function(_0x5af7fa){return{'name':_0x5af7fa[_0x8eb8('0x35')],'type':_0x5af7fa['type'][_0x8eb8('0x36')]};});_0x41818e[_0x8eb8('0x37')]=_['map'](_0x373f81,_0x8eb8('0x30'));_0x41818e[_0x8eb8('0x38')]=_['keys'](_0x4735c0[_0x8eb8('0x38')]);_0x41818e[_0x8eb8('0x39')]=_['intersection'](_0x41818e[_0x8eb8('0x37')],_0x41818e[_0x8eb8('0x38')]);_0x20abb3['attributes']=_['intersection'](_0x41818e['model'],qs[_0x8eb8('0x3a')](_0x4735c0[_0x8eb8('0x38')][_0x8eb8('0x3a')]));_0x20abb3[_0x8eb8('0x3b')]=_0x20abb3[_0x8eb8('0x3b')][_0x8eb8('0x3c')]?_0x20abb3[_0x8eb8('0x3b')]:_0x41818e[_0x8eb8('0x37')];if(!_0x4735c0[_0x8eb8('0x38')][_0x8eb8('0x3d')](_0x8eb8('0x3e'))){_0x20abb3[_0x8eb8('0x29')]=qs[_0x8eb8('0x29')](_0x4735c0[_0x8eb8('0x38')][_0x8eb8('0x29')]);_0x20abb3[_0x8eb8('0x28')]=qs['offset'](_0x4735c0[_0x8eb8('0x38')][_0x8eb8('0x28')]);}_0x20abb3[_0x8eb8('0x3f')]=qs[_0x8eb8('0x40')](_0x4735c0[_0x8eb8('0x38')][_0x8eb8('0x40')]);_0x20abb3[_0x8eb8('0x41')]=qs[_0x8eb8('0x39')](_['pick'](_0x4735c0[_0x8eb8('0x38')],_0x41818e[_0x8eb8('0x39')]),_0x373f81);if(_0x4735c0['query'][_0x8eb8('0x42')]){_0x20abb3['where']=_[_0x8eb8('0x43')](_0x20abb3['where'],{'$or':_['map'](_0x373f81,function(_0x476060){if(_0x476060[_0x8eb8('0x44')]!==_0x8eb8('0x45')){var _0x37a027={};_0x37a027[_0x476060[_0x8eb8('0x30')]]={'$like':'%'+_0x4735c0[_0x8eb8('0x38')][_0x8eb8('0x42')]+'%'};return _0x37a027;}})});}_0x20abb3=_[_0x8eb8('0x43')]({},_0x20abb3,_0x4735c0[_0x8eb8('0x46')]);var _0x3fd58e={'where':_0x20abb3[_0x8eb8('0x41')]};return db[_0x8eb8('0x33')][_0x8eb8('0x47')](_0x3fd58e)['then'](function(_0x3307fa){_0x471894['count']=_0x3307fa;if(_0x4735c0[_0x8eb8('0x38')][_0x8eb8('0x48')]){_0x20abb3[_0x8eb8('0x49')]=[{'all':!![]}];}return db['FreshdeskAccount'][_0x8eb8('0x4a')](_0x20abb3);})['then'](function(_0x4f27d9){_0x471894[_0x8eb8('0x4b')]=_0x4f27d9;return _0x471894;})['then'](respondWithFilteredResult(_0x5b9bf8,_0x20abb3))[_0x8eb8('0x24')](handleError(_0x5b9bf8,null));};exports['show']=function(_0x213b52,_0x47890b){var _0xedd24f={'raw':![],'where':{'id':_0x213b52[_0x8eb8('0x4c')]['id']}},_0x4b88ec={};_0x4b88ec[_0x8eb8('0x37')]=_['keys'](db[_0x8eb8('0x33')]['rawAttributes']);_0x4b88ec[_0x8eb8('0x38')]=_['keys'](_0x213b52['query']);_0x4b88ec[_0x8eb8('0x39')]=_['intersection'](_0x4b88ec[_0x8eb8('0x37')],_0x4b88ec[_0x8eb8('0x38')]);_0xedd24f['attributes']=_[_0x8eb8('0x4d')](_0x4b88ec[_0x8eb8('0x37')],qs[_0x8eb8('0x3a')](_0x213b52[_0x8eb8('0x38')][_0x8eb8('0x3a')]));_0xedd24f[_0x8eb8('0x3b')]=_0xedd24f[_0x8eb8('0x3b')][_0x8eb8('0x3c')]?_0xedd24f[_0x8eb8('0x3b')]:_0x4b88ec[_0x8eb8('0x37')];if(_0x213b52['query'][_0x8eb8('0x48')]){_0xedd24f[_0x8eb8('0x49')]=[{'all':!![]}];}_0xedd24f=_[_0x8eb8('0x43')]({},_0xedd24f,_0x213b52[_0x8eb8('0x46')]);return db[_0x8eb8('0x33')]['find'](_0xedd24f)[_0x8eb8('0x1a')](handleEntityNotFound(_0x47890b,null))['then'](respondWithResult(_0x47890b,null))[_0x8eb8('0x24')](handleError(_0x47890b,null));};exports[_0x8eb8('0x4e')]=function(_0xe7c89e,_0x386015){return db['FreshdeskAccount']['create'](_0xe7c89e['body'],{})[_0x8eb8('0x1a')](respondWithResult(_0x386015,0xc9))[_0x8eb8('0x24')](handleError(_0x386015,null));};exports[_0x8eb8('0x4f')]=function(_0x5e79f3,_0x4d4149){if(_0x5e79f3[_0x8eb8('0x50')]['id']){delete _0x5e79f3['body']['id'];}return db['FreshdeskAccount'][_0x8eb8('0x51')]({'where':{'id':_0x5e79f3[_0x8eb8('0x4c')]['id']}})[_0x8eb8('0x1a')](handleEntityNotFound(_0x4d4149,null))[_0x8eb8('0x1a')](saveUpdates(_0x5e79f3[_0x8eb8('0x50')],null))[_0x8eb8('0x1a')](respondWithResult(_0x4d4149,null))[_0x8eb8('0x24')](handleError(_0x4d4149,null));};exports[_0x8eb8('0x2e')]=function(_0x438d8b,_0x5d4062){return db[_0x8eb8('0x33')][_0x8eb8('0x51')]({'where':{'id':_0x438d8b['params']['id']}})[_0x8eb8('0x1a')](handleEntityNotFound(_0x5d4062,null))[_0x8eb8('0x1a')](removeEntity(_0x5d4062,null))['catch'](handleError(_0x5d4062,null));};exports['getConfigurations']=function(_0x4426e8,_0x4fe5b4,_0x15c827){var _0x5b7380={};var _0x515c3e={};var _0x2f6e6d;var _0x2ba704;return db['FreshdeskAccount']['findOne']({'where':{'id':_0x4426e8[_0x8eb8('0x4c')]['id']}})[_0x8eb8('0x1a')](handleEntityNotFound(_0x4fe5b4,null))[_0x8eb8('0x1a')](function(_0x233c2e){if(_0x233c2e){_0x2f6e6d=_0x233c2e;_0x515c3e['model']=_[_0x8eb8('0x52')](db['FreshdeskConfiguration'][_0x8eb8('0x34')]);_0x515c3e[_0x8eb8('0x38')]=_['keys'](_0x4426e8[_0x8eb8('0x38')]);_0x515c3e['filters']=_[_0x8eb8('0x4d')](_0x515c3e[_0x8eb8('0x37')],_0x515c3e[_0x8eb8('0x38')]);_0x5b7380['attributes']=_[_0x8eb8('0x4d')](_0x515c3e[_0x8eb8('0x37')],qs[_0x8eb8('0x3a')](_0x4426e8[_0x8eb8('0x38')][_0x8eb8('0x3a')]));_0x5b7380[_0x8eb8('0x3b')]=_0x5b7380[_0x8eb8('0x3b')][_0x8eb8('0x3c')]?_0x5b7380[_0x8eb8('0x3b')]:_0x515c3e[_0x8eb8('0x37')];_0x5b7380[_0x8eb8('0x3f')]=qs[_0x8eb8('0x40')](_0x4426e8[_0x8eb8('0x38')][_0x8eb8('0x40')]);_0x5b7380[_0x8eb8('0x41')]=qs['filters'](_['pick'](_0x4426e8[_0x8eb8('0x38')],_0x515c3e[_0x8eb8('0x39')]));if(_0x4426e8[_0x8eb8('0x38')][_0x8eb8('0x42')]){_0x5b7380[_0x8eb8('0x41')]=_[_0x8eb8('0x43')](_0x5b7380[_0x8eb8('0x41')],{'$or':_['map'](_0x5b7380[_0x8eb8('0x3b')],function(_0x2022a9){var _0x41a527={};_0x41a527[_0x2022a9]={'$like':'%'+_0x4426e8[_0x8eb8('0x38')][_0x8eb8('0x42')]+'%'};return _0x41a527;})});}_0x5b7380=_[_0x8eb8('0x43')]({},_0x5b7380,_0x4426e8[_0x8eb8('0x46')]);return _0x2f6e6d[_0x8eb8('0x53')](_0x5b7380);}})[_0x8eb8('0x1a')](function(_0x5dfe29){if(_0x5dfe29){_0x2ba704=_0x5dfe29[_0x8eb8('0x3c')];if(!_0x4426e8[_0x8eb8('0x38')]['hasOwnProperty']('nolimit')){_0x5b7380[_0x8eb8('0x29')]=qs[_0x8eb8('0x29')](_0x4426e8[_0x8eb8('0x38')][_0x8eb8('0x29')]);_0x5b7380[_0x8eb8('0x28')]=qs[_0x8eb8('0x28')](_0x4426e8[_0x8eb8('0x38')][_0x8eb8('0x28')]);}return _0x2f6e6d[_0x8eb8('0x53')](_0x5b7380);}})[_0x8eb8('0x1a')](function(_0x25e1bf){if(_0x25e1bf){return _0x25e1bf?{'count':_0x2ba704,'rows':_0x25e1bf}:null;}})[_0x8eb8('0x1a')](respondWithResult(_0x4fe5b4,null))[_0x8eb8('0x24')](handleError(_0x4fe5b4,null));};exports[_0x8eb8('0x54')]=function(_0x334364,_0x2c6b4a,_0x16c2fc){if(_0x334364[_0x8eb8('0x50')]['id']){delete _0x334364['body']['id'];}return db['FreshdeskAccount'][_0x8eb8('0x55')]({'where':{'id':_0x334364[_0x8eb8('0x4c')]['id']}})[_0x8eb8('0x1a')](handleEntityNotFound(_0x2c6b4a,null))[_0x8eb8('0x1a')](function(_0x55f68e){if(_0x55f68e){_0x334364[_0x8eb8('0x50')][_0x8eb8('0x56')]=_0x55f68e['id'];_0x334364[_0x8eb8('0x50')]['Subjects']=integrations[_0x8eb8('0x57')](_0x334364[_0x8eb8('0x50')][_0x8eb8('0x58')],_0x334364[_0x8eb8('0x50')][_0x8eb8('0x44')]);_0x334364[_0x8eb8('0x50')]['Descriptions']=integrations[_0x8eb8('0x59')](_0x334364['body'][_0x8eb8('0x58')],_0x334364[_0x8eb8('0x50')][_0x8eb8('0x44')]);return db[_0x8eb8('0x5a')][_0x8eb8('0x4e')](_0x334364[_0x8eb8('0x50')],{'include':[{'model':db['FreshdeskField'],'as':_0x8eb8('0x5b')},{'model':db['FreshdeskField'],'as':_0x8eb8('0x5c')}]});}return null;})[_0x8eb8('0x1a')](respondWithResult(_0x2c6b4a,null))[_0x8eb8('0x24')](handleError(_0x2c6b4a,null));};exports[_0x8eb8('0x5d')]=function(_0x5cba03,_0x4e2aa0,_0x2e1011){var _0x5c59a0='';return db[_0x8eb8('0x33')][_0x8eb8('0x55')]({'where':{'id':_0x5cba03['params']['id']},'attributes':['id',_0x8eb8('0x5e'),'apiKey']})[_0x8eb8('0x1a')](handleEntityNotFound(_0x4e2aa0,null))[_0x8eb8('0x1a')](function(_0x4f10c0){if(_0x4f10c0){_0x5c59a0=_0x4f10c0[_0x8eb8('0x5e')];var _0x3b6401=_0x5c59a0[_0x8eb8('0x5f')](-0x1);if(_0x3b6401==='/'){_0x5c59a0=_0x5c59a0['substring'](0x0,_0x5c59a0['lastIndexOf'](_0x3b6401));}return rp({'method':_0x8eb8('0x60'),'uri':util[_0x8eb8('0x61')](_0x8eb8('0x62'),_0x5c59a0,_0x8eb8('0x63')),'headers':{'Authorization':util[_0x8eb8('0x61')](_0x8eb8('0x64'),new Buffer(util['format']('%s:X',_0x4f10c0[_0x8eb8('0x65')]))[_0x8eb8('0x66')](_0x8eb8('0x67')))},'json':!![]});}})[_0x8eb8('0x1a')](function(_0x1504ff){if(_0x1504ff){var _0x109306=['requester',_0x8eb8('0x68'),'agent',_0x8eb8('0x69'),_0x8eb8('0x26'),'priority','group',_0x8eb8('0x6a'),'ticket_type',_0x8eb8('0x6b'),'company'];_[_0x8eb8('0x6c')](_0x1504ff,function(_0x3edc74){return _0x109306['includes'](_0x3edc74[_0x8eb8('0x30')])||!_0x3edc74['default']&&_0x3edc74[_0x8eb8('0x44')]!==_0x8eb8('0x6d')&&_0x3edc74[_0x8eb8('0x44')]!=='custom_dropdown';});return{'count':_0x1504ff['length'],'rows':_[_0x8eb8('0x32')](_0x1504ff,function(_0x560a06){return{'id':_0x560a06['name'],'name':_0x560a06[_0x8eb8('0x6e')],'custom':!_0x560a06[_0x8eb8('0x6f')],'options':_0x560a06[_0x8eb8('0x70')]?_[_0x8eb8('0x32')](_0x560a06['choices'],function(_0x3dd6a7){return{'name':_['capitalize'](_0x3dd6a7),'value':_0x3dd6a7};}):[]};})};}})[_0x8eb8('0x1a')](respondWithResult(_0x4e2aa0,null))['catch'](function(_0x4a8f94){var _0x38492f=_0x5cba03[_0x8eb8('0x38')]['test']?0x1f4:_0x4a8f94[_0x8eb8('0x71')]||0x1f4;logger['error'](_0x8eb8('0x72'),_0x8eb8('0x5d'),_0x38492f,JSON[_0x8eb8('0x20')](_0x4a8f94));delete _0x4a8f94[_0x8eb8('0x30')];if(_0x38492f===0x191){_0x38492f=0x190;}_0x4e2aa0[_0x8eb8('0x26')](_0x38492f)[_0x8eb8('0x31')](_0x5cba03[_0x8eb8('0x38')]['test']?{'message':_0x8eb8('0x73'),'statusCode':_0x4a8f94[_0x8eb8('0x71')]}:_0x4a8f94);});};