Reduce complexity by extracting a complex condition to a function

This commit is contained in:
Daniel García Aubert
2019-03-11 17:01:13 +01:00
parent 9f71fcf255
commit f0c82f21d2
+14 -2
View File
@@ -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);
}