3b6486819e6c321e214bfde23715d9d27655d7c6
[motion2.git] / server / api / dashboardItem / dashboardItem.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 _0x6333=['keys','DashboardItem','rawAttributes','query','filters','attributes','intersection','includeAll','include','find','catch','iframe','attrUrl','attrs','value','https://','attrPath','attrHourFormat','HH:mm:ss','attrTimezone','0.00','attrFontSize','attrMetric','total','attrVoiceQueues','attrChannel','voice','attrFaxAccounts','attrSmsAccounts','attrOpenchannelAccounts','attrWhatsappAccounts','attrChatWebsites','multibar-chart','attrSerie1','attrSerie2','attrSerie3','web-report','attrReport','reportType','attrRefresh','widgetAttributes:\x20%s','stringify','create','body','fast-json-patch','moment','bluebird','util','sox','to-csv','lodash','squel','crypto','ioredis','../../components/auth/service','../../components/parsers/qs','../../config/license/hardware','../../config/logger','../../config/environment','redis','defaults','localhost','socket.io-emitter','./dashboardItem.socket','register','status','end','offset','undefined','count','limit','Content-Range','json','apply','reject','save','update','then','destroy','sendStatus','error','stack','name','send','show','params','model'];(function(_0x2bf768,_0x5692aa){var _0x521c1e=function(_0x839cd){while(--_0x839cd){_0x2bf768['push'](_0x2bf768['shift']());}};_0x521c1e(++_0x5692aa);}(_0x6333,0x83));var _0x3633=function(_0x3c0be1,_0x193384){_0x3c0be1=_0x3c0be1-0x0;var _0x3d3787=_0x6333[_0x3c0be1];return _0x3d3787;};'use strict';var emlformat=require('eml-format');var rimraf=require('rimraf');var zipdir=require('zip-dir');var jsonpatch=require(_0x3633('0x0'));var rp=require('request-promise');var moment=require(_0x3633('0x1'));var BPromise=require(_0x3633('0x2'));var Mustache=require('mustache');var util=require(_0x3633('0x3'));var path=require('path');var sox=require(_0x3633('0x4'));var csv=require(_0x3633('0x5'));var ejs=require('ejs');var fs=require('fs');var fs_extra=require('fs-extra');var _=require(_0x3633('0x6'));var squel=require(_0x3633('0x7'));var crypto=require(_0x3633('0x8'));var jsforce=require('jsforce');var deskjs=require('desk.js');var toCsv=require(_0x3633('0x5'));var querystring=require('querystring');var Papa=require('papaparse');var Redis=require(_0x3633('0x9'));var authService=require(_0x3633('0xa'));var qs=require(_0x3633('0xb'));var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0x3633('0xc'));var logger=require(_0x3633('0xd'))('api');var utils=require('../../config/utils');var config=require(_0x3633('0xe'));var licenseUtil=require('../../config/license/util');var db=require('../../mysqldb')['db'];config[_0x3633('0xf')]=_[_0x3633('0x10')](config[_0x3633('0xf')],{'host':_0x3633('0x11'),'port':0x18eb});var socket=require(_0x3633('0x12'))(new Redis(config['redis']));require(_0x3633('0x13'))[_0x3633('0x14')](socket);function respondWithStatusCode(_0x5bf51b,_0x57460b){_0x57460b=_0x57460b||0xcc;return function(_0x55b6cd){if(_0x55b6cd){return _0x5bf51b['sendStatus'](_0x57460b);}return _0x5bf51b[_0x3633('0x15')](_0x57460b)[_0x3633('0x16')]();};}function respondWithResult(_0x2f5eab,_0x42a894){_0x42a894=_0x42a894||0xc8;return function(_0x1acaeb){if(_0x1acaeb){return _0x2f5eab[_0x3633('0x15')](_0x42a894)['json'](_0x1acaeb);}};}function respondWithFilteredResult(_0x2d123e,_0x349ee7){return function(_0x578e08){if(_0x578e08){var _0x57961b=typeof _0x349ee7[_0x3633('0x17')]==='undefined'&&typeof _0x349ee7['limit']===_0x3633('0x18');var _0x13f7af=_0x578e08[_0x3633('0x19')];var _0x4bdb9b=_0x57961b?0x0:_0x349ee7[_0x3633('0x17')];var _0xd34aef=_0x57961b?_0x578e08[_0x3633('0x19')]:_0x349ee7['offset']+_0x349ee7[_0x3633('0x1a')];var _0x2b7f86;if(_0xd34aef>=_0x13f7af){_0xd34aef=_0x13f7af;_0x2b7f86=0xc8;}else{_0x2b7f86=0xce;}_0x2d123e[_0x3633('0x15')](_0x2b7f86);return _0x2d123e['set'](_0x3633('0x1b'),_0x4bdb9b+'-'+_0xd34aef+'/'+_0x13f7af)[_0x3633('0x1c')](_0x578e08);}return null;};}function patchUpdates(_0x4ed8e3){return function(_0x2efd9a){try{jsonpatch[_0x3633('0x1d')](_0x2efd9a,_0x4ed8e3,!![]);}catch(_0x1d5270){return BPromise[_0x3633('0x1e')](_0x1d5270);}return _0x2efd9a[_0x3633('0x1f')]();};}function saveUpdates(_0x27511a,_0x2db92e){return function(_0xedb9dc){if(_0xedb9dc){return _0xedb9dc[_0x3633('0x20')](_0x27511a)[_0x3633('0x21')](function(_0x2c781f){return _0x2c781f;});}return null;};}function removeEntity(_0x3844d5,_0x4b0ad7){return function(_0x2a1b70){if(_0x2a1b70){return _0x2a1b70[_0x3633('0x22')]()['then'](function(){_0x3844d5[_0x3633('0x15')](0xcc)[_0x3633('0x16')]();});}};}function handleEntityNotFound(_0x394c0a,_0x3c3c22){return function(_0x19df4a){if(!_0x19df4a){_0x394c0a[_0x3633('0x23')](0x194);}return _0x19df4a;};}function handleError(_0x5ef18c,_0x5b8524){_0x5b8524=_0x5b8524||0x1f4;return function(_0x9fa0fd){logger[_0x3633('0x24')](_0x9fa0fd[_0x3633('0x25')]);if(_0x9fa0fd[_0x3633('0x26')]){delete _0x9fa0fd[_0x3633('0x26')];}_0x5ef18c[_0x3633('0x15')](_0x5b8524)[_0x3633('0x27')](_0x9fa0fd);};}exports[_0x3633('0x28')]=function(_0x448aef,_0x2ceabb){var _0x4fddd5={'raw':![],'where':{'id':_0x448aef[_0x3633('0x29')]['id']}},_0x37b887={};_0x37b887[_0x3633('0x2a')]=_[_0x3633('0x2b')](db[_0x3633('0x2c')][_0x3633('0x2d')]);_0x37b887[_0x3633('0x2e')]=_['keys'](_0x448aef[_0x3633('0x2e')]);_0x37b887[_0x3633('0x2f')]=_['intersection'](_0x37b887[_0x3633('0x2a')],_0x37b887[_0x3633('0x2e')]);_0x4fddd5[_0x3633('0x30')]=_[_0x3633('0x31')](_0x37b887[_0x3633('0x2a')],qs['fields'](_0x448aef[_0x3633('0x2e')]['fields']));_0x4fddd5['attributes']=_0x4fddd5[_0x3633('0x30')]['length']?_0x4fddd5[_0x3633('0x30')]:_0x37b887[_0x3633('0x2a')];if(_0x448aef['query'][_0x3633('0x32')]){_0x4fddd5[_0x3633('0x33')]=[{'all':!![]}];}_0x4fddd5=_['merge']({},_0x4fddd5,_0x448aef['options']);return db[_0x3633('0x2c')][_0x3633('0x34')](_0x4fddd5)[_0x3633('0x21')](handleEntityNotFound(_0x2ceabb,null))[_0x3633('0x21')](respondWithResult(_0x2ceabb,null))['catch'](handleError(_0x2ceabb,null));};exports[_0x3633('0x22')]=function(_0x2ce894,_0x18ece9){return db['DashboardItem'][_0x3633('0x34')]({'where':{'id':_0x2ce894['params']['id']}})[_0x3633('0x21')](handleEntityNotFound(_0x18ece9,null))[_0x3633('0x21')](removeEntity(_0x18ece9,null))[_0x3633('0x35')](handleError(_0x18ece9,null));};function widgetAttributes(_0x1efeb8){try{switch(_0x1efeb8['type']){case _0x3633('0x36'):return[{'name':_0x3633('0x37'),'value':_0x1efeb8['attrUrl']||(_0x1efeb8[_0x3633('0x38')]&&_0x1efeb8['attrs'][0x0]?_0x1efeb8[_0x3633('0x38')][0x0][_0x3633('0x39')]:undefined)||_0x3633('0x3a')}];case'custom':return[{'name':_0x3633('0x3b'),'value':_0x1efeb8['attrPath']||(_0x1efeb8[_0x3633('0x38')]&&_0x1efeb8[_0x3633('0x38')][0x0]?_0x1efeb8[_0x3633('0x38')][0x0][_0x3633('0x39')]:undefined)||''}];case'clock':return[{'name':_0x3633('0x3c'),'value':_0x1efeb8[_0x3633('0x3c')]||(_0x1efeb8[_0x3633('0x38')]&&_0x1efeb8[_0x3633('0x38')][0x0]?_0x1efeb8[_0x3633('0x38')][0x0][_0x3633('0x39')]:undefined)||_0x3633('0x3d')},{'name':_0x3633('0x3e'),'value':_0x1efeb8['attrTimezone']||(_0x1efeb8['attrs']&&_0x1efeb8[_0x3633('0x38')][0x1]?_0x1efeb8['attrs'][0x1][_0x3633('0x39')]:undefined)||_0x3633('0x3f')},{'name':'attrFontSize','value':_0x1efeb8[_0x3633('0x40')]||(_0x1efeb8[_0x3633('0x38')]&&_0x1efeb8[_0x3633('0x38')][0x2]?_0x1efeb8[_0x3633('0x38')][0x2][_0x3633('0x39')]:undefined)||0xc}];case'counter':return[{'name':'attrMetric','value':_0x1efeb8[_0x3633('0x41')]||(_0x1efeb8[_0x3633('0x38')]&&_0x1efeb8[_0x3633('0x38')][0x0]?_0x1efeb8[_0x3633('0x38')][0x0][_0x3633('0x39')]:undefined)||_0x3633('0x42')},{'name':_0x3633('0x43'),'value':_0x1efeb8['attrVoiceQueues']||(_0x1efeb8[_0x3633('0x38')]&&_0x1efeb8[_0x3633('0x38')][0x1]?_0x1efeb8[_0x3633('0x38')][0x1]['value']:undefined)||[]},{'name':'attrFontSize','value':_0x1efeb8[_0x3633('0x40')]||(_0x1efeb8['attrs']&&_0x1efeb8[_0x3633('0x38')][0x2]?_0x1efeb8['attrs'][0x2][_0x3633('0x39')]:undefined)||0x14},{'name':_0x3633('0x44'),'value':_0x1efeb8[_0x3633('0x44')]||(_0x1efeb8[_0x3633('0x38')]&&_0x1efeb8[_0x3633('0x38')][0x3]?_0x1efeb8[_0x3633('0x38')][0x3][_0x3633('0x39')]:undefined)||_0x3633('0x45')},{'name':'attrEmailAccounts','value':_0x1efeb8['attrEmailAccounts']||(_0x1efeb8[_0x3633('0x38')]&&_0x1efeb8[_0x3633('0x38')][0x4]?_0x1efeb8['attrs'][0x4][_0x3633('0x39')]:undefined)||''},{'name':_0x3633('0x46'),'value':_0x1efeb8[_0x3633('0x46')]||(_0x1efeb8['attrs']&&_0x1efeb8['attrs'][0x5]?_0x1efeb8[_0x3633('0x38')][0x5][_0x3633('0x39')]:undefined)||''},{'name':_0x3633('0x47'),'value':_0x1efeb8[_0x3633('0x47')]||(_0x1efeb8[_0x3633('0x38')]&&_0x1efeb8[_0x3633('0x38')][0x6]?_0x1efeb8['attrs'][0x6][_0x3633('0x39')]:undefined)||''},{'name':_0x3633('0x48'),'value':_0x1efeb8[_0x3633('0x48')]||(_0x1efeb8[_0x3633('0x38')]&&_0x1efeb8[_0x3633('0x38')][0x7]?_0x1efeb8[_0x3633('0x38')][0x7][_0x3633('0x39')]:undefined)||''},{'name':'attrWhatsappAccounts','value':_0x1efeb8[_0x3633('0x49')]||(_0x1efeb8['attrs']&&_0x1efeb8[_0x3633('0x38')][0x8]?_0x1efeb8[_0x3633('0x38')][0x8]['value']:undefined)||''},{'name':_0x3633('0x4a'),'value':_0x1efeb8['attrChatWebsites']||(_0x1efeb8[_0x3633('0x38')]&&_0x1efeb8[_0x3633('0x38')][0x9]?_0x1efeb8[_0x3633('0x38')][0x9][_0x3633('0x39')]:undefined)||''}];case _0x3633('0x4b'):case'pie-chart':return[{'name':_0x3633('0x4c'),'value':_0x1efeb8[_0x3633('0x4c')]||(_0x1efeb8[_0x3633('0x38')]&&_0x1efeb8['attrs'][0x0]?_0x1efeb8['attrs'][0x0][_0x3633('0x39')]:undefined)||'total'},{'name':'attrSerie2','value':_0x1efeb8[_0x3633('0x4d')]||(_0x1efeb8[_0x3633('0x38')]&&_0x1efeb8[_0x3633('0x38')][0x1]?_0x1efeb8[_0x3633('0x38')][0x1]['value']:undefined)||null},{'name':_0x3633('0x4e'),'value':_0x1efeb8[_0x3633('0x4e')]||(_0x1efeb8[_0x3633('0x38')]&&_0x1efeb8[_0x3633('0x38')][0x2]?_0x1efeb8[_0x3633('0x38')][0x2][_0x3633('0x39')]:undefined)||null},{'name':_0x3633('0x43'),'value':_0x1efeb8['attrVoiceQueues']||(_0x1efeb8['attrs']&&_0x1efeb8['attrs'][0x3]?_0x1efeb8[_0x3633('0x38')][0x3][_0x3633('0x39')]:undefined)||[]},{'name':_0x3633('0x44'),'value':_0x1efeb8['attrChannel']||(_0x1efeb8[_0x3633('0x38')]&&_0x1efeb8[_0x3633('0x38')][0x4]?_0x1efeb8['attrs'][0x4][_0x3633('0x39')]:undefined)||_0x3633('0x45')},{'name':'attrEmailAccounts','value':_0x1efeb8['attrEmailAccounts']||(_0x1efeb8['attrs']&&_0x1efeb8[_0x3633('0x38')][0x5]?_0x1efeb8[_0x3633('0x38')][0x5][_0x3633('0x39')]:undefined)||''},{'name':_0x3633('0x46'),'value':_0x1efeb8[_0x3633('0x46')]||(_0x1efeb8['attrs']&&_0x1efeb8[_0x3633('0x38')][0x6]?_0x1efeb8[_0x3633('0x38')][0x6][_0x3633('0x39')]:undefined)||''},{'name':'attrSmsAccounts','value':_0x1efeb8[_0x3633('0x47')]||(_0x1efeb8[_0x3633('0x38')]&&_0x1efeb8['attrs'][0x7]?_0x1efeb8['attrs'][0x7][_0x3633('0x39')]:undefined)||''},{'name':_0x3633('0x48'),'value':_0x1efeb8['attrOpenchannelAccounts']||(_0x1efeb8['attrs']&&_0x1efeb8['attrs'][0x8]?_0x1efeb8[_0x3633('0x38')][0x8][_0x3633('0x39')]:undefined)||''},{'name':_0x3633('0x49'),'value':_0x1efeb8[_0x3633('0x49')]||(_0x1efeb8[_0x3633('0x38')]&&_0x1efeb8['attrs'][0x9]?_0x1efeb8[_0x3633('0x38')][0x9][_0x3633('0x39')]:undefined)||''},{'name':_0x3633('0x4a'),'value':_0x1efeb8[_0x3633('0x4a')]||(_0x1efeb8[_0x3633('0x38')]&&_0x1efeb8[_0x3633('0x38')][0xa]?_0x1efeb8['attrs'][0xa][_0x3633('0x39')]:undefined)||''}];case _0x3633('0x4f'):return[{'name':_0x3633('0x50'),'type':_0x1efeb8[_0x3633('0x51')]||(_0x1efeb8[_0x3633('0x38')]&&_0x1efeb8[_0x3633('0x38')][0x0]?_0x1efeb8[_0x3633('0x38')][0x0]['type']:undefined)||null,'value':_0x1efeb8['attrReport']||(_0x1efeb8[_0x3633('0x38')]&&_0x1efeb8[_0x3633('0x38')][0x0]?_0x1efeb8['attrs'][0x0]['value']:undefined)||null},{'name':_0x3633('0x52'),'value':_0x1efeb8['attrRefresh']||(_0x1efeb8[_0x3633('0x38')]&&_0x1efeb8[_0x3633('0x38')][0x1]?_0x1efeb8[_0x3633('0x38')][0x1]['value']:undefined)||0x0}];default:return[];}}catch(_0x57a449){logger['error'](_0x3633('0x53'),JSON[_0x3633('0x54')](_0x57a449));return[];}}exports['create']=function(_0xfc959a,_0x14c8e0){if(_0xfc959a['body']){_0xfc959a['body'][_0x3633('0x38')]=widgetAttributes(_0xfc959a['body']);}return db[_0x3633('0x2c')][_0x3633('0x55')](_0xfc959a[_0x3633('0x56')],{})[_0x3633('0x21')](respondWithResult(_0x14c8e0,0xc9))[_0x3633('0x35')](handleError(_0x14c8e0,null));};exports[_0x3633('0x20')]=function(_0x2e52e7,_0x49a462){if(_0x2e52e7[_0x3633('0x56')]['id']){delete _0x2e52e7[_0x3633('0x56')]['id'];}_0x2e52e7[_0x3633('0x56')][_0x3633('0x38')]=widgetAttributes(_0x2e52e7['body']);return db[_0x3633('0x2c')][_0x3633('0x34')]({'where':{'id':_0x2e52e7[_0x3633('0x29')]['id']}})[_0x3633('0x21')](handleEntityNotFound(_0x49a462,null))[_0x3633('0x21')](saveUpdates(_0x2e52e7['body'],null))[_0x3633('0x21')](respondWithResult(_0x49a462,null))[_0x3633('0x35')](handleError(_0x49a462,null));};