f80132335da27afd7bfaf39d288b7d04c7e8f534
[motion2.git] / server / api / analyticFieldReport / analyticFieldReport.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 _0x3f66=['limit','set','Content-Range','apply','reject','save','update','then','destroy','error','stack','name','send','map','fieldName','type','keys','intersection','model','query','attributes','fields','length','hasOwnProperty','sort','where','pick','filters','filter','merge','options','AnalyticFieldReport','count','includeAll','include','findAll','rows','catch','show','params','find','create','body','bulkCreate','bulkDestroy','ids','addFields','AnalyticCustomReport','sequelize','transaction','CustomReportId','html-pdf','eml-format','fast-json-patch','request-promise','mustache','util','path','sox','to-csv','ejs','lodash','squel','crypto','jsforce','desk.js','querystring','ioredis','../../components/auth/service','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','../../config/utils','../../config/license/util','../../mysqldb','sendStatus','status','end','json','offset'];(function(_0x5519a9,_0x1c5147){var _0x32f2cc=function(_0x52ed58){while(--_0x52ed58){_0x5519a9['push'](_0x5519a9['shift']());}};_0x32f2cc(++_0x1c5147);}(_0x3f66,0x1c3));var _0x63f6=function(_0x2e05ec,_0xe28c57){_0x2e05ec=_0x2e05ec-0x0;var _0x20e3ea=_0x3f66[_0x2e05ec];return _0x20e3ea;};'use strict';var pdf=require(_0x63f6('0x0'));var emlformat=require(_0x63f6('0x1'));var rimraf=require('rimraf');var zipdir=require('zip-dir');var jsonpatch=require(_0x63f6('0x2'));var rp=require(_0x63f6('0x3'));var moment=require('moment');var BPromise=require('bluebird');var Mustache=require(_0x63f6('0x4'));var util=require(_0x63f6('0x5'));var path=require(_0x63f6('0x6'));var sox=require(_0x63f6('0x7'));var csv=require(_0x63f6('0x8'));var ejs=require(_0x63f6('0x9'));var fs=require('fs');var _=require(_0x63f6('0xa'));var squel=require(_0x63f6('0xb'));var crypto=require(_0x63f6('0xc'));var jsforce=require(_0x63f6('0xd'));var deskjs=require(_0x63f6('0xe'));var toCsv=require(_0x63f6('0x8'));var querystring=require(_0x63f6('0xf'));var Papa=require('papaparse');var Redis=require(_0x63f6('0x10'));var authService=require(_0x63f6('0x11'));var qs=require('../../components/parsers/qs');var as=require(_0x63f6('0x12'));var hardwareService=require(_0x63f6('0x13'));var logger=require(_0x63f6('0x14'))('api');var utils=require(_0x63f6('0x15'));var config=require('../../config/environment');var licenseUtil=require(_0x63f6('0x16'));var db=require(_0x63f6('0x17'))['db'];function respondWithStatusCode(_0x2294ab,_0x2205e4){_0x2205e4=_0x2205e4||0xcc;return function(_0x4cee49){if(_0x4cee49){return _0x2294ab[_0x63f6('0x18')](_0x2205e4);}return _0x2294ab[_0x63f6('0x19')](_0x2205e4)[_0x63f6('0x1a')]();};}function respondWithResult(_0x3bfb87,_0x4ac78d){_0x4ac78d=_0x4ac78d||0xc8;return function(_0x257dae){if(_0x257dae){return _0x3bfb87['status'](_0x4ac78d)[_0x63f6('0x1b')](_0x257dae);}};}function respondWithFilteredResult(_0x45a9a6,_0x4232e6){return function(_0x40196c){if(_0x40196c){var _0x34d144=_0x40196c['count'],_0x3360e3=_0x4232e6[_0x63f6('0x1c')],_0x398c46=_0x4232e6['offset']+_0x4232e6[_0x63f6('0x1d')],_0xa1de44;if(_0x398c46>=_0x34d144){_0x398c46=_0x34d144;_0xa1de44=0xc8;}else{_0xa1de44=0xce;}_0x45a9a6[_0x63f6('0x19')](_0xa1de44);return _0x45a9a6[_0x63f6('0x1e')](_0x63f6('0x1f'),_0x3360e3+'-'+_0x398c46+'/'+_0x34d144)[_0x63f6('0x1b')](_0x40196c);}return null;};}function patchUpdates(_0x41fb83){return function(_0x303d9f){try{jsonpatch[_0x63f6('0x20')](_0x303d9f,_0x41fb83,!![]);}catch(_0x4330b5){return BPromise[_0x63f6('0x21')](_0x4330b5);}return _0x303d9f[_0x63f6('0x22')]();};}function saveUpdates(_0x3d600c,_0x2c7ec2){return function(_0x46c548){if(_0x46c548){return _0x46c548[_0x63f6('0x23')](_0x3d600c)[_0x63f6('0x24')](function(_0x217b91){return _0x217b91;});}return null;};}function removeEntity(_0x2d3ad4,_0x584e42){return function(_0x5cab3b){if(_0x5cab3b){return _0x5cab3b[_0x63f6('0x25')]()[_0x63f6('0x24')](function(){_0x2d3ad4['status'](0xcc)['end']();});}};}function handleEntityNotFound(_0x5df7ee,_0x59cd2c){return function(_0x46cad4){if(!_0x46cad4){_0x5df7ee[_0x63f6('0x18')](0x194);}return _0x46cad4;};}function handleError(_0x5adf00,_0x382c96){_0x382c96=_0x382c96||0x1f4;return function(_0x5e55ea){logger[_0x63f6('0x26')](_0x5e55ea[_0x63f6('0x27')]);if(_0x5e55ea[_0x63f6('0x28')]){delete _0x5e55ea['name'];}_0x5adf00[_0x63f6('0x19')](_0x382c96)[_0x63f6('0x29')](_0x5e55ea);};}exports['index']=function(_0x5f53d3,_0xe0ee9e){var _0x4ef95b={},_0x414926={},_0x40c271={'count':0x0,'rows':[]};var _0x109835=_[_0x63f6('0x2a')](db['AnalyticFieldReport']['rawAttributes'],function(_0x15bf8d){return{'name':_0x15bf8d[_0x63f6('0x2b')],'type':_0x15bf8d[_0x63f6('0x2c')]['key']};});_0x414926['model']=_['map'](_0x109835,_0x63f6('0x28'));_0x414926['query']=_[_0x63f6('0x2d')](_0x5f53d3['query']);_0x414926['filters']=_[_0x63f6('0x2e')](_0x414926[_0x63f6('0x2f')],_0x414926[_0x63f6('0x30')]);_0x4ef95b[_0x63f6('0x31')]=_['intersection'](_0x414926[_0x63f6('0x2f')],qs[_0x63f6('0x32')](_0x5f53d3[_0x63f6('0x30')]['fields']));_0x4ef95b[_0x63f6('0x31')]=_0x4ef95b['attributes'][_0x63f6('0x33')]?_0x4ef95b[_0x63f6('0x31')]:_0x414926['model'];if(!_0x5f53d3[_0x63f6('0x30')][_0x63f6('0x34')]('nolimit')){_0x4ef95b[_0x63f6('0x1d')]=qs[_0x63f6('0x1d')](_0x5f53d3[_0x63f6('0x30')][_0x63f6('0x1d')]);_0x4ef95b[_0x63f6('0x1c')]=qs[_0x63f6('0x1c')](_0x5f53d3[_0x63f6('0x30')][_0x63f6('0x1c')]);}_0x4ef95b['order']=qs[_0x63f6('0x35')](_0x5f53d3[_0x63f6('0x30')]['sort']);_0x4ef95b[_0x63f6('0x36')]=qs['filters'](_[_0x63f6('0x37')](_0x5f53d3[_0x63f6('0x30')],_0x414926[_0x63f6('0x38')]),_0x109835);if(_0x5f53d3[_0x63f6('0x30')][_0x63f6('0x39')]){_0x4ef95b[_0x63f6('0x36')]=_[_0x63f6('0x3a')](_0x4ef95b[_0x63f6('0x36')],{'$or':_['map'](_0x109835,function(_0x49a828){if(_0x49a828['type']!=='VIRTUAL'){var _0x4d8dba={};_0x4d8dba[_0x49a828['name']]={'$like':'%'+_0x5f53d3[_0x63f6('0x30')][_0x63f6('0x39')]+'%'};return _0x4d8dba;}})});}_0x4ef95b=_[_0x63f6('0x3a')]({},_0x4ef95b,_0x5f53d3[_0x63f6('0x3b')]);var _0x5efd36={'where':_0x4ef95b[_0x63f6('0x36')]};return db[_0x63f6('0x3c')][_0x63f6('0x3d')](_0x5efd36)[_0x63f6('0x24')](function(_0x25dfc1){_0x40c271[_0x63f6('0x3d')]=_0x25dfc1;if(_0x5f53d3[_0x63f6('0x30')][_0x63f6('0x3e')]){_0x4ef95b[_0x63f6('0x3f')]=[{'all':!![]}];}return db[_0x63f6('0x3c')][_0x63f6('0x40')](_0x4ef95b);})['then'](function(_0x4411ac){_0x40c271[_0x63f6('0x41')]=_0x4411ac;return _0x40c271;})[_0x63f6('0x24')](respondWithFilteredResult(_0xe0ee9e,_0x4ef95b))[_0x63f6('0x42')](handleError(_0xe0ee9e,null));};exports[_0x63f6('0x43')]=function(_0x34d76a,_0x232d71){var _0x56b1bb={'raw':![],'where':{'id':_0x34d76a[_0x63f6('0x44')]['id']}},_0x5ce816={};_0x5ce816[_0x63f6('0x2f')]=_[_0x63f6('0x2d')](db[_0x63f6('0x3c')]['rawAttributes']);_0x5ce816[_0x63f6('0x30')]=_[_0x63f6('0x2d')](_0x34d76a[_0x63f6('0x30')]);_0x5ce816['filters']=_['intersection'](_0x5ce816[_0x63f6('0x2f')],_0x5ce816[_0x63f6('0x30')]);_0x56b1bb[_0x63f6('0x31')]=_['intersection'](_0x5ce816[_0x63f6('0x2f')],qs[_0x63f6('0x32')](_0x34d76a['query'][_0x63f6('0x32')]));_0x56b1bb[_0x63f6('0x31')]=_0x56b1bb['attributes'][_0x63f6('0x33')]?_0x56b1bb[_0x63f6('0x31')]:_0x5ce816[_0x63f6('0x2f')];if(_0x34d76a['query'][_0x63f6('0x3e')]){_0x56b1bb[_0x63f6('0x3f')]=[{'all':!![]}];}_0x56b1bb=_[_0x63f6('0x3a')]({},_0x56b1bb,_0x34d76a[_0x63f6('0x3b')]);return db[_0x63f6('0x3c')][_0x63f6('0x45')](_0x56b1bb)[_0x63f6('0x24')](handleEntityNotFound(_0x232d71,null))[_0x63f6('0x24')](respondWithResult(_0x232d71,null))[_0x63f6('0x42')](handleError(_0x232d71,null));};exports['create']=function(_0x2592c8,_0x1e7e18){return db[_0x63f6('0x3c')][_0x63f6('0x46')](_0x2592c8[_0x63f6('0x47')],{})[_0x63f6('0x24')](respondWithResult(_0x1e7e18,0xc9))[_0x63f6('0x42')](handleError(_0x1e7e18,null));};exports[_0x63f6('0x48')]=function(_0x54716b,_0x567980){return db[_0x63f6('0x3c')][_0x63f6('0x48')](_0x54716b['body'],{'raw':![],'individualHooks':!![]})[_0x63f6('0x24')](respondWithResult(_0x567980,0xc9))[_0x63f6('0x42')](handleError(_0x567980,null));};exports[_0x63f6('0x23')]=function(_0x2d857e,_0xe8bc9a){if(_0x2d857e[_0x63f6('0x47')]['id']){delete _0x2d857e[_0x63f6('0x47')]['id'];}return db[_0x63f6('0x3c')][_0x63f6('0x45')]({'where':{'id':_0x2d857e['params']['id']}})['then'](handleEntityNotFound(_0xe8bc9a,null))[_0x63f6('0x24')](saveUpdates(_0x2d857e[_0x63f6('0x47')],null))[_0x63f6('0x24')](respondWithResult(_0xe8bc9a,null))[_0x63f6('0x42')](handleError(_0xe8bc9a,null));};exports[_0x63f6('0x25')]=function(_0x4db906,_0x3918e4){return db['AnalyticFieldReport'][_0x63f6('0x45')]({'where':{'id':_0x4db906[_0x63f6('0x44')]['id']}})['then'](handleEntityNotFound(_0x3918e4,null))[_0x63f6('0x24')](removeEntity(_0x3918e4,null))[_0x63f6('0x42')](handleError(_0x3918e4,null));};exports[_0x63f6('0x49')]=function(_0x3070ef,_0x174444){return db[_0x63f6('0x3c')]['destroy']({'where':{'id':_0x3070ef[_0x63f6('0x30')][_0x63f6('0x4a')]||[]},'raw':![],'individualHooks':!![]})['then'](respondWithResult(_0x174444,0xcc))['catch'](handleError(_0x174444,null));};exports[_0x63f6('0x4b')]=function(_0x534e18,_0x3ed1cb,_0xa4ea87){return db[_0x63f6('0x4c')]['findOne']({'where':{'id':_0x534e18[_0x63f6('0x44')]['id']}})[_0x63f6('0x24')](handleEntityNotFound(_0x3ed1cb,null))['then'](function(_0x24b750){if(_0x24b750){return db[_0x63f6('0x4d')][_0x63f6('0x4e')](function(_0x2a1131){return db[_0x63f6('0x3c')][_0x63f6('0x25')]({'where':{'CustomReportId':_0x534e18[_0x63f6('0x44')]['id']},'transaction':_0x2a1131})[_0x63f6('0x24')](function(_0x37c7df){var _0x23b82e=_[_0x63f6('0x2a')](_0x534e18[_0x63f6('0x47')],function(_0x15b2d2){delete _0x15b2d2['id'];_0x15b2d2[_0x63f6('0x4f')]=_0x534e18[_0x63f6('0x44')]['id'];return _0x15b2d2;});return db['AnalyticFieldReport']['bulkCreate'](_0x23b82e,{'transaction':_0x2a1131});});})[_0x63f6('0x24')](function(){return db[_0x63f6('0x3c')][_0x63f6('0x40')]({'where':{'CustomReportId':_0x534e18['params']['id']}});});}})[_0x63f6('0x24')](respondWithResult(_0x3ed1cb,null))[_0x63f6('0x42')](handleError(_0x3ed1cb,null));};