2014-12-10 19:11:52 +01:00
|
|
|
var Promise = require("bluebird");
|
2016-04-28 16:58:59 +02:00
|
|
|
var eventType = require('../eventType');
|
|
|
|
var logger = require('../logger');
|
|
|
|
var NameExistsError = require('../error/NameExistsError');
|
|
|
|
var ValidationError = require('../error/ValidationError');
|
|
|
|
var NotFoundError = require('../error/NotFoundError');
|
|
|
|
var validateRequest = require('../error/validateRequest');
|
|
|
|
var extractUser = require('../extractUser');
|
2014-11-17 22:39:04 +01:00
|
|
|
|
2016-05-01 22:53:09 +02:00
|
|
|
module.exports = function (app, config) {
|
|
|
|
var strategyDb = config.strategyDb;
|
|
|
|
var eventStore = config.eventStore;
|
|
|
|
|
2014-10-30 21:10:48 +01:00
|
|
|
app.get('/strategies', function (req, res) {
|
2014-11-17 22:39:04 +01:00
|
|
|
strategyDb.getStrategies().then(function (strategies) {
|
2016-04-24 22:41:37 +02:00
|
|
|
res.json({ strategies: strategies });
|
2014-11-17 22:39:04 +01:00
|
|
|
});
|
2014-10-30 21:10:48 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
app.get('/strategies/:name', function (req, res) {
|
2014-11-25 15:28:08 +01:00
|
|
|
strategyDb.getStrategy(req.params.name)
|
2016-04-24 22:41:37 +02:00
|
|
|
.then(function (strategy) {
|
|
|
|
res.json(strategy);
|
|
|
|
})
|
|
|
|
.catch(function () {
|
|
|
|
res.status(404).json({ error: 'Could not find strategy' });
|
|
|
|
});
|
2014-10-30 21:10:48 +01:00
|
|
|
});
|
|
|
|
|
2014-12-08 20:56:22 +01:00
|
|
|
app.delete('/strategies/:name', function (req, res) {
|
2015-02-12 11:41:30 +01:00
|
|
|
var strategyName = req.params.name;
|
|
|
|
|
|
|
|
strategyDb.getStrategy(strategyName)
|
|
|
|
.then(function() {
|
|
|
|
return eventStore.create({
|
|
|
|
type: eventType.strategyDeleted,
|
|
|
|
createdBy: extractUser(req),
|
|
|
|
data: {
|
|
|
|
name: strategyName
|
|
|
|
}
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.then(function() {
|
|
|
|
res.status(200).end();
|
|
|
|
})
|
|
|
|
.catch(NotFoundError, function() {
|
|
|
|
res.status(404).end();
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
logger.error('Could not delete strategy='+strategyName, err);
|
|
|
|
res.status(500).end();
|
|
|
|
});
|
2014-12-08 20:56:22 +01:00
|
|
|
});
|
|
|
|
|
2014-10-30 21:10:48 +01:00
|
|
|
app.post('/strategies', function (req, res) {
|
2014-11-25 15:28:08 +01:00
|
|
|
req.checkBody('name', 'Name is required').notEmpty();
|
|
|
|
req.checkBody('name', 'Name must match format ^[a-zA-Z\\.\\-]+$').matches(/^[a-zA-Z\\.\\-]+$/i);
|
|
|
|
|
|
|
|
var newStrategy = req.body;
|
|
|
|
|
2014-12-10 19:11:52 +01:00
|
|
|
validateRequest(req)
|
|
|
|
.then(validateStrategyName)
|
|
|
|
.then(function() {
|
|
|
|
return eventStore.create({
|
|
|
|
type: eventType.strategyCreated,
|
2015-02-05 13:39:00 +01:00
|
|
|
createdBy: extractUser(req),
|
2014-12-10 19:11:52 +01:00
|
|
|
data: newStrategy
|
|
|
|
});
|
2014-11-25 15:28:08 +01:00
|
|
|
})
|
2014-12-10 19:11:52 +01:00
|
|
|
.then(function () {
|
|
|
|
res.status(201).end();
|
|
|
|
})
|
|
|
|
.catch(NameExistsError, function() {
|
2016-04-24 22:41:37 +02:00
|
|
|
res.status(403).json([{ msg: "A strategy named '" + req.body.name + "' already exists." }]).end();
|
2014-12-10 19:11:52 +01:00
|
|
|
})
|
|
|
|
.catch(ValidationError, function() {
|
|
|
|
res.status(400).json(req.validationErrors());
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
logger.error("Could not create strategy", err);
|
|
|
|
res.status(500).end();
|
|
|
|
});
|
2014-10-30 21:10:48 +01:00
|
|
|
});
|
|
|
|
|
2014-12-10 19:11:52 +01:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2014-10-30 21:10:48 +01:00
|
|
|
};
|