Built motion from commit 85328c08.|2.5.50
[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 _0x0fb1=['model','query','keys','filters','intersection','attributes','fields','hasOwnProperty','nolimit','order','where','pick','type','includeAll','rows','catch','params','AnalyticExtractedReport','length','include','merge','options','create','user','Unable\x20to\x20retrieve\x20the\x20current\x20user','UserProfileSection','find','userProfileId','autoAssociation','error','body','download','basename','savename','existsSync','Sequelize','ValidationError','Report\x20not\x20found\x20or\x20empty','join','server/files/reports','%s.%s','output','toLowerCase','eml-format','rimraf','zip-dir','request-promise','bluebird','util','path','sox','ejs','fs-extra','squel','crypto','jsforce','desk.js','to-csv','querystring','papaparse','../../components/auth/service','../../components/parsers/advancedSearch','../../config/license/hardware','api','../../config/utils','../../config/environment','../../config/license/util','sendStatus','status','end','json','offset','undefined','limit','count','set','Content-Range','apply','reject','save','update','then','destroy','get','ExtractedReports','stack','name','send','index','map','fieldName'];(function(_0x1b13c8,_0x46c81f){var _0x707ce9=function(_0x40edab){while(--_0x40edab){_0x1b13c8['push'](_0x1b13c8['shift']());}};_0x707ce9(++_0x46c81f);}(_0x0fb1,0xe1));var _0x10fb=function(_0x301d02,_0x25bf0f){_0x301d02=_0x301d02-0x0;var _0x4ce8e5=_0x0fb1[_0x301d02];return _0x4ce8e5;};'use strict';var emlformat=require(_0x10fb('0x0'));var rimraf=require(_0x10fb('0x1'));var zipdir=require(_0x10fb('0x2'));var jsonpatch=require('fast-json-patch');var rp=require(_0x10fb('0x3'));var moment=require('moment');var BPromise=require(_0x10fb('0x4'));var Mustache=require('mustache');var util=require(_0x10fb('0x5'));var path=require(_0x10fb('0x6'));var sox=require(_0x10fb('0x7'));var csv=require('to-csv');var ejs=require(_0x10fb('0x8'));var fs=require('fs');var fs_extra=require(_0x10fb('0x9'));var _=require('lodash');var squel=require(_0x10fb('0xa'));var crypto=require(_0x10fb('0xb'));var jsforce=require(_0x10fb('0xc'));var deskjs=require(_0x10fb('0xd'));var toCsv=require(_0x10fb('0xe'));var querystring=require(_0x10fb('0xf'));var Papa=require(_0x10fb('0x10'));var Redis=require('ioredis');var authService=require(_0x10fb('0x11'));var qs=require('../../components/parsers/qs');var as=require(_0x10fb('0x12'));var hardwareService=require(_0x10fb('0x13'));var logger=require('../../config/logger')(_0x10fb('0x14'));var utils=require(_0x10fb('0x15'));var config=require(_0x10fb('0x16'));var licenseUtil=require(_0x10fb('0x17'));var db=require('../../mysqldb')['db'];function respondWithStatusCode(_0x5dcabf,_0x4f5d3c){_0x4f5d3c=_0x4f5d3c||0xcc;return function(_0x4c95f5){if(_0x4c95f5){return _0x5dcabf[_0x10fb('0x18')](_0x4f5d3c);}return _0x5dcabf[_0x10fb('0x19')](_0x4f5d3c)[_0x10fb('0x1a')]();};}function respondWithResult(_0x5224da,_0x17b0a3){_0x17b0a3=_0x17b0a3||0xc8;return function(_0x324516){if(_0x324516){return _0x5224da[_0x10fb('0x19')](_0x17b0a3)[_0x10fb('0x1b')](_0x324516);}};}function respondWithFilteredResult(_0x245a04,_0xfcb15d){return function(_0xafb92e){if(_0xafb92e){var _0x3b7414=typeof _0xfcb15d[_0x10fb('0x1c')]===_0x10fb('0x1d')&&typeof _0xfcb15d[_0x10fb('0x1e')]===_0x10fb('0x1d');var _0x1f422a=_0xafb92e[_0x10fb('0x1f')];var _0x3618db=_0x3b7414?0x0:_0xfcb15d[_0x10fb('0x1c')];var _0x389993=_0x3b7414?_0xafb92e[_0x10fb('0x1f')]:_0xfcb15d[_0x10fb('0x1c')]+_0xfcb15d[_0x10fb('0x1e')];var _0x26a742;if(_0x389993>=_0x1f422a){_0x389993=_0x1f422a;_0x26a742=0xc8;}else{_0x26a742=0xce;}_0x245a04[_0x10fb('0x19')](_0x26a742);return _0x245a04[_0x10fb('0x20')](_0x10fb('0x21'),_0x3618db+'-'+_0x389993+'/'+_0x1f422a)[_0x10fb('0x1b')](_0xafb92e);}return null;};}function patchUpdates(_0x2909c0){return function(_0x3b6e95){try{jsonpatch[_0x10fb('0x22')](_0x3b6e95,_0x2909c0,!![]);}catch(_0x2c4904){return BPromise[_0x10fb('0x23')](_0x2c4904);}return _0x3b6e95[_0x10fb('0x24')]();};}function saveUpdates(_0x5f0d1b,_0x8f2eb5){return function(_0x378a69){if(_0x378a69){return _0x378a69[_0x10fb('0x25')](_0x5f0d1b)[_0x10fb('0x26')](function(_0x334eda){return _0x334eda;});}return null;};}function removeEntity(_0x13a100,_0xbfb3b8){return function(_0xb9a1da){if(_0xb9a1da){return _0xb9a1da[_0x10fb('0x27')]()[_0x10fb('0x26')](function(){var _0x2b1e59=_0xb9a1da[_0x10fb('0x28')]({'plain':!![]});var _0x3eaa02=_0x10fb('0x29');return db['UserProfileResource'][_0x10fb('0x27')]({'where':{'type':_0x3eaa02,'resourceId':_0x2b1e59['id']}})['then'](function(){return _0xb9a1da;});})[_0x10fb('0x26')](function(){_0x13a100[_0x10fb('0x19')](0xcc)['end']();});}};}function handleEntityNotFound(_0xac09b6,_0x46c33c){return function(_0x4171b6){if(!_0x4171b6){_0xac09b6[_0x10fb('0x18')](0x194);}return _0x4171b6;};}function handleError(_0x1ec224,_0x17fd94){_0x17fd94=_0x17fd94||0x1f4;return function(_0x4af9fa){logger['error'](_0x4af9fa[_0x10fb('0x2a')]);if(_0x4af9fa[_0x10fb('0x2b')]){delete _0x4af9fa[_0x10fb('0x2b')];}_0x1ec224[_0x10fb('0x19')](_0x17fd94)[_0x10fb('0x2c')](_0x4af9fa);};}exports[_0x10fb('0x2d')]=function(_0x521fe7,_0x2a99e0){var _0x496d6e={},_0x366b35={},_0x45ef3c={'count':0x0,'rows':[]};var _0x531417=_[_0x10fb('0x2e')](db['AnalyticExtractedReport']['rawAttributes'],function(_0x49b53f){return{'name':_0x49b53f[_0x10fb('0x2f')],'type':_0x49b53f['type']['key']};});_0x366b35[_0x10fb('0x30')]=_[_0x10fb('0x2e')](_0x531417,'name');_0x366b35[_0x10fb('0x31')]=_[_0x10fb('0x32')](_0x521fe7[_0x10fb('0x31')]);_0x366b35[_0x10fb('0x33')]=_[_0x10fb('0x34')](_0x366b35['model'],_0x366b35[_0x10fb('0x31')]);_0x496d6e[_0x10fb('0x35')]=_[_0x10fb('0x34')](_0x366b35[_0x10fb('0x30')],qs[_0x10fb('0x36')](_0x521fe7[_0x10fb('0x31')]['fields']));_0x496d6e[_0x10fb('0x35')]=_0x496d6e[_0x10fb('0x35')]['length']?_0x496d6e[_0x10fb('0x35')]:_0x366b35[_0x10fb('0x30')];if(!_0x521fe7[_0x10fb('0x31')][_0x10fb('0x37')](_0x10fb('0x38'))){_0x496d6e[_0x10fb('0x1e')]=qs[_0x10fb('0x1e')](_0x521fe7[_0x10fb('0x31')][_0x10fb('0x1e')]);_0x496d6e[_0x10fb('0x1c')]=qs[_0x10fb('0x1c')](_0x521fe7['query'][_0x10fb('0x1c')]);}_0x496d6e[_0x10fb('0x39')]=qs['sort'](_0x521fe7[_0x10fb('0x31')]['sort']);_0x496d6e[_0x10fb('0x3a')]=qs['filters'](_[_0x10fb('0x3b')](_0x521fe7[_0x10fb('0x31')],_0x366b35[_0x10fb('0x33')]),_0x531417);if(_0x521fe7[_0x10fb('0x31')]['filter']){_0x496d6e[_0x10fb('0x3a')]=_['merge'](_0x496d6e[_0x10fb('0x3a')],{'$or':_[_0x10fb('0x2e')](_0x531417,function(_0x1841fe){if(_0x1841fe[_0x10fb('0x3c')]!=='VIRTUAL'){var _0x5283dd={};_0x5283dd[_0x1841fe[_0x10fb('0x2b')]]={'$like':'%'+_0x521fe7['query']['filter']+'%'};return _0x5283dd;}})});}_0x496d6e=_['merge']({},_0x496d6e,_0x521fe7['options']);var _0x3507fd={'where':_0x496d6e[_0x10fb('0x3a')]};return db['AnalyticExtractedReport'][_0x10fb('0x1f')](_0x3507fd)[_0x10fb('0x26')](function(_0x323225){_0x45ef3c['count']=_0x323225;if(_0x521fe7['query'][_0x10fb('0x3d')]){_0x496d6e['include']=[{'all':!![]}];}return db['AnalyticExtractedReport']['findAll'](_0x496d6e);})[_0x10fb('0x26')](function(_0x21123c){_0x45ef3c[_0x10fb('0x3e')]=_0x21123c;return _0x45ef3c;})['then'](respondWithFilteredResult(_0x2a99e0,_0x496d6e))[_0x10fb('0x3f')](handleError(_0x2a99e0,null));};exports['show']=function(_0x296e93,_0x1095f3){var _0x358394={'raw':!![],'where':{'id':_0x296e93[_0x10fb('0x40')]['id']}},_0x2dece7={};_0x2dece7[_0x10fb('0x30')]=_[_0x10fb('0x32')](db[_0x10fb('0x41')]['rawAttributes']);_0x2dece7[_0x10fb('0x31')]=_[_0x10fb('0x32')](_0x296e93[_0x10fb('0x31')]);_0x2dece7[_0x10fb('0x33')]=_[_0x10fb('0x34')](_0x2dece7['model'],_0x2dece7[_0x10fb('0x31')]);_0x358394[_0x10fb('0x35')]=_[_0x10fb('0x34')](_0x2dece7[_0x10fb('0x30')],qs[_0x10fb('0x36')](_0x296e93[_0x10fb('0x31')]['fields']));_0x358394[_0x10fb('0x35')]=_0x358394[_0x10fb('0x35')][_0x10fb('0x42')]?_0x358394[_0x10fb('0x35')]:_0x2dece7[_0x10fb('0x30')];if(_0x296e93[_0x10fb('0x31')][_0x10fb('0x3d')]){_0x358394[_0x10fb('0x43')]=[{'all':!![]}];}_0x358394=_[_0x10fb('0x44')]({},_0x358394,_0x296e93[_0x10fb('0x45')]);return db[_0x10fb('0x41')]['find'](_0x358394)[_0x10fb('0x26')](handleEntityNotFound(_0x1095f3,null))[_0x10fb('0x26')](respondWithResult(_0x1095f3,null))[_0x10fb('0x3f')](handleError(_0x1095f3,null));};exports[_0x10fb('0x46')]=function(_0x414ed7,_0x125e0e){return db[_0x10fb('0x41')][_0x10fb('0x46')](_0x414ed7['body'],{})['then'](function(_0x406ee6){var _0x20d423=_0x414ed7[_0x10fb('0x47')][_0x10fb('0x28')]({'plain':!![]});if(!_0x20d423)throw new Error(_0x10fb('0x48'));if(_0x20d423['role']===_0x10fb('0x47')){var _0x17aa80=_0x406ee6[_0x10fb('0x28')]({'plain':!![]});var _0x4a9778='ExtractedReports';return db[_0x10fb('0x49')][_0x10fb('0x4a')]({'where':{'name':_0x4a9778,'userProfileId':_0x20d423[_0x10fb('0x4b')]},'raw':!![]})[_0x10fb('0x26')](function(_0x2337d0){if(_0x2337d0&&_0x2337d0[_0x10fb('0x4c')]===0x0){return db['UserProfileResource']['create']({'name':_0x17aa80[_0x10fb('0x2b')],'resourceId':_0x17aa80['id'],'type':_0x2337d0[_0x10fb('0x2b')],'sectionId':_0x2337d0['id']},{})[_0x10fb('0x26')](function(){return _0x406ee6;});}else{return _0x406ee6;}})[_0x10fb('0x3f')](function(_0x3bea93){logger[_0x10fb('0x4d')]('Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s',_0x3bea93);throw _0x3bea93;});}return _0x406ee6;})['then'](respondWithResult(_0x125e0e,0xc9))[_0x10fb('0x3f')](handleError(_0x125e0e,null));};exports[_0x10fb('0x25')]=function(_0x4df615,_0x28580f){if(_0x4df615[_0x10fb('0x4e')]['id']){delete _0x4df615[_0x10fb('0x4e')]['id'];}return db[_0x10fb('0x41')][_0x10fb('0x4a')]({'where':{'id':_0x4df615[_0x10fb('0x40')]['id']}})[_0x10fb('0x26')](handleEntityNotFound(_0x28580f,null))[_0x10fb('0x26')](saveUpdates(_0x4df615['body'],null))[_0x10fb('0x26')](respondWithResult(_0x28580f,null))[_0x10fb('0x3f')](handleError(_0x28580f,null));};exports[_0x10fb('0x27')]=function(_0x5a043c,_0x3c9796){return db[_0x10fb('0x41')]['find']({'where':{'id':_0x5a043c[_0x10fb('0x40')]['id']}})['then'](handleEntityNotFound(_0x3c9796,null))[_0x10fb('0x26')](removeEntity(_0x3c9796,null))[_0x10fb('0x3f')](handleError(_0x3c9796,null));};exports[_0x10fb('0x4f')]=function(_0x1ecf9a,_0x35c59e,_0x2b2f98){return db[_0x10fb('0x41')]['find']({'where':{'id':_0x1ecf9a[_0x10fb('0x40')]['id']},'attributes':['name',_0x10fb('0x50'),_0x10fb('0x51'),'output'],'raw':!![]})[_0x10fb('0x26')](handleEntityNotFound(_0x35c59e,null))[_0x10fb('0x26')](function(_0x43ed19){if(_0x43ed19){if(!fs[_0x10fb('0x52')](path['join'](config['root'],'server/files/reports',_0x43ed19[_0x10fb('0x51')]))){throw new db[(_0x10fb('0x53'))][(_0x10fb('0x54'))](_0x10fb('0x55'));}if(_0x1ecf9a[_0x10fb('0x31')]['exists']){return _0x35c59e[_0x10fb('0x18')](0xc8);}return _0x35c59e[_0x10fb('0x4f')](path[_0x10fb('0x56')](config['root'],_0x10fb('0x57'),_0x43ed19['savename']),util['format'](_0x10fb('0x58'),_0x43ed19[_0x10fb('0x50')]||_0x43ed19[_0x10fb('0x2b')],_0x43ed19[_0x10fb('0x59')][_0x10fb('0x5a')]()));}})[_0x10fb('0x3f')](handleError(_0x35c59e,null));};