494f61c7eb2b97be2a97e08709ad813637201133
[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 _0xf072=['../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/environment','../../config/license/util','sendStatus','status','end','json','count','offset','limit','set','apply','reject','save','update','then','destroy','error','stack','name','send','index','AnalyticMetric','rawAttributes','key','query','keys','filters','intersection','attributes','fields','length','model','nolimit','order','sort','where','pick','merge','map','VIRTUAL','filter','options','includeAll','include','findAll','rows','catch','create','user','get','Metrics','UserProfileSection','find','autoAssociation','UserProfileResource','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','body','html-pdf','zip-dir','moment','mustache','path','ejs','lodash','squel','crypto','jsforce','desk.js','to-csv','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs'];(function(_0x3a3dff,_0x3dc85a){var _0xf1fcf9=function(_0x483116){while(--_0x483116){_0x3a3dff['push'](_0x3a3dff['shift']());}};_0xf1fcf9(++_0x3dc85a);}(_0xf072,0x17a));var _0x2f07=function(_0x40a125,_0x270386){_0x40a125=_0x40a125-0x0;var _0x3e67f4=_0xf072[_0x40a125];return _0x3e67f4;};'use strict';var pdf=require(_0x2f07('0x0'));var emlformat=require('eml-format');var rimraf=require('rimraf');var zipdir=require(_0x2f07('0x1'));var jsonpatch=require('fast-json-patch');var rp=require('request-promise');var moment=require(_0x2f07('0x2'));var BPromise=require('bluebird');var Mustache=require(_0x2f07('0x3'));var util=require('util');var path=require(_0x2f07('0x4'));var sox=require('sox');var csv=require('to-csv');var ejs=require(_0x2f07('0x5'));var fs=require('fs');var _=require(_0x2f07('0x6'));var squel=require(_0x2f07('0x7'));var crypto=require(_0x2f07('0x8'));var jsforce=require(_0x2f07('0x9'));var deskjs=require(_0x2f07('0xa'));var toCsv=require(_0x2f07('0xb'));var querystring=require(_0x2f07('0xc'));var Papa=require(_0x2f07('0xd'));var Redis=require(_0x2f07('0xe'));var authService=require(_0x2f07('0xf'));var qs=require(_0x2f07('0x10'));var as=require(_0x2f07('0x11'));var hardwareService=require(_0x2f07('0x12'));var logger=require(_0x2f07('0x13'))(_0x2f07('0x14'));var utils=require(_0x2f07('0x15'));var config=require(_0x2f07('0x16'));var licenseUtil=require(_0x2f07('0x17'));var db=require('../../mysqldb')['db'];function respondWithStatusCode(_0x1aacce,_0x21384c){_0x21384c=_0x21384c||0xcc;return function(_0x5cba5c){if(_0x5cba5c){return _0x1aacce[_0x2f07('0x18')](_0x21384c);}return _0x1aacce[_0x2f07('0x19')](_0x21384c)[_0x2f07('0x1a')]();};}function respondWithResult(_0x31ea53,_0x103314){_0x103314=_0x103314||0xc8;return function(_0x2f27e3){if(_0x2f27e3){return _0x31ea53[_0x2f07('0x19')](_0x103314)[_0x2f07('0x1b')](_0x2f27e3);}};}function respondWithFilteredResult(_0x1638c5,_0x2b5d4f){return function(_0x30d59e){if(_0x30d59e){var _0x28f6be=_0x30d59e[_0x2f07('0x1c')],_0x5cdb28=_0x2b5d4f[_0x2f07('0x1d')],_0x324806=_0x2b5d4f[_0x2f07('0x1d')]+_0x2b5d4f[_0x2f07('0x1e')],_0x979e72;if(_0x324806>=_0x28f6be){_0x324806=_0x28f6be;_0x979e72=0xc8;}else{_0x979e72=0xce;}_0x1638c5[_0x2f07('0x19')](_0x979e72);return _0x1638c5[_0x2f07('0x1f')]('Content-Range',_0x5cdb28+'-'+_0x324806+'/'+_0x28f6be)['json'](_0x30d59e);}return null;};}function patchUpdates(_0x4ffe14){return function(_0x12788b){try{jsonpatch[_0x2f07('0x20')](_0x12788b,_0x4ffe14,!![]);}catch(_0x2c4f18){return BPromise[_0x2f07('0x21')](_0x2c4f18);}return _0x12788b[_0x2f07('0x22')]();};}function saveUpdates(_0x1ba269,_0x4ccd0a){return function(_0xaa6a66){if(_0xaa6a66){return _0xaa6a66[_0x2f07('0x23')](_0x1ba269)[_0x2f07('0x24')](function(_0x40d602){return _0x40d602;});}return null;};}function removeEntity(_0xb80906,_0x3880db){return function(_0x58e264){if(_0x58e264){return _0x58e264[_0x2f07('0x25')]()[_0x2f07('0x24')](function(){var _0x36b36a=_0x58e264['get']({'plain':!![]});var _0x111694='Metrics';return db['UserProfileResource']['destroy']({'where':{'type':_0x111694,'resourceId':_0x36b36a['id']}})[_0x2f07('0x24')](function(){return _0x58e264;});})[_0x2f07('0x24')](function(){_0xb80906['status'](0xcc)['end']();});}};}function handleEntityNotFound(_0x7f3914,_0x3b6216){return function(_0xe750d){if(!_0xe750d){_0x7f3914[_0x2f07('0x18')](0x194);}return _0xe750d;};}function handleError(_0x2fbeec,_0x4a1577){_0x4a1577=_0x4a1577||0x1f4;return function(_0x1b7bdd){logger[_0x2f07('0x26')](_0x1b7bdd[_0x2f07('0x27')]);if(_0x1b7bdd['name']){delete _0x1b7bdd[_0x2f07('0x28')];}_0x2fbeec[_0x2f07('0x19')](_0x4a1577)[_0x2f07('0x29')](_0x1b7bdd);};}exports[_0x2f07('0x2a')]=function(_0x487d6b,_0x468f35){var _0x39ff60={},_0x31556d={},_0x52584a={'count':0x0,'rows':[]};var _0x399915=_['map'](db[_0x2f07('0x2b')][_0x2f07('0x2c')],function(_0x50da60){return{'name':_0x50da60['fieldName'],'type':_0x50da60['type'][_0x2f07('0x2d')]};});_0x31556d['model']=_['map'](_0x399915,_0x2f07('0x28'));_0x31556d[_0x2f07('0x2e')]=_[_0x2f07('0x2f')](_0x487d6b[_0x2f07('0x2e')]);_0x31556d[_0x2f07('0x30')]=_[_0x2f07('0x31')](_0x31556d['model'],_0x31556d[_0x2f07('0x2e')]);_0x39ff60[_0x2f07('0x32')]=_[_0x2f07('0x31')](_0x31556d['model'],qs[_0x2f07('0x33')](_0x487d6b[_0x2f07('0x2e')]['fields']));_0x39ff60['attributes']=_0x39ff60[_0x2f07('0x32')][_0x2f07('0x34')]?_0x39ff60['attributes']:_0x31556d[_0x2f07('0x35')];if(!_0x487d6b[_0x2f07('0x2e')]['hasOwnProperty'](_0x2f07('0x36'))){_0x39ff60[_0x2f07('0x1e')]=qs[_0x2f07('0x1e')](_0x487d6b['query'][_0x2f07('0x1e')]);_0x39ff60[_0x2f07('0x1d')]=qs[_0x2f07('0x1d')](_0x487d6b[_0x2f07('0x2e')][_0x2f07('0x1d')]);}_0x39ff60[_0x2f07('0x37')]=qs[_0x2f07('0x38')](_0x487d6b['query'][_0x2f07('0x38')]);_0x39ff60[_0x2f07('0x39')]=qs[_0x2f07('0x30')](_[_0x2f07('0x3a')](_0x487d6b[_0x2f07('0x2e')],_0x31556d[_0x2f07('0x30')]),_0x399915);if(_0x487d6b[_0x2f07('0x2e')]['filter']){_0x39ff60[_0x2f07('0x39')]=_[_0x2f07('0x3b')](_0x39ff60[_0x2f07('0x39')],{'$or':_[_0x2f07('0x3c')](_0x399915,function(_0x59d4d0){if(_0x59d4d0['type']!==_0x2f07('0x3d')){var _0x4ea61b={};_0x4ea61b[_0x59d4d0[_0x2f07('0x28')]]={'$like':'%'+_0x487d6b[_0x2f07('0x2e')][_0x2f07('0x3e')]+'%'};return _0x4ea61b;}})});}_0x39ff60=_[_0x2f07('0x3b')]({},_0x39ff60,_0x487d6b[_0x2f07('0x3f')]);var _0x130155={'where':_0x39ff60['where']};return db[_0x2f07('0x2b')]['count'](_0x130155)[_0x2f07('0x24')](function(_0x40ca66){_0x52584a['count']=_0x40ca66;if(_0x487d6b['query'][_0x2f07('0x40')]){_0x39ff60[_0x2f07('0x41')]=[{'all':!![]}];}return db['AnalyticMetric'][_0x2f07('0x42')](_0x39ff60);})[_0x2f07('0x24')](function(_0x521d76){_0x52584a[_0x2f07('0x43')]=_0x521d76;return _0x52584a;})[_0x2f07('0x24')](respondWithFilteredResult(_0x468f35,_0x39ff60))[_0x2f07('0x44')](handleError(_0x468f35,null));};exports['show']=function(_0x55c947,_0x587eac){var _0x3c8a65={'raw':!![],'where':{'id':_0x55c947['params']['id']}},_0x4a911c={};_0x4a911c[_0x2f07('0x35')]=_['keys'](db[_0x2f07('0x2b')][_0x2f07('0x2c')]);_0x4a911c[_0x2f07('0x2e')]=_[_0x2f07('0x2f')](_0x55c947[_0x2f07('0x2e')]);_0x4a911c[_0x2f07('0x30')]=_['intersection'](_0x4a911c[_0x2f07('0x35')],_0x4a911c[_0x2f07('0x2e')]);_0x3c8a65[_0x2f07('0x32')]=_[_0x2f07('0x31')](_0x4a911c[_0x2f07('0x35')],qs[_0x2f07('0x33')](_0x55c947[_0x2f07('0x2e')][_0x2f07('0x33')]));_0x3c8a65[_0x2f07('0x32')]=_0x3c8a65[_0x2f07('0x32')][_0x2f07('0x34')]?_0x3c8a65['attributes']:_0x4a911c[_0x2f07('0x35')];if(_0x55c947[_0x2f07('0x2e')][_0x2f07('0x40')]){_0x3c8a65[_0x2f07('0x41')]=[{'all':!![]}];}_0x3c8a65=_[_0x2f07('0x3b')]({},_0x3c8a65,_0x55c947[_0x2f07('0x3f')]);return db[_0x2f07('0x2b')]['find'](_0x3c8a65)[_0x2f07('0x24')](handleEntityNotFound(_0x587eac,null))[_0x2f07('0x24')](respondWithResult(_0x587eac,null))[_0x2f07('0x44')](handleError(_0x587eac,null));};exports[_0x2f07('0x45')]=function(_0x55f721,_0x2985ca){return db[_0x2f07('0x2b')][_0x2f07('0x45')](_0x55f721['body'],{})[_0x2f07('0x24')](function(_0x41f3b7){var _0x29f4b8=_0x55f721[_0x2f07('0x46')]['get']({'plain':!![]});if(!_0x29f4b8)throw new Error('Unable\x20to\x20retrieve\x20the\x20current\x20user');if(_0x29f4b8['role']===_0x2f07('0x46')){var _0x543677=_0x41f3b7[_0x2f07('0x47')]({'plain':!![]});var _0x368983=_0x2f07('0x48');return db[_0x2f07('0x49')][_0x2f07('0x4a')]({'where':{'name':_0x368983,'userProfileId':_0x29f4b8['userProfileId']},'raw':!![]})[_0x2f07('0x24')](function(_0x3f17e6){if(_0x3f17e6&&_0x3f17e6[_0x2f07('0x4b')]===0x0){return db[_0x2f07('0x4c')][_0x2f07('0x45')]({'name':_0x543677[_0x2f07('0x28')],'resourceId':_0x543677['id'],'type':_0x3f17e6[_0x2f07('0x28')],'sectionId':_0x3f17e6['id']},{})[_0x2f07('0x24')](function(){return _0x41f3b7;});}else{return _0x41f3b7;}})['catch'](function(_0x1e4695){logger[_0x2f07('0x26')](_0x2f07('0x4d'),_0x1e4695);throw _0x1e4695;});}return _0x41f3b7;})[_0x2f07('0x24')](respondWithResult(_0x2985ca,0xc9))[_0x2f07('0x44')](handleError(_0x2985ca,null));};exports[_0x2f07('0x23')]=function(_0x24944c,_0xaee878){if(_0x24944c[_0x2f07('0x4e')]['id']){delete _0x24944c[_0x2f07('0x4e')]['id'];}return db[_0x2f07('0x2b')][_0x2f07('0x4a')]({'where':{'id':_0x24944c['params']['id']}})[_0x2f07('0x24')](handleEntityNotFound(_0xaee878,null))[_0x2f07('0x24')](saveUpdates(_0x24944c[_0x2f07('0x4e')],null))[_0x2f07('0x24')](respondWithResult(_0xaee878,null))[_0x2f07('0x44')](handleError(_0xaee878,null));};exports['destroy']=function(_0x2b9d4b,_0xc32567){return db[_0x2f07('0x2b')]['find']({'where':{'id':_0x2b9d4b['params']['id']}})[_0x2f07('0x24')](handleEntityNotFound(_0xc32567,null))[_0x2f07('0x24')](removeEntity(_0xc32567,null))[_0x2f07('0x44')](handleError(_0xc32567,null));};