7dbc79733fef99f8b626008b9d0db44326454ea7
[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 _0xf039=['update','then','ExtractedReports','name','index','AnalyticExtractedReport','rawAttributes','fieldName','type','key','map','query','keys','filters','intersection','model','attributes','fields','length','hasOwnProperty','nolimit','order','sort','where','pick','filter','merge','VIRTUAL','options','include','rows','catch','show','params','find','create','user','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','UserProfileSection','userProfileId','autoAssociation','UserProfileResource','error','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','body','destroy','download','output','existsSync','join','root','Sequelize','ValidationError','Report\x20not\x20found\x20or\x20empty','exists','server/files/reports','savename','format','basename','toLowerCase','rimraf','fast-json-patch','mustache','util','path','sox','to-csv','fs-extra','lodash','squel','crypto','desk.js','querystring','papaparse','ioredis','../../components/auth/service','../../config/license/hardware','api','../../config/utils','../../config/environment','../../config/license/util','sendStatus','end','json','undefined','limit','offset','count','status','set','apply','reject','save'];(function(_0x3f9f0f,_0x531b30){var _0x2eb31d=function(_0x43feb1){while(--_0x43feb1){_0x3f9f0f['push'](_0x3f9f0f['shift']());}};_0x2eb31d(++_0x531b30);}(_0xf039,0x1b5));var _0x9f03=function(_0x5dd778,_0x5045e8){_0x5dd778=_0x5dd778-0x0;var _0x359c75=_0xf039[_0x5dd778];return _0x359c75;};'use strict';var emlformat=require('eml-format');var rimraf=require(_0x9f03('0x0'));var zipdir=require('zip-dir');var jsonpatch=require(_0x9f03('0x1'));var rp=require('request-promise');var moment=require('moment');var BPromise=require('bluebird');var Mustache=require(_0x9f03('0x2'));var util=require(_0x9f03('0x3'));var path=require(_0x9f03('0x4'));var sox=require(_0x9f03('0x5'));var csv=require(_0x9f03('0x6'));var ejs=require('ejs');var fs=require('fs');var fs_extra=require(_0x9f03('0x7'));var _=require(_0x9f03('0x8'));var squel=require(_0x9f03('0x9'));var crypto=require(_0x9f03('0xa'));var jsforce=require('jsforce');var deskjs=require(_0x9f03('0xb'));var toCsv=require(_0x9f03('0x6'));var querystring=require(_0x9f03('0xc'));var Papa=require(_0x9f03('0xd'));var Redis=require(_0x9f03('0xe'));var authService=require(_0x9f03('0xf'));var qs=require('../../components/parsers/qs');var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0x9f03('0x10'));var logger=require('../../config/logger')(_0x9f03('0x11'));var utils=require(_0x9f03('0x12'));var config=require(_0x9f03('0x13'));var licenseUtil=require(_0x9f03('0x14'));var db=require('../../mysqldb')['db'];function respondWithStatusCode(_0x48d7b5,_0x2c7b97){_0x2c7b97=_0x2c7b97||0xcc;return function(_0x324fa4){if(_0x324fa4){return _0x48d7b5[_0x9f03('0x15')](_0x2c7b97);}return _0x48d7b5['status'](_0x2c7b97)[_0x9f03('0x16')]();};}function respondWithResult(_0x54e557,_0x56f90b){_0x56f90b=_0x56f90b||0xc8;return function(_0x28379f){if(_0x28379f){return _0x54e557['status'](_0x56f90b)[_0x9f03('0x17')](_0x28379f);}};}function respondWithFilteredResult(_0x1f3101,_0x2f4937){return function(_0x4f2c80){if(_0x4f2c80){var _0x50b236=typeof _0x2f4937['offset']===_0x9f03('0x18')&&typeof _0x2f4937[_0x9f03('0x19')]==='undefined';var _0x56e5c8=_0x4f2c80['count'];var _0x47beca=_0x50b236?0x0:_0x2f4937[_0x9f03('0x1a')];var _0x342c2c=_0x50b236?_0x4f2c80[_0x9f03('0x1b')]:_0x2f4937[_0x9f03('0x1a')]+_0x2f4937[_0x9f03('0x19')];var _0x191788;if(_0x342c2c>=_0x56e5c8){_0x342c2c=_0x56e5c8;_0x191788=0xc8;}else{_0x191788=0xce;}_0x1f3101[_0x9f03('0x1c')](_0x191788);return _0x1f3101[_0x9f03('0x1d')]('Content-Range',_0x47beca+'-'+_0x342c2c+'/'+_0x56e5c8)[_0x9f03('0x17')](_0x4f2c80);}return null;};}function patchUpdates(_0x45003a){return function(_0x10b780){try{jsonpatch[_0x9f03('0x1e')](_0x10b780,_0x45003a,!![]);}catch(_0x4ef30a){return BPromise[_0x9f03('0x1f')](_0x4ef30a);}return _0x10b780[_0x9f03('0x20')]();};}function saveUpdates(_0x3fa7ce,_0x5aff90){return function(_0x525463){if(_0x525463){return _0x525463[_0x9f03('0x21')](_0x3fa7ce)[_0x9f03('0x22')](function(_0x18fefe){return _0x18fefe;});}return null;};}function removeEntity(_0xfb7a2e,_0x573e5b){return function(_0x4a6442){if(_0x4a6442){return _0x4a6442['destroy']()['then'](function(){var _0x467b98=_0x4a6442['get']({'plain':!![]});var _0x3f8e5e=_0x9f03('0x23');return db['UserProfileResource']['destroy']({'where':{'type':_0x3f8e5e,'resourceId':_0x467b98['id']}})[_0x9f03('0x22')](function(){return _0x4a6442;});})['then'](function(){_0xfb7a2e['status'](0xcc)[_0x9f03('0x16')]();});}};}function handleEntityNotFound(_0x3399b1,_0x1b24da){return function(_0x273081){if(!_0x273081){_0x3399b1['sendStatus'](0x194);}return _0x273081;};}function handleError(_0x4911a2,_0x2b8443){_0x2b8443=_0x2b8443||0x1f4;return function(_0x472de6){logger['error'](_0x472de6['stack']);if(_0x472de6['name']){delete _0x472de6[_0x9f03('0x24')];}_0x4911a2[_0x9f03('0x1c')](_0x2b8443)['send'](_0x472de6);};}exports[_0x9f03('0x25')]=function(_0x2bc133,_0x1a185a){var _0x2965ea={},_0x26086f={},_0x1e4f57={'count':0x0,'rows':[]};var _0x2edd38=_['map'](db[_0x9f03('0x26')][_0x9f03('0x27')],function(_0x3d7601){return{'name':_0x3d7601[_0x9f03('0x28')],'type':_0x3d7601[_0x9f03('0x29')][_0x9f03('0x2a')]};});_0x26086f['model']=_[_0x9f03('0x2b')](_0x2edd38,_0x9f03('0x24'));_0x26086f[_0x9f03('0x2c')]=_[_0x9f03('0x2d')](_0x2bc133[_0x9f03('0x2c')]);_0x26086f[_0x9f03('0x2e')]=_[_0x9f03('0x2f')](_0x26086f[_0x9f03('0x30')],_0x26086f[_0x9f03('0x2c')]);_0x2965ea[_0x9f03('0x31')]=_[_0x9f03('0x2f')](_0x26086f[_0x9f03('0x30')],qs[_0x9f03('0x32')](_0x2bc133['query'][_0x9f03('0x32')]));_0x2965ea[_0x9f03('0x31')]=_0x2965ea[_0x9f03('0x31')][_0x9f03('0x33')]?_0x2965ea[_0x9f03('0x31')]:_0x26086f[_0x9f03('0x30')];if(!_0x2bc133[_0x9f03('0x2c')][_0x9f03('0x34')](_0x9f03('0x35'))){_0x2965ea[_0x9f03('0x19')]=qs[_0x9f03('0x19')](_0x2bc133[_0x9f03('0x2c')]['limit']);_0x2965ea[_0x9f03('0x1a')]=qs[_0x9f03('0x1a')](_0x2bc133[_0x9f03('0x2c')][_0x9f03('0x1a')]);}_0x2965ea[_0x9f03('0x36')]=qs[_0x9f03('0x37')](_0x2bc133[_0x9f03('0x2c')][_0x9f03('0x37')]);_0x2965ea[_0x9f03('0x38')]=qs[_0x9f03('0x2e')](_[_0x9f03('0x39')](_0x2bc133['query'],_0x26086f['filters']),_0x2edd38);if(_0x2bc133[_0x9f03('0x2c')][_0x9f03('0x3a')]){_0x2965ea['where']=_[_0x9f03('0x3b')](_0x2965ea['where'],{'$or':_[_0x9f03('0x2b')](_0x2edd38,function(_0x1f00c4){if(_0x1f00c4['type']!==_0x9f03('0x3c')){var _0x33b29b={};_0x33b29b[_0x1f00c4[_0x9f03('0x24')]]={'$like':'%'+_0x2bc133[_0x9f03('0x2c')]['filter']+'%'};return _0x33b29b;}})});}_0x2965ea=_[_0x9f03('0x3b')]({},_0x2965ea,_0x2bc133[_0x9f03('0x3d')]);var _0x4d26c8={'where':_0x2965ea['where']};return db[_0x9f03('0x26')][_0x9f03('0x1b')](_0x4d26c8)[_0x9f03('0x22')](function(_0x44effa){_0x1e4f57['count']=_0x44effa;if(_0x2bc133[_0x9f03('0x2c')]['includeAll']){_0x2965ea[_0x9f03('0x3e')]=[{'all':!![]}];}return db[_0x9f03('0x26')]['findAll'](_0x2965ea);})[_0x9f03('0x22')](function(_0x1211a5){_0x1e4f57[_0x9f03('0x3f')]=_0x1211a5;return _0x1e4f57;})[_0x9f03('0x22')](respondWithFilteredResult(_0x1a185a,_0x2965ea))[_0x9f03('0x40')](handleError(_0x1a185a,null));};exports[_0x9f03('0x41')]=function(_0x543935,_0x8e3d63){var _0x52a5dd={'raw':!![],'where':{'id':_0x543935[_0x9f03('0x42')]['id']}},_0x56063c={};_0x56063c[_0x9f03('0x30')]=_['keys'](db[_0x9f03('0x26')]['rawAttributes']);_0x56063c[_0x9f03('0x2c')]=_[_0x9f03('0x2d')](_0x543935[_0x9f03('0x2c')]);_0x56063c[_0x9f03('0x2e')]=_[_0x9f03('0x2f')](_0x56063c[_0x9f03('0x30')],_0x56063c[_0x9f03('0x2c')]);_0x52a5dd[_0x9f03('0x31')]=_[_0x9f03('0x2f')](_0x56063c['model'],qs[_0x9f03('0x32')](_0x543935['query'][_0x9f03('0x32')]));_0x52a5dd[_0x9f03('0x31')]=_0x52a5dd[_0x9f03('0x31')][_0x9f03('0x33')]?_0x52a5dd['attributes']:_0x56063c[_0x9f03('0x30')];if(_0x543935[_0x9f03('0x2c')]['includeAll']){_0x52a5dd[_0x9f03('0x3e')]=[{'all':!![]}];}_0x52a5dd=_[_0x9f03('0x3b')]({},_0x52a5dd,_0x543935[_0x9f03('0x3d')]);return db[_0x9f03('0x26')][_0x9f03('0x43')](_0x52a5dd)[_0x9f03('0x22')](handleEntityNotFound(_0x8e3d63,null))[_0x9f03('0x22')](respondWithResult(_0x8e3d63,null))[_0x9f03('0x40')](handleError(_0x8e3d63,null));};exports[_0x9f03('0x44')]=function(_0x3b6371,_0x27ecb7){return db['AnalyticExtractedReport']['create'](_0x3b6371['body'],{})[_0x9f03('0x22')](function(_0x3b81ec){var _0x52d149=_0x3b6371[_0x9f03('0x45')]['get']({'plain':!![]});if(!_0x52d149)throw new Error(_0x9f03('0x46'));if(_0x52d149[_0x9f03('0x47')]===_0x9f03('0x45')){var _0x5ea14f=_0x3b81ec['get']({'plain':!![]});var _0x541bd1=_0x9f03('0x23');return db[_0x9f03('0x48')][_0x9f03('0x43')]({'where':{'name':_0x541bd1,'userProfileId':_0x52d149[_0x9f03('0x49')]},'raw':!![]})[_0x9f03('0x22')](function(_0x5ad7dd){if(_0x5ad7dd&&_0x5ad7dd[_0x9f03('0x4a')]===0x0){return db[_0x9f03('0x4b')][_0x9f03('0x44')]({'name':_0x5ea14f[_0x9f03('0x24')],'resourceId':_0x5ea14f['id'],'type':_0x5ad7dd[_0x9f03('0x24')],'sectionId':_0x5ad7dd['id']},{})['then'](function(){return _0x3b81ec;});}else{return _0x3b81ec;}})[_0x9f03('0x40')](function(_0x2f8fdd){logger[_0x9f03('0x4c')](_0x9f03('0x4d'),_0x2f8fdd);throw _0x2f8fdd;});}return _0x3b81ec;})[_0x9f03('0x22')](respondWithResult(_0x27ecb7,0xc9))[_0x9f03('0x40')](handleError(_0x27ecb7,null));};exports['update']=function(_0x43283e,_0x5e6fd0){if(_0x43283e[_0x9f03('0x4e')]['id']){delete _0x43283e[_0x9f03('0x4e')]['id'];}return db[_0x9f03('0x26')][_0x9f03('0x43')]({'where':{'id':_0x43283e['params']['id']}})[_0x9f03('0x22')](handleEntityNotFound(_0x5e6fd0,null))['then'](saveUpdates(_0x43283e[_0x9f03('0x4e')],null))[_0x9f03('0x22')](respondWithResult(_0x5e6fd0,null))[_0x9f03('0x40')](handleError(_0x5e6fd0,null));};exports[_0x9f03('0x4f')]=function(_0x50b801,_0x31d0f2){return db[_0x9f03('0x26')][_0x9f03('0x43')]({'where':{'id':_0x50b801[_0x9f03('0x42')]['id']}})[_0x9f03('0x22')](handleEntityNotFound(_0x31d0f2,null))[_0x9f03('0x22')](removeEntity(_0x31d0f2,null))['catch'](handleError(_0x31d0f2,null));};exports[_0x9f03('0x50')]=function(_0x47d783,_0x182f28,_0x4027b0){return db['AnalyticExtractedReport'][_0x9f03('0x43')]({'where':{'id':_0x47d783[_0x9f03('0x42')]['id']},'attributes':[_0x9f03('0x24'),'basename','savename',_0x9f03('0x51')],'raw':!![]})[_0x9f03('0x22')](handleEntityNotFound(_0x182f28,null))['then'](function(_0x518149){if(_0x518149){if(!fs[_0x9f03('0x52')](path[_0x9f03('0x53')](config[_0x9f03('0x54')],'server/files/reports',_0x518149['savename']))){throw new db[(_0x9f03('0x55'))][(_0x9f03('0x56'))](_0x9f03('0x57'));}if(_0x47d783[_0x9f03('0x2c')][_0x9f03('0x58')]){return _0x182f28[_0x9f03('0x15')](0xc8);}return _0x182f28[_0x9f03('0x50')](path[_0x9f03('0x53')](config['root'],_0x9f03('0x59'),_0x518149[_0x9f03('0x5a')]),util[_0x9f03('0x5b')]('%s.%s',_0x518149[_0x9f03('0x5c')]||_0x518149['name'],_0x518149['output'][_0x9f03('0x5d')]()));}})[_0x9f03('0x40')](handleError(_0x182f28,null));};