Built motion from commit (unavailable).|2.5.11
[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 _0xf8b1=['then','destroy','UserProfileResource','end','error','stack','name','send','index','map','AnalyticExtractedReport','rawAttributes','type','key','query','keys','filters','intersection','model','fields','attributes','length','hasOwnProperty','nolimit','order','sort','where','pick','filter','merge','VIRTUAL','options','includeAll','include','rows','catch','show','params','create','body','user','get','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','find','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','download','savename','output','existsSync','join','server/files/reports','Sequelize','exists','root','format','%s.%s','basename','toLowerCase','html-pdf','rimraf','request-promise','bluebird','mustache','sox','ejs','crypto','jsforce','to-csv','querystring','papaparse','../../components/auth/service','../../components/parsers/qs','../../config/license/hardware','api','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','sendStatus','status','json','count','offset','limit','set','Content-Range','apply','reject','save','update'];(function(_0x4ecd60,_0x142e11){var _0x29e598=function(_0x2478e0){while(--_0x2478e0){_0x4ecd60['push'](_0x4ecd60['shift']());}};_0x29e598(++_0x142e11);}(_0xf8b1,0x1ac));var _0x1f8b=function(_0x336dbd,_0x21b342){_0x336dbd=_0x336dbd-0x0;var _0x5bcb06=_0xf8b1[_0x336dbd];return _0x5bcb06;};'use strict';var pdf=require(_0x1f8b('0x0'));var emlformat=require('eml-format');var rimraf=require(_0x1f8b('0x1'));var zipdir=require('zip-dir');var jsonpatch=require('fast-json-patch');var rp=require(_0x1f8b('0x2'));var moment=require('moment');var BPromise=require(_0x1f8b('0x3'));var Mustache=require(_0x1f8b('0x4'));var util=require('util');var path=require('path');var sox=require(_0x1f8b('0x5'));var csv=require('to-csv');var ejs=require(_0x1f8b('0x6'));var fs=require('fs');var _=require('lodash');var squel=require('squel');var crypto=require(_0x1f8b('0x7'));var jsforce=require(_0x1f8b('0x8'));var deskjs=require('desk.js');var toCsv=require(_0x1f8b('0x9'));var querystring=require(_0x1f8b('0xa'));var Papa=require(_0x1f8b('0xb'));var Redis=require('ioredis');var authService=require(_0x1f8b('0xc'));var qs=require(_0x1f8b('0xd'));var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0x1f8b('0xe'));var logger=require('../../config/logger')(_0x1f8b('0xf'));var utils=require(_0x1f8b('0x10'));var config=require(_0x1f8b('0x11'));var licenseUtil=require(_0x1f8b('0x12'));var db=require(_0x1f8b('0x13'))['db'];function respondWithStatusCode(_0x36af5d,_0x3d39b1){_0x3d39b1=_0x3d39b1||0xcc;return function(_0x2a11f4){if(_0x2a11f4){return _0x36af5d[_0x1f8b('0x14')](_0x3d39b1);}return _0x36af5d[_0x1f8b('0x15')](_0x3d39b1)['end']();};}function respondWithResult(_0x57a6ef,_0x253f14){_0x253f14=_0x253f14||0xc8;return function(_0x1473fe){if(_0x1473fe){return _0x57a6ef[_0x1f8b('0x15')](_0x253f14)[_0x1f8b('0x16')](_0x1473fe);}};}function respondWithFilteredResult(_0x16a11d,_0x1bea01){return function(_0x53c0c9){if(_0x53c0c9){var _0x12d150=_0x53c0c9[_0x1f8b('0x17')],_0x58436b=_0x1bea01[_0x1f8b('0x18')],_0x51a05e=_0x1bea01[_0x1f8b('0x18')]+_0x1bea01[_0x1f8b('0x19')],_0x5cdd7d;if(_0x51a05e>=_0x12d150){_0x51a05e=_0x12d150;_0x5cdd7d=0xc8;}else{_0x5cdd7d=0xce;}_0x16a11d['status'](_0x5cdd7d);return _0x16a11d[_0x1f8b('0x1a')](_0x1f8b('0x1b'),_0x58436b+'-'+_0x51a05e+'/'+_0x12d150)[_0x1f8b('0x16')](_0x53c0c9);}return null;};}function patchUpdates(_0x2d7b19){return function(_0x58f68d){try{jsonpatch[_0x1f8b('0x1c')](_0x58f68d,_0x2d7b19,!![]);}catch(_0x24819a){return BPromise[_0x1f8b('0x1d')](_0x24819a);}return _0x58f68d[_0x1f8b('0x1e')]();};}function saveUpdates(_0x21bc20,_0x1c255a){return function(_0x39f937){if(_0x39f937){return _0x39f937[_0x1f8b('0x1f')](_0x21bc20)[_0x1f8b('0x20')](function(_0x32f3b4){return _0x32f3b4;});}return null;};}function removeEntity(_0x97c36b,_0x2ed10d){return function(_0x4973a5){if(_0x4973a5){return _0x4973a5[_0x1f8b('0x21')]()[_0x1f8b('0x20')](function(){var _0x3e719c=_0x4973a5['get']({'plain':!![]});var _0x5bde8='ExtractedReports';return db[_0x1f8b('0x22')][_0x1f8b('0x21')]({'where':{'type':_0x5bde8,'resourceId':_0x3e719c['id']}})[_0x1f8b('0x20')](function(){return _0x4973a5;});})[_0x1f8b('0x20')](function(){_0x97c36b[_0x1f8b('0x15')](0xcc)[_0x1f8b('0x23')]();});}};}function handleEntityNotFound(_0x15935b,_0xeb921){return function(_0x16539e){if(!_0x16539e){_0x15935b[_0x1f8b('0x14')](0x194);}return _0x16539e;};}function handleError(_0x215836,_0x5f4f8d){_0x5f4f8d=_0x5f4f8d||0x1f4;return function(_0x409f72){logger[_0x1f8b('0x24')](_0x409f72[_0x1f8b('0x25')]);if(_0x409f72[_0x1f8b('0x26')]){delete _0x409f72[_0x1f8b('0x26')];}_0x215836[_0x1f8b('0x15')](_0x5f4f8d)[_0x1f8b('0x27')](_0x409f72);};}exports[_0x1f8b('0x28')]=function(_0x127368,_0x4b04b9){var _0x5907ed={},_0x3ffb46={},_0x14130e={'count':0x0,'rows':[]};var _0x4a4304=_[_0x1f8b('0x29')](db[_0x1f8b('0x2a')][_0x1f8b('0x2b')],function(_0x4c749a){return{'name':_0x4c749a['fieldName'],'type':_0x4c749a[_0x1f8b('0x2c')][_0x1f8b('0x2d')]};});_0x3ffb46['model']=_['map'](_0x4a4304,_0x1f8b('0x26'));_0x3ffb46[_0x1f8b('0x2e')]=_[_0x1f8b('0x2f')](_0x127368[_0x1f8b('0x2e')]);_0x3ffb46[_0x1f8b('0x30')]=_[_0x1f8b('0x31')](_0x3ffb46[_0x1f8b('0x32')],_0x3ffb46['query']);_0x5907ed['attributes']=_[_0x1f8b('0x31')](_0x3ffb46['model'],qs[_0x1f8b('0x33')](_0x127368[_0x1f8b('0x2e')]['fields']));_0x5907ed[_0x1f8b('0x34')]=_0x5907ed[_0x1f8b('0x34')][_0x1f8b('0x35')]?_0x5907ed[_0x1f8b('0x34')]:_0x3ffb46[_0x1f8b('0x32')];if(!_0x127368[_0x1f8b('0x2e')][_0x1f8b('0x36')](_0x1f8b('0x37'))){_0x5907ed[_0x1f8b('0x19')]=qs[_0x1f8b('0x19')](_0x127368[_0x1f8b('0x2e')]['limit']);_0x5907ed[_0x1f8b('0x18')]=qs[_0x1f8b('0x18')](_0x127368[_0x1f8b('0x2e')][_0x1f8b('0x18')]);}_0x5907ed[_0x1f8b('0x38')]=qs['sort'](_0x127368[_0x1f8b('0x2e')][_0x1f8b('0x39')]);_0x5907ed[_0x1f8b('0x3a')]=qs[_0x1f8b('0x30')](_[_0x1f8b('0x3b')](_0x127368['query'],_0x3ffb46[_0x1f8b('0x30')]),_0x4a4304);if(_0x127368['query'][_0x1f8b('0x3c')]){_0x5907ed[_0x1f8b('0x3a')]=_[_0x1f8b('0x3d')](_0x5907ed['where'],{'$or':_['map'](_0x4a4304,function(_0x342508){if(_0x342508[_0x1f8b('0x2c')]!==_0x1f8b('0x3e')){var _0x2e831f={};_0x2e831f[_0x342508[_0x1f8b('0x26')]]={'$like':'%'+_0x127368[_0x1f8b('0x2e')][_0x1f8b('0x3c')]+'%'};return _0x2e831f;}})});}_0x5907ed=_[_0x1f8b('0x3d')]({},_0x5907ed,_0x127368[_0x1f8b('0x3f')]);var _0x258f33={'where':_0x5907ed[_0x1f8b('0x3a')]};return db[_0x1f8b('0x2a')][_0x1f8b('0x17')](_0x258f33)[_0x1f8b('0x20')](function(_0x188f61){_0x14130e[_0x1f8b('0x17')]=_0x188f61;if(_0x127368['query'][_0x1f8b('0x40')]){_0x5907ed[_0x1f8b('0x41')]=[{'all':!![]}];}return db[_0x1f8b('0x2a')]['findAll'](_0x5907ed);})['then'](function(_0x162642){_0x14130e[_0x1f8b('0x42')]=_0x162642;return _0x14130e;})[_0x1f8b('0x20')](respondWithFilteredResult(_0x4b04b9,_0x5907ed))[_0x1f8b('0x43')](handleError(_0x4b04b9,null));};exports[_0x1f8b('0x44')]=function(_0x28b43c,_0x236f39){var _0x4103c3={'raw':!![],'where':{'id':_0x28b43c[_0x1f8b('0x45')]['id']}},_0x277dc6={};_0x277dc6['model']=_[_0x1f8b('0x2f')](db['AnalyticExtractedReport'][_0x1f8b('0x2b')]);_0x277dc6[_0x1f8b('0x2e')]=_[_0x1f8b('0x2f')](_0x28b43c[_0x1f8b('0x2e')]);_0x277dc6[_0x1f8b('0x30')]=_[_0x1f8b('0x31')](_0x277dc6[_0x1f8b('0x32')],_0x277dc6['query']);_0x4103c3['attributes']=_[_0x1f8b('0x31')](_0x277dc6['model'],qs['fields'](_0x28b43c[_0x1f8b('0x2e')][_0x1f8b('0x33')]));_0x4103c3[_0x1f8b('0x34')]=_0x4103c3[_0x1f8b('0x34')]['length']?_0x4103c3[_0x1f8b('0x34')]:_0x277dc6['model'];if(_0x28b43c[_0x1f8b('0x2e')]['includeAll']){_0x4103c3['include']=[{'all':!![]}];}_0x4103c3=_[_0x1f8b('0x3d')]({},_0x4103c3,_0x28b43c[_0x1f8b('0x3f')]);return db[_0x1f8b('0x2a')]['find'](_0x4103c3)[_0x1f8b('0x20')](handleEntityNotFound(_0x236f39,null))[_0x1f8b('0x20')](respondWithResult(_0x236f39,null))[_0x1f8b('0x43')](handleError(_0x236f39,null));};exports[_0x1f8b('0x46')]=function(_0x4e9783,_0x58479e){return db['AnalyticExtractedReport'][_0x1f8b('0x46')](_0x4e9783[_0x1f8b('0x47')],{})['then'](function(_0x10d2ed){var _0x18fb2c=_0x4e9783[_0x1f8b('0x48')][_0x1f8b('0x49')]({'plain':!![]});if(!_0x18fb2c)throw new Error(_0x1f8b('0x4a'));if(_0x18fb2c[_0x1f8b('0x4b')]===_0x1f8b('0x48')){var _0x19b6ce=_0x10d2ed[_0x1f8b('0x49')]({'plain':!![]});var _0xfeb37a='ExtractedReports';return db['UserProfileSection'][_0x1f8b('0x4c')]({'where':{'name':_0xfeb37a,'userProfileId':_0x18fb2c['userProfileId']},'raw':!![]})['then'](function(_0x6fe73){if(_0x6fe73&&_0x6fe73[_0x1f8b('0x4d')]===0x0){return db[_0x1f8b('0x22')][_0x1f8b('0x46')]({'name':_0x19b6ce['name'],'resourceId':_0x19b6ce['id'],'type':_0x6fe73[_0x1f8b('0x26')],'sectionId':_0x6fe73['id']},{})[_0x1f8b('0x20')](function(){return _0x10d2ed;});}else{return _0x10d2ed;}})['catch'](function(_0x521d90){logger[_0x1f8b('0x24')](_0x1f8b('0x4e'),_0x521d90);throw _0x521d90;});}return _0x10d2ed;})['then'](respondWithResult(_0x58479e,0xc9))[_0x1f8b('0x43')](handleError(_0x58479e,null));};exports['update']=function(_0x4b9251,_0x29d7d4){if(_0x4b9251[_0x1f8b('0x47')]['id']){delete _0x4b9251[_0x1f8b('0x47')]['id'];}return db[_0x1f8b('0x2a')][_0x1f8b('0x4c')]({'where':{'id':_0x4b9251['params']['id']}})[_0x1f8b('0x20')](handleEntityNotFound(_0x29d7d4,null))['then'](saveUpdates(_0x4b9251[_0x1f8b('0x47')],null))[_0x1f8b('0x20')](respondWithResult(_0x29d7d4,null))[_0x1f8b('0x43')](handleError(_0x29d7d4,null));};exports[_0x1f8b('0x21')]=function(_0x5e7282,_0x331a52){return db[_0x1f8b('0x2a')][_0x1f8b('0x4c')]({'where':{'id':_0x5e7282[_0x1f8b('0x45')]['id']}})[_0x1f8b('0x20')](handleEntityNotFound(_0x331a52,null))[_0x1f8b('0x20')](removeEntity(_0x331a52,null))[_0x1f8b('0x43')](handleError(_0x331a52,null));};exports[_0x1f8b('0x4f')]=function(_0x409a53,_0x49d021,_0x35e378){return db[_0x1f8b('0x2a')][_0x1f8b('0x4c')]({'where':{'id':_0x409a53[_0x1f8b('0x45')]['id']},'attributes':['name','basename',_0x1f8b('0x50'),_0x1f8b('0x51')],'raw':!![]})[_0x1f8b('0x20')](handleEntityNotFound(_0x49d021,null))[_0x1f8b('0x20')](function(_0x1633f5){if(_0x1633f5){if(!fs[_0x1f8b('0x52')](path[_0x1f8b('0x53')](config['root'],_0x1f8b('0x54'),_0x1633f5[_0x1f8b('0x50')]))){throw new db[(_0x1f8b('0x55'))]['ValidationError']('Report\x20not\x20found\x20or\x20empty');}if(_0x409a53[_0x1f8b('0x2e')][_0x1f8b('0x56')]){return _0x49d021[_0x1f8b('0x14')](0xc8);}return _0x49d021[_0x1f8b('0x4f')](path['join'](config[_0x1f8b('0x57')],_0x1f8b('0x54'),_0x1633f5[_0x1f8b('0x50')]),util[_0x1f8b('0x58')](_0x1f8b('0x59'),_0x1633f5[_0x1f8b('0x5a')]||_0x1633f5['name'],_0x1633f5[_0x1f8b('0x51')][_0x1f8b('0x5b')]()));}})[_0x1f8b('0x43')](handleError(_0x49d021,null));};