9074bee7485347f7bc3f9ccc918bf8d608d08ee0
[motion2.git] / server / api / analyticExtractedReport / analyticExtractedReport.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 _0x42f8=['end','status','json','offset','limit','undefined','count','set','Content-Range','save','then','get','ExtractedReports','UserProfileResource','destroy','error','name','send','index','map','fieldName','type','key','intersection','attributes','model','fields','length','query','hasOwnProperty','nolimit','order','sort','where','filters','pick','filter','merge','VIRTUAL','options','AnalyticExtractedReport','include','rows','show','params','rawAttributes','includeAll','find','catch','create','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','user','UserProfileSection','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','body','savename','output','existsSync','ValidationError','Report\x20not\x20found\x20or\x20empty','exists','download','server/files/reports','format','%s.%s','basename','toLowerCase','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','moment','path','sox','to-csv','ejs','fs-extra','lodash','squel','crypto','jsforce','desk.js','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','api','../../config/environment','sendStatus'];(function(_0x3278fd,_0x1ceb20){var _0x293832=function(_0x115e24){while(--_0x115e24){_0x3278fd['push'](_0x3278fd['shift']());}};_0x293832(++_0x1ceb20);}(_0x42f8,0x103));var _0x842f=function(_0x1ab155,_0x374fc3){_0x1ab155=_0x1ab155-0x0;var _0x25cd61=_0x42f8[_0x1ab155];return _0x25cd61;};'use strict';var emlformat=require(_0x842f('0x0'));var rimraf=require(_0x842f('0x1'));var zipdir=require(_0x842f('0x2'));var jsonpatch=require(_0x842f('0x3'));var rp=require(_0x842f('0x4'));var moment=require(_0x842f('0x5'));var BPromise=require('bluebird');var Mustache=require('mustache');var util=require('util');var path=require(_0x842f('0x6'));var sox=require(_0x842f('0x7'));var csv=require(_0x842f('0x8'));var ejs=require(_0x842f('0x9'));var fs=require('fs');var fs_extra=require(_0x842f('0xa'));var _=require(_0x842f('0xb'));var squel=require(_0x842f('0xc'));var crypto=require(_0x842f('0xd'));var jsforce=require(_0x842f('0xe'));var deskjs=require(_0x842f('0xf'));var toCsv=require(_0x842f('0x8'));var querystring=require(_0x842f('0x10'));var Papa=require(_0x842f('0x11'));var Redis=require(_0x842f('0x12'));var authService=require(_0x842f('0x13'));var qs=require(_0x842f('0x14'));var as=require(_0x842f('0x15'));var hardwareService=require(_0x842f('0x16'));var logger=require('../../config/logger')(_0x842f('0x17'));var utils=require('../../config/utils');var config=require(_0x842f('0x18'));var licenseUtil=require('../../config/license/util');var db=require('../../mysqldb')['db'];function respondWithStatusCode(_0x54e4e1,_0x44d1ac){_0x44d1ac=_0x44d1ac||0xcc;return function(_0x487f18){if(_0x487f18){return _0x54e4e1[_0x842f('0x19')](_0x44d1ac);}return _0x54e4e1['status'](_0x44d1ac)[_0x842f('0x1a')]();};}function respondWithResult(_0x5e080f,_0x51514a){_0x51514a=_0x51514a||0xc8;return function(_0x2cf9c1){if(_0x2cf9c1){return _0x5e080f[_0x842f('0x1b')](_0x51514a)[_0x842f('0x1c')](_0x2cf9c1);}};}function respondWithFilteredResult(_0x349974,_0x127441){return function(_0x339e05){if(_0x339e05){var _0x5d04ba=typeof _0x127441[_0x842f('0x1d')]==='undefined'&&typeof _0x127441[_0x842f('0x1e')]===_0x842f('0x1f');var _0x36ac2a=_0x339e05['count'];var _0x5dfa92=_0x5d04ba?0x0:_0x127441[_0x842f('0x1d')];var _0x1f0131=_0x5d04ba?_0x339e05[_0x842f('0x20')]:_0x127441[_0x842f('0x1d')]+_0x127441[_0x842f('0x1e')];var _0x51a0d9;if(_0x1f0131>=_0x36ac2a){_0x1f0131=_0x36ac2a;_0x51a0d9=0xc8;}else{_0x51a0d9=0xce;}_0x349974[_0x842f('0x1b')](_0x51a0d9);return _0x349974[_0x842f('0x21')](_0x842f('0x22'),_0x5dfa92+'-'+_0x1f0131+'/'+_0x36ac2a)[_0x842f('0x1c')](_0x339e05);}return null;};}function patchUpdates(_0x49a486){return function(_0x1b5dff){try{jsonpatch['apply'](_0x1b5dff,_0x49a486,!![]);}catch(_0x9efd99){return BPromise['reject'](_0x9efd99);}return _0x1b5dff[_0x842f('0x23')]();};}function saveUpdates(_0x3184a0,_0x3cc8cf){return function(_0x2d2155){if(_0x2d2155){return _0x2d2155['update'](_0x3184a0)[_0x842f('0x24')](function(_0x20c28f){return _0x20c28f;});}return null;};}function removeEntity(_0x3126e7,_0x30ddcd){return function(_0x1e0895){if(_0x1e0895){return _0x1e0895['destroy']()[_0x842f('0x24')](function(){var _0x11a292=_0x1e0895[_0x842f('0x25')]({'plain':!![]});var _0x36066c=_0x842f('0x26');return db[_0x842f('0x27')][_0x842f('0x28')]({'where':{'type':_0x36066c,'resourceId':_0x11a292['id']}})[_0x842f('0x24')](function(){return _0x1e0895;});})[_0x842f('0x24')](function(){_0x3126e7[_0x842f('0x1b')](0xcc)[_0x842f('0x1a')]();});}};}function handleEntityNotFound(_0x51fa30,_0x2bb040){return function(_0x252d26){if(!_0x252d26){_0x51fa30[_0x842f('0x19')](0x194);}return _0x252d26;};}function handleError(_0x3c61ed,_0x14480f){_0x14480f=_0x14480f||0x1f4;return function(_0x3aa298){logger[_0x842f('0x29')](_0x3aa298['stack']);if(_0x3aa298[_0x842f('0x2a')]){delete _0x3aa298[_0x842f('0x2a')];}_0x3c61ed[_0x842f('0x1b')](_0x14480f)[_0x842f('0x2b')](_0x3aa298);};}exports[_0x842f('0x2c')]=function(_0x48e6bd,_0xff2598){var _0x3d5fce={},_0x312660={},_0x53f1b8={'count':0x0,'rows':[]};var _0x89ef1=_[_0x842f('0x2d')](db['AnalyticExtractedReport']['rawAttributes'],function(_0x20c54d){return{'name':_0x20c54d[_0x842f('0x2e')],'type':_0x20c54d[_0x842f('0x2f')][_0x842f('0x30')]};});_0x312660['model']=_[_0x842f('0x2d')](_0x89ef1,_0x842f('0x2a'));_0x312660['query']=_['keys'](_0x48e6bd['query']);_0x312660['filters']=_[_0x842f('0x31')](_0x312660['model'],_0x312660['query']);_0x3d5fce[_0x842f('0x32')]=_[_0x842f('0x31')](_0x312660[_0x842f('0x33')],qs[_0x842f('0x34')](_0x48e6bd['query']['fields']));_0x3d5fce[_0x842f('0x32')]=_0x3d5fce[_0x842f('0x32')][_0x842f('0x35')]?_0x3d5fce[_0x842f('0x32')]:_0x312660[_0x842f('0x33')];if(!_0x48e6bd[_0x842f('0x36')][_0x842f('0x37')](_0x842f('0x38'))){_0x3d5fce[_0x842f('0x1e')]=qs['limit'](_0x48e6bd[_0x842f('0x36')][_0x842f('0x1e')]);_0x3d5fce[_0x842f('0x1d')]=qs[_0x842f('0x1d')](_0x48e6bd[_0x842f('0x36')]['offset']);}_0x3d5fce[_0x842f('0x39')]=qs[_0x842f('0x3a')](_0x48e6bd['query'][_0x842f('0x3a')]);_0x3d5fce[_0x842f('0x3b')]=qs[_0x842f('0x3c')](_[_0x842f('0x3d')](_0x48e6bd[_0x842f('0x36')],_0x312660['filters']),_0x89ef1);if(_0x48e6bd['query'][_0x842f('0x3e')]){_0x3d5fce[_0x842f('0x3b')]=_[_0x842f('0x3f')](_0x3d5fce[_0x842f('0x3b')],{'$or':_[_0x842f('0x2d')](_0x89ef1,function(_0x1ca9b0){if(_0x1ca9b0[_0x842f('0x2f')]!==_0x842f('0x40')){var _0x1838f5={};_0x1838f5[_0x1ca9b0[_0x842f('0x2a')]]={'$like':'%'+_0x48e6bd[_0x842f('0x36')][_0x842f('0x3e')]+'%'};return _0x1838f5;}})});}_0x3d5fce=_[_0x842f('0x3f')]({},_0x3d5fce,_0x48e6bd[_0x842f('0x41')]);var _0x393f56={'where':_0x3d5fce[_0x842f('0x3b')]};return db[_0x842f('0x42')][_0x842f('0x20')](_0x393f56)['then'](function(_0x2e08fc){_0x53f1b8[_0x842f('0x20')]=_0x2e08fc;if(_0x48e6bd[_0x842f('0x36')]['includeAll']){_0x3d5fce[_0x842f('0x43')]=[{'all':!![]}];}return db[_0x842f('0x42')]['findAll'](_0x3d5fce);})['then'](function(_0x101b94){_0x53f1b8[_0x842f('0x44')]=_0x101b94;return _0x53f1b8;})[_0x842f('0x24')](respondWithFilteredResult(_0xff2598,_0x3d5fce))['catch'](handleError(_0xff2598,null));};exports[_0x842f('0x45')]=function(_0x4a403,_0x234394){var _0x30d3dc={'raw':!![],'where':{'id':_0x4a403[_0x842f('0x46')]['id']}},_0x169831={};_0x169831[_0x842f('0x33')]=_['keys'](db[_0x842f('0x42')][_0x842f('0x47')]);_0x169831[_0x842f('0x36')]=_['keys'](_0x4a403[_0x842f('0x36')]);_0x169831[_0x842f('0x3c')]=_[_0x842f('0x31')](_0x169831[_0x842f('0x33')],_0x169831[_0x842f('0x36')]);_0x30d3dc['attributes']=_[_0x842f('0x31')](_0x169831[_0x842f('0x33')],qs[_0x842f('0x34')](_0x4a403[_0x842f('0x36')][_0x842f('0x34')]));_0x30d3dc[_0x842f('0x32')]=_0x30d3dc[_0x842f('0x32')][_0x842f('0x35')]?_0x30d3dc[_0x842f('0x32')]:_0x169831[_0x842f('0x33')];if(_0x4a403[_0x842f('0x36')][_0x842f('0x48')]){_0x30d3dc[_0x842f('0x43')]=[{'all':!![]}];}_0x30d3dc=_[_0x842f('0x3f')]({},_0x30d3dc,_0x4a403[_0x842f('0x41')]);return db[_0x842f('0x42')][_0x842f('0x49')](_0x30d3dc)[_0x842f('0x24')](handleEntityNotFound(_0x234394,null))[_0x842f('0x24')](respondWithResult(_0x234394,null))[_0x842f('0x4a')](handleError(_0x234394,null));};exports[_0x842f('0x4b')]=function(_0x4c2133,_0x33884c){return db[_0x842f('0x42')][_0x842f('0x4b')](_0x4c2133['body'],{})[_0x842f('0x24')](function(_0x54fe4a){var _0x586760=_0x4c2133['user']['get']({'plain':!![]});if(!_0x586760)throw new Error(_0x842f('0x4c'));if(_0x586760[_0x842f('0x4d')]===_0x842f('0x4e')){var _0x1da3d9=_0x54fe4a[_0x842f('0x25')]({'plain':!![]});var _0x568dd3=_0x842f('0x26');return db[_0x842f('0x4f')]['find']({'where':{'name':_0x568dd3,'userProfileId':_0x586760['userProfileId']},'raw':!![]})[_0x842f('0x24')](function(_0x20b0e9){if(_0x20b0e9&&_0x20b0e9[_0x842f('0x50')]===0x0){return db[_0x842f('0x27')][_0x842f('0x4b')]({'name':_0x1da3d9[_0x842f('0x2a')],'resourceId':_0x1da3d9['id'],'type':_0x20b0e9['name'],'sectionId':_0x20b0e9['id']},{})[_0x842f('0x24')](function(){return _0x54fe4a;});}else{return _0x54fe4a;}})[_0x842f('0x4a')](function(_0x41f196){logger[_0x842f('0x29')](_0x842f('0x51'),_0x41f196);throw _0x41f196;});}return _0x54fe4a;})['then'](respondWithResult(_0x33884c,0xc9))[_0x842f('0x4a')](handleError(_0x33884c,null));};exports['update']=function(_0x26ee0c,_0x37f842){if(_0x26ee0c[_0x842f('0x52')]['id']){delete _0x26ee0c['body']['id'];}return db['AnalyticExtractedReport']['find']({'where':{'id':_0x26ee0c[_0x842f('0x46')]['id']}})[_0x842f('0x24')](handleEntityNotFound(_0x37f842,null))[_0x842f('0x24')](saveUpdates(_0x26ee0c[_0x842f('0x52')],null))['then'](respondWithResult(_0x37f842,null))[_0x842f('0x4a')](handleError(_0x37f842,null));};exports[_0x842f('0x28')]=function(_0x22e3ad,_0x54e2b8){return db['AnalyticExtractedReport'][_0x842f('0x49')]({'where':{'id':_0x22e3ad[_0x842f('0x46')]['id']}})['then'](handleEntityNotFound(_0x54e2b8,null))['then'](removeEntity(_0x54e2b8,null))[_0x842f('0x4a')](handleError(_0x54e2b8,null));};exports['download']=function(_0x169b93,_0x45954f,_0x51a28c){return db['AnalyticExtractedReport'][_0x842f('0x49')]({'where':{'id':_0x169b93['params']['id']},'attributes':['name','basename',_0x842f('0x53'),_0x842f('0x54')],'raw':!![]})[_0x842f('0x24')](handleEntityNotFound(_0x45954f,null))[_0x842f('0x24')](function(_0x5d4d66){if(_0x5d4d66){if(!fs[_0x842f('0x55')](path['join'](config['root'],'server/files/reports',_0x5d4d66[_0x842f('0x53')]))){throw new db['Sequelize'][(_0x842f('0x56'))](_0x842f('0x57'));}if(_0x169b93['query'][_0x842f('0x58')]){return _0x45954f[_0x842f('0x19')](0xc8);}return _0x45954f[_0x842f('0x59')](path['join'](config['root'],_0x842f('0x5a'),_0x5d4d66['savename']),util[_0x842f('0x5b')](_0x842f('0x5c'),_0x5d4d66[_0x842f('0x5d')]||_0x5d4d66[_0x842f('0x2a')],_0x5d4d66[_0x842f('0x54')][_0x842f('0x5e')]()));}})[_0x842f('0x4a')](handleError(_0x45954f,null));};