Built motion from commit (unavailable).|2.5.21
[motion2.git] / server / api / analyticCustomReport / analyticCustomReport.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 _0xfcbb=['fieldName','type','model','query','filters','intersection','attributes','length','hasOwnProperty','nolimit','sort','where','filter','merge','options','includeAll','include','findAll','catch','show','keys','fields','create','body','role','Reports','find','autoAssociation','join','left_join','right_join','COUNT(%s)','COUNT(DISTINCT\x20%s)','MAX(%s)','MIN(%s)','GROUP_CONCAT(%s\x20ORDER\x20BY\x20%s\x20ASC)','SEC_TO_TIME(%s)','DATE(%s)','HOUR(%s)','ROUND(%s)','UNIX_TIMESTAMP(%s)','includes','IS\x20NULL','IS\x20NOT\x20NULL','IS\x20EMPTY','IS\x20NOT\x20EMPTY','rules','operator','group','field','condition','value','getFields','ValidationErrorItem','Please\x20insert\x20at\x20least\x20one\x20field','AnalyticMetric','keyBy','select','table','MetricId','function','replace','format','groupBy','orderBy','DESC','alias','joins','parse','foreignTable','foreignKey','conditions','clone','debug','Limited\x20Query:','sequelize','toString','SELECT','cross_join','SUM(%s)','GROUP_CONCAT(%s)','GROUP_CONCAT(%s\x20ORDER\x20BY\x20%s\x20DESC)','Sequelize','parentKey','%s.%s\x20=\x20%s.%s','web','fullPath','%s-%s.%s','randomstring','generate','output','csv','custom','AnalyticExtractedReport','startDate','endDate','Run\x20Query:','child_process','fork','../../components/export/','savename','from','COUNT(*)','QueryTypes','run','params','runReport','metric','order','html-pdf','eml-format','rimraf','request-promise','moment','bluebird','mustache','util','to-csv','ejs','lodash','squel','crypto','jsforce','desk.js','querystring','papaparse','ioredis','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/license/util','../../mysqldb','dbH','sendStatus','status','end','json','count','offset','limit','set','Content-Range','reject','update','then','destroy','get','CustomReports','UserProfileResource','error','stack','name','send','index','map','AnalyticCustomReport','rawAttributes'];(function(_0x40220a,_0x22feb1){var _0x3903cc=function(_0x33a296){while(--_0x33a296){_0x40220a['push'](_0x40220a['shift']());}};_0x3903cc(++_0x22feb1);}(_0xfcbb,0x10d));var _0xbfcb=function(_0xf9b2a5,_0x4987f7){_0xf9b2a5=_0xf9b2a5-0x0;var _0x2ec89d=_0xfcbb[_0xf9b2a5];return _0x2ec89d;};'use strict';var pdf=require(_0xbfcb('0x0'));var emlformat=require(_0xbfcb('0x1'));var rimraf=require(_0xbfcb('0x2'));var zipdir=require('zip-dir');var jsonpatch=require('fast-json-patch');var rp=require(_0xbfcb('0x3'));var moment=require(_0xbfcb('0x4'));var BPromise=require(_0xbfcb('0x5'));var Mustache=require(_0xbfcb('0x6'));var util=require(_0xbfcb('0x7'));var path=require('path');var sox=require('sox');var csv=require(_0xbfcb('0x8'));var ejs=require(_0xbfcb('0x9'));var fs=require('fs');var _=require(_0xbfcb('0xa'));var squel=require(_0xbfcb('0xb'));var crypto=require(_0xbfcb('0xc'));var jsforce=require(_0xbfcb('0xd'));var deskjs=require(_0xbfcb('0xe'));var toCsv=require('to-csv');var querystring=require(_0xbfcb('0xf'));var Papa=require(_0xbfcb('0x10'));var Redis=require(_0xbfcb('0x11'));var authService=require('../../components/auth/service');var qs=require(_0xbfcb('0x12'));var as=require(_0xbfcb('0x13'));var hardwareService=require(_0xbfcb('0x14'));var logger=require(_0xbfcb('0x15'))(_0xbfcb('0x16'));var utils=require(_0xbfcb('0x17'));var config=require('../../config/environment');var licenseUtil=require(_0xbfcb('0x18'));var db=require(_0xbfcb('0x19'))['db'];var dbH=require(_0xbfcb('0x19'))[_0xbfcb('0x1a')];function respondWithStatusCode(_0x4ba4f1,_0x3dc54a){_0x3dc54a=_0x3dc54a||0xcc;return function(_0x53eeaf){if(_0x53eeaf){return _0x4ba4f1[_0xbfcb('0x1b')](_0x3dc54a);}return _0x4ba4f1[_0xbfcb('0x1c')](_0x3dc54a)[_0xbfcb('0x1d')]();};}function respondWithResult(_0x1047c4,_0x2074d4){_0x2074d4=_0x2074d4||0xc8;return function(_0xe47707){if(_0xe47707){return _0x1047c4[_0xbfcb('0x1c')](_0x2074d4)[_0xbfcb('0x1e')](_0xe47707);}};}function respondWithFilteredResult(_0x90ed8c,_0x339347){return function(_0x2c4a85){if(_0x2c4a85){var _0x3f847f=_0x2c4a85[_0xbfcb('0x1f')],_0x26f335=_0x339347[_0xbfcb('0x20')],_0x41dc15=_0x339347[_0xbfcb('0x20')]+_0x339347[_0xbfcb('0x21')],_0x318d07;if(_0x41dc15>=_0x3f847f){_0x41dc15=_0x3f847f;_0x318d07=0xc8;}else{_0x318d07=0xce;}_0x90ed8c[_0xbfcb('0x1c')](_0x318d07);return _0x90ed8c[_0xbfcb('0x22')](_0xbfcb('0x23'),_0x26f335+'-'+_0x41dc15+'/'+_0x3f847f)['json'](_0x2c4a85);}return null;};}function patchUpdates(_0x26451d){return function(_0x37263b){try{jsonpatch['apply'](_0x37263b,_0x26451d,!![]);}catch(_0xa3c089){return BPromise[_0xbfcb('0x24')](_0xa3c089);}return _0x37263b['save']();};}function saveUpdates(_0x58f16b,_0x4199c5){return function(_0x45f0a0){if(_0x45f0a0){return _0x45f0a0[_0xbfcb('0x25')](_0x58f16b)[_0xbfcb('0x26')](function(_0x1c5924){return _0x1c5924;});}return null;};}function removeEntity(_0x56c158,_0x365d6c){return function(_0x423bba){if(_0x423bba){return _0x423bba[_0xbfcb('0x27')]()[_0xbfcb('0x26')](function(){var _0x1651e5=_0x423bba[_0xbfcb('0x28')]({'plain':!![]});var _0x69c8e9=_0xbfcb('0x29');return db[_0xbfcb('0x2a')][_0xbfcb('0x27')]({'where':{'type':_0x69c8e9,'resourceId':_0x1651e5['id']}})[_0xbfcb('0x26')](function(){return _0x423bba;});})[_0xbfcb('0x26')](function(){_0x56c158[_0xbfcb('0x1c')](0xcc)[_0xbfcb('0x1d')]();});}};}function handleEntityNotFound(_0x3e954d,_0x2a1272){return function(_0x5cd703){if(!_0x5cd703){_0x3e954d[_0xbfcb('0x1b')](0x194);}return _0x5cd703;};}function handleError(_0x2881f8,_0x4cf7ca){_0x4cf7ca=_0x4cf7ca||0x1f4;return function(_0x64a69){logger[_0xbfcb('0x2b')](_0x64a69[_0xbfcb('0x2c')]);if(_0x64a69[_0xbfcb('0x2d')]){delete _0x64a69[_0xbfcb('0x2d')];}_0x2881f8[_0xbfcb('0x1c')](_0x4cf7ca)[_0xbfcb('0x2e')](_0x64a69);};}exports[_0xbfcb('0x2f')]=function(_0x434764,_0x27606f){var _0x59719b={},_0x17cd0e={},_0x3aa3cd={'count':0x0,'rows':[]};var _0x43c3c3=_[_0xbfcb('0x30')](db[_0xbfcb('0x31')][_0xbfcb('0x32')],function(_0x286fb7){return{'name':_0x286fb7[_0xbfcb('0x33')],'type':_0x286fb7[_0xbfcb('0x34')]['key']};});_0x17cd0e[_0xbfcb('0x35')]=_[_0xbfcb('0x30')](_0x43c3c3,_0xbfcb('0x2d'));_0x17cd0e[_0xbfcb('0x36')]=_['keys'](_0x434764['query']);_0x17cd0e[_0xbfcb('0x37')]=_[_0xbfcb('0x38')](_0x17cd0e[_0xbfcb('0x35')],_0x17cd0e[_0xbfcb('0x36')]);_0x59719b[_0xbfcb('0x39')]=_[_0xbfcb('0x38')](_0x17cd0e[_0xbfcb('0x35')],qs['fields'](_0x434764['query']['fields']));_0x59719b[_0xbfcb('0x39')]=_0x59719b['attributes'][_0xbfcb('0x3a')]?_0x59719b[_0xbfcb('0x39')]:_0x17cd0e['model'];if(!_0x434764['query'][_0xbfcb('0x3b')](_0xbfcb('0x3c'))){_0x59719b[_0xbfcb('0x21')]=qs[_0xbfcb('0x21')](_0x434764['query'][_0xbfcb('0x21')]);_0x59719b[_0xbfcb('0x20')]=qs['offset'](_0x434764['query'][_0xbfcb('0x20')]);}_0x59719b['order']=qs['sort'](_0x434764[_0xbfcb('0x36')][_0xbfcb('0x3d')]);_0x59719b[_0xbfcb('0x3e')]=qs[_0xbfcb('0x37')](_['pick'](_0x434764[_0xbfcb('0x36')],_0x17cd0e[_0xbfcb('0x37')]),_0x43c3c3);if(_0x434764[_0xbfcb('0x36')][_0xbfcb('0x3f')]){_0x59719b[_0xbfcb('0x3e')]=_[_0xbfcb('0x40')](_0x59719b['where'],{'$or':_[_0xbfcb('0x30')](_0x43c3c3,function(_0x4d53fd){if(_0x4d53fd[_0xbfcb('0x34')]!=='VIRTUAL'){var _0x809fde={};_0x809fde[_0x4d53fd[_0xbfcb('0x2d')]]={'$like':'%'+_0x434764[_0xbfcb('0x36')][_0xbfcb('0x3f')]+'%'};return _0x809fde;}})});}_0x59719b=_[_0xbfcb('0x40')]({},_0x59719b,_0x434764[_0xbfcb('0x41')]);var _0x453714={'where':_0x59719b[_0xbfcb('0x3e')]};return db[_0xbfcb('0x31')][_0xbfcb('0x1f')](_0x453714)['then'](function(_0x2be840){_0x3aa3cd[_0xbfcb('0x1f')]=_0x2be840;if(_0x434764['query'][_0xbfcb('0x42')]){_0x59719b[_0xbfcb('0x43')]=[{'all':!![]}];}return db[_0xbfcb('0x31')][_0xbfcb('0x44')](_0x59719b);})[_0xbfcb('0x26')](function(_0x205ca3){_0x3aa3cd['rows']=_0x205ca3;return _0x3aa3cd;})[_0xbfcb('0x26')](respondWithFilteredResult(_0x27606f,_0x59719b))[_0xbfcb('0x45')](handleError(_0x27606f,null));};exports[_0xbfcb('0x46')]=function(_0x4a28f2,_0x26b3fd){var _0x21c73f={'raw':![],'where':{'id':_0x4a28f2['params']['id']}},_0xa37d7f={};_0xa37d7f[_0xbfcb('0x35')]=_[_0xbfcb('0x47')](db['AnalyticCustomReport']['rawAttributes']);_0xa37d7f[_0xbfcb('0x36')]=_[_0xbfcb('0x47')](_0x4a28f2['query']);_0xa37d7f[_0xbfcb('0x37')]=_[_0xbfcb('0x38')](_0xa37d7f['model'],_0xa37d7f[_0xbfcb('0x36')]);_0x21c73f[_0xbfcb('0x39')]=_[_0xbfcb('0x38')](_0xa37d7f[_0xbfcb('0x35')],qs[_0xbfcb('0x48')](_0x4a28f2[_0xbfcb('0x36')][_0xbfcb('0x48')]));_0x21c73f[_0xbfcb('0x39')]=_0x21c73f[_0xbfcb('0x39')][_0xbfcb('0x3a')]?_0x21c73f[_0xbfcb('0x39')]:_0xa37d7f[_0xbfcb('0x35')];if(_0x4a28f2['query'][_0xbfcb('0x42')]){_0x21c73f[_0xbfcb('0x43')]=[{'all':!![]}];}_0x21c73f=_[_0xbfcb('0x40')]({},_0x21c73f,_0x4a28f2['options']);return db[_0xbfcb('0x31')]['find'](_0x21c73f)['then'](handleEntityNotFound(_0x26b3fd,null))[_0xbfcb('0x26')](respondWithResult(_0x26b3fd,null))[_0xbfcb('0x45')](handleError(_0x26b3fd,null));};exports[_0xbfcb('0x49')]=function(_0x4d1de5,_0x40f9d9){return db['AnalyticCustomReport']['create'](_0x4d1de5[_0xbfcb('0x4a')],{})['then'](function(_0x3433ac){var _0x7afcba=_0x4d1de5['user'][_0xbfcb('0x28')]({'plain':!![]});if(!_0x7afcba)throw new Error('Unable\x20to\x20retrieve\x20the\x20current\x20user');if(_0x7afcba[_0xbfcb('0x4b')]==='user'){var _0x61b146=_0x3433ac['get']({'plain':!![]});var _0x238d74=_0xbfcb('0x4c');return db['UserProfileSection'][_0xbfcb('0x4d')]({'where':{'name':_0x238d74,'userProfileId':_0x7afcba['userProfileId']},'raw':!![]})['then'](function(_0x3b8683){if(_0x3b8683&&_0x3b8683[_0xbfcb('0x4e')]===0x0){return db[_0xbfcb('0x2a')]['create']({'name':_0x61b146['name'],'resourceId':_0x61b146['id'],'type':_0xbfcb('0x29'),'sectionId':_0x3b8683['id']},{})[_0xbfcb('0x26')](function(){return _0x3433ac;});}else{return _0x3433ac;}})[_0xbfcb('0x45')](function(_0x3d502e){logger[_0xbfcb('0x2b')]('Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s',_0x3d502e);throw _0x3d502e;});}return _0x3433ac;})[_0xbfcb('0x26')](respondWithResult(_0x40f9d9,0xc9))[_0xbfcb('0x45')](handleError(_0x40f9d9,null));};exports[_0xbfcb('0x25')]=function(_0x225c45,_0x4d1264){if(_0x225c45['body']['id']){delete _0x225c45[_0xbfcb('0x4a')]['id'];}return db['AnalyticCustomReport'][_0xbfcb('0x4d')]({'where':{'id':_0x225c45['params']['id']}})['then'](handleEntityNotFound(_0x4d1264,null))[_0xbfcb('0x26')](saveUpdates(_0x225c45['body'],null))['then'](respondWithResult(_0x4d1264,null))[_0xbfcb('0x45')](handleError(_0x4d1264,null));};exports['destroy']=function(_0x216a2a,_0x533b69){return db[_0xbfcb('0x31')][_0xbfcb('0x4d')]({'where':{'id':_0x216a2a['params']['id']}})[_0xbfcb('0x26')](handleEntityNotFound(_0x533b69,null))[_0xbfcb('0x26')](removeEntity(_0x533b69,null))[_0xbfcb('0x45')](handleError(_0x533b69,null));};exports['preview']=function(_0x3cd568,_0xc44640){var _0x2534fe=0xa;var _0x4311d9=[_0xbfcb('0x4f'),_0xbfcb('0x50'),_0xbfcb('0x51'),'cross_join'];var _0x9c9b13={'SUM':'SUM(%s)','COUNT':_0xbfcb('0x52'),'COUNT DISTINCT':_0xbfcb('0x53'),'MAX':_0xbfcb('0x54'),'MIN':_0xbfcb('0x55'),'AVG':'AVG(%s)','GROUP_CONCAT':'GROUP_CONCAT(%s)','GROUP_CONCAT ASC':_0xbfcb('0x56'),'GROUP_CONCAT DESC':'GROUP_CONCAT(%s\x20ORDER\x20BY\x20%s\x20DESC)'};var _0x2c0cbe={'SEC_TO_TIME':_0xbfcb('0x57'),'DATE':_0xbfcb('0x58'),'HOUR':_0xbfcb('0x59'),'ROUND':_0xbfcb('0x5a'),'UNIX_TIMESTAMP':_0xbfcb('0x5b')};var _0x51f8a0=function(_0x153cce){return!_[_0xbfcb('0x5c')]([_0xbfcb('0x5d'),_0xbfcb('0x5e'),_0xbfcb('0x5f'),_0xbfcb('0x60')],_0x153cce);};var _0x417030=function(_0x5d847c){if(!_0x5d847c||!_0x5d847c['rules']['length']){return'';}for(var _0x2b9a96='(',_0x14014d=0x0;_0x14014d<_0x5d847c[_0xbfcb('0x61')]['length'];_0x14014d+=0x1){_0x14014d>0x0&&(_0x2b9a96+='\x20'+_0x5d847c[_0xbfcb('0x62')]+'\x20');_0x2b9a96+=_0x5d847c[_0xbfcb('0x61')][_0x14014d][_0xbfcb('0x63')]?_0x417030(_0x5d847c['rules'][_0x14014d]['group']):_0x5d847c['rules'][_0x14014d][_0xbfcb('0x64')]+'\x20'+_0x5d847c[_0xbfcb('0x61')][_0x14014d][_0xbfcb('0x65')]+(_0x51f8a0(_0x5d847c[_0xbfcb('0x61')][_0x14014d]['condition'])?'\x20'+_0x5d847c[_0xbfcb('0x61')][_0x14014d][_0xbfcb('0x66')]:'');}return _0x2b9a96+')';};var _0x39a3cf={'where':{'id':_0x3cd568['params']['id']}},_0x5c4036,_0x4bf71a,_0x107b1b,_0x2f6065,_0x5e011a;_0x39a3cf=_['merge']({},_0x39a3cf,_0x3cd568['options']);return db[_0xbfcb('0x31')][_0xbfcb('0x4d')](_0x39a3cf)[_0xbfcb('0x26')](function(_0x2d77f9){if(_0x2d77f9){_0x5c4036=_0x2d77f9;return _0x2d77f9[_0xbfcb('0x67')]();}return null;})[_0xbfcb('0x26')](function(_0x2ea974){if(_0x2ea974){if(!_0x2ea974[_0xbfcb('0x3a')]){throw new db['Sequelize'][(_0xbfcb('0x68'))](_0xbfcb('0x69'),0x193);}_0x4bf71a=_0x2ea974;return db[_0xbfcb('0x6a')][_0xbfcb('0x44')]({'raw':!![]});}return null;})[_0xbfcb('0x26')](function(_0x3e685c){if(_0x3e685c){_0x107b1b=_[_0xbfcb('0x6b')](_0x3e685c,'id');var _0x43f60b;var _0x582b60=squel[_0xbfcb('0x6c')]();_0x582b60['from'](_0x5c4036[_0xbfcb('0x6d')]);for(var _0x2a1368=0x0;_0x2a1368<_0x4bf71a[_0xbfcb('0x3a')];_0x2a1368+=0x1){_0x43f60b=_0x4bf71a[_0x2a1368][_0xbfcb('0x6e')]?_0x107b1b[_0x4bf71a[_0x2a1368][_0xbfcb('0x6e')]]['metric']:_0x4bf71a[_0x2a1368][_0xbfcb('0x64')];_0x43f60b=_0x4bf71a[_0x2a1368][_0xbfcb('0x6f')]?_[_0xbfcb('0x70')](_0x9c9b13[_0x4bf71a[_0x2a1368][_0xbfcb('0x6f')]],'%s',_0x43f60b):_0x43f60b;_0x43f60b=_0x4bf71a[_0x2a1368][_0xbfcb('0x71')]?_['replace'](_0x2c0cbe[_0x4bf71a[_0x2a1368][_0xbfcb('0x71')]],'%s',_0x43f60b):_0x43f60b;if(_0x4bf71a[_0x2a1368][_0xbfcb('0x72')]){_0x582b60[_0xbfcb('0x63')](_0x43f60b);}_0x4bf71a[_0x2a1368][_0xbfcb('0x73')]&&_0x582b60['order'](_0x43f60b,_0x4bf71a[_0x2a1368]['orderBy']===_0xbfcb('0x74')?![]:!![]);_0x582b60[_0xbfcb('0x64')](_0x43f60b,_0x4bf71a[_0x2a1368][_0xbfcb('0x75')]||_0x43f60b);}if(_0x5c4036[_0xbfcb('0x76')]){_0x5c4036[_0xbfcb('0x76')]=JSON[_0xbfcb('0x77')](_0x5c4036[_0xbfcb('0x76')]);for(var _0x169dc8=0x0;_0x169dc8<_0x5c4036[_0xbfcb('0x76')][_0xbfcb('0x3a')];_0x169dc8+=0x1){if(_0x5c4036[_0xbfcb('0x76')][_0x169dc8][_0xbfcb('0x34')]&&_[_0xbfcb('0x5c')](_0x4311d9,_0x5c4036[_0xbfcb('0x76')][_0x169dc8]['type'])&&_0x5c4036[_0xbfcb('0x76')][_0x169dc8]['parentKey']&&_0x5c4036[_0xbfcb('0x76')][_0x169dc8][_0xbfcb('0x78')]&&_0x5c4036['joins'][_0x169dc8][_0xbfcb('0x79')]){_0x582b60[_0x5c4036[_0xbfcb('0x76')][_0x169dc8][_0xbfcb('0x34')]](_0x5c4036[_0xbfcb('0x76')][_0x169dc8]['foreignTable'],null,util[_0xbfcb('0x71')]('%s.%s\x20=\x20%s.%s',_0x5c4036[_0xbfcb('0x6d')],_0x5c4036[_0xbfcb('0x76')][_0x169dc8]['parentKey'],_0x5c4036['joins'][_0x169dc8][_0xbfcb('0x78')],_0x5c4036[_0xbfcb('0x76')][_0x169dc8][_0xbfcb('0x79')]));}}}if(_0x5c4036[_0xbfcb('0x7a')]){_0x5c4036[_0xbfcb('0x7a')]=JSON['parse'](_0x5c4036['conditions']);_0x582b60[_0xbfcb('0x3e')](_0x417030(_0x5c4036['conditions']['group']));}_0x5e011a=_0x582b60[_0xbfcb('0x7b')]();_0x582b60[_0xbfcb('0x21')](_0x2534fe);_0x582b60['offset'](0x0);logger[_0xbfcb('0x7c')](_0xbfcb('0x7d'),_0x582b60['toString']());return dbH[_0xbfcb('0x7e')][_0xbfcb('0x36')](_0x582b60[_0xbfcb('0x7f')](),{'type':dbH[_0xbfcb('0x7e')]['QueryTypes'][_0xbfcb('0x80')]});}})[_0xbfcb('0x26')](respondWithResult(_0xc44640,null))[_0xbfcb('0x45')](handleError(_0xc44640,null));};function runReport(_0x4256c4,_0x456539,_0x31b5fc){var _0x5328da=_0x456539[_0xbfcb('0x2d')];var _0x19dd1f=0xa;var _0x3d677a=[_0xbfcb('0x4f'),_0xbfcb('0x50'),_0xbfcb('0x51'),_0xbfcb('0x81')];var _0x4f8cda={'SUM':_0xbfcb('0x82'),'COUNT':_0xbfcb('0x52'),'COUNT DISTINCT':_0xbfcb('0x53'),'MAX':_0xbfcb('0x54'),'MIN':'MIN(%s)','AVG':'AVG(%s)','GROUP_CONCAT':_0xbfcb('0x83'),'GROUP_CONCAT ASC':_0xbfcb('0x56'),'GROUP_CONCAT DESC':_0xbfcb('0x84')};var _0xec7c1f={'SEC_TO_TIME':_0xbfcb('0x57'),'DATE':_0xbfcb('0x58'),'HOUR':_0xbfcb('0x59'),'ROUND':_0xbfcb('0x5a'),'UNIX_TIMESTAMP':'UNIX_TIMESTAMP(%s)'};var _0x258969=function(_0x20e222){return!_[_0xbfcb('0x5c')]([_0xbfcb('0x5d'),_0xbfcb('0x5e'),'IS\x20EMPTY',_0xbfcb('0x60')],_0x20e222);};var _0x5c9f24=function(_0x1799c){if(!_0x1799c||!_0x1799c[_0xbfcb('0x61')]['length']){return'';}for(var _0x591877='(',_0x1a5c5f=0x0;_0x1a5c5f<_0x1799c[_0xbfcb('0x61')][_0xbfcb('0x3a')];_0x1a5c5f+=0x1){_0x1a5c5f>0x0&&(_0x591877+='\x20'+_0x1799c[_0xbfcb('0x62')]+'\x20');_0x591877+=_0x1799c[_0xbfcb('0x61')][_0x1a5c5f][_0xbfcb('0x63')]?_0x5c9f24(_0x1799c['rules'][_0x1a5c5f][_0xbfcb('0x63')]):_0x1799c[_0xbfcb('0x61')][_0x1a5c5f][_0xbfcb('0x64')]+'\x20'+_0x1799c[_0xbfcb('0x61')][_0x1a5c5f][_0xbfcb('0x65')]+(_0x258969(_0x1799c[_0xbfcb('0x61')][_0x1a5c5f][_0xbfcb('0x65')])?'\x20'+_0x1799c[_0xbfcb('0x61')][_0x1a5c5f]['value']:'');}return _0x591877+')';};var _0x1157c5={'where':{'id':_0x4256c4['id']}},_0x125324,_0x5513ee,_0x5ec7d8,_0x21b6b6,_0x5d0c2f;_0x1157c5=_['merge']({},_0x1157c5,_0x31b5fc);return db[_0xbfcb('0x31')]['find'](_0x1157c5)[_0xbfcb('0x26')](function(_0x336ddc){if(_0x336ddc){_0x125324=_0x336ddc;return _0x336ddc[_0xbfcb('0x67')]();}return null;})['then'](function(_0x3edebe){if(_0x3edebe){if(!_0x3edebe[_0xbfcb('0x3a')]){throw new db[(_0xbfcb('0x85'))]['ValidationErrorItem']('Please\x20insert\x20at\x20least\x20one\x20field',0x193);}_0x5513ee=_0x3edebe;return db[_0xbfcb('0x6a')][_0xbfcb('0x44')]({'raw':!![]});}return null;})[_0xbfcb('0x26')](function(_0x4fd470){if(_0x4fd470){_0x5ec7d8=_[_0xbfcb('0x6b')](_0x4fd470,'id');var _0x4dc753;var _0x33b34a=squel[_0xbfcb('0x6c')]();_0x33b34a['from'](_0x125324[_0xbfcb('0x6d')]);for(var _0x6e7287=0x0;_0x6e7287<_0x5513ee[_0xbfcb('0x3a')];_0x6e7287+=0x1){_0x4dc753=_0x5513ee[_0x6e7287]['MetricId']?_0x5ec7d8[_0x5513ee[_0x6e7287][_0xbfcb('0x6e')]]['metric']:_0x5513ee[_0x6e7287][_0xbfcb('0x64')];_0x4dc753=_0x5513ee[_0x6e7287][_0xbfcb('0x6f')]?_[_0xbfcb('0x70')](_0x4f8cda[_0x5513ee[_0x6e7287][_0xbfcb('0x6f')]],'%s',_0x4dc753):_0x4dc753;_0x4dc753=_0x5513ee[_0x6e7287][_0xbfcb('0x71')]?_['replace'](_0xec7c1f[_0x5513ee[_0x6e7287][_0xbfcb('0x71')]],'%s',_0x4dc753):_0x4dc753;if(_0x5513ee[_0x6e7287][_0xbfcb('0x72')]){_0x33b34a['group'](_0x4dc753);}_0x5513ee[_0x6e7287][_0xbfcb('0x73')]&&_0x33b34a['order'](_0x4dc753,_0x5513ee[_0x6e7287]['orderBy']===_0xbfcb('0x74')?![]:!![]);_0x33b34a[_0xbfcb('0x64')](_0x4dc753,_0x5513ee[_0x6e7287]['alias']||_0x4dc753);}if(_0x125324[_0xbfcb('0x76')]){_0x125324[_0xbfcb('0x76')]=JSON[_0xbfcb('0x77')](_0x125324[_0xbfcb('0x76')]);for(var _0x2c2a15=0x0;_0x2c2a15<_0x125324['joins'][_0xbfcb('0x3a')];_0x2c2a15+=0x1){if(_0x125324[_0xbfcb('0x76')][_0x2c2a15][_0xbfcb('0x34')]&&_[_0xbfcb('0x5c')](_0x3d677a,_0x125324[_0xbfcb('0x76')][_0x2c2a15][_0xbfcb('0x34')])&&_0x125324[_0xbfcb('0x76')][_0x2c2a15][_0xbfcb('0x86')]&&_0x125324[_0xbfcb('0x76')][_0x2c2a15][_0xbfcb('0x78')]&&_0x125324[_0xbfcb('0x76')][_0x2c2a15][_0xbfcb('0x79')]){_0x33b34a[_0x125324[_0xbfcb('0x76')][_0x2c2a15]['type']](_0x125324[_0xbfcb('0x76')][_0x2c2a15][_0xbfcb('0x78')],null,util[_0xbfcb('0x71')](_0xbfcb('0x87'),_0x125324[_0xbfcb('0x6d')],_0x125324[_0xbfcb('0x76')][_0x2c2a15][_0xbfcb('0x86')],_0x125324[_0xbfcb('0x76')][_0x2c2a15][_0xbfcb('0x78')],_0x125324['joins'][_0x2c2a15]['foreignKey']));}}}if(_0x125324[_0xbfcb('0x7a')]){_0x125324['conditions']=JSON[_0xbfcb('0x77')](_0x125324['conditions']);_0x33b34a['where'](_0x5c9f24(_0x125324[_0xbfcb('0x7a')]['group']));}_0x5d0c2f=_0x33b34a[_0xbfcb('0x7b')]();_0x33b34a['limit'](_0x19dd1f);_0x33b34a[_0xbfcb('0x20')](0x0);return;}})[_0xbfcb('0x26')](function(){if(_0x456539['output']===_0xbfcb('0x88')){return null;}var _0x3836c8=_[_0xbfcb('0x40')](_0x456539,{'name':_0x456539[_0xbfcb('0x89')]||_0x456539[_0xbfcb('0x2d')]||_0x125324[_0xbfcb('0x2d')],'basename':_0x5328da,'savename':util[_0xbfcb('0x71')](_0xbfcb('0x8a'),_0x456539[_0xbfcb('0x2d')]||_0x125324[_0xbfcb('0x2d')],require(_0xbfcb('0x8b'))[_0xbfcb('0x8c')](0x4),_0x456539[_0xbfcb('0x8d')]||_0xbfcb('0x8e')),'reportId':_0x125324['id'],'reportType':_0xbfcb('0x8f')});return db[_0xbfcb('0x90')][_0xbfcb('0x49')](_0x3836c8,{'raw':!![]});})[_0xbfcb('0x26')](function(_0x557d6d){if(_0x125324[_0xbfcb('0x6d')]==='cdr'){_0x5d0c2f[_0xbfcb('0x3e')](_0x125324[_0xbfcb('0x6d')]+'.calldate\x20BETWEEN\x20?\x20AND\x20?',_0x456539[_0xbfcb('0x91')],_0x456539[_0xbfcb('0x92')]);}else{_0x5d0c2f[_0xbfcb('0x3e')](_0x125324[_0xbfcb('0x6d')]+'.createdAt\x20BETWEEN\x20?\x20AND\x20?',_0x456539[_0xbfcb('0x91')],_0x456539[_0xbfcb('0x92')]);}if(_0x557d6d){logger['info'](_0xbfcb('0x93'),_0x5d0c2f[_0xbfcb('0x7f')]());require(_0xbfcb('0x94'))[_0xbfcb('0x95')](path['join'](__dirname,_0xbfcb('0x96'),_0x557d6d['output']),[_0x557d6d['id'],_0x5d0c2f[_0xbfcb('0x7f')](),path[_0xbfcb('0x4f')](__dirname,'../../files/reports',_0x557d6d[_0xbfcb('0x97')]),_0x5328da]);return _0x557d6d;}else{var _0x14ca97=squel['select']();_0x14ca97[_0xbfcb('0x98')]('('+_0x5d0c2f['toString']()+')','countTable');_0x14ca97[_0xbfcb('0x64')](_0xbfcb('0x99'),_0xbfcb('0x1f'));_0x5d0c2f['limit'](_0x456539[_0xbfcb('0x21')]||_0x19dd1f);_0x5d0c2f[_0xbfcb('0x20')](_0x456539[_0xbfcb('0x20')]||0x0);return dbH[_0xbfcb('0x7e')]['query'](_0x14ca97['toString'](),{'type':dbH[_0xbfcb('0x7e')]['QueryTypes'][_0xbfcb('0x80')]})[_0xbfcb('0x26')](function(_0x157f24){if(!_0x157f24[_0xbfcb('0x3a')]){_0x21b6b6=0x0;}else{_0x21b6b6=_0x157f24[0x0]['count']||0x0;}return dbH[_0xbfcb('0x7e')]['query'](_0x5d0c2f[_0xbfcb('0x7f')](),{'type':dbH['sequelize'][_0xbfcb('0x9a')]['SELECT']});})[_0xbfcb('0x26')](function(_0x5dff66){return{'rows':_0x5dff66||[],'count':_0x21b6b6};});}});}exports[_0xbfcb('0x9b')]=function(_0x51a1db,_0x556993){return runReport(_0x51a1db[_0xbfcb('0x9c')],_0x51a1db[_0xbfcb('0x36')],_0x51a1db['options'])[_0xbfcb('0x26')](respondWithResult(_0x556993,null))[_0xbfcb('0x45')](handleError(_0x556993,null));};exports[_0xbfcb('0x9d')]=runReport;exports[_0xbfcb('0x36')]=function(_0x1f4f0a,_0x3e6d15){var _0x27fde4=0xa;var _0x454abf=[_0xbfcb('0x4f'),_0xbfcb('0x50'),_0xbfcb('0x51'),_0xbfcb('0x81')];var _0x4ed66b={'SUM':_0xbfcb('0x82'),'COUNT':'COUNT(%s)','COUNT DISTINCT':_0xbfcb('0x53'),'MAX':_0xbfcb('0x54'),'MIN':_0xbfcb('0x55'),'AVG':'AVG(%s)','GROUP_CONCAT':_0xbfcb('0x83'),'GROUP_CONCAT ASC':_0xbfcb('0x56'),'GROUP_CONCAT DESC':'GROUP_CONCAT(%s\x20ORDER\x20BY\x20%s\x20DESC)'};var _0x4db416={'SEC_TO_TIME':'SEC_TO_TIME(%s)','DATE':'DATE(%s)','HOUR':_0xbfcb('0x59'),'ROUND':'ROUND(%s)','UNIX_TIMESTAMP':_0xbfcb('0x5b')};var _0x16eebb=function(_0x12f9da){return!_[_0xbfcb('0x5c')]([_0xbfcb('0x5d'),_0xbfcb('0x5e'),_0xbfcb('0x5f'),_0xbfcb('0x60')],_0x12f9da);};var _0x476480=function(_0x372520){if(!_0x372520||!_0x372520['rules'][_0xbfcb('0x3a')]){return'';}for(var _0x25d2ac='(',_0x515253=0x0;_0x515253<_0x372520[_0xbfcb('0x61')][_0xbfcb('0x3a')];_0x515253+=0x1){_0x515253>0x0&&(_0x25d2ac+='\x20'+_0x372520[_0xbfcb('0x62')]+'\x20');_0x25d2ac+=_0x372520[_0xbfcb('0x61')][_0x515253][_0xbfcb('0x63')]?_0x476480(_0x372520['rules'][_0x515253][_0xbfcb('0x63')]):_0x372520[_0xbfcb('0x61')][_0x515253][_0xbfcb('0x64')]+'\x20'+_0x372520[_0xbfcb('0x61')][_0x515253]['condition']+(_0x16eebb(_0x372520[_0xbfcb('0x61')][_0x515253][_0xbfcb('0x65')])?'\x20'+_0x372520['rules'][_0x515253][_0xbfcb('0x66')]:'');}return _0x25d2ac+')';};var _0xa0c250={'where':{'id':_0x1f4f0a[_0xbfcb('0x9c')]['id']}},_0x25e250,_0x4de418,_0x49afb1,_0x12ce37,_0x2609ad;_0xa0c250=_[_0xbfcb('0x40')]({},_0xa0c250,_0x1f4f0a[_0xbfcb('0x41')]);return db[_0xbfcb('0x31')][_0xbfcb('0x4d')](_0xa0c250)[_0xbfcb('0x26')](function(_0x1791a2){if(_0x1791a2){_0x25e250=_0x1791a2;return _0x1791a2[_0xbfcb('0x67')]();}return null;})[_0xbfcb('0x26')](function(_0x13635b){if(_0x13635b){if(!_0x13635b[_0xbfcb('0x3a')]){throw new db[(_0xbfcb('0x85'))]['ValidationErrorItem'](_0xbfcb('0x69'),0x193);}_0x4de418=_0x13635b;return db[_0xbfcb('0x6a')][_0xbfcb('0x44')]({'raw':!![]});}return null;})[_0xbfcb('0x26')](function(_0x48efbd){if(_0x48efbd){_0x49afb1=_[_0xbfcb('0x6b')](_0x48efbd,'id');var _0x2dab93;var _0x23057b=squel['select']();_0x23057b[_0xbfcb('0x98')](_0x25e250['table']);for(var _0x1310f4=0x0;_0x1310f4<_0x4de418[_0xbfcb('0x3a')];_0x1310f4+=0x1){_0x2dab93=_0x4de418[_0x1310f4][_0xbfcb('0x6e')]?_0x49afb1[_0x4de418[_0x1310f4][_0xbfcb('0x6e')]][_0xbfcb('0x9e')]:_0x4de418[_0x1310f4][_0xbfcb('0x64')];_0x2dab93=_0x4de418[_0x1310f4][_0xbfcb('0x6f')]?_[_0xbfcb('0x70')](_0x4ed66b[_0x4de418[_0x1310f4][_0xbfcb('0x6f')]],'%s',_0x2dab93):_0x2dab93;_0x2dab93=_0x4de418[_0x1310f4]['format']?_[_0xbfcb('0x70')](_0x4db416[_0x4de418[_0x1310f4][_0xbfcb('0x71')]],'%s',_0x2dab93):_0x2dab93;if(_0x4de418[_0x1310f4][_0xbfcb('0x72')]){_0x23057b[_0xbfcb('0x63')](_0x2dab93);}_0x4de418[_0x1310f4][_0xbfcb('0x73')]&&_0x23057b[_0xbfcb('0x9f')](_0x2dab93,_0x4de418[_0x1310f4]['orderBy']==='DESC'?![]:!![]);_0x23057b[_0xbfcb('0x64')](_0x2dab93,_0x4de418[_0x1310f4][_0xbfcb('0x75')]||_0x2dab93);}if(_0x25e250[_0xbfcb('0x76')]){_0x25e250[_0xbfcb('0x76')]=JSON[_0xbfcb('0x77')](_0x25e250[_0xbfcb('0x76')]);for(var _0xd678b7=0x0;_0xd678b7<_0x25e250[_0xbfcb('0x76')]['length'];_0xd678b7+=0x1){if(_0x25e250['joins'][_0xd678b7][_0xbfcb('0x34')]&&_[_0xbfcb('0x5c')](_0x454abf,_0x25e250[_0xbfcb('0x76')][_0xd678b7][_0xbfcb('0x34')])&&_0x25e250[_0xbfcb('0x76')][_0xd678b7][_0xbfcb('0x86')]&&_0x25e250[_0xbfcb('0x76')][_0xd678b7][_0xbfcb('0x78')]&&_0x25e250['joins'][_0xd678b7][_0xbfcb('0x79')]){_0x23057b[_0x25e250[_0xbfcb('0x76')][_0xd678b7]['type']](_0x25e250[_0xbfcb('0x76')][_0xd678b7][_0xbfcb('0x78')],null,util[_0xbfcb('0x71')](_0xbfcb('0x87'),_0x25e250[_0xbfcb('0x6d')],_0x25e250[_0xbfcb('0x76')][_0xd678b7]['parentKey'],_0x25e250[_0xbfcb('0x76')][_0xd678b7][_0xbfcb('0x78')],_0x25e250[_0xbfcb('0x76')][_0xd678b7][_0xbfcb('0x79')]));}}}if(_0x25e250[_0xbfcb('0x7a')]){_0x25e250['conditions']=JSON[_0xbfcb('0x77')](_0x25e250[_0xbfcb('0x7a')]);_0x23057b[_0xbfcb('0x3e')](_0x476480(_0x25e250[_0xbfcb('0x7a')]['group']));}_0x2609ad=_0x23057b[_0xbfcb('0x7b')]();_0x23057b['limit'](_0x27fde4);_0x23057b[_0xbfcb('0x20')](0x0);logger[_0xbfcb('0x7c')]('Limited\x20Query:',_0x23057b[_0xbfcb('0x7f')]());return dbH[_0xbfcb('0x7e')]['query'](_0x23057b['toString'](),{'type':dbH['sequelize']['QueryTypes'][_0xbfcb('0x80')]});}})[_0xbfcb('0x26')](function(){return{'sql':_0x2609ad[_0xbfcb('0x7f')]()};})[_0xbfcb('0x26')](respondWithResult(_0x3e6d15,null))[_0xbfcb('0x45')](handleError(_0x3e6d15,null));};