d02c81fe7f8d045611690df693caa13f211a2d57
[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 _0x8fce=['show','params','AnalyticExtractedReport','intersection','options','find','create','body','user','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','ExtractedReports','UserProfileSection','userProfileId','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','download','basename','savename','output','existsSync','join','root','server/files/reports','Sequelize','exists','%s.%s','html-pdf','rimraf','zip-dir','fast-json-patch','request-promise','bluebird','mustache','util','path','sox','ejs','lodash','crypto','jsforce','desk.js','to-csv','querystring','papaparse','ioredis','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','api','../../config/utils','../../mysqldb','sendStatus','status','end','json','offset','limit','save','update','then','destroy','get','UserProfileResource','error','stack','name','send','map','rawAttributes','fieldName','type','key','model','query','keys','filters','attributes','fields','hasOwnProperty','nolimit','order','sort','where','pick','filter','merge','VIRTUAL','count','includeAll','include','findAll','rows','catch'];(function(_0x3092c2,_0x8340dd){var _0x481ec9=function(_0x2b02e7){while(--_0x2b02e7){_0x3092c2['push'](_0x3092c2['shift']());}};_0x481ec9(++_0x8340dd);}(_0x8fce,0x193));var _0xe8fc=function(_0x3d0669,_0x1c5ed0){_0x3d0669=_0x3d0669-0x0;var _0x461e13=_0x8fce[_0x3d0669];return _0x461e13;};'use strict';var pdf=require(_0xe8fc('0x0'));var emlformat=require('eml-format');var rimraf=require(_0xe8fc('0x1'));var zipdir=require(_0xe8fc('0x2'));var jsonpatch=require(_0xe8fc('0x3'));var rp=require(_0xe8fc('0x4'));var moment=require('moment');var BPromise=require(_0xe8fc('0x5'));var Mustache=require(_0xe8fc('0x6'));var util=require(_0xe8fc('0x7'));var path=require(_0xe8fc('0x8'));var sox=require(_0xe8fc('0x9'));var csv=require('to-csv');var ejs=require(_0xe8fc('0xa'));var fs=require('fs');var _=require(_0xe8fc('0xb'));var squel=require('squel');var crypto=require(_0xe8fc('0xc'));var jsforce=require(_0xe8fc('0xd'));var deskjs=require(_0xe8fc('0xe'));var toCsv=require(_0xe8fc('0xf'));var querystring=require(_0xe8fc('0x10'));var Papa=require(_0xe8fc('0x11'));var Redis=require(_0xe8fc('0x12'));var authService=require('../../components/auth/service');var qs=require(_0xe8fc('0x13'));var as=require(_0xe8fc('0x14'));var hardwareService=require(_0xe8fc('0x15'));var logger=require('../../config/logger')(_0xe8fc('0x16'));var utils=require(_0xe8fc('0x17'));var config=require('../../config/environment');var db=require(_0xe8fc('0x18'))['db'];function respondWithStatusCode(_0x2914b6,_0x37aabf){_0x37aabf=_0x37aabf||0xcc;return function(_0x38fedd){if(_0x38fedd){return _0x2914b6[_0xe8fc('0x19')](_0x37aabf);}return _0x2914b6[_0xe8fc('0x1a')](_0x37aabf)[_0xe8fc('0x1b')]();};}function respondWithResult(_0x28d3d0,_0x9a387d){_0x9a387d=_0x9a387d||0xc8;return function(_0x59cdd6){if(_0x59cdd6){return _0x28d3d0[_0xe8fc('0x1a')](_0x9a387d)[_0xe8fc('0x1c')](_0x59cdd6);}};}function respondWithFilteredResult(_0x5b7ce3,_0x3b9541){return function(_0x333dd1){if(_0x333dd1){var _0x51e363=_0x333dd1['count'],_0x2bfc26=_0x3b9541['offset'],_0x10e63d=_0x3b9541[_0xe8fc('0x1d')]+_0x3b9541[_0xe8fc('0x1e')],_0x1ff679;if(_0x10e63d>=_0x51e363){_0x10e63d=_0x51e363;_0x1ff679=0xc8;}else{_0x1ff679=0xce;}_0x5b7ce3[_0xe8fc('0x1a')](_0x1ff679);return _0x5b7ce3['set']('Content-Range',_0x2bfc26+'-'+_0x10e63d+'/'+_0x51e363)[_0xe8fc('0x1c')](_0x333dd1);}return null;};}function patchUpdates(_0x4582f8){return function(_0x120bd0){try{jsonpatch['apply'](_0x120bd0,_0x4582f8,!![]);}catch(_0x133a3c){return BPromise['reject'](_0x133a3c);}return _0x120bd0[_0xe8fc('0x1f')]();};}function saveUpdates(_0x4dfc44,_0x203b8b){return function(_0x43a627){if(_0x43a627){return _0x43a627[_0xe8fc('0x20')](_0x4dfc44)[_0xe8fc('0x21')](function(_0x4f89bc){return _0x4f89bc;});}return null;};}function removeEntity(_0x9866f8,_0x1a78f1){return function(_0x1b6440){if(_0x1b6440){return _0x1b6440[_0xe8fc('0x22')]()[_0xe8fc('0x21')](function(){var _0x4bda4e=_0x1b6440[_0xe8fc('0x23')]({'plain':!![]});var _0x2029a7='ExtractedReports';return db[_0xe8fc('0x24')][_0xe8fc('0x22')]({'where':{'type':_0x2029a7,'resourceId':_0x4bda4e['id']}})[_0xe8fc('0x21')](function(){return _0x1b6440;});})['then'](function(){_0x9866f8['status'](0xcc)[_0xe8fc('0x1b')]();});}};}function handleEntityNotFound(_0x2f543e,_0x2ecb15){return function(_0x19da0e){if(!_0x19da0e){_0x2f543e[_0xe8fc('0x19')](0x194);}return _0x19da0e;};}function handleError(_0x2af0df,_0x1fbbdc){_0x1fbbdc=_0x1fbbdc||0x1f4;return function(_0xe226d5){logger[_0xe8fc('0x25')](_0xe226d5[_0xe8fc('0x26')]);if(_0xe226d5[_0xe8fc('0x27')]){delete _0xe226d5[_0xe8fc('0x27')];}_0x2af0df[_0xe8fc('0x1a')](_0x1fbbdc)[_0xe8fc('0x28')](_0xe226d5);};}exports['index']=function(_0x37edb9,_0x46ced2){var _0x30f506={},_0x32885e={},_0x30f17a={'count':0x0,'rows':[]};var _0x4eb806=_[_0xe8fc('0x29')](db['AnalyticExtractedReport'][_0xe8fc('0x2a')],function(_0x5e4b87){return{'name':_0x5e4b87[_0xe8fc('0x2b')],'type':_0x5e4b87[_0xe8fc('0x2c')][_0xe8fc('0x2d')]};});_0x32885e[_0xe8fc('0x2e')]=_[_0xe8fc('0x29')](_0x4eb806,'name');_0x32885e[_0xe8fc('0x2f')]=_[_0xe8fc('0x30')](_0x37edb9[_0xe8fc('0x2f')]);_0x32885e[_0xe8fc('0x31')]=_['intersection'](_0x32885e['model'],_0x32885e[_0xe8fc('0x2f')]);_0x30f506[_0xe8fc('0x32')]=_['intersection'](_0x32885e[_0xe8fc('0x2e')],qs[_0xe8fc('0x33')](_0x37edb9[_0xe8fc('0x2f')]['fields']));_0x30f506[_0xe8fc('0x32')]=_0x30f506['attributes']['length']?_0x30f506[_0xe8fc('0x32')]:_0x32885e[_0xe8fc('0x2e')];if(!_0x37edb9[_0xe8fc('0x2f')][_0xe8fc('0x34')](_0xe8fc('0x35'))){_0x30f506[_0xe8fc('0x1e')]=qs['limit'](_0x37edb9[_0xe8fc('0x2f')][_0xe8fc('0x1e')]);_0x30f506['offset']=qs[_0xe8fc('0x1d')](_0x37edb9[_0xe8fc('0x2f')][_0xe8fc('0x1d')]);}_0x30f506[_0xe8fc('0x36')]=qs[_0xe8fc('0x37')](_0x37edb9[_0xe8fc('0x2f')][_0xe8fc('0x37')]);_0x30f506[_0xe8fc('0x38')]=qs[_0xe8fc('0x31')](_[_0xe8fc('0x39')](_0x37edb9[_0xe8fc('0x2f')],_0x32885e[_0xe8fc('0x31')]),_0x4eb806);if(_0x37edb9[_0xe8fc('0x2f')][_0xe8fc('0x3a')]){_0x30f506[_0xe8fc('0x38')]=_[_0xe8fc('0x3b')](_0x30f506['where'],{'$or':_[_0xe8fc('0x29')](_0x4eb806,function(_0x1e6128){if(_0x1e6128[_0xe8fc('0x2c')]!==_0xe8fc('0x3c')){var _0x40cf36={};_0x40cf36[_0x1e6128[_0xe8fc('0x27')]]={'$like':'%'+_0x37edb9[_0xe8fc('0x2f')][_0xe8fc('0x3a')]+'%'};return _0x40cf36;}})});}_0x30f506=_[_0xe8fc('0x3b')]({},_0x30f506,_0x37edb9['options']);var _0x3a1417={'where':_0x30f506[_0xe8fc('0x38')]};return db['AnalyticExtractedReport']['count'](_0x3a1417)[_0xe8fc('0x21')](function(_0x4d7728){_0x30f17a[_0xe8fc('0x3d')]=_0x4d7728;if(_0x37edb9[_0xe8fc('0x2f')][_0xe8fc('0x3e')]){_0x30f506[_0xe8fc('0x3f')]=[{'all':!![]}];}return db['AnalyticExtractedReport'][_0xe8fc('0x40')](_0x30f506);})['then'](function(_0x13e27c){_0x30f17a[_0xe8fc('0x41')]=_0x13e27c;return _0x30f17a;})[_0xe8fc('0x21')](respondWithFilteredResult(_0x46ced2,_0x30f506))[_0xe8fc('0x42')](handleError(_0x46ced2,null));};exports[_0xe8fc('0x43')]=function(_0x41b69c,_0x30ef67){var _0x5e398d={'raw':!![],'where':{'id':_0x41b69c[_0xe8fc('0x44')]['id']}},_0x7552f1={};_0x7552f1['model']=_[_0xe8fc('0x30')](db[_0xe8fc('0x45')][_0xe8fc('0x2a')]);_0x7552f1['query']=_[_0xe8fc('0x30')](_0x41b69c[_0xe8fc('0x2f')]);_0x7552f1[_0xe8fc('0x31')]=_[_0xe8fc('0x46')](_0x7552f1[_0xe8fc('0x2e')],_0x7552f1[_0xe8fc('0x2f')]);_0x5e398d[_0xe8fc('0x32')]=_['intersection'](_0x7552f1['model'],qs['fields'](_0x41b69c[_0xe8fc('0x2f')][_0xe8fc('0x33')]));_0x5e398d[_0xe8fc('0x32')]=_0x5e398d['attributes']['length']?_0x5e398d[_0xe8fc('0x32')]:_0x7552f1[_0xe8fc('0x2e')];if(_0x41b69c[_0xe8fc('0x2f')][_0xe8fc('0x3e')]){_0x5e398d[_0xe8fc('0x3f')]=[{'all':!![]}];}_0x5e398d=_['merge']({},_0x5e398d,_0x41b69c[_0xe8fc('0x47')]);return db[_0xe8fc('0x45')][_0xe8fc('0x48')](_0x5e398d)[_0xe8fc('0x21')](handleEntityNotFound(_0x30ef67,null))['then'](respondWithResult(_0x30ef67,null))[_0xe8fc('0x42')](handleError(_0x30ef67,null));};exports[_0xe8fc('0x49')]=function(_0x8364a2,_0x1ae90c){return db['AnalyticExtractedReport'][_0xe8fc('0x49')](_0x8364a2[_0xe8fc('0x4a')],{})[_0xe8fc('0x21')](function(_0x2fb7ea){var _0x352098=_0x8364a2[_0xe8fc('0x4b')]['get']({'plain':!![]});if(!_0x352098)throw new Error(_0xe8fc('0x4c'));if(_0x352098[_0xe8fc('0x4d')]===_0xe8fc('0x4b')){var _0x32487b=_0x2fb7ea[_0xe8fc('0x23')]({'plain':!![]});var _0x487292=_0xe8fc('0x4e');return db[_0xe8fc('0x4f')][_0xe8fc('0x48')]({'where':{'name':_0x487292,'userProfileId':_0x352098[_0xe8fc('0x50')]},'raw':!![]})[_0xe8fc('0x21')](function(_0xeded28){if(_0xeded28&&_0xeded28[_0xe8fc('0x51')]===0x0){return db[_0xe8fc('0x24')][_0xe8fc('0x49')]({'name':_0x32487b[_0xe8fc('0x27')],'resourceId':_0x32487b['id'],'type':_0xeded28[_0xe8fc('0x27')],'sectionId':_0xeded28['id']},{})[_0xe8fc('0x21')](function(){return _0x2fb7ea;});}else{return _0x2fb7ea;}})['catch'](function(_0x4306bd){logger[_0xe8fc('0x25')](_0xe8fc('0x52'),_0x4306bd);throw _0x4306bd;});}return _0x2fb7ea;})[_0xe8fc('0x21')](respondWithResult(_0x1ae90c,0xc9))[_0xe8fc('0x42')](handleError(_0x1ae90c,null));};exports['update']=function(_0x1561e7,_0x56b2bf){if(_0x1561e7[_0xe8fc('0x4a')]['id']){delete _0x1561e7[_0xe8fc('0x4a')]['id'];}return db[_0xe8fc('0x45')][_0xe8fc('0x48')]({'where':{'id':_0x1561e7[_0xe8fc('0x44')]['id']}})[_0xe8fc('0x21')](handleEntityNotFound(_0x56b2bf,null))[_0xe8fc('0x21')](saveUpdates(_0x1561e7[_0xe8fc('0x4a')],null))[_0xe8fc('0x21')](respondWithResult(_0x56b2bf,null))[_0xe8fc('0x42')](handleError(_0x56b2bf,null));};exports[_0xe8fc('0x22')]=function(_0x58acc0,_0x5e2971){return db[_0xe8fc('0x45')][_0xe8fc('0x48')]({'where':{'id':_0x58acc0[_0xe8fc('0x44')]['id']}})[_0xe8fc('0x21')](handleEntityNotFound(_0x5e2971,null))[_0xe8fc('0x21')](removeEntity(_0x5e2971,null))[_0xe8fc('0x42')](handleError(_0x5e2971,null));};exports[_0xe8fc('0x53')]=function(_0x224751,_0x36a543,_0x13983e){return db[_0xe8fc('0x45')][_0xe8fc('0x48')]({'where':{'id':_0x224751[_0xe8fc('0x44')]['id']},'attributes':['name',_0xe8fc('0x54'),_0xe8fc('0x55'),_0xe8fc('0x56')],'raw':!![]})[_0xe8fc('0x21')](handleEntityNotFound(_0x36a543,null))['then'](function(_0x503465){if(_0x503465){if(!fs[_0xe8fc('0x57')](path[_0xe8fc('0x58')](config[_0xe8fc('0x59')],_0xe8fc('0x5a'),_0x503465[_0xe8fc('0x55')]))){throw new db[(_0xe8fc('0x5b'))]['ValidationError']('Report\x20not\x20found\x20or\x20empty');}if(_0x224751['query'][_0xe8fc('0x5c')]){return _0x36a543[_0xe8fc('0x19')](0xc8);}return _0x36a543[_0xe8fc('0x53')](path[_0xe8fc('0x58')](config['root'],_0xe8fc('0x5a'),_0x503465[_0xe8fc('0x55')]),util['format'](_0xe8fc('0x5d'),_0x503465[_0xe8fc('0x54')]||_0x503465['name'],_0x503465['output']['toLowerCase']()));}})[_0xe8fc('0x42')](handleError(_0x36a543,null));};