Extract layergroup-metadata middleware

This commit is contained in:
Daniel García Aubert 2018-03-28 14:06:23 +02:00
parent 78356ab298
commit e5aff3f366
3 changed files with 18 additions and 120 deletions

View File

@ -18,6 +18,7 @@ const lastModifiedHeader = require('../../middleware/last-modified-header');
const lastUpdatedTimeLayergroup = require('./middlewares/last-updated-time-layergroup'); const lastUpdatedTimeLayergroup = require('./middlewares/last-updated-time-layergroup');
const layerStats = require('./middlewares/layer-stats'); const layerStats = require('./middlewares/layer-stats');
const layergroupIdHeader = require('./middlewares/layergroup-id-header'); const layergroupIdHeader = require('./middlewares/layergroup-id-header');
const layergroupMetadata = require('./middlewares/layergroup-metadata');
const sendResponse = require('../../middleware/send-response'); const sendResponse = require('../../middleware/send-response');
const CreateLayergroupMapConfigProvider = require('../../models/mapconfig/provider/create-layergroup-provider'); const CreateLayergroupMapConfigProvider = require('../../models/mapconfig/provider/create-layergroup-provider');
const LayergroupMetadata = require('../../utils/layergroup-metadata'); const LayergroupMetadata = require('../../utils/layergroup-metadata');
@ -105,11 +106,7 @@ AnonymousMapController.prototype.composeCreateMapMiddleware = function () {
lastUpdatedTimeLayergroup(), lastUpdatedTimeLayergroup(),
layerStats(this.pgConnection, this.statsBackend), layerStats(this.pgConnection, this.statsBackend),
layergroupIdHeader(this.templateMaps ,useTemplateHash), layergroupIdHeader(this.templateMaps ,useTemplateHash),
setDataviewsAndWidgetsUrlsToLayergroupMetadata(this.layergroupMetadata), layergroupMetadata(this.layergroupMetadata, includeQuery),
setAnalysesMetadataToLayergroup(this.layergroupMetadata, includeQuery),
setTurboCartoMetadataToLayergroup(this.layergroupMetadata),
setAggregationMetadataToLayergroup(this.layergroupMetadata),
setTilejsonMetadataToLayergroup(this.layergroupMetadata),
sendResponse(), sendResponse(),
augmentError({ label, addContext }) augmentError({ label, addContext })
]; ];
@ -219,61 +216,6 @@ function createLayergroup (mapBackend, userLimitsApi, pgConnection, affectedTabl
}; };
} }
function setDataviewsAndWidgetsUrlsToLayergroupMetadata (layergroupMetadata) {
return function setDataviewsAndWidgetsUrlsToLayergroupMetadataMiddleware (req, res, next) {
const { user, mapConfig } = res.locals;
const layergroup = res.body;
layergroupMetadata.addDataviewsAndWidgetsUrls(user, layergroup, mapConfig.obj());
next();
};
}
function setAnalysesMetadataToLayergroup (layergroupMetadata, includeQuery) {
return function setAnalysesMetadataToLayergroupMiddleware (req, res, next) {
const { user, analysesResults = [] } = res.locals;
const layergroup = res.body;
layergroupMetadata.addAnalysesMetadata(user, layergroup, analysesResults, includeQuery);
next();
};
}
function setTurboCartoMetadataToLayergroup (layergroupMetadata) {
return function setTurboCartoMetadataToLayergroupMiddleware (req, res, next) {
const { mapConfig, context } = res.locals;
const layergroup = res.body;
layergroupMetadata.addTurboCartoContextMetadata(layergroup, mapConfig.obj(), context);
next();
};
}
function setAggregationMetadataToLayergroup (layergroupMetadata) {
return function setAggregationMetadataToLayergroupMiddleware (req, res, next) {
const { mapConfig, context } = res.locals;
const layergroup = res.body;
layergroupMetadata.addAggregationContextMetadata(layergroup, mapConfig.obj(), context);
next();
};
}
function setTilejsonMetadataToLayergroup (layergroupMetadata) {
return function augmentLayergroupTilejsonMiddleware (req, res, next) {
const { user, mapConfig } = res.locals;
const layergroup = res.body;
layergroupMetadata.addTileJsonMetadata(layergroup, user, mapConfig);
next();
};
}
function augmentError (options) { function augmentError (options) {
const { addContext = false, label = 'MAPS CONTROLLER' } = options; const { addContext = false, label = 'MAPS CONTROLLER' } = options;

View File

@ -0,0 +1,14 @@
module.exports = function setMetadataToLayergroup (layergroupMetadata, includeQuery) {
return function setMetadataToLayergroupMiddleware (req, res, next) {
const { user, mapConfig, analysesResults = [], context } = res.locals;
const layergroup = res.body;
layergroupMetadata.addDataviewsAndWidgetsUrls(user, layergroup, mapConfig.obj());
layergroupMetadata.addAnalysesMetadata(user, layergroup, analysesResults, includeQuery);
layergroupMetadata.addTurboCartoContextMetadata(layergroup, mapConfig.obj(), context);
layergroupMetadata.addAggregationContextMetadata(layergroup, mapConfig.obj(), context);
layergroupMetadata.addTileJsonMetadata(layergroup, user, mapConfig);
next();
};
};

View File

@ -15,6 +15,7 @@ const lastModifiedHeader = require('../../middleware/last-modified-header');
const lastUpdatedTimeLayergroup = require('./middlewares/last-updated-time-layergroup'); const lastUpdatedTimeLayergroup = require('./middlewares/last-updated-time-layergroup');
const layerStats = require('./middlewares/layer-stats'); const layerStats = require('./middlewares/layer-stats');
const layergroupIdHeader = require('./middlewares/layergroup-id-header'); const layergroupIdHeader = require('./middlewares/layergroup-id-header');
const layergroupMetadata = require('./middlewares/layergroup-metadata');
const sendResponse = require('../../middleware/send-response'); const sendResponse = require('../../middleware/send-response');
const NamedMapMapConfigProvider = require('../../models/mapconfig/provider/named-map-provider'); const NamedMapMapConfigProvider = require('../../models/mapconfig/provider/named-map-provider');
const CreateLayergroupMapConfigProvider = require('../../models/mapconfig/provider/create-layergroup-provider'); const CreateLayergroupMapConfigProvider = require('../../models/mapconfig/provider/create-layergroup-provider');
@ -116,11 +117,7 @@ NamedMapController.prototype.composeInstantiateTemplateMiddleware = function ()
lastUpdatedTimeLayergroup(), lastUpdatedTimeLayergroup(),
layerStats(this.pgConnection, this.statsBackend), layerStats(this.pgConnection, this.statsBackend),
layergroupIdHeader(this.templateMaps ,useTemplateHash), layergroupIdHeader(this.templateMaps ,useTemplateHash),
setDataviewsAndWidgetsUrlsToLayergroupMetadata(this.layergroupMetadata), layergroupMetadata(this.layergroupMetadata, includeQuery),
setAnalysesMetadataToLayergroup(this.layergroupMetadata, includeQuery),
setTurboCartoMetadataToLayergroup(this.layergroupMetadata),
setAggregationMetadataToLayergroup(this.layergroupMetadata),
setTilejsonMetadataToLayergroup(this.layergroupMetadata),
sendResponse(), sendResponse(),
augmentError({ label, addContext }) augmentError({ label, addContext })
]; ];
@ -226,61 +223,6 @@ function instantiateLayergroup (mapBackend, userLimitsApi, pgConnection, affecte
}; };
} }
function setDataviewsAndWidgetsUrlsToLayergroupMetadata (layergroupMetadata) {
return function setDataviewsAndWidgetsUrlsToLayergroupMetadataMiddleware (req, res, next) {
const { user, mapConfig } = res.locals;
const layergroup = res.body;
layergroupMetadata.addDataviewsAndWidgetsUrls(user, layergroup, mapConfig.obj());
next();
};
}
function setAnalysesMetadataToLayergroup (layergroupMetadata, includeQuery) {
return function setAnalysesMetadataToLayergroupMiddleware (req, res, next) {
const { user, analysesResults = [] } = res.locals;
const layergroup = res.body;
layergroupMetadata.addAnalysesMetadata(user, layergroup, analysesResults, includeQuery);
next();
};
}
function setTurboCartoMetadataToLayergroup (layergroupMetadata) {
return function setTurboCartoMetadataToLayergroupMiddleware (req, res, next) {
const { mapConfig, context } = res.locals;
const layergroup = res.body;
layergroupMetadata.addTurboCartoContextMetadata(layergroup, mapConfig.obj(), context);
next();
};
}
function setAggregationMetadataToLayergroup (layergroupMetadata) {
return function setAggregationMetadataToLayergroupMiddleware (req, res, next) {
const { mapConfig, context } = res.locals;
const layergroup = res.body;
layergroupMetadata.addAggregationContextMetadata(layergroup, mapConfig.obj(), context);
next();
};
}
function setTilejsonMetadataToLayergroup (layergroupMetadata) {
return function augmentLayergroupTilejsonMiddleware (req, res, next) {
const { user, mapConfig } = res.locals;
const layergroup = res.body;
layergroupMetadata.addTileJsonMetadata(layergroup, user, mapConfig);
next();
};
}
function augmentError (options) { function augmentError (options) {
const { addContext = false, label = 'MAPS CONTROLLER' } = options; const { addContext = false, label = 'MAPS CONTROLLER' } = options;