Built motion from commit b5413ace.|2.6.9
[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 _0x4ffa=['index','FreshdeskAccount','rawAttributes','fieldName','type','key','model','map','query','keys','filters','intersection','fields','attributes','length','nolimit','order','sort','where','merge','VIRTUAL','filter','options','includeAll','include','findAll','rows','catch','show','find','create','body','params','getConfigurations','findOne','FreshdeskConfiguration','hasOwnProperty','AccountId','Subjects','getSubjects','channel','Descriptions','FreshdeskField','getFields','remoteUri','slice','substring','GET','format','%s/%s','api/v2/ticket_fields','apiKey','toString','base64','subject','description','priority','group','source','ticket_type','company','default','custom_text','label','choices','capitalize','test','error','getFields,\x20%s,\x20%s,\x20%s','stringify','Wrong\x20credentials','statusCode','zip-dir','request-promise','moment','bluebird','mustache','util','path','to-csv','ejs','fs-extra','squel','jsforce','desk.js','papaparse','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/environment','../../config/license/util','../../components/integrations/configuration','sendStatus','end','status','json','offset','undefined','limit','count','Content-Range','apply','reject','update','destroy','then','stack','name','send'];(function(_0x4e3be8,_0x194867){var _0x496611=function(_0x3cb703){while(--_0x3cb703){_0x4e3be8['push'](_0x4e3be8['shift']());}};_0x496611(++_0x194867);}(_0x4ffa,0x124));var _0xa4ff=function(_0x40a045,_0x516f0e){_0x40a045=_0x40a045-0x0;var _0x1a687c=_0x4ffa[_0x40a045];return _0x1a687c;};'use strict';var emlformat=require('eml-format');var rimraf=require('rimraf');var zipdir=require(_0xa4ff('0x0'));var jsonpatch=require('fast-json-patch');var rp=require(_0xa4ff('0x1'));var moment=require(_0xa4ff('0x2'));var BPromise=require(_0xa4ff('0x3'));var Mustache=require(_0xa4ff('0x4'));var util=require(_0xa4ff('0x5'));var path=require(_0xa4ff('0x6'));var sox=require('sox');var csv=require(_0xa4ff('0x7'));var ejs=require(_0xa4ff('0x8'));var fs=require('fs');var fs_extra=require(_0xa4ff('0x9'));var _=require('lodash');var squel=require(_0xa4ff('0xa'));var crypto=require('crypto');var jsforce=require(_0xa4ff('0xb'));var deskjs=require(_0xa4ff('0xc'));var toCsv=require(_0xa4ff('0x7'));var querystring=require('querystring');var Papa=require(_0xa4ff('0xd'));var Redis=require('ioredis');var authService=require('../../components/auth/service');var qs=require('../../components/parsers/qs');var as=require(_0xa4ff('0xe'));var hardwareService=require(_0xa4ff('0xf'));var logger=require(_0xa4ff('0x10'))(_0xa4ff('0x11'));var utils=require('../../config/utils');var config=require(_0xa4ff('0x12'));var licenseUtil=require(_0xa4ff('0x13'));var db=require('../../mysqldb')['db'];var integrations=require(_0xa4ff('0x14'));function respondWithStatusCode(_0x345044,_0x5b11b0){_0x5b11b0=_0x5b11b0||0xcc;return function(_0x11f78a){if(_0x11f78a){return _0x345044[_0xa4ff('0x15')](_0x5b11b0);}return _0x345044['status'](_0x5b11b0)[_0xa4ff('0x16')]();};}function respondWithResult(_0x4d2d2c,_0x34f681){_0x34f681=_0x34f681||0xc8;return function(_0x5a3869){if(_0x5a3869){return _0x4d2d2c[_0xa4ff('0x17')](_0x34f681)[_0xa4ff('0x18')](_0x5a3869);}};}function respondWithFilteredResult(_0x56e4e9,_0x308f00){return function(_0x199d1e){if(_0x199d1e){var _0x27f8d8=typeof _0x308f00[_0xa4ff('0x19')]===_0xa4ff('0x1a')&&typeof _0x308f00[_0xa4ff('0x1b')]==='undefined';var _0x93e247=_0x199d1e[_0xa4ff('0x1c')];var _0x2fd522=_0x27f8d8?0x0:_0x308f00['offset'];var _0x377665=_0x27f8d8?_0x199d1e[_0xa4ff('0x1c')]:_0x308f00['offset']+_0x308f00['limit'];var _0x28a1bc;if(_0x377665>=_0x93e247){_0x377665=_0x93e247;_0x28a1bc=0xc8;}else{_0x28a1bc=0xce;}_0x56e4e9['status'](_0x28a1bc);return _0x56e4e9['set'](_0xa4ff('0x1d'),_0x2fd522+'-'+_0x377665+'/'+_0x93e247)[_0xa4ff('0x18')](_0x199d1e);}return null;};}function patchUpdates(_0x2857e7){return function(_0x15450d){try{jsonpatch[_0xa4ff('0x1e')](_0x15450d,_0x2857e7,!![]);}catch(_0x569a3f){return BPromise[_0xa4ff('0x1f')](_0x569a3f);}return _0x15450d['save']();};}function saveUpdates(_0x570458,_0x2598b4){return function(_0x53fce2){if(_0x53fce2){return _0x53fce2[_0xa4ff('0x20')](_0x570458)['then'](function(_0x3012fa){return _0x3012fa;});}return null;};}function removeEntity(_0x4a678e,_0x520bdc){return function(_0x132633){if(_0x132633){return _0x132633[_0xa4ff('0x21')]()[_0xa4ff('0x22')](function(){_0x4a678e[_0xa4ff('0x17')](0xcc)['end']();});}};}function handleEntityNotFound(_0x4a649c,_0x1a62ba){return function(_0x38f633){if(!_0x38f633){_0x4a649c[_0xa4ff('0x15')](0x194);}return _0x38f633;};}function handleError(_0x3532e3,_0x20e611){_0x20e611=_0x20e611||0x1f4;return function(_0x289525){logger['error'](_0x289525[_0xa4ff('0x23')]);if(_0x289525[_0xa4ff('0x24')]){delete _0x289525['name'];}_0x3532e3['status'](_0x20e611)[_0xa4ff('0x25')](_0x289525);};}exports[_0xa4ff('0x26')]=function(_0x3c3431,_0x56c618){var _0x30d2aa={},_0x31bf4d={},_0x3548e1={'count':0x0,'rows':[]};var _0x340e2d=_['map'](db[_0xa4ff('0x27')][_0xa4ff('0x28')],function(_0x4ca7ce){return{'name':_0x4ca7ce[_0xa4ff('0x29')],'type':_0x4ca7ce[_0xa4ff('0x2a')][_0xa4ff('0x2b')]};});_0x31bf4d[_0xa4ff('0x2c')]=_[_0xa4ff('0x2d')](_0x340e2d,_0xa4ff('0x24'));_0x31bf4d[_0xa4ff('0x2e')]=_[_0xa4ff('0x2f')](_0x3c3431[_0xa4ff('0x2e')]);_0x31bf4d[_0xa4ff('0x30')]=_['intersection'](_0x31bf4d[_0xa4ff('0x2c')],_0x31bf4d[_0xa4ff('0x2e')]);_0x30d2aa['attributes']=_[_0xa4ff('0x31')](_0x31bf4d[_0xa4ff('0x2c')],qs[_0xa4ff('0x32')](_0x3c3431[_0xa4ff('0x2e')]['fields']));_0x30d2aa[_0xa4ff('0x33')]=_0x30d2aa[_0xa4ff('0x33')][_0xa4ff('0x34')]?_0x30d2aa['attributes']:_0x31bf4d['model'];if(!_0x3c3431[_0xa4ff('0x2e')]['hasOwnProperty'](_0xa4ff('0x35'))){_0x30d2aa[_0xa4ff('0x1b')]=qs[_0xa4ff('0x1b')](_0x3c3431['query'][_0xa4ff('0x1b')]);_0x30d2aa[_0xa4ff('0x19')]=qs[_0xa4ff('0x19')](_0x3c3431[_0xa4ff('0x2e')]['offset']);}_0x30d2aa[_0xa4ff('0x36')]=qs[_0xa4ff('0x37')](_0x3c3431[_0xa4ff('0x2e')]['sort']);_0x30d2aa[_0xa4ff('0x38')]=qs[_0xa4ff('0x30')](_['pick'](_0x3c3431[_0xa4ff('0x2e')],_0x31bf4d['filters']),_0x340e2d);if(_0x3c3431[_0xa4ff('0x2e')]['filter']){_0x30d2aa['where']=_[_0xa4ff('0x39')](_0x30d2aa[_0xa4ff('0x38')],{'$or':_[_0xa4ff('0x2d')](_0x340e2d,function(_0x4a5f68){if(_0x4a5f68[_0xa4ff('0x2a')]!==_0xa4ff('0x3a')){var _0x54f56e={};_0x54f56e[_0x4a5f68[_0xa4ff('0x24')]]={'$like':'%'+_0x3c3431['query'][_0xa4ff('0x3b')]+'%'};return _0x54f56e;}})});}_0x30d2aa=_[_0xa4ff('0x39')]({},_0x30d2aa,_0x3c3431[_0xa4ff('0x3c')]);var _0x21731b={'where':_0x30d2aa[_0xa4ff('0x38')]};return db[_0xa4ff('0x27')][_0xa4ff('0x1c')](_0x21731b)[_0xa4ff('0x22')](function(_0x6148bb){_0x3548e1[_0xa4ff('0x1c')]=_0x6148bb;if(_0x3c3431[_0xa4ff('0x2e')][_0xa4ff('0x3d')]){_0x30d2aa[_0xa4ff('0x3e')]=[{'all':!![]}];}return db[_0xa4ff('0x27')][_0xa4ff('0x3f')](_0x30d2aa);})[_0xa4ff('0x22')](function(_0x5b9152){_0x3548e1[_0xa4ff('0x40')]=_0x5b9152;return _0x3548e1;})['then'](respondWithFilteredResult(_0x56c618,_0x30d2aa))[_0xa4ff('0x41')](handleError(_0x56c618,null));};exports[_0xa4ff('0x42')]=function(_0x5d9a26,_0x15837b){var _0x1b32c9={'raw':![],'where':{'id':_0x5d9a26['params']['id']}},_0x20d0ba={};_0x20d0ba[_0xa4ff('0x2c')]=_[_0xa4ff('0x2f')](db[_0xa4ff('0x27')][_0xa4ff('0x28')]);_0x20d0ba['query']=_[_0xa4ff('0x2f')](_0x5d9a26[_0xa4ff('0x2e')]);_0x20d0ba['filters']=_['intersection'](_0x20d0ba[_0xa4ff('0x2c')],_0x20d0ba[_0xa4ff('0x2e')]);_0x1b32c9['attributes']=_['intersection'](_0x20d0ba[_0xa4ff('0x2c')],qs['fields'](_0x5d9a26[_0xa4ff('0x2e')][_0xa4ff('0x32')]));_0x1b32c9[_0xa4ff('0x33')]=_0x1b32c9[_0xa4ff('0x33')]['length']?_0x1b32c9[_0xa4ff('0x33')]:_0x20d0ba[_0xa4ff('0x2c')];if(_0x5d9a26[_0xa4ff('0x2e')]['includeAll']){_0x1b32c9[_0xa4ff('0x3e')]=[{'all':!![]}];}_0x1b32c9=_[_0xa4ff('0x39')]({},_0x1b32c9,_0x5d9a26['options']);return db[_0xa4ff('0x27')][_0xa4ff('0x43')](_0x1b32c9)['then'](handleEntityNotFound(_0x15837b,null))[_0xa4ff('0x22')](respondWithResult(_0x15837b,null))[_0xa4ff('0x41')](handleError(_0x15837b,null));};exports[_0xa4ff('0x44')]=function(_0xb2772,_0x3fccab){return db[_0xa4ff('0x27')]['create'](_0xb2772[_0xa4ff('0x45')],{})[_0xa4ff('0x22')](respondWithResult(_0x3fccab,0xc9))[_0xa4ff('0x41')](handleError(_0x3fccab,null));};exports[_0xa4ff('0x20')]=function(_0x5bb625,_0x4ec06c){if(_0x5bb625['body']['id']){delete _0x5bb625[_0xa4ff('0x45')]['id'];}return db['FreshdeskAccount']['find']({'where':{'id':_0x5bb625[_0xa4ff('0x46')]['id']}})['then'](handleEntityNotFound(_0x4ec06c,null))[_0xa4ff('0x22')](saveUpdates(_0x5bb625[_0xa4ff('0x45')],null))[_0xa4ff('0x22')](respondWithResult(_0x4ec06c,null))['catch'](handleError(_0x4ec06c,null));};exports[_0xa4ff('0x21')]=function(_0x5da1c6,_0x4055ae){return db[_0xa4ff('0x27')][_0xa4ff('0x43')]({'where':{'id':_0x5da1c6[_0xa4ff('0x46')]['id']}})[_0xa4ff('0x22')](handleEntityNotFound(_0x4055ae,null))[_0xa4ff('0x22')](removeEntity(_0x4055ae,null))[_0xa4ff('0x41')](handleError(_0x4055ae,null));};exports[_0xa4ff('0x47')]=function(_0x370e3a,_0x3ec139,_0x590f1a){var _0x1b31c4={};var _0x598d2c={};var _0x29df4c;var _0x459c69;return db['FreshdeskAccount'][_0xa4ff('0x48')]({'where':{'id':_0x370e3a[_0xa4ff('0x46')]['id']}})[_0xa4ff('0x22')](handleEntityNotFound(_0x3ec139,null))[_0xa4ff('0x22')](function(_0x4b92a8){if(_0x4b92a8){_0x29df4c=_0x4b92a8;_0x598d2c[_0xa4ff('0x2c')]=_[_0xa4ff('0x2f')](db[_0xa4ff('0x49')][_0xa4ff('0x28')]);_0x598d2c['query']=_['keys'](_0x370e3a[_0xa4ff('0x2e')]);_0x598d2c[_0xa4ff('0x30')]=_['intersection'](_0x598d2c[_0xa4ff('0x2c')],_0x598d2c[_0xa4ff('0x2e')]);_0x1b31c4[_0xa4ff('0x33')]=_[_0xa4ff('0x31')](_0x598d2c[_0xa4ff('0x2c')],qs[_0xa4ff('0x32')](_0x370e3a[_0xa4ff('0x2e')]['fields']));_0x1b31c4['attributes']=_0x1b31c4['attributes'][_0xa4ff('0x34')]?_0x1b31c4[_0xa4ff('0x33')]:_0x598d2c[_0xa4ff('0x2c')];_0x1b31c4[_0xa4ff('0x36')]=qs[_0xa4ff('0x37')](_0x370e3a[_0xa4ff('0x2e')]['sort']);_0x1b31c4[_0xa4ff('0x38')]=qs[_0xa4ff('0x30')](_['pick'](_0x370e3a[_0xa4ff('0x2e')],_0x598d2c[_0xa4ff('0x30')]));if(_0x370e3a['query'][_0xa4ff('0x3b')]){_0x1b31c4[_0xa4ff('0x38')]=_[_0xa4ff('0x39')](_0x1b31c4[_0xa4ff('0x38')],{'$or':_[_0xa4ff('0x2d')](_0x1b31c4['attributes'],function(_0x226890){var _0x24414e={};_0x24414e[_0x226890]={'$like':'%'+_0x370e3a[_0xa4ff('0x2e')][_0xa4ff('0x3b')]+'%'};return _0x24414e;})});}_0x1b31c4=_[_0xa4ff('0x39')]({},_0x1b31c4,_0x370e3a[_0xa4ff('0x3c')]);return _0x29df4c[_0xa4ff('0x47')](_0x1b31c4);}})['then'](function(_0x500f03){if(_0x500f03){_0x459c69=_0x500f03[_0xa4ff('0x34')];if(!_0x370e3a['query'][_0xa4ff('0x4a')](_0xa4ff('0x35'))){_0x1b31c4[_0xa4ff('0x1b')]=qs[_0xa4ff('0x1b')](_0x370e3a['query'][_0xa4ff('0x1b')]);_0x1b31c4[_0xa4ff('0x19')]=qs['offset'](_0x370e3a[_0xa4ff('0x2e')][_0xa4ff('0x19')]);}return _0x29df4c['getConfigurations'](_0x1b31c4);}})[_0xa4ff('0x22')](function(_0x55d8d2){if(_0x55d8d2){return _0x55d8d2?{'count':_0x459c69,'rows':_0x55d8d2}:null;}})[_0xa4ff('0x22')](respondWithResult(_0x3ec139,null))[_0xa4ff('0x41')](handleError(_0x3ec139,null));};exports['addConfiguration']=function(_0x132a36,_0x443fa2,_0x10bf9a){if(_0x132a36['body']['id']){delete _0x132a36[_0xa4ff('0x45')]['id'];}return db[_0xa4ff('0x27')][_0xa4ff('0x48')]({'where':{'id':_0x132a36[_0xa4ff('0x46')]['id']}})[_0xa4ff('0x22')](handleEntityNotFound(_0x443fa2,null))[_0xa4ff('0x22')](function(_0x5ed9e9){if(_0x5ed9e9){_0x132a36[_0xa4ff('0x45')][_0xa4ff('0x4b')]=_0x5ed9e9['id'];_0x132a36[_0xa4ff('0x45')][_0xa4ff('0x4c')]=integrations[_0xa4ff('0x4d')](_0x132a36['body'][_0xa4ff('0x4e')],_0x132a36['body'][_0xa4ff('0x2a')]);_0x132a36[_0xa4ff('0x45')][_0xa4ff('0x4f')]=integrations['getDescriptions'](_0x132a36['body'][_0xa4ff('0x4e')],_0x132a36[_0xa4ff('0x45')][_0xa4ff('0x2a')]);return db['FreshdeskConfiguration'][_0xa4ff('0x44')](_0x132a36[_0xa4ff('0x45')],{'include':[{'model':db['FreshdeskField'],'as':_0xa4ff('0x4c')},{'model':db[_0xa4ff('0x50')],'as':_0xa4ff('0x4f')}]});}return null;})[_0xa4ff('0x22')](respondWithResult(_0x443fa2,null))[_0xa4ff('0x41')](handleError(_0x443fa2,null));};exports[_0xa4ff('0x51')]=function(_0x364507,_0x53db00,_0x429211){var _0x27c756='';return db[_0xa4ff('0x27')]['findOne']({'where':{'id':_0x364507[_0xa4ff('0x46')]['id']},'attributes':['id',_0xa4ff('0x52'),'apiKey']})[_0xa4ff('0x22')](handleEntityNotFound(_0x53db00,null))[_0xa4ff('0x22')](function(_0xe0ccb8){if(_0xe0ccb8){_0x27c756=_0xe0ccb8['remoteUri'];var _0x34b2ad=_0x27c756[_0xa4ff('0x53')](-0x1);if(_0x34b2ad==='/'){_0x27c756=_0x27c756[_0xa4ff('0x54')](0x0,_0x27c756['lastIndexOf'](_0x34b2ad));}return rp({'method':_0xa4ff('0x55'),'uri':util[_0xa4ff('0x56')](_0xa4ff('0x57'),_0x27c756,_0xa4ff('0x58')),'headers':{'Authorization':util[_0xa4ff('0x56')]('Basic\x20%s',new Buffer(util[_0xa4ff('0x56')]('%s:X',_0xe0ccb8[_0xa4ff('0x59')]))[_0xa4ff('0x5a')](_0xa4ff('0x5b')))},'json':!![]});}})[_0xa4ff('0x22')](function(_0x4d76e8){if(_0x4d76e8){var _0x5dbb76=['requester',_0xa4ff('0x5c'),'agent',_0xa4ff('0x5d'),_0xa4ff('0x17'),_0xa4ff('0x5e'),_0xa4ff('0x5f'),_0xa4ff('0x60'),_0xa4ff('0x61'),_0xa4ff('0x5f'),_0xa4ff('0x62')];_['remove'](_0x4d76e8,function(_0x5aa8b1){return _0x5dbb76['includes'](_0x5aa8b1[_0xa4ff('0x24')])||!_0x5aa8b1[_0xa4ff('0x63')]&&_0x5aa8b1[_0xa4ff('0x2a')]!==_0xa4ff('0x64')&&_0x5aa8b1[_0xa4ff('0x2a')]!=='custom_dropdown';});return{'count':_0x4d76e8[_0xa4ff('0x34')],'rows':_[_0xa4ff('0x2d')](_0x4d76e8,function(_0x57b9a0){return{'id':_0x57b9a0[_0xa4ff('0x24')],'name':_0x57b9a0[_0xa4ff('0x65')],'custom':!_0x57b9a0[_0xa4ff('0x63')],'options':_0x57b9a0[_0xa4ff('0x66')]?_[_0xa4ff('0x2d')](_0x57b9a0[_0xa4ff('0x66')],function(_0x278c1e){return{'name':_[_0xa4ff('0x67')](_0x278c1e),'value':_0x278c1e};}):[]};})};}})[_0xa4ff('0x22')](respondWithResult(_0x53db00,null))['catch'](function(_0x28daec){var _0x1c6c6d=_0x364507[_0xa4ff('0x2e')][_0xa4ff('0x68')]?0x1f4:_0x28daec['statusCode']||0x1f4;logger[_0xa4ff('0x69')](_0xa4ff('0x6a'),_0xa4ff('0x51'),_0x1c6c6d,JSON[_0xa4ff('0x6b')](_0x28daec));delete _0x28daec['name'];if(_0x1c6c6d===0x191){_0x1c6c6d=0x190;}_0x53db00[_0xa4ff('0x17')](_0x1c6c6d)['send'](_0x364507[_0xa4ff('0x2e')][_0xa4ff('0x68')]?{'message':_0xa4ff('0x6c'),'statusCode':_0x28daec[_0xa4ff('0x6d')]}:_0x28daec);});};