diff --git a/lib/cartodb/controllers/named_maps_admin.js b/lib/cartodb/controllers/named_maps_admin.js index 0728b38c..ef1a0bcc 100644 --- a/lib/cartodb/controllers/named_maps_admin.js +++ b/lib/cartodb/controllers/named_maps_admin.js @@ -26,9 +26,9 @@ NamedMapsAdminController.prototype.register = function (app) { userMiddleware(), localsMiddleware(), credentialsMiddleware(), - this.checkContentType('POST', 'POST TEMPLATE'), - this.authorizedByAPIKey('create', 'POST TEMPLATE'), - this.create() + checkContentType('POST', 'POST TEMPLATE'), + authorizedByAPIKey(this.authApi, 'create', 'POST TEMPLATE'), + create(this.templateMaps) ); app.put( @@ -37,9 +37,9 @@ NamedMapsAdminController.prototype.register = function (app) { userMiddleware(), localsMiddleware(), credentialsMiddleware(), - this.checkContentType('PUT', 'PUT TEMPLATE'), - this.authorizedByAPIKey('update', 'PUT TEMPLATE'), - this.update() + checkContentType('PUT', 'PUT TEMPLATE'), + authorizedByAPIKey(this.authApi, 'update', 'PUT TEMPLATE'), + update(this.templateMaps) ); app.get( @@ -48,8 +48,8 @@ NamedMapsAdminController.prototype.register = function (app) { userMiddleware(), localsMiddleware(), credentialsMiddleware(), - this.authorizedByAPIKey('get', 'GET TEMPLATE'), - this.retrieve() + authorizedByAPIKey(this.authApi, 'get', 'GET TEMPLATE'), + retrieve(this.templateMaps) ); app.delete( @@ -58,8 +58,8 @@ NamedMapsAdminController.prototype.register = function (app) { userMiddleware(), localsMiddleware(), credentialsMiddleware(), - this.authorizedByAPIKey('delete', 'DELETE TEMPLATE'), - this.destroy() + authorizedByAPIKey(this.authApi, 'delete', 'DELETE TEMPLATE'), + destroy(this.templateMaps) ); app.get( @@ -68,8 +68,8 @@ NamedMapsAdminController.prototype.register = function (app) { userMiddleware(), localsMiddleware(), credentialsMiddleware(), - this.authorizedByAPIKey('list', 'GET TEMPLATE LIST'), - this.list() + authorizedByAPIKey(this.authApi, 'list', 'GET TEMPLATE LIST'), + list(this.templateMaps) ); app.options( @@ -78,10 +78,22 @@ NamedMapsAdminController.prototype.register = function (app) { ); }; -NamedMapsAdminController.prototype.authorizedByAPIKey = function (action, label) { +function checkContentType (action, label) { + return function checkContentTypeMiddleware (req, res, next) { + if (!req.is('application/json')) { + const error = new Error(`template ${action} data must be of type application/json`); + error.label = label; + return next(error); + } + next(); + }; +} + +function authorizedByAPIKey (authApi, action, label) { return function authorizedByAPIKeyMiddleware (req, res, next) { const { user } = res.locals; - this.authApi.authorizedByAPIKey(user, res, (err, authenticated) => { + + authApi.authorizedByAPIKey(user, res, (err, authenticated) => { if (err) { return next(err); } @@ -95,26 +107,15 @@ NamedMapsAdminController.prototype.authorizedByAPIKey = function (action, label) next(); }); - }.bind(this); -}; - -NamedMapsAdminController.prototype.checkContentType = function (action, label) { - return function checkContentTypeMiddleware (req, res, next) { - if (!req.is('application/json')) { - const error = new Error(`template ${action} data must be of type application/json`); - error.label = label; - return next(error); - } - next(); }; -}; +} -NamedMapsAdminController.prototype.create = function () { +function create (templateMaps) { return function createTemplateMiddleware (req, res, next) { const { user } = res.locals; const template = req.body; - this.templateMaps.addTemplate(user, template, (err, templateId) => { + templateMaps.addTemplate(user, template, (err, templateId) => { if (err) { return next(err); } @@ -124,16 +125,16 @@ NamedMapsAdminController.prototype.create = function () { const method = req.query.callback ? 'jsonp' : 'json'; res[method]({ template_id: templateId }); }); - }.bind(this); -}; + }; +} -NamedMapsAdminController.prototype.update = function () { +function update (templateMaps) { return function updateTemplateMiddleware (req, res, next) { const { user } = res.locals; const template = req.body; const templateId = templateName(req.params.template_id); - this.templateMaps.updTemplate(user, templateId, template, (err) => { + templateMaps.updTemplate(user, templateId, template, (err) => { if (err) { return next(err); } @@ -143,17 +144,17 @@ NamedMapsAdminController.prototype.update = function () { const method = req.query.callback ? 'jsonp' : 'json'; res[method]({ template_id: templateId }); }); - }.bind(this); -}; + }; +} -NamedMapsAdminController.prototype.retrieve = function () { +function retrieve (templateMaps) { return function retrieveTemplateMiddleware (req, res, next) { req.profiler.start('windshaft-cartodb.get_template'); const { user } = res.locals; const templateId = templateName(req.params.template_id); - this.templateMaps.getTemplate(user, templateId, (err, template) => { + templateMaps.getTemplate(user, templateId, (err, template) => { if (err) { return next(err); } @@ -172,17 +173,17 @@ NamedMapsAdminController.prototype.retrieve = function () { const method = req.query.callback ? 'jsonp' : 'json'; res[method]({ template }); }); - }.bind(this); -}; + }; +} -NamedMapsAdminController.prototype.destroy = function () { +function destroy (templateMaps) { return function destroyTemplateMiddleware (req, res, next) { req.profiler.start('windshaft-cartodb.delete_template'); const { user } = res.locals; const templateId = templateName(req.params.template_id); - this.templateMaps.delTemplate(user, templateId, (err/* , tpl_val */) => { + templateMaps.delTemplate(user, templateId, (err/* , tpl_val */) => { if (err) { return next(err); } @@ -192,16 +193,16 @@ NamedMapsAdminController.prototype.destroy = function () { const method = req.query.callback ? 'jsonp' : 'json'; res[method](''); }); - }.bind(this); -}; + }; +} -NamedMapsAdminController.prototype.list = function () { +function list (templateMaps) { return function listTemplatesMiddleware (req, res, next) { req.profiler.start('windshaft-cartodb.get_template_list'); const { user } = res.locals; - this.templateMaps.listTemplates(user, (err, templateIds) => { + templateMaps.listTemplates(user, (err, templateIds) => { if (err) { return next(err); } @@ -211,5 +212,5 @@ NamedMapsAdminController.prototype.list = function () { const method = req.query.callback ? 'jsonp' : 'json'; res[method]({ template_ids: templateIds }); }); - }.bind(this); -}; + }; +}