e38dd68d2dcbd2bb35c3fbef3c51990daf4d40cd
[motion2.git] / server / api / analyticMetric / analyticMetric.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 _0x86f4=['field','includeAll','include','rows','catch','fields','options','find','create','body','role','get','UserProfileSection','userProfileId','autoAssociation','params','html-pdf','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','moment','mustache','util','path','sox','to-csv','ejs','lodash','jsforce','querystring','papaparse','ioredis','../../components/auth/service','../../config/license/hardware','../../config/logger','api','../../config/utils','sendStatus','status','end','json','count','offset','set','Content-Range','apply','reject','save','update','then','destroy','Metrics','UserProfileResource','error','stack','name','send','AnalyticMetric','rawAttributes','model','keys','query','filters','intersection','attributes','length','limit','sort','where','pick','filter','merge','map','type','VIRTUAL'];(function(_0x358e6a,_0x1dd895){var _0x34da8a=function(_0x3c3798){while(--_0x3c3798){_0x358e6a['push'](_0x358e6a['shift']());}};_0x34da8a(++_0x1dd895);}(_0x86f4,0x191));var _0x486f=function(_0x5cb5c1,_0x4ace1a){_0x5cb5c1=_0x5cb5c1-0x0;var _0x2d1812=_0x86f4[_0x5cb5c1];return _0x2d1812;};'use strict';var pdf=require(_0x486f('0x0'));var emlformat=require(_0x486f('0x1'));var rimraf=require(_0x486f('0x2'));var zipdir=require(_0x486f('0x3'));var jsonpatch=require(_0x486f('0x4'));var rp=require(_0x486f('0x5'));var moment=require(_0x486f('0x6'));var BPromise=require('bluebird');var Mustache=require(_0x486f('0x7'));var util=require(_0x486f('0x8'));var path=require(_0x486f('0x9'));var sox=require(_0x486f('0xa'));var csv=require(_0x486f('0xb'));var ejs=require(_0x486f('0xc'));var fs=require('fs');var _=require(_0x486f('0xd'));var squel=require('squel');var crypto=require('crypto');var jsforce=require(_0x486f('0xe'));var deskjs=require('desk.js');var toCsv=require('to-csv');var querystring=require(_0x486f('0xf'));var Papa=require(_0x486f('0x10'));var Redis=require(_0x486f('0x11'));var authService=require(_0x486f('0x12'));var qs=require('../../components/parsers/qs');var hardwareService=require(_0x486f('0x13'));var logger=require(_0x486f('0x14'))(_0x486f('0x15'));var utils=require(_0x486f('0x16'));var config=require('../../config/environment');var db=require('../../mysqldb')['db'];function respondWithStatusCode(_0x4d87f2,_0x589e48){_0x589e48=_0x589e48||0xcc;return function(_0x46d9d3){if(_0x46d9d3){return _0x4d87f2[_0x486f('0x17')](_0x589e48);}return _0x4d87f2[_0x486f('0x18')](_0x589e48)[_0x486f('0x19')]();};}function respondWithResult(_0x3d436c,_0x27ee78){_0x27ee78=_0x27ee78||0xc8;return function(_0x2156b4){if(_0x2156b4){return _0x3d436c[_0x486f('0x18')](_0x27ee78)[_0x486f('0x1a')](_0x2156b4);}};}function respondWithFilteredResult(_0x10a522,_0x326059){return function(_0x13d434){if(_0x13d434){var _0x5b699a=_0x13d434[_0x486f('0x1b')],_0x152ae4=_0x326059[_0x486f('0x1c')],_0x418228=_0x326059['offset']+_0x326059['limit'],_0x461edd;if(_0x418228>=_0x5b699a){_0x418228=_0x5b699a;_0x461edd=0xc8;}else{_0x461edd=0xce;}_0x10a522[_0x486f('0x18')](_0x461edd);return _0x10a522[_0x486f('0x1d')](_0x486f('0x1e'),_0x152ae4+'-'+_0x418228+'/'+_0x5b699a)[_0x486f('0x1a')](_0x13d434);}return null;};}function patchUpdates(_0x5744be){return function(_0x344d1e){try{jsonpatch[_0x486f('0x1f')](_0x344d1e,_0x5744be,!![]);}catch(_0x106faa){return BPromise[_0x486f('0x20')](_0x106faa);}return _0x344d1e[_0x486f('0x21')]();};}function saveUpdates(_0x218227,_0x340e39){return function(_0x533626){if(_0x533626){return _0x533626[_0x486f('0x22')](_0x218227)[_0x486f('0x23')](function(_0x2063fa){return _0x2063fa;});}return null;};}function removeEntity(_0x503a46,_0x31e076){return function(_0x188f1){if(_0x188f1){return _0x188f1[_0x486f('0x24')]()[_0x486f('0x23')](function(){var _0x2fc8b2=_0x188f1['get']({'plain':!![]});var _0x31e344=_0x486f('0x25');return db[_0x486f('0x26')]['destroy']({'where':{'type':_0x31e344,'resourceId':_0x2fc8b2['id']}})[_0x486f('0x23')](function(){return _0x188f1;});})[_0x486f('0x23')](function(){_0x503a46[_0x486f('0x18')](0xcc)['end']();});}};}function handleEntityNotFound(_0x3d856a,_0x316360){return function(_0x59c15e){if(!_0x59c15e){_0x3d856a[_0x486f('0x17')](0x194);}return _0x59c15e;};}function handleError(_0x18d4af,_0x3277cd){_0x3277cd=_0x3277cd||0x1f4;return function(_0x4f9018){logger[_0x486f('0x27')](_0x4f9018[_0x486f('0x28')]);if(_0x4f9018[_0x486f('0x29')]){delete _0x4f9018[_0x486f('0x29')];}_0x18d4af[_0x486f('0x18')](_0x3277cd)[_0x486f('0x2a')](_0x4f9018);};}exports['index']=function(_0x5866d1,_0x55c25d){var _0x18ef2f={},_0x417805={},_0x18cfd1={'count':0x0,'rows':[]};var _0x54be38=db[_0x486f('0x2b')][_0x486f('0x2c')];_0x417805[_0x486f('0x2d')]=_[_0x486f('0x2e')](_0x54be38);_0x417805[_0x486f('0x2f')]=_[_0x486f('0x2e')](_0x5866d1[_0x486f('0x2f')]);_0x417805[_0x486f('0x30')]=_[_0x486f('0x31')](_0x417805[_0x486f('0x2d')],_0x417805[_0x486f('0x2f')]);_0x18ef2f[_0x486f('0x32')]=_[_0x486f('0x31')](_0x417805['model'],qs['fields'](_0x5866d1[_0x486f('0x2f')]['fields']));_0x18ef2f[_0x486f('0x32')]=_0x18ef2f[_0x486f('0x32')][_0x486f('0x33')]?_0x18ef2f[_0x486f('0x32')]:_0x417805[_0x486f('0x2d')];if(!_0x5866d1[_0x486f('0x2f')]['hasOwnProperty']('nolimit')){_0x18ef2f[_0x486f('0x34')]=qs[_0x486f('0x34')](_0x5866d1['query'][_0x486f('0x34')]);_0x18ef2f[_0x486f('0x1c')]=qs[_0x486f('0x1c')](_0x5866d1[_0x486f('0x2f')]['offset']);}_0x18ef2f['order']=qs['sort'](_0x5866d1[_0x486f('0x2f')][_0x486f('0x35')]);_0x18ef2f[_0x486f('0x36')]=qs[_0x486f('0x30')](_[_0x486f('0x37')](_0x5866d1[_0x486f('0x2f')],_0x417805[_0x486f('0x30')]));if(_0x5866d1[_0x486f('0x2f')][_0x486f('0x38')]){_0x18ef2f['where']=_[_0x486f('0x39')](_0x18ef2f[_0x486f('0x36')],{'$or':_[_0x486f('0x3a')](_0x54be38,function(_0xe50057){if(_0xe50057[_0x486f('0x3b')]['key']!==_0x486f('0x3c')){var _0x5abc6c={};_0x5abc6c[_0xe50057[_0x486f('0x3d')]]={'$like':'%'+_0x5866d1[_0x486f('0x2f')]['filter']+'%'};return _0x5abc6c;}})});}_0x18ef2f=_[_0x486f('0x39')]({},_0x18ef2f,_0x5866d1['options']);var _0x97fe0f={'where':_0x18ef2f[_0x486f('0x36')]};return db[_0x486f('0x2b')][_0x486f('0x1b')](_0x97fe0f)[_0x486f('0x23')](function(_0x1e29fe){_0x18cfd1[_0x486f('0x1b')]=_0x1e29fe;if(_0x5866d1[_0x486f('0x2f')][_0x486f('0x3e')]){_0x18ef2f[_0x486f('0x3f')]=[{'all':!![]}];}return db[_0x486f('0x2b')]['findAll'](_0x18ef2f);})[_0x486f('0x23')](function(_0x30f13c){_0x18cfd1[_0x486f('0x40')]=_0x30f13c;return _0x18cfd1;})[_0x486f('0x23')](respondWithFilteredResult(_0x55c25d,_0x18ef2f))[_0x486f('0x41')](handleError(_0x55c25d,null));};exports['show']=function(_0x16dae1,_0x19493a){var _0x33d295={'raw':!![],'where':{'id':_0x16dae1['params']['id']}},_0xbb8683={};_0xbb8683[_0x486f('0x2d')]=_[_0x486f('0x2e')](db['AnalyticMetric'][_0x486f('0x2c')]);_0xbb8683['query']=_[_0x486f('0x2e')](_0x16dae1['query']);_0xbb8683[_0x486f('0x30')]=_[_0x486f('0x31')](_0xbb8683['model'],_0xbb8683['query']);_0x33d295[_0x486f('0x32')]=_[_0x486f('0x31')](_0xbb8683['model'],qs['fields'](_0x16dae1[_0x486f('0x2f')][_0x486f('0x42')]));_0x33d295['attributes']=_0x33d295[_0x486f('0x32')][_0x486f('0x33')]?_0x33d295[_0x486f('0x32')]:_0xbb8683[_0x486f('0x2d')];if(_0x16dae1[_0x486f('0x2f')]['includeAll']){_0x33d295['include']=[{'all':!![]}];}_0x33d295=_['merge']({},_0x33d295,_0x16dae1[_0x486f('0x43')]);return db[_0x486f('0x2b')][_0x486f('0x44')](_0x33d295)[_0x486f('0x23')](handleEntityNotFound(_0x19493a,null))[_0x486f('0x23')](respondWithResult(_0x19493a,null))[_0x486f('0x41')](handleError(_0x19493a,null));};exports[_0x486f('0x45')]=function(_0x117128,_0x2f67a5){return db[_0x486f('0x2b')][_0x486f('0x45')](_0x117128[_0x486f('0x46')],{})[_0x486f('0x23')](function(_0x3673b9){var _0x187fc9=_0x117128['user']['get']({'plain':!![]});if(!_0x187fc9)throw new Error('Unable\x20to\x20retrieve\x20the\x20current\x20user');if(_0x187fc9[_0x486f('0x47')]==='user'){var _0x3aef18=_0x3673b9[_0x486f('0x48')]({'plain':!![]});var _0xb06407=_0x486f('0x25');return db[_0x486f('0x49')][_0x486f('0x44')]({'where':{'name':_0xb06407,'userProfileId':_0x187fc9[_0x486f('0x4a')]},'raw':!![]})[_0x486f('0x23')](function(_0x50f023){if(_0x50f023&&_0x50f023[_0x486f('0x4b')]===0x0){return db[_0x486f('0x26')][_0x486f('0x45')]({'name':_0x3aef18[_0x486f('0x29')],'resourceId':_0x3aef18['id'],'type':_0x50f023[_0x486f('0x29')],'sectionId':_0x50f023['id']},{})['then'](function(){return _0x3673b9;});}else{return _0x3673b9;}})[_0x486f('0x41')](function(_0x188d88){logger['error']('Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s',_0x188d88);throw _0x188d88;});}return _0x3673b9;})[_0x486f('0x23')](respondWithResult(_0x2f67a5,0xc9))[_0x486f('0x41')](handleError(_0x2f67a5,null));};exports[_0x486f('0x22')]=function(_0x10b81a,_0x4eee53){if(_0x10b81a['body']['id']){delete _0x10b81a[_0x486f('0x46')]['id'];}return db[_0x486f('0x2b')][_0x486f('0x44')]({'where':{'id':_0x10b81a[_0x486f('0x4c')]['id']}})[_0x486f('0x23')](handleEntityNotFound(_0x4eee53,null))[_0x486f('0x23')](saveUpdates(_0x10b81a[_0x486f('0x46')],null))['then'](respondWithResult(_0x4eee53,null))['catch'](handleError(_0x4eee53,null));};exports[_0x486f('0x24')]=function(_0x197b74,_0x35d18a){return db[_0x486f('0x2b')][_0x486f('0x44')]({'where':{'id':_0x197b74['params']['id']}})[_0x486f('0x23')](handleEntityNotFound(_0x35d18a,null))['then'](removeEntity(_0x35d18a,null))[_0x486f('0x41')](handleError(_0x35d18a,null));};