05f6b01fd008555832b00871253f51413bcd9d3d
[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 _0x6f73=['get','UserProfileResource','end','error','name','send','index','AnalyticExtractedReport','rawAttributes','fieldName','type','key','model','query','keys','filters','attributes','fields','length','hasOwnProperty','nolimit','limit','order','sort','where','pick','VIRTUAL','filter','merge','options','includeAll','findAll','rows','show','params','intersection','find','catch','create','user','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','ExtractedReports','UserProfileSection','userProfileId','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','body','download','basename','savename','output','existsSync','join','server/files/reports','Sequelize','Report\x20not\x20found\x20or\x20empty','exists','root','format','%s.%s','toLowerCase','html-pdf','rimraf','zip-dir','fast-json-patch','request-promise','bluebird','mustache','util','path','to-csv','ejs','lodash','squel','crypto','desk.js','querystring','papaparse','ioredis','../../components/auth/service','../../config/logger','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','sendStatus','status','json','count','offset','Content-Range','apply','reject','save','update','destroy','then'];(function(_0x1694f5,_0x49b7c3){var _0x5b4321=function(_0x58c183){while(--_0x58c183){_0x1694f5['push'](_0x1694f5['shift']());}};_0x5b4321(++_0x49b7c3);}(_0x6f73,0x164));var _0x36f7=function(_0xd2c445,_0x3c5b7f){_0xd2c445=_0xd2c445-0x0;var _0x215bf7=_0x6f73[_0xd2c445];return _0x215bf7;};'use strict';var pdf=require(_0x36f7('0x0'));var emlformat=require('eml-format');var rimraf=require(_0x36f7('0x1'));var zipdir=require(_0x36f7('0x2'));var jsonpatch=require(_0x36f7('0x3'));var rp=require(_0x36f7('0x4'));var moment=require('moment');var BPromise=require(_0x36f7('0x5'));var Mustache=require(_0x36f7('0x6'));var util=require(_0x36f7('0x7'));var path=require(_0x36f7('0x8'));var sox=require('sox');var csv=require(_0x36f7('0x9'));var ejs=require(_0x36f7('0xa'));var fs=require('fs');var _=require(_0x36f7('0xb'));var squel=require(_0x36f7('0xc'));var crypto=require(_0x36f7('0xd'));var jsforce=require('jsforce');var deskjs=require(_0x36f7('0xe'));var toCsv=require(_0x36f7('0x9'));var querystring=require(_0x36f7('0xf'));var Papa=require(_0x36f7('0x10'));var Redis=require(_0x36f7('0x11'));var authService=require(_0x36f7('0x12'));var qs=require('../../components/parsers/qs');var as=require('../../components/parsers/advancedSearch');var hardwareService=require('../../config/license/hardware');var logger=require(_0x36f7('0x13'))('api');var utils=require(_0x36f7('0x14'));var config=require(_0x36f7('0x15'));var licenseUtil=require(_0x36f7('0x16'));var db=require(_0x36f7('0x17'))['db'];function respondWithStatusCode(_0x243084,_0x1c3271){_0x1c3271=_0x1c3271||0xcc;return function(_0x1fb13e){if(_0x1fb13e){return _0x243084[_0x36f7('0x18')](_0x1c3271);}return _0x243084[_0x36f7('0x19')](_0x1c3271)['end']();};}function respondWithResult(_0x422d9f,_0x256836){_0x256836=_0x256836||0xc8;return function(_0x3dfca8){if(_0x3dfca8){return _0x422d9f[_0x36f7('0x19')](_0x256836)[_0x36f7('0x1a')](_0x3dfca8);}};}function respondWithFilteredResult(_0xe500c7,_0x34f7bc){return function(_0x664a72){if(_0x664a72){var _0x35694e=_0x664a72[_0x36f7('0x1b')],_0x34a889=_0x34f7bc[_0x36f7('0x1c')],_0x3b3b99=_0x34f7bc[_0x36f7('0x1c')]+_0x34f7bc['limit'],_0x3d300c;if(_0x3b3b99>=_0x35694e){_0x3b3b99=_0x35694e;_0x3d300c=0xc8;}else{_0x3d300c=0xce;}_0xe500c7[_0x36f7('0x19')](_0x3d300c);return _0xe500c7['set'](_0x36f7('0x1d'),_0x34a889+'-'+_0x3b3b99+'/'+_0x35694e)[_0x36f7('0x1a')](_0x664a72);}return null;};}function patchUpdates(_0x1761c4){return function(_0x5ba3ce){try{jsonpatch[_0x36f7('0x1e')](_0x5ba3ce,_0x1761c4,!![]);}catch(_0xad4527){return BPromise[_0x36f7('0x1f')](_0xad4527);}return _0x5ba3ce[_0x36f7('0x20')]();};}function saveUpdates(_0xf27351,_0x2672e6){return function(_0x497870){if(_0x497870){return _0x497870[_0x36f7('0x21')](_0xf27351)['then'](function(_0x318db1){return _0x318db1;});}return null;};}function removeEntity(_0xce2c4c,_0x44a956){return function(_0x12b270){if(_0x12b270){return _0x12b270[_0x36f7('0x22')]()[_0x36f7('0x23')](function(){var _0x318fa1=_0x12b270[_0x36f7('0x24')]({'plain':!![]});var _0x17b0dc='ExtractedReports';return db[_0x36f7('0x25')][_0x36f7('0x22')]({'where':{'type':_0x17b0dc,'resourceId':_0x318fa1['id']}})[_0x36f7('0x23')](function(){return _0x12b270;});})[_0x36f7('0x23')](function(){_0xce2c4c[_0x36f7('0x19')](0xcc)[_0x36f7('0x26')]();});}};}function handleEntityNotFound(_0x2be592,_0x3854c5){return function(_0x547629){if(!_0x547629){_0x2be592[_0x36f7('0x18')](0x194);}return _0x547629;};}function handleError(_0x271547,_0x359821){_0x359821=_0x359821||0x1f4;return function(_0x5581fe){logger[_0x36f7('0x27')](_0x5581fe['stack']);if(_0x5581fe[_0x36f7('0x28')]){delete _0x5581fe[_0x36f7('0x28')];}_0x271547[_0x36f7('0x19')](_0x359821)[_0x36f7('0x29')](_0x5581fe);};}exports[_0x36f7('0x2a')]=function(_0x4f2254,_0x139bfa){var _0x5ba7c2={},_0x5288e3={},_0x1e6fae={'count':0x0,'rows':[]};var _0x1744ec=_['map'](db[_0x36f7('0x2b')][_0x36f7('0x2c')],function(_0x2a5e3d){return{'name':_0x2a5e3d[_0x36f7('0x2d')],'type':_0x2a5e3d[_0x36f7('0x2e')][_0x36f7('0x2f')]};});_0x5288e3[_0x36f7('0x30')]=_['map'](_0x1744ec,_0x36f7('0x28'));_0x5288e3[_0x36f7('0x31')]=_[_0x36f7('0x32')](_0x4f2254['query']);_0x5288e3[_0x36f7('0x33')]=_['intersection'](_0x5288e3[_0x36f7('0x30')],_0x5288e3[_0x36f7('0x31')]);_0x5ba7c2[_0x36f7('0x34')]=_['intersection'](_0x5288e3[_0x36f7('0x30')],qs[_0x36f7('0x35')](_0x4f2254[_0x36f7('0x31')][_0x36f7('0x35')]));_0x5ba7c2[_0x36f7('0x34')]=_0x5ba7c2[_0x36f7('0x34')][_0x36f7('0x36')]?_0x5ba7c2[_0x36f7('0x34')]:_0x5288e3['model'];if(!_0x4f2254[_0x36f7('0x31')][_0x36f7('0x37')](_0x36f7('0x38'))){_0x5ba7c2[_0x36f7('0x39')]=qs['limit'](_0x4f2254[_0x36f7('0x31')][_0x36f7('0x39')]);_0x5ba7c2[_0x36f7('0x1c')]=qs[_0x36f7('0x1c')](_0x4f2254[_0x36f7('0x31')]['offset']);}_0x5ba7c2[_0x36f7('0x3a')]=qs[_0x36f7('0x3b')](_0x4f2254['query'][_0x36f7('0x3b')]);_0x5ba7c2[_0x36f7('0x3c')]=qs['filters'](_[_0x36f7('0x3d')](_0x4f2254['query'],_0x5288e3[_0x36f7('0x33')]),_0x1744ec);if(_0x4f2254[_0x36f7('0x31')]['filter']){_0x5ba7c2[_0x36f7('0x3c')]=_['merge'](_0x5ba7c2[_0x36f7('0x3c')],{'$or':_['map'](_0x1744ec,function(_0x18560e){if(_0x18560e['type']!==_0x36f7('0x3e')){var _0x3188be={};_0x3188be[_0x18560e[_0x36f7('0x28')]]={'$like':'%'+_0x4f2254[_0x36f7('0x31')][_0x36f7('0x3f')]+'%'};return _0x3188be;}})});}_0x5ba7c2=_[_0x36f7('0x40')]({},_0x5ba7c2,_0x4f2254[_0x36f7('0x41')]);var _0x45ffad={'where':_0x5ba7c2[_0x36f7('0x3c')]};return db['AnalyticExtractedReport'][_0x36f7('0x1b')](_0x45ffad)['then'](function(_0x37d45e){_0x1e6fae['count']=_0x37d45e;if(_0x4f2254[_0x36f7('0x31')][_0x36f7('0x42')]){_0x5ba7c2['include']=[{'all':!![]}];}return db[_0x36f7('0x2b')][_0x36f7('0x43')](_0x5ba7c2);})[_0x36f7('0x23')](function(_0x4f0a29){_0x1e6fae[_0x36f7('0x44')]=_0x4f0a29;return _0x1e6fae;})[_0x36f7('0x23')](respondWithFilteredResult(_0x139bfa,_0x5ba7c2))['catch'](handleError(_0x139bfa,null));};exports[_0x36f7('0x45')]=function(_0x484c8f,_0x5b17a5){var _0x3ed35e={'raw':!![],'where':{'id':_0x484c8f[_0x36f7('0x46')]['id']}},_0xc5cadf={};_0xc5cadf[_0x36f7('0x30')]=_[_0x36f7('0x32')](db[_0x36f7('0x2b')][_0x36f7('0x2c')]);_0xc5cadf['query']=_['keys'](_0x484c8f[_0x36f7('0x31')]);_0xc5cadf[_0x36f7('0x33')]=_['intersection'](_0xc5cadf[_0x36f7('0x30')],_0xc5cadf[_0x36f7('0x31')]);_0x3ed35e[_0x36f7('0x34')]=_[_0x36f7('0x47')](_0xc5cadf['model'],qs[_0x36f7('0x35')](_0x484c8f[_0x36f7('0x31')][_0x36f7('0x35')]));_0x3ed35e[_0x36f7('0x34')]=_0x3ed35e[_0x36f7('0x34')][_0x36f7('0x36')]?_0x3ed35e[_0x36f7('0x34')]:_0xc5cadf[_0x36f7('0x30')];if(_0x484c8f[_0x36f7('0x31')][_0x36f7('0x42')]){_0x3ed35e['include']=[{'all':!![]}];}_0x3ed35e=_[_0x36f7('0x40')]({},_0x3ed35e,_0x484c8f[_0x36f7('0x41')]);return db['AnalyticExtractedReport'][_0x36f7('0x48')](_0x3ed35e)[_0x36f7('0x23')](handleEntityNotFound(_0x5b17a5,null))[_0x36f7('0x23')](respondWithResult(_0x5b17a5,null))[_0x36f7('0x49')](handleError(_0x5b17a5,null));};exports[_0x36f7('0x4a')]=function(_0x4d0b42,_0xfd6dab){return db[_0x36f7('0x2b')][_0x36f7('0x4a')](_0x4d0b42['body'],{})['then'](function(_0x54786e){var _0x3bd0bc=_0x4d0b42[_0x36f7('0x4b')][_0x36f7('0x24')]({'plain':!![]});if(!_0x3bd0bc)throw new Error(_0x36f7('0x4c'));if(_0x3bd0bc[_0x36f7('0x4d')]==='user'){var _0x26c67d=_0x54786e[_0x36f7('0x24')]({'plain':!![]});var _0x1d0d55=_0x36f7('0x4e');return db[_0x36f7('0x4f')][_0x36f7('0x48')]({'where':{'name':_0x1d0d55,'userProfileId':_0x3bd0bc[_0x36f7('0x50')]},'raw':!![]})[_0x36f7('0x23')](function(_0x22ebcc){if(_0x22ebcc&&_0x22ebcc[_0x36f7('0x51')]===0x0){return db['UserProfileResource'][_0x36f7('0x4a')]({'name':_0x26c67d[_0x36f7('0x28')],'resourceId':_0x26c67d['id'],'type':_0x22ebcc[_0x36f7('0x28')],'sectionId':_0x22ebcc['id']},{})[_0x36f7('0x23')](function(){return _0x54786e;});}else{return _0x54786e;}})['catch'](function(_0x265496){logger['error'](_0x36f7('0x52'),_0x265496);throw _0x265496;});}return _0x54786e;})['then'](respondWithResult(_0xfd6dab,0xc9))[_0x36f7('0x49')](handleError(_0xfd6dab,null));};exports['update']=function(_0x304210,_0x59aed2){if(_0x304210['body']['id']){delete _0x304210[_0x36f7('0x53')]['id'];}return db['AnalyticExtractedReport'][_0x36f7('0x48')]({'where':{'id':_0x304210[_0x36f7('0x46')]['id']}})[_0x36f7('0x23')](handleEntityNotFound(_0x59aed2,null))[_0x36f7('0x23')](saveUpdates(_0x304210[_0x36f7('0x53')],null))['then'](respondWithResult(_0x59aed2,null))[_0x36f7('0x49')](handleError(_0x59aed2,null));};exports[_0x36f7('0x22')]=function(_0x7d4d44,_0x3a7869){return db[_0x36f7('0x2b')][_0x36f7('0x48')]({'where':{'id':_0x7d4d44[_0x36f7('0x46')]['id']}})[_0x36f7('0x23')](handleEntityNotFound(_0x3a7869,null))[_0x36f7('0x23')](removeEntity(_0x3a7869,null))[_0x36f7('0x49')](handleError(_0x3a7869,null));};exports[_0x36f7('0x54')]=function(_0x595c2f,_0x308e67,_0x22eddb){return db[_0x36f7('0x2b')]['find']({'where':{'id':_0x595c2f[_0x36f7('0x46')]['id']},'attributes':[_0x36f7('0x28'),_0x36f7('0x55'),_0x36f7('0x56'),_0x36f7('0x57')],'raw':!![]})[_0x36f7('0x23')](handleEntityNotFound(_0x308e67,null))['then'](function(_0x4bf118){if(_0x4bf118){if(!fs[_0x36f7('0x58')](path[_0x36f7('0x59')](config['root'],_0x36f7('0x5a'),_0x4bf118[_0x36f7('0x56')]))){throw new db[(_0x36f7('0x5b'))]['ValidationError'](_0x36f7('0x5c'));}if(_0x595c2f['query'][_0x36f7('0x5d')]){return _0x308e67[_0x36f7('0x18')](0xc8);}return _0x308e67['download'](path['join'](config[_0x36f7('0x5e')],_0x36f7('0x5a'),_0x4bf118[_0x36f7('0x56')]),util[_0x36f7('0x5f')](_0x36f7('0x60'),_0x4bf118[_0x36f7('0x55')]||_0x4bf118[_0x36f7('0x28')],_0x4bf118[_0x36f7('0x57')][_0x36f7('0x61')]()));}})[_0x36f7('0x49')](handleError(_0x308e67,null));};