check user template limit in the right way

This commit is contained in:
Simon Martín 2018-08-01 13:13:25 +02:00
parent f27d5ba7d1
commit 2134bf898a

View File

@ -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);