diff --git a/lib/cartodb/controllers/named_maps_admin.js b/lib/cartodb/controllers/named_maps_admin.js index ca7b4901..2efac8cc 100644 --- a/lib/cartodb/controllers/named_maps_admin.js +++ b/lib/cartodb/controllers/named_maps_admin.js @@ -1,11 +1,8 @@ -var step = require('step'); -var assert = require('assert'); var templateName = require('../backends/template_maps').templateName; var cors = require('../middleware/cors'); var userMiddleware = require('../middleware/user'); - /** * @param {AuthApi} authApi * @param {PgConnection} pgConnection @@ -69,123 +66,98 @@ NamedMapsAdminController.prototype.register = function (app) { }; NamedMapsAdminController.prototype.create = function(req, res, next) { - var self = this; + const cdbuser = res.locals.user; + const cfg = req.body; - var cdbuser = res.locals.user; - - step( - function addTemplate() { - var cfg = req.body; - self.templateMaps.addTemplate(cdbuser, cfg, this); - }, - function prepareResponse(err, tpl_id){ - assert.ifError(err); - - res.status(200); - - const method = req.query.callback ? 'jsonp' : 'json'; - res[method]({ template_id: tpl_id }); + this.templateMaps.addTemplate(cdbuser, cfg, (err, tpl_id) => { + if (err) { + return next(err); } - ); + + res.status(200); + + const method = req.query.callback ? 'jsonp' : 'json'; + res[method]({ template_id: tpl_id }); + }); }; NamedMapsAdminController.prototype.update = function(req, res, next) { - var self = this; + const cdbuser = res.locals.user; + const template = req.body; + const tpl_id = templateName(req.params.template_id); - var cdbuser = res.locals.user; - var template; - var tpl_id; - - step( - function updateTemplate() { - template = req.body; - tpl_id = templateName(req.params.template_id); - self.templateMaps.updTemplate(cdbuser, tpl_id, template, this); - }, - function prepareResponse(err){ - assert.ifError(err); - - res.status(200); - - const method = req.query.callback ? 'jsonp' : 'json'; - res[method]({ template_id: tpl_id }); + this.templateMaps.updTemplate(cdbuser, tpl_id, template, (err) => { + if (err) { + return next(err); } - ); + + res.status(200); + + const method = req.query.callback ? 'jsonp' : 'json'; + res[method]({ template_id: tpl_id }); + }); }; NamedMapsAdminController.prototype.retrieve = function(req, res, next) { - var self = this; - req.profiler.start('windshaft-cartodb.get_template'); - var cdbuser = res.locals.user; - var tpl_id; - step( - function getTemplate() { - tpl_id = templateName(req.params.template_id); - self.templateMaps.getTemplate(cdbuser, tpl_id, this); - }, - function prepareResponse(err, tpl_val) { - assert.ifError(err); - if ( ! tpl_val ) { - const error = new Error(`Cannot find template '${tpl_id}' of user '${cdbuser}'`); - error.http_status = 404; - return next(error); - } - // auth_id was added by ourselves, - // so we remove it before returning to the user - delete tpl_val.auth_id; + const cdbuser = res.locals.user; + const tpl_id = templateName(req.params.template_id); - res.status(200); - - const method = req.query.callback ? 'jsonp' : 'json'; - res[method]({ template: tpl_val }); + this.templateMaps.getTemplate(cdbuser, tpl_id, (err, tpl_val) => { + if (err) { + return next(err); } - ); + + if (!tpl_val) { + const error = new Error(`Cannot find template '${tpl_id}' of user '${cdbuser}'`); + error.http_status = 404; + return next(error); + } + // auth_id was added by ourselves, + // so we remove it before returning to the user + delete tpl_val.auth_id; + + res.status(200); + + const method = req.query.callback ? 'jsonp' : 'json'; + res[method]({ template: tpl_val }); + }); }; NamedMapsAdminController.prototype.destroy = function(req, res, next) { - var self = this; - req.profiler.start('windshaft-cartodb.delete_template'); - var cdbuser = res.locals.user; - var tpl_id; - step( - function deleteTemplate() { - tpl_id = templateName(req.params.template_id); - self.templateMaps.delTemplate(cdbuser, tpl_id, this); - }, - function prepareResponse(err/*, tpl_val*/){ - assert.ifError(err); + const cdbuser = res.locals.user; + const tpl_id = templateName(req.params.template_id); - res.status(204); - - const method = req.query.callback ? 'jsonp' : 'json'; - res[method](''); + this.templateMaps.delTemplate(cdbuser, tpl_id, (err/*, tpl_val*/) => { + if (err) { + return next(err); } - ); + + res.status(204); + + const method = req.query.callback ? 'jsonp' : 'json'; + res[method](''); + }); }; NamedMapsAdminController.prototype.list = function(req, res, next) { - var self = this; req.profiler.start('windshaft-cartodb.get_template_list'); - var cdbuser = res.locals.user; + const cdbuser = res.locals.user; - step( - function listTemplates() { - self.templateMaps.listTemplates(cdbuser, this); - }, - function prepareResponse(err, tpl_ids){ - assert.ifError(err); - - res.status(200); - - const method = req.query.callback ? 'jsonp' : 'json'; - res[method]({ template_ids: tpl_ids }); + this.templateMaps.listTemplates(cdbuser, (err, tpl_ids) => { + if (err) { + return next(err); } - ); + + res.status(200); + + const method = req.query.callback ? 'jsonp' : 'json'; + res[method]({ template_ids: tpl_ids }); + }); }; NamedMapsAdminController.prototype.authorizedByAPIKey = function (action, label) {