From 804c6645fad2863a667d1551bbc5d90751e8bbb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Fri, 17 Nov 2017 18:28:37 +0100 Subject: [PATCH] Make catalog method a regular middleware factory --- lib/cartodb/controllers/analyses.js | 52 +++++++++++++++-------------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/lib/cartodb/controllers/analyses.js b/lib/cartodb/controllers/analyses.js index eab5056b..7a6d75f8 100644 --- a/lib/cartodb/controllers/analyses.js +++ b/lib/cartodb/controllers/analyses.js @@ -18,13 +18,39 @@ AnalysesController.prototype.register = function(app) { cors(), userMiddleware, this.prepareContext, - this.catalog.bind(this), + this.catalog(), this.prepareResponse(), this.setCacheControlHeader(), this.sendResponse() ); }; +AnalysesController.prototype.catalog = function () { + return function catalogMiddleware(req, res, next) { + const { user } = res.locals; + + step( + function catalogQuery() { + var pg = new PSQL(dbParamsFromReqParams(res.locals)); + getMetadata(user, pg, this); + }, + function prepareResponse(err, catalogWithTables) { + if (err) { + if (err.message.match(/permission\sdenied/)) { + err = new Error('Unauthorized'); + err.http_status = 401; + } + + return next(err); + } + + res.locals.catalogWithTables = catalogWithTables; + next(); + } + ); + }; +}; + AnalysesController.prototype.prepareResponse = function () { return function prepareResponseMiddleware (req, res, next) { const { catalogWithTables } = res.locals; @@ -83,30 +109,6 @@ AnalysesController.prototype.sendResponse = function() { }; }; -AnalysesController.prototype.catalog = function (req, res, next) { - var username = res.locals.user; - - step( - function catalogQuery() { - var pg = new PSQL(dbParamsFromReqParams(res.locals)); - getMetadata(username, pg, this); - }, - function prepareResponse(err, catalogWithTables) { - if (err) { - if (err.message.match(/permission\sdenied/)) { - err = new Error('Unauthorized'); - err.http_status = 401; - } - - return next(err); - } - - res.locals.catalogWithTables = catalogWithTables; - next(); - } - ); -}; - var catalogQueryTpl = dot.template( 'SELECT analysis_def->>\'type\' as type, * FROM cdb_analysis_catalog WHERE username = \'{{=it._username}}\'' );