Remove step

This commit is contained in:
Daniel García Aubert 2018-05-30 18:15:51 +02:00
parent 5cfffcfa83
commit 1b6a722c0c

View File

@ -1,8 +1,6 @@
var _ = require('underscore');
var assert = require('assert');
var crypto = require('crypto');
var dot = require('dot');
var step = require('step');
var MapConfig = require('windshaft').model.MapConfig;
var templateName = require('../../../backends/template_maps').templateName;
var QueryTables = require('cartodb-query-tables');
@ -58,135 +56,150 @@ NamedMapMapConfigProvider.prototype.getMapConfig = function(callback) {
return callback(this.err, this.mapConfig, this.rendererParams, this.context);
}
var self = this;
var mapConfig = null;
var rendererParams;
var apiKey;
var context = {};
step(
function getTemplate() {
self.getTemplate(this);
},
function prepareDbParams(err, tpl) {
assert.ifError(err);
self.template = tpl;
this.getTemplate((err, tpl) => {
if (err) {
this.err = err;
return callback(err);
}
rendererParams = _.extend({}, self.params, {
user: self.owner
});
self.setDBParams(self.owner, rendererParams, this);
},
function getUserApiKey(err) {
assert.ifError(err);
self.metadataBackend.getUserMapKey(self.owner, this);
},
function prepareParams(err, _apiKey) {
assert.ifError(err);
this.template = tpl;
apiKey = _apiKey;
rendererParams = _.extend({}, this.params, {
user: this.owner
});
var templateParams = {};
if (self.config) {
try {
templateParams = _.isString(self.config) ? JSON.parse(self.config) : self.config;
} catch (e) {
throw new Error('malformed config parameter, should be a valid JSON');
}
this.setDBParams(this.owner, rendererParams, (err) => {
if (err) {
this.err = err;
return callback(err);
}
return templateParams;
},
function instantiateTemplate(err, templateParams) {
assert.ifError(err);
context.templateParams = templateParams;
return self.templateMaps.instance(self.template, templateParams);
},
function prepareAdapterMapConfig(err, requestMapConfig) {
assert.ifError(err);
context.analysisConfiguration = {
user: self.owner,
db: {
host: rendererParams.dbhost,
port: rendererParams.dbport,
dbname: rendererParams.dbname,
user: rendererParams.dbuser,
pass: rendererParams.dbpassword
},
batch: {
username: self.owner,
apiKey: apiKey
this.metadataBackend.getUserMapKey(this.owner, (err, _apiKey) => {
if (err) {
this.err = err;
return callback(err);
}
};
self.mapConfigAdapter.getMapConfig(self.owner, requestMapConfig, rendererParams, context, this);
},
function prepareContextLimits(err, _mapConfig) {
assert.ifError(err);
mapConfig = _mapConfig;
self.userLimitsBackend.getRenderLimits(self.owner, self.params.api_key, this);
},
function cacheAndReturnMapConfig(err, renderLimits) {
self.err = err;
self.mapConfig = (mapConfig === null) ? null : new MapConfig(mapConfig, context.datasource);
self.analysesResults = context.analysesResults || [];
self.rendererParams = rendererParams;
self.context = context;
self.context.limits = renderLimits || {};
return callback(self.err, self.mapConfig, self.rendererParams, self.context);
}
);
apiKey = _apiKey;
var templateParams = {};
if (this.config) {
try {
templateParams = _.isString(this.config) ? JSON.parse(this.config) : this.config;
} catch (e) {
const error = new Error('malformed config parameter, should be a valid JSON');
this.err = error;
return callback(err);
}
}
context.templateParams = templateParams;
let requestMapConfig;
try {
requestMapConfig = this.templateMaps.instance(this.template, templateParams);
} catch (err) {
this.err = err;
return callback(err);
}
context.analysisConfiguration = {
user: this.owner,
db: {
host: rendererParams.dbhost,
port: rendererParams.dbport,
dbname: rendererParams.dbname,
user: rendererParams.dbuser,
pass: rendererParams.dbpassword
},
batch: {
username: this.owner,
apiKey: apiKey
}
};
this.mapConfigAdapter.getMapConfig(this.owner, requestMapConfig, rendererParams, context, (err, _mapConfig) => {
if (err) {
this.err = err;
return callback(err);
}
mapConfig = _mapConfig;
this.userLimitsBackend.getRenderLimits(this.owner, this.params.api_key, (err, renderLimits) => {
if (err) {
this.err = err;
return callback(err);
}
this.mapConfig = (mapConfig === null) ? null : new MapConfig(mapConfig, context.datasource);
this.analysesResults = context.analysesResults || [];
this.rendererParams = rendererParams;
this.context = context;
this.context.limits = renderLimits || {};
return callback(null, this.mapConfig, this.rendererParams, this.context);
});
});
});
});
});
};
NamedMapMapConfigProvider.prototype.getTemplate = function(callback) {
var self = this;
if (!!this.err || this.template !== null) {
return callback(this.err, this.template);
}
step(
function getTemplate() {
self.templateMaps.getTemplate(self.owner, self.templateName, this);
},
function checkExists(err, tpl) {
assert.ifError(err);
if (!tpl) {
var notFoundErr = new Error(
"Template '" + self.templateName + "' of user '" + self.owner + "' not found"
);
notFoundErr.http_status = 404;
throw notFoundErr;
}
return tpl;
},
function checkAuthorized(err, tpl) {
assert.ifError(err);
var authorized = false;
try {
authorized = self.templateMaps.isAuthorized(tpl, self.authToken);
} catch (err) {
// we catch to add http_status
var authorizationFailedErr = new Error('Failed to authorize template');
authorizationFailedErr.http_status = 403;
throw authorizationFailedErr;
}
if ( ! authorized ) {
var unauthorizedErr = new Error('Unauthorized template instantiation');
unauthorizedErr.http_status = 403;
throw unauthorizedErr;
}
return tpl;
},
function cacheAndReturnTemplate(err, template) {
self.err = err;
self.template = template;
return callback(self.err, self.template);
this.templateMaps.getTemplate(this.owner, this.templateName, (err, tpl) => {
if (err) {
this.err = err;
return callback(err);
}
);
if (!tpl) {
var notFoundErr = new Error(
"Template '" + this.templateName + "' of user '" + this.owner + "' not found"
);
notFoundErr.http_status = 404;
this.err = notFoundErr;
return callback(notFoundErr);
}
var authorized = false;
try {
authorized = this.templateMaps.isAuthorized(tpl, this.authToken);
} catch (err) {
// we catch to add http_status
var authorizationFailedErr = new Error('Failed to authorize template');
authorizationFailedErr.http_status = 403;
this.err = authorizationFailedErr;
return callback(authorizationFailedErr);
}
if (!authorized) {
var unauthorizedErr = new Error('Unauthorized template instantiation');
unauthorizedErr.http_status = 403;
this.err = unauthorizedErr;
return callback(unauthorizedErr);
}
this.template = tpl;
return callback(null, this.template);
});
};
NamedMapMapConfigProvider.prototype.getKey = function() {