From b1b2054f0adae42517164ae1d70bbe2b15d7083d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Fri, 17 Nov 2017 19:14:31 +0100 Subject: [PATCH] Split metadata function into two middlewares --- lib/cartodb/controllers/analyses.js | 102 +++++++++++++--------------- 1 file changed, 48 insertions(+), 54 deletions(-) diff --git a/lib/cartodb/controllers/analyses.js b/lib/cartodb/controllers/analyses.js index 19c1f91b..3655702e 100644 --- a/lib/cartodb/controllers/analyses.js +++ b/lib/cartodb/controllers/analyses.js @@ -18,39 +18,58 @@ AnalysesController.prototype.register = function(app) { cors(), userMiddleware, this.prepareContext, - this.catalog(), + this.getCatalog(), + this.getTables(), this.prepareResponse(), this.setCacheControlHeader(), - this.sendResponse() + this.sendResponse(), + this.unathorizedError() ); }; -AnalysesController.prototype.catalog = function () { - return function catalogMiddleware(req, res, next) { +AnalysesController.prototype.getCatalog = function () { + return function getCatalogMiddleware(req, res, next) { + const pg = res.locals.pg = new PSQL(dbParamsFromReqParams(res.locals)); const { user } = res.locals; - const pg = new PSQL(dbParamsFromReqParams(res.locals)); + const readOnlyTransactionOn = true; + const catalogQuery = catalogQueryTpl({ _username: user }); - getMetadata(user, pg, (err, catalogWithTables) => { + pg.query(catalogQuery, (err, resultSet = {}) => { if (err) { - if (err.message.match(/permission\sdenied/)) { - err = new Error('Unauthorized'); - err.http_status = 401; - } - return next(err); } - res.locals.catalogWithTables = catalogWithTables; + res.locals.catalog = resultSet.rows || []; + next(); - }); + }, readOnlyTransactionOn); + }; +}; + +AnalysesController.prototype.getTables = function () { + return function getTablesMiddleware(req, res, next) { + const pg = res.locals.pg = new PSQL(dbParamsFromReqParams(res.locals)); + const { user } = res.locals; + const readOnlyTransactionOn = true; + const tablesQuery = tablesQueryTpl({ _username: user }); + + pg.query(tablesQuery, (err, resultSet = {}) => { + if (err) { + return next(err); + } + + res.locals.tables = resultSet.rows || []; + + next(); + }, readOnlyTransactionOn); }; }; AnalysesController.prototype.prepareResponse = function () { return function prepareResponseMiddleware (req, res, next) { - const { catalogWithTables } = res.locals; + const { catalog, tables } = res.locals; - const analysisIdToTable = catalogWithTables.tables.reduce((analysisIdToTable, table) => { + const analysisIdToTable = tables.reduce((analysisIdToTable, table) => { const analysisId = table.relname.split('_')[2]; if (analysisId && analysisId.length === 40) { analysisIdToTable[analysisId] = table; @@ -58,7 +77,7 @@ AnalysesController.prototype.prepareResponse = function () { return analysisIdToTable; }, {}); - const catalog = catalogWithTables.catalog.map(analysis => { + const analysisCatalog = catalog.map(analysis => { if (analysisIdToTable.hasOwnProperty(analysis.node_id)) { analysis.table = analysisIdToTable[analysis.node_id]; } @@ -80,7 +99,8 @@ AnalysesController.prototype.prepareResponse = function () { return -1; }); - res.body = { catalog }; + res.body = { catalog: analysisCatalog }; + next(); }; }; @@ -104,6 +124,17 @@ AnalysesController.prototype.sendResponse = function() { }; }; +AnalysesController.prototype.unathorizedError = function () { + return function unathorizedErrorMiddleware(err, req, res, next) { + if (err.message.match(/permission\sdenied/)) { + err = new Error('Unauthorized'); + err.http_status = 401; + } + + next(err); + }; +}; + var catalogQueryTpl = dot.template( 'SELECT analysis_def->>\'type\' as type, * FROM cdb_analysis_catalog WHERE username = \'{{=it._username}}\'' ); @@ -127,43 +158,6 @@ var tablesQueryTpl = dot.template([ "ORDER BY size DESC" ].join('\n')); - -function getMetadata(username, pg, callback) { - var results = { - catalog: [], - tables: [] - }; - step( - function getCatalog() { - pg.query(catalogQueryTpl({_username: username}), this, true); // use read-only transaction - }, - function handleCatalog(err, resultSet) { - assert.ifError(err); - resultSet = resultSet || {}; - results.catalog = resultSet.rows || []; - this(); - }, - function getTables(err) { - assert.ifError(err); - pg.query(tablesQueryTpl({_username: username}), this, true); // use read-only transaction - }, - function handleTables(err, resultSet) { - assert.ifError(err); - resultSet = resultSet || {}; - results.tables = resultSet.rows || []; - this(); - }, - function finish(err) { - if (err) { - return callback(err); - } - - return callback(null, results); - } - ); -} - - function dbParamsFromReqParams(params) { var dbParams = {}; if ( params.dbuser ) {