From f0c82f21d23e874fb21687ec9d015b69525bc65c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Mon, 11 Mar 2019 17:01:13 +0100 Subject: [PATCH] Reduce complexity by extracting a complex condition to a function --- lib/cartodb/backends/cluster.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/cartodb/backends/cluster.js b/lib/cartodb/backends/cluster.js index c9776eba..31a2c7e9 100644 --- a/lib/cartodb/backends/cluster.js +++ b/lib/cartodb/backends/cluster.js @@ -7,7 +7,7 @@ const AggregationMapConfig = require('../models/aggregation/aggregation-mapconfi module.exports = class ClusterBackend { // TODO: reduce complexity - // jshint maxcomplexity: 17 + // jshint maxcomplexity: 16 getClusterFeatures (mapConfigProvider, params, callback) { mapConfigProvider.getMapConfig((err, _mapConfig) => { if (err) { @@ -26,7 +26,7 @@ module.exports = class ClusterBackend { const layer = mapConfig.getLayer(layerIndex); - if (layer.options.aggregation === false || !mapConfig.isAggregationLayer(layerIndex)) { + if (!hasAggregationLayer(mapConfig, layerIndex)) { const error = new Error(`Map ${token} has no aggregation defined for layer ${layerIndex}`); error.http_status = 400; error.type = 'layer'; @@ -37,6 +37,7 @@ module.exports = class ClusterBackend { }; debug(error); + return callback(error); } @@ -249,3 +250,14 @@ const aggregationQuery = ctx => ` FROM (${ctx.query}) __cdb_aggregation ${ctx.columns.length ? `GROUP BY ${ctx.columns.join(', ')}` : ''} `; + +// TODO: update when https://github.com/CartoDB/Windshaft-cartodb/pull/1082 is merged +function hasAggregationLayer (mapConfig, layerIndex) { + const layer = mapConfig.getLayer(layerIndex); + + if (typeof layer.options.aggregation === 'boolean') { + return layer.options.aggregation; + } + + return mapConfig.isAggregationLayer(layerIndex); +}