461800b27e76e328ab1516206fa7ec96419348e4
[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 _0xf025=['json','save','update','then','get','Metrics','UserProfileResource','destroy','error','stack','name','index','rawAttributes','model','map','query','keys','intersection','attributes','fields','length','nolimit','sort','filters','where','type','options','AnalyticMetric','include','findAll','show','params','includeAll','merge','find','catch','create','body','user','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','UserProfileSection','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','html-pdf','eml-format','rimraf','zip-dir','bluebird','mustache','util','path','sox','ejs','lodash','squel','crypto','jsforce','querystring','papaparse','ioredis','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','../../config/utils','../../config/license/util','../../mysqldb','sendStatus','status','end','count','offset','limit','set','Content-Range'];(function(_0x1bc42d,_0x1b8a92){var _0x2a4351=function(_0x247ad2){while(--_0x247ad2){_0x1bc42d['push'](_0x1bc42d['shift']());}};_0x2a4351(++_0x1b8a92);}(_0xf025,0x1f4));var _0x5f02=function(_0x4b3e49,_0x9e566c){_0x4b3e49=_0x4b3e49-0x0;var _0x57e339=_0xf025[_0x4b3e49];return _0x57e339;};'use strict';var pdf=require(_0x5f02('0x0'));var emlformat=require(_0x5f02('0x1'));var rimraf=require(_0x5f02('0x2'));var zipdir=require(_0x5f02('0x3'));var jsonpatch=require('fast-json-patch');var rp=require('request-promise');var moment=require('moment');var BPromise=require(_0x5f02('0x4'));var Mustache=require(_0x5f02('0x5'));var util=require(_0x5f02('0x6'));var path=require(_0x5f02('0x7'));var sox=require(_0x5f02('0x8'));var csv=require('to-csv');var ejs=require(_0x5f02('0x9'));var fs=require('fs');var _=require(_0x5f02('0xa'));var squel=require(_0x5f02('0xb'));var crypto=require(_0x5f02('0xc'));var jsforce=require(_0x5f02('0xd'));var deskjs=require('desk.js');var toCsv=require('to-csv');var querystring=require(_0x5f02('0xe'));var Papa=require(_0x5f02('0xf'));var Redis=require(_0x5f02('0x10'));var authService=require('../../components/auth/service');var qs=require(_0x5f02('0x11'));var as=require(_0x5f02('0x12'));var hardwareService=require(_0x5f02('0x13'));var logger=require(_0x5f02('0x14'))('api');var utils=require(_0x5f02('0x15'));var config=require('../../config/environment');var licenseUtil=require(_0x5f02('0x16'));var db=require(_0x5f02('0x17'))['db'];function respondWithStatusCode(_0x1a9508,_0x528bb4){_0x528bb4=_0x528bb4||0xcc;return function(_0x18ae13){if(_0x18ae13){return _0x1a9508[_0x5f02('0x18')](_0x528bb4);}return _0x1a9508[_0x5f02('0x19')](_0x528bb4)[_0x5f02('0x1a')]();};}function respondWithResult(_0x6bde8f,_0x2b5324){_0x2b5324=_0x2b5324||0xc8;return function(_0x53e3b1){if(_0x53e3b1){return _0x6bde8f[_0x5f02('0x19')](_0x2b5324)['json'](_0x53e3b1);}};}function respondWithFilteredResult(_0x463a07,_0x5c7319){return function(_0x47ccb1){if(_0x47ccb1){var _0x58cb32=_0x47ccb1[_0x5f02('0x1b')],_0x43419a=_0x5c7319[_0x5f02('0x1c')],_0x4ac999=_0x5c7319[_0x5f02('0x1c')]+_0x5c7319[_0x5f02('0x1d')],_0x464e2a;if(_0x4ac999>=_0x58cb32){_0x4ac999=_0x58cb32;_0x464e2a=0xc8;}else{_0x464e2a=0xce;}_0x463a07[_0x5f02('0x19')](_0x464e2a);return _0x463a07[_0x5f02('0x1e')](_0x5f02('0x1f'),_0x43419a+'-'+_0x4ac999+'/'+_0x58cb32)[_0x5f02('0x20')](_0x47ccb1);}return null;};}function patchUpdates(_0x3a6cbd){return function(_0x5108c8){try{jsonpatch['apply'](_0x5108c8,_0x3a6cbd,!![]);}catch(_0x2999ba){return BPromise['reject'](_0x2999ba);}return _0x5108c8[_0x5f02('0x21')]();};}function saveUpdates(_0x12b05f,_0x3e0a74){return function(_0x2164b9){if(_0x2164b9){return _0x2164b9[_0x5f02('0x22')](_0x12b05f)[_0x5f02('0x23')](function(_0x504764){return _0x504764;});}return null;};}function removeEntity(_0x368b75,_0x20841a){return function(_0x7fb423){if(_0x7fb423){return _0x7fb423['destroy']()[_0x5f02('0x23')](function(){var _0x11aa48=_0x7fb423[_0x5f02('0x24')]({'plain':!![]});var _0x4dafff=_0x5f02('0x25');return db[_0x5f02('0x26')][_0x5f02('0x27')]({'where':{'type':_0x4dafff,'resourceId':_0x11aa48['id']}})['then'](function(){return _0x7fb423;});})[_0x5f02('0x23')](function(){_0x368b75[_0x5f02('0x19')](0xcc)[_0x5f02('0x1a')]();});}};}function handleEntityNotFound(_0x5b7ddc,_0x483397){return function(_0x56cd97){if(!_0x56cd97){_0x5b7ddc['sendStatus'](0x194);}return _0x56cd97;};}function handleError(_0x30608d,_0x4acd92){_0x4acd92=_0x4acd92||0x1f4;return function(_0x2c5a7a){logger[_0x5f02('0x28')](_0x2c5a7a[_0x5f02('0x29')]);if(_0x2c5a7a['name']){delete _0x2c5a7a[_0x5f02('0x2a')];}_0x30608d[_0x5f02('0x19')](_0x4acd92)['send'](_0x2c5a7a);};}exports[_0x5f02('0x2b')]=function(_0x253943,_0x285a8b){var _0x5b384a={},_0x1d1389={},_0x34904e={'count':0x0,'rows':[]};var _0x2ca0dc=_['map'](db['AnalyticMetric'][_0x5f02('0x2c')],function(_0x2a44da){return{'name':_0x2a44da['fieldName'],'type':_0x2a44da['type']['key']};});_0x1d1389[_0x5f02('0x2d')]=_[_0x5f02('0x2e')](_0x2ca0dc,'name');_0x1d1389[_0x5f02('0x2f')]=_[_0x5f02('0x30')](_0x253943[_0x5f02('0x2f')]);_0x1d1389['filters']=_[_0x5f02('0x31')](_0x1d1389[_0x5f02('0x2d')],_0x1d1389[_0x5f02('0x2f')]);_0x5b384a[_0x5f02('0x32')]=_['intersection'](_0x1d1389[_0x5f02('0x2d')],qs[_0x5f02('0x33')](_0x253943['query'][_0x5f02('0x33')]));_0x5b384a[_0x5f02('0x32')]=_0x5b384a[_0x5f02('0x32')][_0x5f02('0x34')]?_0x5b384a[_0x5f02('0x32')]:_0x1d1389[_0x5f02('0x2d')];if(!_0x253943[_0x5f02('0x2f')]['hasOwnProperty'](_0x5f02('0x35'))){_0x5b384a[_0x5f02('0x1d')]=qs['limit'](_0x253943[_0x5f02('0x2f')]['limit']);_0x5b384a['offset']=qs[_0x5f02('0x1c')](_0x253943[_0x5f02('0x2f')][_0x5f02('0x1c')]);}_0x5b384a['order']=qs[_0x5f02('0x36')](_0x253943[_0x5f02('0x2f')]['sort']);_0x5b384a['where']=qs[_0x5f02('0x37')](_['pick'](_0x253943['query'],_0x1d1389[_0x5f02('0x37')]),_0x2ca0dc);if(_0x253943[_0x5f02('0x2f')]['filter']){_0x5b384a['where']=_['merge'](_0x5b384a[_0x5f02('0x38')],{'$or':_['map'](_0x2ca0dc,function(_0x40a78b){if(_0x40a78b[_0x5f02('0x39')]!=='VIRTUAL'){var _0x4de04f={};_0x4de04f[_0x40a78b[_0x5f02('0x2a')]]={'$like':'%'+_0x253943[_0x5f02('0x2f')]['filter']+'%'};return _0x4de04f;}})});}_0x5b384a=_['merge']({},_0x5b384a,_0x253943[_0x5f02('0x3a')]);var _0x116b0a={'where':_0x5b384a[_0x5f02('0x38')]};return db[_0x5f02('0x3b')]['count'](_0x116b0a)[_0x5f02('0x23')](function(_0xa0732c){_0x34904e[_0x5f02('0x1b')]=_0xa0732c;if(_0x253943[_0x5f02('0x2f')]['includeAll']){_0x5b384a[_0x5f02('0x3c')]=[{'all':!![]}];}return db[_0x5f02('0x3b')][_0x5f02('0x3d')](_0x5b384a);})['then'](function(_0x151e27){_0x34904e['rows']=_0x151e27;return _0x34904e;})[_0x5f02('0x23')](respondWithFilteredResult(_0x285a8b,_0x5b384a))['catch'](handleError(_0x285a8b,null));};exports[_0x5f02('0x3e')]=function(_0x1966c2,_0x4f70a5){var _0x54d680={'raw':!![],'where':{'id':_0x1966c2[_0x5f02('0x3f')]['id']}},_0x2f9838={};_0x2f9838[_0x5f02('0x2d')]=_[_0x5f02('0x30')](db['AnalyticMetric']['rawAttributes']);_0x2f9838[_0x5f02('0x2f')]=_['keys'](_0x1966c2[_0x5f02('0x2f')]);_0x2f9838['filters']=_[_0x5f02('0x31')](_0x2f9838[_0x5f02('0x2d')],_0x2f9838[_0x5f02('0x2f')]);_0x54d680['attributes']=_['intersection'](_0x2f9838['model'],qs[_0x5f02('0x33')](_0x1966c2[_0x5f02('0x2f')]['fields']));_0x54d680[_0x5f02('0x32')]=_0x54d680[_0x5f02('0x32')][_0x5f02('0x34')]?_0x54d680[_0x5f02('0x32')]:_0x2f9838['model'];if(_0x1966c2['query'][_0x5f02('0x40')]){_0x54d680['include']=[{'all':!![]}];}_0x54d680=_[_0x5f02('0x41')]({},_0x54d680,_0x1966c2[_0x5f02('0x3a')]);return db[_0x5f02('0x3b')][_0x5f02('0x42')](_0x54d680)[_0x5f02('0x23')](handleEntityNotFound(_0x4f70a5,null))[_0x5f02('0x23')](respondWithResult(_0x4f70a5,null))[_0x5f02('0x43')](handleError(_0x4f70a5,null));};exports[_0x5f02('0x44')]=function(_0x35d2ed,_0x27d724){return db[_0x5f02('0x3b')][_0x5f02('0x44')](_0x35d2ed[_0x5f02('0x45')],{})[_0x5f02('0x23')](function(_0x14a9e4){var _0x277f38=_0x35d2ed[_0x5f02('0x46')][_0x5f02('0x24')]({'plain':!![]});if(!_0x277f38)throw new Error(_0x5f02('0x47'));if(_0x277f38[_0x5f02('0x48')]==='user'){var _0x495e66=_0x14a9e4[_0x5f02('0x24')]({'plain':!![]});var _0x2cf05a=_0x5f02('0x25');return db[_0x5f02('0x49')]['find']({'where':{'name':_0x2cf05a,'userProfileId':_0x277f38['userProfileId']},'raw':!![]})[_0x5f02('0x23')](function(_0x1141b0){if(_0x1141b0&&_0x1141b0[_0x5f02('0x4a')]===0x0){return db[_0x5f02('0x26')]['create']({'name':_0x495e66['name'],'resourceId':_0x495e66['id'],'type':_0x1141b0[_0x5f02('0x2a')],'sectionId':_0x1141b0['id']},{})[_0x5f02('0x23')](function(){return _0x14a9e4;});}else{return _0x14a9e4;}})[_0x5f02('0x43')](function(_0x1de8db){logger['error'](_0x5f02('0x4b'),_0x1de8db);throw _0x1de8db;});}return _0x14a9e4;})[_0x5f02('0x23')](respondWithResult(_0x27d724,0xc9))[_0x5f02('0x43')](handleError(_0x27d724,null));};exports[_0x5f02('0x22')]=function(_0x4e34dd,_0x465002){if(_0x4e34dd[_0x5f02('0x45')]['id']){delete _0x4e34dd[_0x5f02('0x45')]['id'];}return db[_0x5f02('0x3b')][_0x5f02('0x42')]({'where':{'id':_0x4e34dd[_0x5f02('0x3f')]['id']}})['then'](handleEntityNotFound(_0x465002,null))[_0x5f02('0x23')](saveUpdates(_0x4e34dd['body'],null))['then'](respondWithResult(_0x465002,null))[_0x5f02('0x43')](handleError(_0x465002,null));};exports[_0x5f02('0x27')]=function(_0x5d7cfb,_0x3b6df7){return db[_0x5f02('0x3b')][_0x5f02('0x42')]({'where':{'id':_0x5d7cfb['params']['id']}})['then'](handleEntityNotFound(_0x3b6df7,null))[_0x5f02('0x23')](removeEntity(_0x3b6df7,null))[_0x5f02('0x43')](handleError(_0x3b6df7,null));};