8669e127fa0431f4d54e22ffa624b56e141b15d3
[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 _0x3a74=['download','basename','savename','existsSync','join','server/files/reports','Sequelize','ValidationError','Report\x20not\x20found\x20or\x20empty','exists','root','format','%s.%s','output','toLowerCase','eml-format','rimraf','zip-dir','fast-json-patch','moment','bluebird','mustache','util','path','sox','to-csv','ejs','fs-extra','lodash','squel','jsforce','desk.js','querystring','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/environment','../../config/license/util','../../mysqldb','sendStatus','status','end','json','offset','undefined','limit','count','Content-Range','apply','reject','save','then','destroy','get','ExtractedReports','UserProfileResource','error','stack','name','send','index','map','AnalyticExtractedReport','rawAttributes','fieldName','key','query','filters','model','attributes','intersection','fields','hasOwnProperty','order','sort','where','pick','filter','merge','VIRTUAL','includeAll','catch','show','params','keys','include','find','create','body','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','UserProfileSection','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','update'];(function(_0x2fe0df,_0x1f78e3){var _0x4b832e=function(_0x1042d5){while(--_0x1042d5){_0x2fe0df['push'](_0x2fe0df['shift']());}};_0x4b832e(++_0x1f78e3);}(_0x3a74,0x138));var _0x43a7=function(_0x5cceb8,_0xa8e57a){_0x5cceb8=_0x5cceb8-0x0;var _0xeaf4be=_0x3a74[_0x5cceb8];return _0xeaf4be;};'use strict';var emlformat=require(_0x43a7('0x0'));var rimraf=require(_0x43a7('0x1'));var zipdir=require(_0x43a7('0x2'));var jsonpatch=require(_0x43a7('0x3'));var rp=require('request-promise');var moment=require(_0x43a7('0x4'));var BPromise=require(_0x43a7('0x5'));var Mustache=require(_0x43a7('0x6'));var util=require(_0x43a7('0x7'));var path=require(_0x43a7('0x8'));var sox=require(_0x43a7('0x9'));var csv=require(_0x43a7('0xa'));var ejs=require(_0x43a7('0xb'));var fs=require('fs');var fs_extra=require(_0x43a7('0xc'));var _=require(_0x43a7('0xd'));var squel=require(_0x43a7('0xe'));var crypto=require('crypto');var jsforce=require(_0x43a7('0xf'));var deskjs=require(_0x43a7('0x10'));var toCsv=require('to-csv');var querystring=require(_0x43a7('0x11'));var Papa=require('papaparse');var Redis=require(_0x43a7('0x12'));var authService=require(_0x43a7('0x13'));var qs=require(_0x43a7('0x14'));var as=require(_0x43a7('0x15'));var hardwareService=require(_0x43a7('0x16'));var logger=require(_0x43a7('0x17'))(_0x43a7('0x18'));var utils=require('../../config/utils');var config=require(_0x43a7('0x19'));var licenseUtil=require(_0x43a7('0x1a'));var db=require(_0x43a7('0x1b'))['db'];function respondWithStatusCode(_0x404d9a,_0x152806){_0x152806=_0x152806||0xcc;return function(_0x59b094){if(_0x59b094){return _0x404d9a[_0x43a7('0x1c')](_0x152806);}return _0x404d9a[_0x43a7('0x1d')](_0x152806)[_0x43a7('0x1e')]();};}function respondWithResult(_0x3a6df7,_0x21f563){_0x21f563=_0x21f563||0xc8;return function(_0x43f65a){if(_0x43f65a){return _0x3a6df7[_0x43a7('0x1d')](_0x21f563)[_0x43a7('0x1f')](_0x43f65a);}};}function respondWithFilteredResult(_0x4a88a8,_0x2b372d){return function(_0x5c2194){if(_0x5c2194){var _0x188ab3=typeof _0x2b372d[_0x43a7('0x20')]===_0x43a7('0x21')&&typeof _0x2b372d[_0x43a7('0x22')]===_0x43a7('0x21');var _0x44d9b8=_0x5c2194[_0x43a7('0x23')];var _0x564d56=_0x188ab3?0x0:_0x2b372d[_0x43a7('0x20')];var _0x5075c6=_0x188ab3?_0x5c2194['count']:_0x2b372d[_0x43a7('0x20')]+_0x2b372d[_0x43a7('0x22')];var _0x4a7bec;if(_0x5075c6>=_0x44d9b8){_0x5075c6=_0x44d9b8;_0x4a7bec=0xc8;}else{_0x4a7bec=0xce;}_0x4a88a8[_0x43a7('0x1d')](_0x4a7bec);return _0x4a88a8['set'](_0x43a7('0x24'),_0x564d56+'-'+_0x5075c6+'/'+_0x44d9b8)['json'](_0x5c2194);}return null;};}function patchUpdates(_0x5eb2a9){return function(_0x63107){try{jsonpatch[_0x43a7('0x25')](_0x63107,_0x5eb2a9,!![]);}catch(_0xa301e2){return BPromise[_0x43a7('0x26')](_0xa301e2);}return _0x63107[_0x43a7('0x27')]();};}function saveUpdates(_0x4fbd35,_0x938cb0){return function(_0x46978f){if(_0x46978f){return _0x46978f['update'](_0x4fbd35)[_0x43a7('0x28')](function(_0x82ab96){return _0x82ab96;});}return null;};}function removeEntity(_0x464fff,_0x2ad871){return function(_0x17c4dd){if(_0x17c4dd){return _0x17c4dd[_0x43a7('0x29')]()[_0x43a7('0x28')](function(){var _0x10ee70=_0x17c4dd[_0x43a7('0x2a')]({'plain':!![]});var _0xcb1d70=_0x43a7('0x2b');return db[_0x43a7('0x2c')][_0x43a7('0x29')]({'where':{'type':_0xcb1d70,'resourceId':_0x10ee70['id']}})[_0x43a7('0x28')](function(){return _0x17c4dd;});})['then'](function(){_0x464fff[_0x43a7('0x1d')](0xcc)[_0x43a7('0x1e')]();});}};}function handleEntityNotFound(_0xce4b4,_0x3cb983){return function(_0x341af3){if(!_0x341af3){_0xce4b4[_0x43a7('0x1c')](0x194);}return _0x341af3;};}function handleError(_0x5b8e6c,_0x2ea195){_0x2ea195=_0x2ea195||0x1f4;return function(_0x4da4cc){logger[_0x43a7('0x2d')](_0x4da4cc[_0x43a7('0x2e')]);if(_0x4da4cc['name']){delete _0x4da4cc[_0x43a7('0x2f')];}_0x5b8e6c['status'](_0x2ea195)[_0x43a7('0x30')](_0x4da4cc);};}exports[_0x43a7('0x31')]=function(_0xe13c14,_0x2ddb2e){var _0x140814={},_0x14fec1={},_0x100fa2={'count':0x0,'rows':[]};var _0x379d04=_[_0x43a7('0x32')](db[_0x43a7('0x33')][_0x43a7('0x34')],function(_0xa3bb0c){return{'name':_0xa3bb0c[_0x43a7('0x35')],'type':_0xa3bb0c['type'][_0x43a7('0x36')]};});_0x14fec1['model']=_[_0x43a7('0x32')](_0x379d04,'name');_0x14fec1[_0x43a7('0x37')]=_['keys'](_0xe13c14['query']);_0x14fec1[_0x43a7('0x38')]=_['intersection'](_0x14fec1[_0x43a7('0x39')],_0x14fec1[_0x43a7('0x37')]);_0x140814[_0x43a7('0x3a')]=_[_0x43a7('0x3b')](_0x14fec1[_0x43a7('0x39')],qs[_0x43a7('0x3c')](_0xe13c14[_0x43a7('0x37')]['fields']));_0x140814[_0x43a7('0x3a')]=_0x140814[_0x43a7('0x3a')]['length']?_0x140814[_0x43a7('0x3a')]:_0x14fec1['model'];if(!_0xe13c14['query'][_0x43a7('0x3d')]('nolimit')){_0x140814[_0x43a7('0x22')]=qs[_0x43a7('0x22')](_0xe13c14[_0x43a7('0x37')][_0x43a7('0x22')]);_0x140814[_0x43a7('0x20')]=qs[_0x43a7('0x20')](_0xe13c14[_0x43a7('0x37')][_0x43a7('0x20')]);}_0x140814[_0x43a7('0x3e')]=qs[_0x43a7('0x3f')](_0xe13c14['query'][_0x43a7('0x3f')]);_0x140814[_0x43a7('0x40')]=qs[_0x43a7('0x38')](_[_0x43a7('0x41')](_0xe13c14[_0x43a7('0x37')],_0x14fec1['filters']),_0x379d04);if(_0xe13c14[_0x43a7('0x37')][_0x43a7('0x42')]){_0x140814[_0x43a7('0x40')]=_[_0x43a7('0x43')](_0x140814[_0x43a7('0x40')],{'$or':_['map'](_0x379d04,function(_0x165443){if(_0x165443['type']!==_0x43a7('0x44')){var _0x35b4cb={};_0x35b4cb[_0x165443['name']]={'$like':'%'+_0xe13c14[_0x43a7('0x37')][_0x43a7('0x42')]+'%'};return _0x35b4cb;}})});}_0x140814=_[_0x43a7('0x43')]({},_0x140814,_0xe13c14['options']);var _0x4463c7={'where':_0x140814['where']};return db[_0x43a7('0x33')][_0x43a7('0x23')](_0x4463c7)[_0x43a7('0x28')](function(_0x3c69d9){_0x100fa2[_0x43a7('0x23')]=_0x3c69d9;if(_0xe13c14[_0x43a7('0x37')][_0x43a7('0x45')]){_0x140814['include']=[{'all':!![]}];}return db[_0x43a7('0x33')]['findAll'](_0x140814);})[_0x43a7('0x28')](function(_0x39a896){_0x100fa2['rows']=_0x39a896;return _0x100fa2;})[_0x43a7('0x28')](respondWithFilteredResult(_0x2ddb2e,_0x140814))[_0x43a7('0x46')](handleError(_0x2ddb2e,null));};exports[_0x43a7('0x47')]=function(_0x4374ed,_0x78c963){var _0x28049d={'raw':!![],'where':{'id':_0x4374ed[_0x43a7('0x48')]['id']}},_0x329f34={};_0x329f34[_0x43a7('0x39')]=_[_0x43a7('0x49')](db[_0x43a7('0x33')][_0x43a7('0x34')]);_0x329f34['query']=_[_0x43a7('0x49')](_0x4374ed[_0x43a7('0x37')]);_0x329f34[_0x43a7('0x38')]=_[_0x43a7('0x3b')](_0x329f34[_0x43a7('0x39')],_0x329f34[_0x43a7('0x37')]);_0x28049d[_0x43a7('0x3a')]=_['intersection'](_0x329f34[_0x43a7('0x39')],qs['fields'](_0x4374ed['query'][_0x43a7('0x3c')]));_0x28049d['attributes']=_0x28049d[_0x43a7('0x3a')]['length']?_0x28049d[_0x43a7('0x3a')]:_0x329f34['model'];if(_0x4374ed[_0x43a7('0x37')][_0x43a7('0x45')]){_0x28049d[_0x43a7('0x4a')]=[{'all':!![]}];}_0x28049d=_['merge']({},_0x28049d,_0x4374ed['options']);return db[_0x43a7('0x33')][_0x43a7('0x4b')](_0x28049d)[_0x43a7('0x28')](handleEntityNotFound(_0x78c963,null))['then'](respondWithResult(_0x78c963,null))[_0x43a7('0x46')](handleError(_0x78c963,null));};exports[_0x43a7('0x4c')]=function(_0x38c750,_0x53eb26){return db[_0x43a7('0x33')][_0x43a7('0x4c')](_0x38c750[_0x43a7('0x4d')],{})[_0x43a7('0x28')](function(_0x26438e){var _0x132733=_0x38c750['user'][_0x43a7('0x2a')]({'plain':!![]});if(!_0x132733)throw new Error(_0x43a7('0x4e'));if(_0x132733[_0x43a7('0x4f')]==='user'){var _0x3a9db4=_0x26438e['get']({'plain':!![]});var _0x10eb79=_0x43a7('0x2b');return db[_0x43a7('0x50')][_0x43a7('0x4b')]({'where':{'name':_0x10eb79,'userProfileId':_0x132733['userProfileId']},'raw':!![]})[_0x43a7('0x28')](function(_0x2d0d3a){if(_0x2d0d3a&&_0x2d0d3a[_0x43a7('0x51')]===0x0){return db['UserProfileResource']['create']({'name':_0x3a9db4['name'],'resourceId':_0x3a9db4['id'],'type':_0x2d0d3a['name'],'sectionId':_0x2d0d3a['id']},{})[_0x43a7('0x28')](function(){return _0x26438e;});}else{return _0x26438e;}})[_0x43a7('0x46')](function(_0x2f665e){logger[_0x43a7('0x2d')](_0x43a7('0x52'),_0x2f665e);throw _0x2f665e;});}return _0x26438e;})[_0x43a7('0x28')](respondWithResult(_0x53eb26,0xc9))[_0x43a7('0x46')](handleError(_0x53eb26,null));};exports[_0x43a7('0x53')]=function(_0x5630de,_0x11b2ac){if(_0x5630de[_0x43a7('0x4d')]['id']){delete _0x5630de[_0x43a7('0x4d')]['id'];}return db[_0x43a7('0x33')]['find']({'where':{'id':_0x5630de[_0x43a7('0x48')]['id']}})[_0x43a7('0x28')](handleEntityNotFound(_0x11b2ac,null))[_0x43a7('0x28')](saveUpdates(_0x5630de['body'],null))['then'](respondWithResult(_0x11b2ac,null))[_0x43a7('0x46')](handleError(_0x11b2ac,null));};exports[_0x43a7('0x29')]=function(_0x3fe3a2,_0x1417b7){return db[_0x43a7('0x33')][_0x43a7('0x4b')]({'where':{'id':_0x3fe3a2[_0x43a7('0x48')]['id']}})[_0x43a7('0x28')](handleEntityNotFound(_0x1417b7,null))[_0x43a7('0x28')](removeEntity(_0x1417b7,null))['catch'](handleError(_0x1417b7,null));};exports[_0x43a7('0x54')]=function(_0xd57b76,_0x60c6e8,_0x490c10){return db[_0x43a7('0x33')][_0x43a7('0x4b')]({'where':{'id':_0xd57b76['params']['id']},'attributes':[_0x43a7('0x2f'),_0x43a7('0x55'),_0x43a7('0x56'),'output'],'raw':!![]})['then'](handleEntityNotFound(_0x60c6e8,null))[_0x43a7('0x28')](function(_0x2bc4ef){if(_0x2bc4ef){if(!fs[_0x43a7('0x57')](path[_0x43a7('0x58')](config['root'],_0x43a7('0x59'),_0x2bc4ef[_0x43a7('0x56')]))){throw new db[(_0x43a7('0x5a'))][(_0x43a7('0x5b'))](_0x43a7('0x5c'));}if(_0xd57b76[_0x43a7('0x37')][_0x43a7('0x5d')]){return _0x60c6e8[_0x43a7('0x1c')](0xc8);}return _0x60c6e8['download'](path[_0x43a7('0x58')](config[_0x43a7('0x5e')],_0x43a7('0x59'),_0x2bc4ef[_0x43a7('0x56')]),util[_0x43a7('0x5f')](_0x43a7('0x60'),_0x2bc4ef[_0x43a7('0x55')]||_0x2bc4ef[_0x43a7('0x2f')],_0x2bc4ef[_0x43a7('0x61')][_0x43a7('0x62')]()));}})[_0x43a7('0x46')](handleError(_0x60c6e8,null));};