Built motion from commit e02f907f.|2.6.14
[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 _0x001f=['fast-json-patch','request-promise','moment','bluebird','util','to-csv','ejs','fs-extra','lodash','squel','crypto','desk.js','querystring','../../components/parsers/qs','../../components/parsers/advancedSearch','api','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','status','end','json','offset','undefined','limit','count','set','apply','reject','update','then','destroy','ExtractedReports','UserProfileResource','sendStatus','error','stack','name','send','index','map','AnalyticExtractedReport','fieldName','type','key','model','keys','query','intersection','attributes','fields','length','hasOwnProperty','nolimit','sort','where','pick','filters','filter','VIRTUAL','merge','include','rows','catch','show','params','includeAll','options','find','create','user','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','get','userProfileId','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','body','basename','savename','output','existsSync','join','server/files/reports','Sequelize','ValidationError','Report\x20not\x20found\x20or\x20empty','exists','download','root','format','%s.%s','toLowerCase','eml-format','rimraf','zip-dir'];(function(_0x3bd229,_0x18a659){var _0x342bc2=function(_0x40f321){while(--_0x40f321){_0x3bd229['push'](_0x3bd229['shift']());}};_0x342bc2(++_0x18a659);}(_0x001f,0x181));var _0xf001=function(_0x36dee4,_0x3d3cfe){_0x36dee4=_0x36dee4-0x0;var _0x13872f=_0x001f[_0x36dee4];return _0x13872f;};'use strict';var emlformat=require(_0xf001('0x0'));var rimraf=require(_0xf001('0x1'));var zipdir=require(_0xf001('0x2'));var jsonpatch=require(_0xf001('0x3'));var rp=require(_0xf001('0x4'));var moment=require(_0xf001('0x5'));var BPromise=require(_0xf001('0x6'));var Mustache=require('mustache');var util=require(_0xf001('0x7'));var path=require('path');var sox=require('sox');var csv=require(_0xf001('0x8'));var ejs=require(_0xf001('0x9'));var fs=require('fs');var fs_extra=require(_0xf001('0xa'));var _=require(_0xf001('0xb'));var squel=require(_0xf001('0xc'));var crypto=require(_0xf001('0xd'));var jsforce=require('jsforce');var deskjs=require(_0xf001('0xe'));var toCsv=require(_0xf001('0x8'));var querystring=require(_0xf001('0xf'));var Papa=require('papaparse');var Redis=require('ioredis');var authService=require('../../components/auth/service');var qs=require(_0xf001('0x10'));var as=require(_0xf001('0x11'));var hardwareService=require('../../config/license/hardware');var logger=require('../../config/logger')(_0xf001('0x12'));var utils=require(_0xf001('0x13'));var config=require(_0xf001('0x14'));var licenseUtil=require(_0xf001('0x15'));var db=require(_0xf001('0x16'))['db'];function respondWithStatusCode(_0x318137,_0x33a20b){_0x33a20b=_0x33a20b||0xcc;return function(_0xa2bd6f){if(_0xa2bd6f){return _0x318137['sendStatus'](_0x33a20b);}return _0x318137[_0xf001('0x17')](_0x33a20b)[_0xf001('0x18')]();};}function respondWithResult(_0x2275d1,_0x4facbf){_0x4facbf=_0x4facbf||0xc8;return function(_0x2837a8){if(_0x2837a8){return _0x2275d1['status'](_0x4facbf)[_0xf001('0x19')](_0x2837a8);}};}function respondWithFilteredResult(_0x403448,_0x163af4){return function(_0x32dd4b){if(_0x32dd4b){var _0x2bb5d7=typeof _0x163af4[_0xf001('0x1a')]===_0xf001('0x1b')&&typeof _0x163af4[_0xf001('0x1c')]===_0xf001('0x1b');var _0x2d93ba=_0x32dd4b[_0xf001('0x1d')];var _0x3385e2=_0x2bb5d7?0x0:_0x163af4['offset'];var _0x54b3a6=_0x2bb5d7?_0x32dd4b[_0xf001('0x1d')]:_0x163af4['offset']+_0x163af4['limit'];var _0x4c3d1d;if(_0x54b3a6>=_0x2d93ba){_0x54b3a6=_0x2d93ba;_0x4c3d1d=0xc8;}else{_0x4c3d1d=0xce;}_0x403448[_0xf001('0x17')](_0x4c3d1d);return _0x403448[_0xf001('0x1e')]('Content-Range',_0x3385e2+'-'+_0x54b3a6+'/'+_0x2d93ba)[_0xf001('0x19')](_0x32dd4b);}return null;};}function patchUpdates(_0x153d84){return function(_0x81e5ef){try{jsonpatch[_0xf001('0x1f')](_0x81e5ef,_0x153d84,!![]);}catch(_0x274a52){return BPromise[_0xf001('0x20')](_0x274a52);}return _0x81e5ef['save']();};}function saveUpdates(_0x1d7309,_0xc7e3b2){return function(_0x584098){if(_0x584098){return _0x584098[_0xf001('0x21')](_0x1d7309)[_0xf001('0x22')](function(_0x260bf8){return _0x260bf8;});}return null;};}function removeEntity(_0x367671,_0x48e8f3){return function(_0x3665a9){if(_0x3665a9){return _0x3665a9[_0xf001('0x23')]()['then'](function(){var _0x216204=_0x3665a9['get']({'plain':!![]});var _0x7aaa34=_0xf001('0x24');return db[_0xf001('0x25')]['destroy']({'where':{'type':_0x7aaa34,'resourceId':_0x216204['id']}})['then'](function(){return _0x3665a9;});})[_0xf001('0x22')](function(){_0x367671[_0xf001('0x17')](0xcc)[_0xf001('0x18')]();});}};}function handleEntityNotFound(_0x32bdf4,_0x4eff76){return function(_0x45f53f){if(!_0x45f53f){_0x32bdf4[_0xf001('0x26')](0x194);}return _0x45f53f;};}function handleError(_0x26e72d,_0x37e81e){_0x37e81e=_0x37e81e||0x1f4;return function(_0x4d0a13){logger[_0xf001('0x27')](_0x4d0a13[_0xf001('0x28')]);if(_0x4d0a13['name']){delete _0x4d0a13[_0xf001('0x29')];}_0x26e72d[_0xf001('0x17')](_0x37e81e)[_0xf001('0x2a')](_0x4d0a13);};}exports[_0xf001('0x2b')]=function(_0x2f7425,_0x365e9d){var _0x495e47={},_0x31c61a={},_0x344634={'count':0x0,'rows':[]};var _0x4ed1a8=_[_0xf001('0x2c')](db[_0xf001('0x2d')]['rawAttributes'],function(_0x2dc16c){return{'name':_0x2dc16c[_0xf001('0x2e')],'type':_0x2dc16c[_0xf001('0x2f')][_0xf001('0x30')]};});_0x31c61a[_0xf001('0x31')]=_[_0xf001('0x2c')](_0x4ed1a8,_0xf001('0x29'));_0x31c61a['query']=_[_0xf001('0x32')](_0x2f7425[_0xf001('0x33')]);_0x31c61a['filters']=_[_0xf001('0x34')](_0x31c61a[_0xf001('0x31')],_0x31c61a[_0xf001('0x33')]);_0x495e47[_0xf001('0x35')]=_['intersection'](_0x31c61a[_0xf001('0x31')],qs[_0xf001('0x36')](_0x2f7425[_0xf001('0x33')]['fields']));_0x495e47[_0xf001('0x35')]=_0x495e47[_0xf001('0x35')][_0xf001('0x37')]?_0x495e47['attributes']:_0x31c61a[_0xf001('0x31')];if(!_0x2f7425[_0xf001('0x33')][_0xf001('0x38')](_0xf001('0x39'))){_0x495e47[_0xf001('0x1c')]=qs['limit'](_0x2f7425[_0xf001('0x33')][_0xf001('0x1c')]);_0x495e47[_0xf001('0x1a')]=qs[_0xf001('0x1a')](_0x2f7425[_0xf001('0x33')][_0xf001('0x1a')]);}_0x495e47['order']=qs['sort'](_0x2f7425[_0xf001('0x33')][_0xf001('0x3a')]);_0x495e47[_0xf001('0x3b')]=qs['filters'](_[_0xf001('0x3c')](_0x2f7425[_0xf001('0x33')],_0x31c61a[_0xf001('0x3d')]),_0x4ed1a8);if(_0x2f7425[_0xf001('0x33')][_0xf001('0x3e')]){_0x495e47[_0xf001('0x3b')]=_['merge'](_0x495e47[_0xf001('0x3b')],{'$or':_[_0xf001('0x2c')](_0x4ed1a8,function(_0x27439e){if(_0x27439e[_0xf001('0x2f')]!==_0xf001('0x3f')){var _0xa7a1a5={};_0xa7a1a5[_0x27439e[_0xf001('0x29')]]={'$like':'%'+_0x2f7425[_0xf001('0x33')][_0xf001('0x3e')]+'%'};return _0xa7a1a5;}})});}_0x495e47=_[_0xf001('0x40')]({},_0x495e47,_0x2f7425['options']);var _0x11a8f6={'where':_0x495e47['where']};return db['AnalyticExtractedReport'][_0xf001('0x1d')](_0x11a8f6)[_0xf001('0x22')](function(_0x23b7c1){_0x344634['count']=_0x23b7c1;if(_0x2f7425[_0xf001('0x33')]['includeAll']){_0x495e47[_0xf001('0x41')]=[{'all':!![]}];}return db[_0xf001('0x2d')]['findAll'](_0x495e47);})[_0xf001('0x22')](function(_0x3a6e47){_0x344634[_0xf001('0x42')]=_0x3a6e47;return _0x344634;})['then'](respondWithFilteredResult(_0x365e9d,_0x495e47))[_0xf001('0x43')](handleError(_0x365e9d,null));};exports[_0xf001('0x44')]=function(_0x30c5e4,_0x52fe60){var _0x1d87b4={'raw':!![],'where':{'id':_0x30c5e4[_0xf001('0x45')]['id']}},_0xc5bf16={};_0xc5bf16[_0xf001('0x31')]=_[_0xf001('0x32')](db['AnalyticExtractedReport']['rawAttributes']);_0xc5bf16[_0xf001('0x33')]=_['keys'](_0x30c5e4[_0xf001('0x33')]);_0xc5bf16[_0xf001('0x3d')]=_['intersection'](_0xc5bf16['model'],_0xc5bf16[_0xf001('0x33')]);_0x1d87b4[_0xf001('0x35')]=_[_0xf001('0x34')](_0xc5bf16['model'],qs['fields'](_0x30c5e4['query'][_0xf001('0x36')]));_0x1d87b4[_0xf001('0x35')]=_0x1d87b4[_0xf001('0x35')][_0xf001('0x37')]?_0x1d87b4[_0xf001('0x35')]:_0xc5bf16[_0xf001('0x31')];if(_0x30c5e4['query'][_0xf001('0x46')]){_0x1d87b4['include']=[{'all':!![]}];}_0x1d87b4=_[_0xf001('0x40')]({},_0x1d87b4,_0x30c5e4[_0xf001('0x47')]);return db[_0xf001('0x2d')][_0xf001('0x48')](_0x1d87b4)[_0xf001('0x22')](handleEntityNotFound(_0x52fe60,null))['then'](respondWithResult(_0x52fe60,null))[_0xf001('0x43')](handleError(_0x52fe60,null));};exports[_0xf001('0x49')]=function(_0x53471d,_0x44b414){return db[_0xf001('0x2d')]['create'](_0x53471d['body'],{})[_0xf001('0x22')](function(_0x6612e7){var _0x43449c=_0x53471d[_0xf001('0x4a')]['get']({'plain':!![]});if(!_0x43449c)throw new Error(_0xf001('0x4b'));if(_0x43449c[_0xf001('0x4c')]==='user'){var _0x20ddb1=_0x6612e7[_0xf001('0x4d')]({'plain':!![]});var _0xb44471=_0xf001('0x24');return db['UserProfileSection'][_0xf001('0x48')]({'where':{'name':_0xb44471,'userProfileId':_0x43449c[_0xf001('0x4e')]},'raw':!![]})['then'](function(_0x11abf1){if(_0x11abf1&&_0x11abf1[_0xf001('0x4f')]===0x0){return db[_0xf001('0x25')][_0xf001('0x49')]({'name':_0x20ddb1[_0xf001('0x29')],'resourceId':_0x20ddb1['id'],'type':_0x11abf1[_0xf001('0x29')],'sectionId':_0x11abf1['id']},{})['then'](function(){return _0x6612e7;});}else{return _0x6612e7;}})[_0xf001('0x43')](function(_0x5e4ba8){logger[_0xf001('0x27')](_0xf001('0x50'),_0x5e4ba8);throw _0x5e4ba8;});}return _0x6612e7;})['then'](respondWithResult(_0x44b414,0xc9))[_0xf001('0x43')](handleError(_0x44b414,null));};exports[_0xf001('0x21')]=function(_0x1bfca3,_0x4c2aaa){if(_0x1bfca3['body']['id']){delete _0x1bfca3[_0xf001('0x51')]['id'];}return db[_0xf001('0x2d')]['find']({'where':{'id':_0x1bfca3['params']['id']}})[_0xf001('0x22')](handleEntityNotFound(_0x4c2aaa,null))[_0xf001('0x22')](saveUpdates(_0x1bfca3[_0xf001('0x51')],null))[_0xf001('0x22')](respondWithResult(_0x4c2aaa,null))[_0xf001('0x43')](handleError(_0x4c2aaa,null));};exports[_0xf001('0x23')]=function(_0x3024d2,_0x214ce3){return db[_0xf001('0x2d')][_0xf001('0x48')]({'where':{'id':_0x3024d2[_0xf001('0x45')]['id']}})[_0xf001('0x22')](handleEntityNotFound(_0x214ce3,null))[_0xf001('0x22')](removeEntity(_0x214ce3,null))[_0xf001('0x43')](handleError(_0x214ce3,null));};exports['download']=function(_0x1fd2b2,_0xcd4511,_0x3145c0){return db[_0xf001('0x2d')]['find']({'where':{'id':_0x1fd2b2[_0xf001('0x45')]['id']},'attributes':[_0xf001('0x29'),_0xf001('0x52'),_0xf001('0x53'),_0xf001('0x54')],'raw':!![]})[_0xf001('0x22')](handleEntityNotFound(_0xcd4511,null))[_0xf001('0x22')](function(_0x284cb8){if(_0x284cb8){if(!fs[_0xf001('0x55')](path[_0xf001('0x56')](config['root'],_0xf001('0x57'),_0x284cb8[_0xf001('0x53')]))){throw new db[(_0xf001('0x58'))][(_0xf001('0x59'))](_0xf001('0x5a'));}if(_0x1fd2b2[_0xf001('0x33')][_0xf001('0x5b')]){return _0xcd4511[_0xf001('0x26')](0xc8);}return _0xcd4511[_0xf001('0x5c')](path[_0xf001('0x56')](config[_0xf001('0x5d')],'server/files/reports',_0x284cb8[_0xf001('0x53')]),util[_0xf001('0x5e')](_0xf001('0x5f'),_0x284cb8[_0xf001('0x52')]||_0x284cb8['name'],_0x284cb8['output'][_0xf001('0x60')]()));}})['catch'](handleError(_0xcd4511,null));};