From 2134bf898aaaab50fbe4a0da764446342afe8388 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Mart=C3=ADn?= Date: Wed, 1 Aug 2018 13:13:25 +0200 Subject: [PATCH] check user template limit in the right way --- lib/cartodb/backends/template_maps.js | 46 +++++++++++++++++++-------- 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/lib/cartodb/backends/template_maps.js b/lib/cartodb/backends/template_maps.js index bb7d5485..7ba0b13f 100644 --- a/lib/cartodb/backends/template_maps.js +++ b/lib/cartodb/backends/template_maps.js @@ -173,6 +173,37 @@ function templateDefaults(template) { }); } +/** + * Checks if the if the user reaches the templetes limit + * + * @param userTemplatesKey user templat key in Redis + * @param owner cartodb username of the template owner + * @param callback returns error if the user reaches the limit + */ +TemplateMaps.prototype._checkUserTemplatesLimit = function(userTemplatesKey, owner, callback) { + const limit = this._userTemplateLimit(); + + if(!limit) { + return callback(null); + } + + this._redisCmd('HLEN', [userTemplatesKey], (err, numberOfTemplates) => { + if (err) { + return callback(err); + } + + if (numberOfTemplates >= limit) { + const limitReachedError = new Error( + `User '${owner}' reached limit on number of templates (${numberOfTemplates}/${limit})` + ); + limitReachedError.http_status = 409; + return callback(limitReachedError); + } + + return callback(null); + }); +}; + //--------------- PUBLIC API ------------------------------------- // Add a template @@ -196,25 +227,12 @@ TemplateMaps.prototype.addTemplate = function(owner, template, callback) { } var userTemplatesKey = this.key_usr_tpl({ owner }); - var limit = this._userTemplateLimit(); - if (!limit) { - return 0; - } - - this._redisCmd('HLEN', [userTemplatesKey], (err, numberOfTemplates) => { + this._checkUserTemplatesLimit(userTemplatesKey, owner, err => { if (err) { return callback(err); } - if (numberOfTemplates >= limit) { - var limitReachedError = new Error( - `User '${owner}' reached limit on number of templates (${numberOfTemplates}/${limit})` - ); - limitReachedError.http_status = 409; - return callback(limitReachedError); - } - let templateString; try { templateString = JSON.stringify(template);