mirror of
https://github.com/Unleash/unleash.git
synced 2025-01-11 00:08:30 +01:00
Clean up strategyApi with chained-promises
This commit is contained in:
parent
553f20c3a6
commit
00da8d1e3b
@ -6,7 +6,7 @@ var featureDb = require('./featureDb');
|
|||||||
var NameExistsError = require('./error/NameExistsError');
|
var NameExistsError = require('./error/NameExistsError');
|
||||||
var NotFoundError = require('./error/NotFoundError');
|
var NotFoundError = require('./error/NotFoundError');
|
||||||
var ValidationError = require('./error/ValidationError');
|
var ValidationError = require('./error/ValidationError');
|
||||||
var requestValidator = require('./error/requestValidator');
|
var validateRequest = require('./error/validateRequest');
|
||||||
|
|
||||||
module.exports = function (app) {
|
module.exports = function (app) {
|
||||||
|
|
||||||
@ -28,7 +28,7 @@ module.exports = function (app) {
|
|||||||
req.checkBody('name', 'Name is required').notEmpty();
|
req.checkBody('name', 'Name is required').notEmpty();
|
||||||
req.checkBody('name', 'Name must match format ^[a-zA-Z\\.\\-]+$').matches(/^[a-zA-Z\\.\\-]+$/i);
|
req.checkBody('name', 'Name must match format ^[a-zA-Z\\.\\-]+$').matches(/^[a-zA-Z\\.\\-]+$/i);
|
||||||
|
|
||||||
requestValidator(req)
|
validateRequest(req)
|
||||||
.then(validateUniqueName)
|
.then(validateUniqueName)
|
||||||
.then(function() {
|
.then(function() {
|
||||||
return eventStore.create({
|
return eventStore.create({
|
||||||
|
@ -1,6 +1,11 @@
|
|||||||
|
var Promise = require("bluebird");
|
||||||
var eventStore = require('./eventStore');
|
var eventStore = require('./eventStore');
|
||||||
var eventType = require('./eventType');
|
var eventType = require('./eventType');
|
||||||
var strategyDb = require('./strategyDb');
|
var strategyDb = require('./strategyDb');
|
||||||
|
var logger = require('./logger');
|
||||||
|
var NameExistsError = require('./error/NameExistsError');
|
||||||
|
var ValidationError = require('./error/ValidationError');
|
||||||
|
var validateRequest = require('./error/validateRequest');
|
||||||
|
|
||||||
module.exports = function (app) {
|
module.exports = function (app) {
|
||||||
|
|
||||||
@ -32,34 +37,42 @@ module.exports = function (app) {
|
|||||||
req.checkBody('name', 'Name is required').notEmpty();
|
req.checkBody('name', 'Name is required').notEmpty();
|
||||||
req.checkBody('name', 'Name must match format ^[a-zA-Z\\.\\-]+$').matches(/^[a-zA-Z\\.\\-]+$/i);
|
req.checkBody('name', 'Name must match format ^[a-zA-Z\\.\\-]+$').matches(/^[a-zA-Z\\.\\-]+$/i);
|
||||||
|
|
||||||
var errors = req.validationErrors();
|
|
||||||
|
|
||||||
if (errors) {
|
|
||||||
res.status(400).json(errors);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var newStrategy = req.body;
|
var newStrategy = req.body;
|
||||||
|
|
||||||
var handleStrategyExists = function() {
|
validateRequest(req)
|
||||||
var errors = [{msg: "A strategy named " + newStrategy.name + " already exists."}];
|
.then(validateStrategyName)
|
||||||
res.status(403).json(errors);
|
.then(function() {
|
||||||
};
|
return eventStore.create({
|
||||||
|
|
||||||
var handleCreateStrategy = function() {
|
|
||||||
eventStore.create({
|
|
||||||
type: eventType.strategyCreated,
|
type: eventType.strategyCreated,
|
||||||
createdBy: req.connection.remoteAddress,
|
createdBy: req.connection.remoteAddress,
|
||||||
data: newStrategy
|
data: newStrategy
|
||||||
|
});
|
||||||
})
|
})
|
||||||
.then(function () { res.status(201).end(); })
|
.then(function () {
|
||||||
.catch(function () { res.status(500).end(); });
|
res.status(201).end();
|
||||||
};
|
})
|
||||||
|
.catch(NameExistsError, function() {
|
||||||
strategyDb.getStrategy(newStrategy.name)
|
res.status(403).json([{msg: "A strategy named '" + req.body.name + "' already exists."}]).end();
|
||||||
.then(handleStrategyExists)
|
})
|
||||||
.catch(handleCreateStrategy);
|
.catch(ValidationError, function() {
|
||||||
|
res.status(400).json(req.validationErrors());
|
||||||
|
})
|
||||||
|
.catch(function(err) {
|
||||||
|
logger.error("Could not create strategy", err);
|
||||||
|
res.status(500).end();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function validateStrategyName(req) {
|
||||||
|
return new Promise(function(resolve, reject) {
|
||||||
|
strategyDb.getStrategy(req.body.name)
|
||||||
|
.then(function() {
|
||||||
|
reject(new NameExistsError("Feature name already exist"));
|
||||||
|
}, function() {
|
||||||
|
resolve(req);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user