From 46c76d6a4c1ffde26c85243453bb5c1b278674f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Wed, 1 Nov 2017 19:57:20 +0100 Subject: [PATCH] Create middleware for layergroup creation (anonymous map) --- lib/cartodb/controllers/map.js | 46 ++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/lib/cartodb/controllers/map.js b/lib/cartodb/controllers/map.js index 4bd4457b..2d65b801 100644 --- a/lib/cartodb/controllers/map.js +++ b/lib/cartodb/controllers/map.js @@ -58,6 +58,7 @@ MapController.prototype.register = function(app) { this.prepareContext, createGetPrepareConfig, this.prepareAdapterMapConfig.bind(this), + this.createLayergroup.bind(this), this.createGet.bind(this), mapErrorMiddleware({ label: 'ANONYMOUS LAYERGROUP', @@ -71,6 +72,7 @@ MapController.prototype.register = function(app) { this.prepareContext, createPostPrepareConfig, this.prepareAdapterMapConfig.bind(this), + this.createLayergroup.bind(this), this.createPost.bind(this), mapErrorMiddleware({ label: 'ANONYMOUS LAYERGROUP', @@ -212,32 +214,32 @@ MapController.prototype.prepareAdapterMapConfig = function (req, res, next) { }); }; +MapController.prototype.createLayergroup = function(req, res, next) { + const requestMapConfig = req.body; + const { context, user } = res.locals; + const datasource = context.datasource || Datasource.EmptyDatasource(); + const mapconfig = new MapConfig(requestMapConfig, datasource); + const mapconfigProvider = new CreateLayergroupMapConfigProvider(mapconfig, user, this.userLimitsApi, res.locals); + + res.locals.mapconfig = mapconfig; + res.locals.analysesResults = context.analysesResults; + + this.mapBackend.createLayergroup(mapconfig, res.locals, mapconfigProvider, (err, layergroup) => { + if (err) { + return next(err); + } + + res.locals.layergroup = layergroup; + + next(); + }); +}; + MapController.prototype.create = function(req, res, next) { var self = this; step( - function createLayergroup() { - const requestMapConfig = req.body; - const { context } = res.locals; - - const datasource = context.datasource || Datasource.EmptyDatasource(); - const mapconfig = res.locals.mapconfig = new MapConfig(requestMapConfig, datasource); - self.mapBackend.createLayergroup( - mapconfig, - res.locals, - new CreateLayergroupMapConfigProvider(mapconfig, res.locals.user, self.userLimitsApi, res.locals), - this - ); - }, - function afterLayergroupCreate(err, layergroup) { - assert.ifError(err); - - const { context } = res.locals; - - res.locals.analysesResults = context.analysesResults; - res.locals.layergroup = layergroup; - res.locals.context = context; - + function afterLayergroupCreate() { const afterLayergroupCreate = self.afterLayergroupCreateBuilder({ includeQuery: true });