Built motion from commit d1eab355.|2.6.28
[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 _0x13f9=['to-csv','ejs','fs-extra','squel','crypto','jsforce','desk.js','querystring','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/environment','../../mysqldb','sendStatus','status','end','json','offset','undefined','count','limit','set','Content-Range','apply','reject','save','update','then','destroy','get','ExtractedReports','UserProfileResource','error','stack','send','map','AnalyticExtractedReport','rawAttributes','model','name','query','filters','intersection','attributes','fields','length','hasOwnProperty','nolimit','order','sort','where','pick','filter','merge','includeAll','rows','catch','show','params','keys','options','find','create','body','Unable\x20to\x20retrieve\x20the\x20current\x20user','UserProfileSection','userProfileId','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','download','basename','savename','existsSync','join','root','server/files/reports','ValidationError','Report\x20not\x20found\x20or\x20empty','exists','format','output','eml-format','zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','path','sox'];(function(_0x122096,_0x60c295){var _0x3d1966=function(_0x3d569e){while(--_0x3d569e){_0x122096['push'](_0x122096['shift']());}};_0x3d1966(++_0x60c295);}(_0x13f9,0x111));var _0x913f=function(_0x167cc1,_0x56ef5a){_0x167cc1=_0x167cc1-0x0;var _0x10bc4b=_0x13f9[_0x167cc1];return _0x10bc4b;};'use strict';var emlformat=require(_0x913f('0x0'));var rimraf=require('rimraf');var zipdir=require(_0x913f('0x1'));var jsonpatch=require(_0x913f('0x2'));var rp=require(_0x913f('0x3'));var moment=require(_0x913f('0x4'));var BPromise=require(_0x913f('0x5'));var Mustache=require(_0x913f('0x6'));var util=require('util');var path=require(_0x913f('0x7'));var sox=require(_0x913f('0x8'));var csv=require(_0x913f('0x9'));var ejs=require(_0x913f('0xa'));var fs=require('fs');var fs_extra=require(_0x913f('0xb'));var _=require('lodash');var squel=require(_0x913f('0xc'));var crypto=require(_0x913f('0xd'));var jsforce=require(_0x913f('0xe'));var deskjs=require(_0x913f('0xf'));var toCsv=require(_0x913f('0x9'));var querystring=require(_0x913f('0x10'));var Papa=require('papaparse');var Redis=require(_0x913f('0x11'));var authService=require(_0x913f('0x12'));var qs=require(_0x913f('0x13'));var as=require(_0x913f('0x14'));var hardwareService=require(_0x913f('0x15'));var logger=require(_0x913f('0x16'))(_0x913f('0x17'));var utils=require(_0x913f('0x18'));var config=require(_0x913f('0x19'));var licenseUtil=require('../../config/license/util');var db=require(_0x913f('0x1a'))['db'];function respondWithStatusCode(_0x314193,_0x1599e8){_0x1599e8=_0x1599e8||0xcc;return function(_0x3b5f28){if(_0x3b5f28){return _0x314193[_0x913f('0x1b')](_0x1599e8);}return _0x314193[_0x913f('0x1c')](_0x1599e8)[_0x913f('0x1d')]();};}function respondWithResult(_0x4d9623,_0x11fa5d){_0x11fa5d=_0x11fa5d||0xc8;return function(_0x515313){if(_0x515313){return _0x4d9623[_0x913f('0x1c')](_0x11fa5d)[_0x913f('0x1e')](_0x515313);}};}function respondWithFilteredResult(_0x29a5d7,_0x49741f){return function(_0x3fc036){if(_0x3fc036){var _0x2a2613=typeof _0x49741f[_0x913f('0x1f')]===_0x913f('0x20')&&typeof _0x49741f['limit']==='undefined';var _0x496757=_0x3fc036[_0x913f('0x21')];var _0x400efd=_0x2a2613?0x0:_0x49741f[_0x913f('0x1f')];var _0x12260d=_0x2a2613?_0x3fc036[_0x913f('0x21')]:_0x49741f[_0x913f('0x1f')]+_0x49741f[_0x913f('0x22')];var _0x35e072;if(_0x12260d>=_0x496757){_0x12260d=_0x496757;_0x35e072=0xc8;}else{_0x35e072=0xce;}_0x29a5d7[_0x913f('0x1c')](_0x35e072);return _0x29a5d7[_0x913f('0x23')](_0x913f('0x24'),_0x400efd+'-'+_0x12260d+'/'+_0x496757)['json'](_0x3fc036);}return null;};}function patchUpdates(_0x403493){return function(_0xf5c204){try{jsonpatch[_0x913f('0x25')](_0xf5c204,_0x403493,!![]);}catch(_0x4bca30){return BPromise[_0x913f('0x26')](_0x4bca30);}return _0xf5c204[_0x913f('0x27')]();};}function saveUpdates(_0x30d2e2,_0xdaa146){return function(_0xa7083c){if(_0xa7083c){return _0xa7083c[_0x913f('0x28')](_0x30d2e2)[_0x913f('0x29')](function(_0x30e010){return _0x30e010;});}return null;};}function removeEntity(_0x3c1b9f,_0x35de63){return function(_0x378ea2){if(_0x378ea2){return _0x378ea2[_0x913f('0x2a')]()['then'](function(){var _0x57c3bf=_0x378ea2[_0x913f('0x2b')]({'plain':!![]});var _0x19c8be=_0x913f('0x2c');return db[_0x913f('0x2d')]['destroy']({'where':{'type':_0x19c8be,'resourceId':_0x57c3bf['id']}})[_0x913f('0x29')](function(){return _0x378ea2;});})['then'](function(){_0x3c1b9f[_0x913f('0x1c')](0xcc)[_0x913f('0x1d')]();});}};}function handleEntityNotFound(_0x34adda,_0x465f8b){return function(_0x1db481){if(!_0x1db481){_0x34adda['sendStatus'](0x194);}return _0x1db481;};}function handleError(_0x485369,_0x15ba24){_0x15ba24=_0x15ba24||0x1f4;return function(_0x3260c0){logger[_0x913f('0x2e')](_0x3260c0[_0x913f('0x2f')]);if(_0x3260c0['name']){delete _0x3260c0['name'];}_0x485369[_0x913f('0x1c')](_0x15ba24)[_0x913f('0x30')](_0x3260c0);};}exports['index']=function(_0x2d4f18,_0x56f61a){var _0x4f81b0={},_0x45a467={},_0x3c4535={'count':0x0,'rows':[]};var _0x2ad431=_[_0x913f('0x31')](db[_0x913f('0x32')][_0x913f('0x33')],function(_0x1c05df){return{'name':_0x1c05df['fieldName'],'type':_0x1c05df['type']['key']};});_0x45a467[_0x913f('0x34')]=_[_0x913f('0x31')](_0x2ad431,_0x913f('0x35'));_0x45a467[_0x913f('0x36')]=_['keys'](_0x2d4f18['query']);_0x45a467[_0x913f('0x37')]=_[_0x913f('0x38')](_0x45a467[_0x913f('0x34')],_0x45a467[_0x913f('0x36')]);_0x4f81b0[_0x913f('0x39')]=_[_0x913f('0x38')](_0x45a467[_0x913f('0x34')],qs[_0x913f('0x3a')](_0x2d4f18['query'][_0x913f('0x3a')]));_0x4f81b0[_0x913f('0x39')]=_0x4f81b0[_0x913f('0x39')][_0x913f('0x3b')]?_0x4f81b0[_0x913f('0x39')]:_0x45a467['model'];if(!_0x2d4f18[_0x913f('0x36')][_0x913f('0x3c')](_0x913f('0x3d'))){_0x4f81b0[_0x913f('0x22')]=qs[_0x913f('0x22')](_0x2d4f18[_0x913f('0x36')][_0x913f('0x22')]);_0x4f81b0[_0x913f('0x1f')]=qs[_0x913f('0x1f')](_0x2d4f18[_0x913f('0x36')][_0x913f('0x1f')]);}_0x4f81b0[_0x913f('0x3e')]=qs[_0x913f('0x3f')](_0x2d4f18['query'][_0x913f('0x3f')]);_0x4f81b0[_0x913f('0x40')]=qs[_0x913f('0x37')](_[_0x913f('0x41')](_0x2d4f18[_0x913f('0x36')],_0x45a467['filters']),_0x2ad431);if(_0x2d4f18['query'][_0x913f('0x42')]){_0x4f81b0[_0x913f('0x40')]=_[_0x913f('0x43')](_0x4f81b0[_0x913f('0x40')],{'$or':_[_0x913f('0x31')](_0x2ad431,function(_0x15a1fa){if(_0x15a1fa['type']!=='VIRTUAL'){var _0x1990c5={};_0x1990c5[_0x15a1fa['name']]={'$like':'%'+_0x2d4f18[_0x913f('0x36')][_0x913f('0x42')]+'%'};return _0x1990c5;}})});}_0x4f81b0=_[_0x913f('0x43')]({},_0x4f81b0,_0x2d4f18['options']);var _0x57368a={'where':_0x4f81b0['where']};return db[_0x913f('0x32')][_0x913f('0x21')](_0x57368a)['then'](function(_0x13efee){_0x3c4535['count']=_0x13efee;if(_0x2d4f18[_0x913f('0x36')][_0x913f('0x44')]){_0x4f81b0['include']=[{'all':!![]}];}return db['AnalyticExtractedReport']['findAll'](_0x4f81b0);})['then'](function(_0x388594){_0x3c4535[_0x913f('0x45')]=_0x388594;return _0x3c4535;})[_0x913f('0x29')](respondWithFilteredResult(_0x56f61a,_0x4f81b0))[_0x913f('0x46')](handleError(_0x56f61a,null));};exports[_0x913f('0x47')]=function(_0x5d405f,_0xa25475){var _0x4d2cac={'raw':!![],'where':{'id':_0x5d405f[_0x913f('0x48')]['id']}},_0x1dd529={};_0x1dd529[_0x913f('0x34')]=_[_0x913f('0x49')](db[_0x913f('0x32')][_0x913f('0x33')]);_0x1dd529[_0x913f('0x36')]=_[_0x913f('0x49')](_0x5d405f[_0x913f('0x36')]);_0x1dd529[_0x913f('0x37')]=_[_0x913f('0x38')](_0x1dd529[_0x913f('0x34')],_0x1dd529[_0x913f('0x36')]);_0x4d2cac[_0x913f('0x39')]=_[_0x913f('0x38')](_0x1dd529[_0x913f('0x34')],qs['fields'](_0x5d405f[_0x913f('0x36')]['fields']));_0x4d2cac['attributes']=_0x4d2cac[_0x913f('0x39')][_0x913f('0x3b')]?_0x4d2cac[_0x913f('0x39')]:_0x1dd529[_0x913f('0x34')];if(_0x5d405f[_0x913f('0x36')][_0x913f('0x44')]){_0x4d2cac['include']=[{'all':!![]}];}_0x4d2cac=_[_0x913f('0x43')]({},_0x4d2cac,_0x5d405f[_0x913f('0x4a')]);return db[_0x913f('0x32')][_0x913f('0x4b')](_0x4d2cac)[_0x913f('0x29')](handleEntityNotFound(_0xa25475,null))[_0x913f('0x29')](respondWithResult(_0xa25475,null))[_0x913f('0x46')](handleError(_0xa25475,null));};exports[_0x913f('0x4c')]=function(_0x581468,_0x3cabc9){return db['AnalyticExtractedReport'][_0x913f('0x4c')](_0x581468[_0x913f('0x4d')],{})['then'](function(_0x1b1f7b){var _0x3cb630=_0x581468['user'][_0x913f('0x2b')]({'plain':!![]});if(!_0x3cb630)throw new Error(_0x913f('0x4e'));if(_0x3cb630['role']==='user'){var _0x333382=_0x1b1f7b[_0x913f('0x2b')]({'plain':!![]});var _0x5aa2ca='ExtractedReports';return db[_0x913f('0x4f')][_0x913f('0x4b')]({'where':{'name':_0x5aa2ca,'userProfileId':_0x3cb630[_0x913f('0x50')]},'raw':!![]})['then'](function(_0x19dd8b){if(_0x19dd8b&&_0x19dd8b['autoAssociation']===0x0){return db[_0x913f('0x2d')][_0x913f('0x4c')]({'name':_0x333382[_0x913f('0x35')],'resourceId':_0x333382['id'],'type':_0x19dd8b[_0x913f('0x35')],'sectionId':_0x19dd8b['id']},{})[_0x913f('0x29')](function(){return _0x1b1f7b;});}else{return _0x1b1f7b;}})['catch'](function(_0x5510d7){logger[_0x913f('0x2e')](_0x913f('0x51'),_0x5510d7);throw _0x5510d7;});}return _0x1b1f7b;})[_0x913f('0x29')](respondWithResult(_0x3cabc9,0xc9))[_0x913f('0x46')](handleError(_0x3cabc9,null));};exports['update']=function(_0x3a3bb9,_0x2180ba){if(_0x3a3bb9[_0x913f('0x4d')]['id']){delete _0x3a3bb9[_0x913f('0x4d')]['id'];}return db[_0x913f('0x32')]['find']({'where':{'id':_0x3a3bb9['params']['id']}})[_0x913f('0x29')](handleEntityNotFound(_0x2180ba,null))[_0x913f('0x29')](saveUpdates(_0x3a3bb9[_0x913f('0x4d')],null))['then'](respondWithResult(_0x2180ba,null))['catch'](handleError(_0x2180ba,null));};exports['destroy']=function(_0x353592,_0x23408b){return db[_0x913f('0x32')][_0x913f('0x4b')]({'where':{'id':_0x353592[_0x913f('0x48')]['id']}})[_0x913f('0x29')](handleEntityNotFound(_0x23408b,null))[_0x913f('0x29')](removeEntity(_0x23408b,null))[_0x913f('0x46')](handleError(_0x23408b,null));};exports[_0x913f('0x52')]=function(_0x5dca1c,_0x2961c9,_0x1df12a){return db['AnalyticExtractedReport']['find']({'where':{'id':_0x5dca1c['params']['id']},'attributes':['name',_0x913f('0x53'),_0x913f('0x54'),'output'],'raw':!![]})[_0x913f('0x29')](handleEntityNotFound(_0x2961c9,null))[_0x913f('0x29')](function(_0xeaee14){if(_0xeaee14){if(!fs[_0x913f('0x55')](path[_0x913f('0x56')](config[_0x913f('0x57')],_0x913f('0x58'),_0xeaee14[_0x913f('0x54')]))){throw new db['Sequelize'][(_0x913f('0x59'))](_0x913f('0x5a'));}if(_0x5dca1c[_0x913f('0x36')][_0x913f('0x5b')]){return _0x2961c9['sendStatus'](0xc8);}return _0x2961c9[_0x913f('0x52')](path['join'](config['root'],_0x913f('0x58'),_0xeaee14[_0x913f('0x54')]),util[_0x913f('0x5c')]('%s.%s',_0xeaee14[_0x913f('0x53')]||_0xeaee14[_0x913f('0x35')],_0xeaee14[_0x913f('0x5d')]['toLowerCase']()));}})[_0x913f('0x46')](handleError(_0x2961c9,null));};