28a8e4cccbdfc62b4f0b7e44456785526acf0a94
[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 _0x4f07=['nolimit','sort','where','pick','filter','VIRTUAL','options','includeAll','include','findAll','rows','catch','find','create','user','role','UserProfileSection','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','params','download','basename','output','existsSync','join','Sequelize','ValidationError','Report\x20not\x20found\x20or\x20empty','exists','root','server/files/reports','%s.%s','toLowerCase','zip-dir','request-promise','bluebird','mustache','path','sox','ejs','fs-extra','jsforce','desk.js','to-csv','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/license/util','../../mysqldb','sendStatus','status','json','offset','undefined','limit','count','set','Content-Range','apply','save','update','destroy','then','get','ExtractedReports','UserProfileResource','end','error','name','send','render','merge','body','contact','message','agent','interaction','account','index','map','AnalyticExtractedReport','rawAttributes','type','key','model','query','keys','filters','intersection','attributes','fields','length','hasOwnProperty'];(function(_0x340a4d,_0x55f8c5){var _0x1e132d=function(_0x4481b5){while(--_0x4481b5){_0x340a4d['push'](_0x340a4d['shift']());}};_0x1e132d(++_0x55f8c5);}(_0x4f07,0xe9));var _0x74f0=function(_0x57b54c,_0x54ce0d){_0x57b54c=_0x57b54c-0x0;var _0x59cc19=_0x4f07[_0x57b54c];return _0x59cc19;};'use strict';var emlformat=require('eml-format');var rimraf=require('rimraf');var zipdir=require(_0x74f0('0x0'));var jsonpatch=require('fast-json-patch');var rp=require(_0x74f0('0x1'));var moment=require('moment');var BPromise=require(_0x74f0('0x2'));var Mustache=require(_0x74f0('0x3'));var util=require('util');var path=require(_0x74f0('0x4'));var sox=require(_0x74f0('0x5'));var csv=require('to-csv');var ejs=require(_0x74f0('0x6'));var fs=require('fs');var fs_extra=require(_0x74f0('0x7'));var _=require('lodash');var squel=require('squel');var crypto=require('crypto');var jsforce=require(_0x74f0('0x8'));var deskjs=require(_0x74f0('0x9'));var toCsv=require(_0x74f0('0xa'));var querystring=require(_0x74f0('0xb'));var Papa=require(_0x74f0('0xc'));var Redis=require(_0x74f0('0xd'));var authService=require(_0x74f0('0xe'));var qs=require(_0x74f0('0xf'));var as=require(_0x74f0('0x10'));var hardwareService=require(_0x74f0('0x11'));var logger=require(_0x74f0('0x12'))(_0x74f0('0x13'));var utils=require(_0x74f0('0x14'));var config=require('../../config/environment');var licenseUtil=require(_0x74f0('0x15'));var db=require(_0x74f0('0x16'))['db'];function respondWithStatusCode(_0x411155,_0x11fe1e){_0x11fe1e=_0x11fe1e||0xcc;return function(_0x2ab823){if(_0x2ab823){return _0x411155[_0x74f0('0x17')](_0x11fe1e);}return _0x411155[_0x74f0('0x18')](_0x11fe1e)['end']();};}function respondWithResult(_0x240d1e,_0x11af06){_0x11af06=_0x11af06||0xc8;return function(_0x14908b){if(_0x14908b){return _0x240d1e[_0x74f0('0x18')](_0x11af06)[_0x74f0('0x19')](_0x14908b);}};}function respondWithFilteredResult(_0xf81d44,_0x43dfd1){return function(_0x17f7e4){if(_0x17f7e4){var _0x55c6d6=typeof _0x43dfd1[_0x74f0('0x1a')]===_0x74f0('0x1b')&&typeof _0x43dfd1[_0x74f0('0x1c')]===_0x74f0('0x1b');var _0xaf859f=_0x17f7e4[_0x74f0('0x1d')];var _0x392e16=_0x55c6d6?0x0:_0x43dfd1['offset'];var _0x345d61=_0x55c6d6?_0x17f7e4[_0x74f0('0x1d')]:_0x43dfd1[_0x74f0('0x1a')]+_0x43dfd1[_0x74f0('0x1c')];var _0x5bd03a;if(_0x345d61>=_0xaf859f){_0x345d61=_0xaf859f;_0x5bd03a=0xc8;}else{_0x5bd03a=0xce;}_0xf81d44[_0x74f0('0x18')](_0x5bd03a);return _0xf81d44[_0x74f0('0x1e')](_0x74f0('0x1f'),_0x392e16+'-'+_0x345d61+'/'+_0xaf859f)[_0x74f0('0x19')](_0x17f7e4);}return null;};}function patchUpdates(_0x3b2ebb){return function(_0x4b7d8c){try{jsonpatch[_0x74f0('0x20')](_0x4b7d8c,_0x3b2ebb,!![]);}catch(_0x3893a1){return BPromise['reject'](_0x3893a1);}return _0x4b7d8c[_0x74f0('0x21')]();};}function saveUpdates(_0x5e6f4c,_0x1fe9bb){return function(_0x150484){if(_0x150484){return _0x150484[_0x74f0('0x22')](_0x5e6f4c)['then'](function(_0x4ef031){return _0x4ef031;});}return null;};}function removeEntity(_0x395bf0,_0x456cf8){return function(_0x229496){if(_0x229496){return _0x229496[_0x74f0('0x23')]()[_0x74f0('0x24')](function(){var _0xd961a2=_0x229496[_0x74f0('0x25')]({'plain':!![]});var _0x6be7d3=_0x74f0('0x26');return db[_0x74f0('0x27')][_0x74f0('0x23')]({'where':{'type':_0x6be7d3,'resourceId':_0xd961a2['id']}})[_0x74f0('0x24')](function(){return _0x229496;});})['then'](function(){_0x395bf0['status'](0xcc)[_0x74f0('0x28')]();});}};}function handleEntityNotFound(_0x73cd13,_0x40e71c){return function(_0x1669ee){if(!_0x1669ee){_0x73cd13[_0x74f0('0x17')](0x194);}return _0x1669ee;};}function handleError(_0x50bcc0,_0x2ce7b5){_0x2ce7b5=_0x2ce7b5||0x1f4;return function(_0x1d6c23){logger[_0x74f0('0x29')](_0x1d6c23['stack']);if(_0x1d6c23[_0x74f0('0x2a')]){delete _0x1d6c23[_0x74f0('0x2a')];}_0x50bcc0[_0x74f0('0x18')](_0x2ce7b5)[_0x74f0('0x2b')](_0x1d6c23);};}function getMustacheRender(_0x14fa6a,_0x514645){return Mustache[_0x74f0('0x2c')](_0x14fa6a,_[_0x74f0('0x2d')](_0x514645[_0x74f0('0x2e')],{'contact':_0x514645[_0x74f0('0x2f')],'message':_0x514645[_0x74f0('0x30')],'agent':_0x514645[_0x74f0('0x31')],'interaction':_0x514645[_0x74f0('0x32')],'account':_0x514645[_0x74f0('0x33')]}))||'';}exports[_0x74f0('0x34')]=function(_0x5ea544,_0x2b6ba5){var _0x392546={},_0x2bbaf4={},_0x4b7dab={'count':0x0,'rows':[]};var _0x14f2e0=_[_0x74f0('0x35')](db[_0x74f0('0x36')][_0x74f0('0x37')],function(_0x405088){return{'name':_0x405088['fieldName'],'type':_0x405088[_0x74f0('0x38')][_0x74f0('0x39')]};});_0x2bbaf4[_0x74f0('0x3a')]=_[_0x74f0('0x35')](_0x14f2e0,_0x74f0('0x2a'));_0x2bbaf4[_0x74f0('0x3b')]=_[_0x74f0('0x3c')](_0x5ea544['query']);_0x2bbaf4[_0x74f0('0x3d')]=_[_0x74f0('0x3e')](_0x2bbaf4['model'],_0x2bbaf4[_0x74f0('0x3b')]);_0x392546[_0x74f0('0x3f')]=_[_0x74f0('0x3e')](_0x2bbaf4['model'],qs[_0x74f0('0x40')](_0x5ea544[_0x74f0('0x3b')]['fields']));_0x392546['attributes']=_0x392546[_0x74f0('0x3f')][_0x74f0('0x41')]?_0x392546[_0x74f0('0x3f')]:_0x2bbaf4[_0x74f0('0x3a')];if(!_0x5ea544[_0x74f0('0x3b')][_0x74f0('0x42')](_0x74f0('0x43'))){_0x392546[_0x74f0('0x1c')]=qs['limit'](_0x5ea544['query'][_0x74f0('0x1c')]);_0x392546['offset']=qs[_0x74f0('0x1a')](_0x5ea544[_0x74f0('0x3b')][_0x74f0('0x1a')]);}_0x392546['order']=qs[_0x74f0('0x44')](_0x5ea544[_0x74f0('0x3b')][_0x74f0('0x44')]);_0x392546[_0x74f0('0x45')]=qs[_0x74f0('0x3d')](_[_0x74f0('0x46')](_0x5ea544[_0x74f0('0x3b')],_0x2bbaf4['filters']),_0x14f2e0);if(_0x5ea544[_0x74f0('0x3b')][_0x74f0('0x47')]){_0x392546['where']=_['merge'](_0x392546[_0x74f0('0x45')],{'$or':_[_0x74f0('0x35')](_0x14f2e0,function(_0x47c2d9){if(_0x47c2d9[_0x74f0('0x38')]!==_0x74f0('0x48')){var _0x397702={};_0x397702[_0x47c2d9[_0x74f0('0x2a')]]={'$like':'%'+_0x5ea544['query']['filter']+'%'};return _0x397702;}})});}_0x392546=_[_0x74f0('0x2d')]({},_0x392546,_0x5ea544[_0x74f0('0x49')]);var _0x47abf5={'where':_0x392546[_0x74f0('0x45')]};return db['AnalyticExtractedReport']['count'](_0x47abf5)[_0x74f0('0x24')](function(_0x887715){_0x4b7dab['count']=_0x887715;if(_0x5ea544[_0x74f0('0x3b')][_0x74f0('0x4a')]){_0x392546[_0x74f0('0x4b')]=[{'all':!![]}];}return db[_0x74f0('0x36')][_0x74f0('0x4c')](_0x392546);})[_0x74f0('0x24')](function(_0x3ce613){_0x4b7dab[_0x74f0('0x4d')]=_0x3ce613;return _0x4b7dab;})[_0x74f0('0x24')](respondWithFilteredResult(_0x2b6ba5,_0x392546))[_0x74f0('0x4e')](handleError(_0x2b6ba5,null));};exports['show']=function(_0x34efd1,_0x2821ae){var _0x36bdd3={'raw':!![],'where':{'id':_0x34efd1['params']['id']}},_0x5dd6ea={};_0x5dd6ea[_0x74f0('0x3a')]=_[_0x74f0('0x3c')](db[_0x74f0('0x36')][_0x74f0('0x37')]);_0x5dd6ea['query']=_[_0x74f0('0x3c')](_0x34efd1[_0x74f0('0x3b')]);_0x5dd6ea[_0x74f0('0x3d')]=_['intersection'](_0x5dd6ea[_0x74f0('0x3a')],_0x5dd6ea[_0x74f0('0x3b')]);_0x36bdd3[_0x74f0('0x3f')]=_['intersection'](_0x5dd6ea[_0x74f0('0x3a')],qs['fields'](_0x34efd1['query']['fields']));_0x36bdd3['attributes']=_0x36bdd3[_0x74f0('0x3f')][_0x74f0('0x41')]?_0x36bdd3[_0x74f0('0x3f')]:_0x5dd6ea['model'];if(_0x34efd1[_0x74f0('0x3b')][_0x74f0('0x4a')]){_0x36bdd3[_0x74f0('0x4b')]=[{'all':!![]}];}_0x36bdd3=_['merge']({},_0x36bdd3,_0x34efd1[_0x74f0('0x49')]);return db[_0x74f0('0x36')][_0x74f0('0x4f')](_0x36bdd3)['then'](handleEntityNotFound(_0x2821ae,null))[_0x74f0('0x24')](respondWithResult(_0x2821ae,null))[_0x74f0('0x4e')](handleError(_0x2821ae,null));};exports[_0x74f0('0x50')]=function(_0x219ba9,_0x531d03){return db['AnalyticExtractedReport'][_0x74f0('0x50')](_0x219ba9[_0x74f0('0x2e')],{})['then'](function(_0x517db4){var _0x4e25d1=_0x219ba9[_0x74f0('0x51')][_0x74f0('0x25')]({'plain':!![]});if(!_0x4e25d1)throw new Error('Unable\x20to\x20retrieve\x20the\x20current\x20user');if(_0x4e25d1[_0x74f0('0x52')]===_0x74f0('0x51')){var _0x52d2af=_0x517db4[_0x74f0('0x25')]({'plain':!![]});var _0x4b3d44=_0x74f0('0x26');return db[_0x74f0('0x53')]['find']({'where':{'name':_0x4b3d44,'userProfileId':_0x4e25d1['userProfileId']},'raw':!![]})['then'](function(_0x40e6f9){if(_0x40e6f9&&_0x40e6f9[_0x74f0('0x54')]===0x0){return db['UserProfileResource'][_0x74f0('0x50')]({'name':_0x52d2af[_0x74f0('0x2a')],'resourceId':_0x52d2af['id'],'type':_0x40e6f9[_0x74f0('0x2a')],'sectionId':_0x40e6f9['id']},{})[_0x74f0('0x24')](function(){return _0x517db4;});}else{return _0x517db4;}})[_0x74f0('0x4e')](function(_0x9cfb71){logger['error'](_0x74f0('0x55'),_0x9cfb71);throw _0x9cfb71;});}return _0x517db4;})[_0x74f0('0x24')](respondWithResult(_0x531d03,0xc9))['catch'](handleError(_0x531d03,null));};exports[_0x74f0('0x22')]=function(_0x1934ca,_0x42ec46){if(_0x1934ca[_0x74f0('0x2e')]['id']){delete _0x1934ca[_0x74f0('0x2e')]['id'];}return db[_0x74f0('0x36')][_0x74f0('0x4f')]({'where':{'id':_0x1934ca['params']['id']}})['then'](handleEntityNotFound(_0x42ec46,null))[_0x74f0('0x24')](saveUpdates(_0x1934ca['body'],null))[_0x74f0('0x24')](respondWithResult(_0x42ec46,null))[_0x74f0('0x4e')](handleError(_0x42ec46,null));};exports[_0x74f0('0x23')]=function(_0x535d6d,_0x43473e){return db[_0x74f0('0x36')][_0x74f0('0x4f')]({'where':{'id':_0x535d6d[_0x74f0('0x56')]['id']}})[_0x74f0('0x24')](handleEntityNotFound(_0x43473e,null))[_0x74f0('0x24')](removeEntity(_0x43473e,null))[_0x74f0('0x4e')](handleError(_0x43473e,null));};exports[_0x74f0('0x57')]=function(_0x1fd0a5,_0x3fa4ae,_0x3fa8eb){return db[_0x74f0('0x36')]['find']({'where':{'id':_0x1fd0a5[_0x74f0('0x56')]['id']},'attributes':[_0x74f0('0x2a'),_0x74f0('0x58'),'savename',_0x74f0('0x59')],'raw':!![]})['then'](handleEntityNotFound(_0x3fa4ae,null))['then'](function(_0xb640be){if(_0xb640be){if(!fs[_0x74f0('0x5a')](path[_0x74f0('0x5b')](config['root'],'server/files/reports',_0xb640be['savename']))){throw new db[(_0x74f0('0x5c'))][(_0x74f0('0x5d'))](_0x74f0('0x5e'));}if(_0x1fd0a5[_0x74f0('0x3b')][_0x74f0('0x5f')]){return _0x3fa4ae['sendStatus'](0xc8);}return _0x3fa4ae[_0x74f0('0x57')](path[_0x74f0('0x5b')](config[_0x74f0('0x60')],_0x74f0('0x61'),_0xb640be['savename']),util['format'](_0x74f0('0x62'),_0xb640be[_0x74f0('0x58')]||_0xb640be[_0x74f0('0x2a')],_0xb640be['output'][_0x74f0('0x63')]()));}})[_0x74f0('0x4e')](handleError(_0x3fa4ae,null));};