5393246db4e6edfa30c65cef0eaf41247415d017
[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 _0x78f2=['length','nolimit','order','sort','pick','filter','where','VIRTUAL','merge','options','AnalyticExtractedReport','includeAll','findAll','rows','catch','show','params','find','create','body','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','user','UserProfileSection','userProfileId','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','basename','savename','output','existsSync','join','root','server/files/reports','Sequelize','ValidationError','Report\x20not\x20found\x20or\x20empty','exists','download','format','toLowerCase','rimraf','zip-dir','fast-json-patch','request-promise','moment','bluebird','to-csv','ejs','fs-extra','lodash','crypto','jsforce','papaparse','../../components/auth/service','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/utils','../../config/environment','../../config/license/util','sendStatus','end','status','json','offset','limit','undefined','count','set','Content-Range','reject','save','update','then','destroy','get','ExtractedReports','UserProfileResource','error','stack','name','send','index','map','rawAttributes','key','model','query','keys','filters','intersection','attributes','fields'];(function(_0x2a8841,_0x355633){var _0x164f64=function(_0x5f29e2){while(--_0x5f29e2){_0x2a8841['push'](_0x2a8841['shift']());}};_0x164f64(++_0x355633);}(_0x78f2,0x86));var _0x278f=function(_0x12addf,_0x4901af){_0x12addf=_0x12addf-0x0;var _0x5713ac=_0x78f2[_0x12addf];return _0x5713ac;};'use strict';var emlformat=require('eml-format');var rimraf=require(_0x278f('0x0'));var zipdir=require(_0x278f('0x1'));var jsonpatch=require(_0x278f('0x2'));var rp=require(_0x278f('0x3'));var moment=require(_0x278f('0x4'));var BPromise=require(_0x278f('0x5'));var Mustache=require('mustache');var util=require('util');var path=require('path');var sox=require('sox');var csv=require(_0x278f('0x6'));var ejs=require(_0x278f('0x7'));var fs=require('fs');var fs_extra=require(_0x278f('0x8'));var _=require(_0x278f('0x9'));var squel=require('squel');var crypto=require(_0x278f('0xa'));var jsforce=require(_0x278f('0xb'));var deskjs=require('desk.js');var toCsv=require(_0x278f('0x6'));var querystring=require('querystring');var Papa=require(_0x278f('0xc'));var Redis=require('ioredis');var authService=require(_0x278f('0xd'));var qs=require('../../components/parsers/qs');var as=require(_0x278f('0xe'));var hardwareService=require(_0x278f('0xf'));var logger=require('../../config/logger')('api');var utils=require(_0x278f('0x10'));var config=require(_0x278f('0x11'));var licenseUtil=require(_0x278f('0x12'));var db=require('../../mysqldb')['db'];function respondWithStatusCode(_0x26da91,_0x5b4b07){_0x5b4b07=_0x5b4b07||0xcc;return function(_0x4ff9e7){if(_0x4ff9e7){return _0x26da91[_0x278f('0x13')](_0x5b4b07);}return _0x26da91['status'](_0x5b4b07)[_0x278f('0x14')]();};}function respondWithResult(_0x5cb076,_0x4f086f){_0x4f086f=_0x4f086f||0xc8;return function(_0x3961e6){if(_0x3961e6){return _0x5cb076[_0x278f('0x15')](_0x4f086f)[_0x278f('0x16')](_0x3961e6);}};}function respondWithFilteredResult(_0x1bb854,_0x5b9b91){return function(_0x141916){if(_0x141916){var _0x155a42=typeof _0x5b9b91[_0x278f('0x17')]==='undefined'&&typeof _0x5b9b91[_0x278f('0x18')]===_0x278f('0x19');var _0x12b422=_0x141916[_0x278f('0x1a')];var _0x29fde2=_0x155a42?0x0:_0x5b9b91[_0x278f('0x17')];var _0x4efd2c=_0x155a42?_0x141916[_0x278f('0x1a')]:_0x5b9b91[_0x278f('0x17')]+_0x5b9b91[_0x278f('0x18')];var _0x1d96d5;if(_0x4efd2c>=_0x12b422){_0x4efd2c=_0x12b422;_0x1d96d5=0xc8;}else{_0x1d96d5=0xce;}_0x1bb854[_0x278f('0x15')](_0x1d96d5);return _0x1bb854[_0x278f('0x1b')](_0x278f('0x1c'),_0x29fde2+'-'+_0x4efd2c+'/'+_0x12b422)[_0x278f('0x16')](_0x141916);}return null;};}function patchUpdates(_0x4ac181){return function(_0x236b04){try{jsonpatch['apply'](_0x236b04,_0x4ac181,!![]);}catch(_0x19dba3){return BPromise[_0x278f('0x1d')](_0x19dba3);}return _0x236b04[_0x278f('0x1e')]();};}function saveUpdates(_0x3751a9,_0x1a8c37){return function(_0x5d0cf4){if(_0x5d0cf4){return _0x5d0cf4[_0x278f('0x1f')](_0x3751a9)[_0x278f('0x20')](function(_0x2d428e){return _0x2d428e;});}return null;};}function removeEntity(_0x427b53,_0x3c4445){return function(_0x17ce6c){if(_0x17ce6c){return _0x17ce6c[_0x278f('0x21')]()['then'](function(){var _0x18de2e=_0x17ce6c[_0x278f('0x22')]({'plain':!![]});var _0x53b49f=_0x278f('0x23');return db[_0x278f('0x24')][_0x278f('0x21')]({'where':{'type':_0x53b49f,'resourceId':_0x18de2e['id']}})[_0x278f('0x20')](function(){return _0x17ce6c;});})[_0x278f('0x20')](function(){_0x427b53['status'](0xcc)[_0x278f('0x14')]();});}};}function handleEntityNotFound(_0x295c33,_0x5a8a07){return function(_0xcc575a){if(!_0xcc575a){_0x295c33[_0x278f('0x13')](0x194);}return _0xcc575a;};}function handleError(_0x480ccd,_0x437cdb){_0x437cdb=_0x437cdb||0x1f4;return function(_0x42b3f3){logger[_0x278f('0x25')](_0x42b3f3[_0x278f('0x26')]);if(_0x42b3f3[_0x278f('0x27')]){delete _0x42b3f3[_0x278f('0x27')];}_0x480ccd[_0x278f('0x15')](_0x437cdb)[_0x278f('0x28')](_0x42b3f3);};}exports[_0x278f('0x29')]=function(_0xae258a,_0x6a8f26){var _0xabd0a8={},_0x2e6b84={},_0x2ac072={'count':0x0,'rows':[]};var _0x2fe9ec=_[_0x278f('0x2a')](db['AnalyticExtractedReport'][_0x278f('0x2b')],function(_0x4c6e57){return{'name':_0x4c6e57['fieldName'],'type':_0x4c6e57['type'][_0x278f('0x2c')]};});_0x2e6b84[_0x278f('0x2d')]=_['map'](_0x2fe9ec,_0x278f('0x27'));_0x2e6b84[_0x278f('0x2e')]=_[_0x278f('0x2f')](_0xae258a[_0x278f('0x2e')]);_0x2e6b84[_0x278f('0x30')]=_[_0x278f('0x31')](_0x2e6b84['model'],_0x2e6b84[_0x278f('0x2e')]);_0xabd0a8[_0x278f('0x32')]=_['intersection'](_0x2e6b84[_0x278f('0x2d')],qs[_0x278f('0x33')](_0xae258a[_0x278f('0x2e')][_0x278f('0x33')]));_0xabd0a8['attributes']=_0xabd0a8[_0x278f('0x32')][_0x278f('0x34')]?_0xabd0a8[_0x278f('0x32')]:_0x2e6b84[_0x278f('0x2d')];if(!_0xae258a[_0x278f('0x2e')]['hasOwnProperty'](_0x278f('0x35'))){_0xabd0a8[_0x278f('0x18')]=qs[_0x278f('0x18')](_0xae258a[_0x278f('0x2e')][_0x278f('0x18')]);_0xabd0a8[_0x278f('0x17')]=qs[_0x278f('0x17')](_0xae258a[_0x278f('0x2e')][_0x278f('0x17')]);}_0xabd0a8[_0x278f('0x36')]=qs[_0x278f('0x37')](_0xae258a[_0x278f('0x2e')][_0x278f('0x37')]);_0xabd0a8['where']=qs[_0x278f('0x30')](_[_0x278f('0x38')](_0xae258a['query'],_0x2e6b84['filters']),_0x2fe9ec);if(_0xae258a[_0x278f('0x2e')][_0x278f('0x39')]){_0xabd0a8[_0x278f('0x3a')]=_['merge'](_0xabd0a8[_0x278f('0x3a')],{'$or':_[_0x278f('0x2a')](_0x2fe9ec,function(_0xe65dbe){if(_0xe65dbe['type']!==_0x278f('0x3b')){var _0x27830a={};_0x27830a[_0xe65dbe[_0x278f('0x27')]]={'$like':'%'+_0xae258a[_0x278f('0x2e')][_0x278f('0x39')]+'%'};return _0x27830a;}})});}_0xabd0a8=_[_0x278f('0x3c')]({},_0xabd0a8,_0xae258a[_0x278f('0x3d')]);var _0x1183f0={'where':_0xabd0a8['where']};return db[_0x278f('0x3e')][_0x278f('0x1a')](_0x1183f0)[_0x278f('0x20')](function(_0xa788d1){_0x2ac072['count']=_0xa788d1;if(_0xae258a[_0x278f('0x2e')][_0x278f('0x3f')]){_0xabd0a8['include']=[{'all':!![]}];}return db[_0x278f('0x3e')][_0x278f('0x40')](_0xabd0a8);})['then'](function(_0x10000f){_0x2ac072[_0x278f('0x41')]=_0x10000f;return _0x2ac072;})[_0x278f('0x20')](respondWithFilteredResult(_0x6a8f26,_0xabd0a8))[_0x278f('0x42')](handleError(_0x6a8f26,null));};exports[_0x278f('0x43')]=function(_0x50a1f5,_0x6687d1){var _0x5bd5c1={'raw':!![],'where':{'id':_0x50a1f5[_0x278f('0x44')]['id']}},_0x13ec98={};_0x13ec98[_0x278f('0x2d')]=_[_0x278f('0x2f')](db['AnalyticExtractedReport'][_0x278f('0x2b')]);_0x13ec98[_0x278f('0x2e')]=_[_0x278f('0x2f')](_0x50a1f5[_0x278f('0x2e')]);_0x13ec98[_0x278f('0x30')]=_[_0x278f('0x31')](_0x13ec98[_0x278f('0x2d')],_0x13ec98[_0x278f('0x2e')]);_0x5bd5c1['attributes']=_['intersection'](_0x13ec98['model'],qs[_0x278f('0x33')](_0x50a1f5[_0x278f('0x2e')][_0x278f('0x33')]));_0x5bd5c1['attributes']=_0x5bd5c1[_0x278f('0x32')][_0x278f('0x34')]?_0x5bd5c1[_0x278f('0x32')]:_0x13ec98[_0x278f('0x2d')];if(_0x50a1f5[_0x278f('0x2e')][_0x278f('0x3f')]){_0x5bd5c1['include']=[{'all':!![]}];}_0x5bd5c1=_[_0x278f('0x3c')]({},_0x5bd5c1,_0x50a1f5[_0x278f('0x3d')]);return db[_0x278f('0x3e')][_0x278f('0x45')](_0x5bd5c1)[_0x278f('0x20')](handleEntityNotFound(_0x6687d1,null))[_0x278f('0x20')](respondWithResult(_0x6687d1,null))['catch'](handleError(_0x6687d1,null));};exports[_0x278f('0x46')]=function(_0x4465bc,_0x404c1d){return db[_0x278f('0x3e')][_0x278f('0x46')](_0x4465bc[_0x278f('0x47')],{})['then'](function(_0x46e66c){var _0x5ac2f7=_0x4465bc['user'][_0x278f('0x22')]({'plain':!![]});if(!_0x5ac2f7)throw new Error(_0x278f('0x48'));if(_0x5ac2f7[_0x278f('0x49')]===_0x278f('0x4a')){var _0x467baa=_0x46e66c[_0x278f('0x22')]({'plain':!![]});var _0x550274=_0x278f('0x23');return db[_0x278f('0x4b')]['find']({'where':{'name':_0x550274,'userProfileId':_0x5ac2f7[_0x278f('0x4c')]},'raw':!![]})[_0x278f('0x20')](function(_0x34933e){if(_0x34933e&&_0x34933e[_0x278f('0x4d')]===0x0){return db[_0x278f('0x24')][_0x278f('0x46')]({'name':_0x467baa[_0x278f('0x27')],'resourceId':_0x467baa['id'],'type':_0x34933e[_0x278f('0x27')],'sectionId':_0x34933e['id']},{})[_0x278f('0x20')](function(){return _0x46e66c;});}else{return _0x46e66c;}})[_0x278f('0x42')](function(_0x5db30d){logger[_0x278f('0x25')](_0x278f('0x4e'),_0x5db30d);throw _0x5db30d;});}return _0x46e66c;})['then'](respondWithResult(_0x404c1d,0xc9))[_0x278f('0x42')](handleError(_0x404c1d,null));};exports[_0x278f('0x1f')]=function(_0x135530,_0x3197c7){if(_0x135530[_0x278f('0x47')]['id']){delete _0x135530[_0x278f('0x47')]['id'];}return db[_0x278f('0x3e')][_0x278f('0x45')]({'where':{'id':_0x135530['params']['id']}})['then'](handleEntityNotFound(_0x3197c7,null))[_0x278f('0x20')](saveUpdates(_0x135530['body'],null))[_0x278f('0x20')](respondWithResult(_0x3197c7,null))[_0x278f('0x42')](handleError(_0x3197c7,null));};exports[_0x278f('0x21')]=function(_0x1c0bfb,_0x266ae5){return db[_0x278f('0x3e')][_0x278f('0x45')]({'where':{'id':_0x1c0bfb['params']['id']}})[_0x278f('0x20')](handleEntityNotFound(_0x266ae5,null))[_0x278f('0x20')](removeEntity(_0x266ae5,null))[_0x278f('0x42')](handleError(_0x266ae5,null));};exports['download']=function(_0x47546f,_0x2ad986,_0x3dee39){return db[_0x278f('0x3e')][_0x278f('0x45')]({'where':{'id':_0x47546f[_0x278f('0x44')]['id']},'attributes':[_0x278f('0x27'),_0x278f('0x4f'),_0x278f('0x50'),_0x278f('0x51')],'raw':!![]})[_0x278f('0x20')](handleEntityNotFound(_0x2ad986,null))[_0x278f('0x20')](function(_0x2e3161){if(_0x2e3161){if(!fs[_0x278f('0x52')](path[_0x278f('0x53')](config[_0x278f('0x54')],_0x278f('0x55'),_0x2e3161[_0x278f('0x50')]))){throw new db[(_0x278f('0x56'))][(_0x278f('0x57'))](_0x278f('0x58'));}if(_0x47546f[_0x278f('0x2e')][_0x278f('0x59')]){return _0x2ad986[_0x278f('0x13')](0xc8);}return _0x2ad986[_0x278f('0x5a')](path[_0x278f('0x53')](config[_0x278f('0x54')],_0x278f('0x55'),_0x2e3161[_0x278f('0x50')]),util[_0x278f('0x5b')]('%s.%s',_0x2e3161[_0x278f('0x4f')]||_0x2e3161[_0x278f('0x27')],_0x2e3161[_0x278f('0x51')][_0x278f('0x5c')]()));}})[_0x278f('0x42')](handleError(_0x2ad986,null));};