2016-06-18 21:53:18 +02:00
|
|
|
'use strict';
|
2016-08-22 15:12:05 +02:00
|
|
|
const BPromise = require('bluebird');
|
2016-06-20 10:27:19 +02:00
|
|
|
const eventType = require('../eventType');
|
|
|
|
const logger = require('../logger');
|
|
|
|
const NameExistsError = require('../error/NameExistsError');
|
|
|
|
const ValidationError = require('../error/ValidationError');
|
|
|
|
const NotFoundError = require('../error/NotFoundError');
|
|
|
|
const validateRequest = require('../error/validateRequest');
|
|
|
|
const extractUser = require('../extractUser');
|
2016-09-05 16:50:52 +02:00
|
|
|
const version = 1;
|
2014-11-17 22:39:04 +01:00
|
|
|
|
2016-05-01 22:53:09 +02:00
|
|
|
module.exports = function (app, config) {
|
2016-06-18 21:53:18 +02:00
|
|
|
const strategyDb = config.strategyDb;
|
|
|
|
const eventStore = config.eventStore;
|
2016-05-01 22:53:09 +02:00
|
|
|
|
2016-06-18 21:53:18 +02:00
|
|
|
app.get('/strategies', (req, res) => {
|
|
|
|
strategyDb.getStrategies().then(strategies => {
|
2016-09-05 16:50:52 +02:00
|
|
|
res.json({ version, strategies });
|
2014-11-17 22:39:04 +01:00
|
|
|
});
|
2014-10-30 21:10:48 +01:00
|
|
|
});
|
|
|
|
|
2016-06-18 21:53:18 +02:00
|
|
|
app.get('/strategies/:name', (req, res) => {
|
2014-11-25 15:28:08 +01:00
|
|
|
strategyDb.getStrategy(req.params.name)
|
2016-06-18 21:53:18 +02:00
|
|
|
.then(strategy => {
|
2016-04-24 22:41:37 +02:00
|
|
|
res.json(strategy);
|
|
|
|
})
|
2016-06-18 21:53:18 +02:00
|
|
|
.catch(() => {
|
2016-04-24 22:41:37 +02:00
|
|
|
res.status(404).json({ error: 'Could not find strategy' });
|
|
|
|
});
|
2014-10-30 21:10:48 +01:00
|
|
|
});
|
|
|
|
|
2016-06-18 21:53:18 +02:00
|
|
|
app.delete('/strategies/:name', (req, res) => {
|
|
|
|
const strategyName = req.params.name;
|
2015-02-12 11:41:30 +01:00
|
|
|
|
|
|
|
strategyDb.getStrategy(strategyName)
|
2016-06-18 21:53:18 +02:00
|
|
|
.then(() => eventStore.create({
|
2016-06-18 21:55:46 +02:00
|
|
|
type: eventType.strategyDeleted,
|
|
|
|
createdBy: extractUser(req),
|
|
|
|
data: {
|
|
|
|
name: strategyName,
|
|
|
|
},
|
|
|
|
}))
|
2016-06-18 21:53:18 +02:00
|
|
|
.then(() => {
|
2015-02-12 11:41:30 +01:00
|
|
|
res.status(200).end();
|
|
|
|
})
|
2016-06-18 21:53:18 +02:00
|
|
|
.catch(NotFoundError, () => {
|
2015-02-12 11:41:30 +01:00
|
|
|
res.status(404).end();
|
|
|
|
})
|
2016-06-18 21:53:18 +02:00
|
|
|
.catch(err => {
|
|
|
|
logger.error(`Could not delete strategy=${strategyName}`, err);
|
2015-02-12 11:41:30 +01:00
|
|
|
res.status(500).end();
|
|
|
|
});
|
2014-12-08 20:56:22 +01:00
|
|
|
});
|
|
|
|
|
2016-06-18 21:53:18 +02:00
|
|
|
app.post('/strategies', (req, res) => {
|
2014-11-25 15:28:08 +01:00
|
|
|
req.checkBody('name', 'Name is required').notEmpty();
|
2016-08-22 15:16:50 +02:00
|
|
|
req.checkBody('name', 'Name must match format ^[0-9a-zA-Z\\.\\-]+$').matches(/^[0-9a-zA-Z\\.\\-]+$/i);
|
2014-11-25 15:28:08 +01:00
|
|
|
|
2016-06-18 21:53:18 +02:00
|
|
|
const newStrategy = req.body;
|
2014-11-25 15:28:08 +01:00
|
|
|
|
2014-12-10 19:11:52 +01:00
|
|
|
validateRequest(req)
|
|
|
|
.then(validateStrategyName)
|
2016-06-18 21:53:18 +02:00
|
|
|
.then(() => eventStore.create({
|
2016-06-18 21:55:46 +02:00
|
|
|
type: eventType.strategyCreated,
|
|
|
|
createdBy: extractUser(req),
|
|
|
|
data: newStrategy,
|
|
|
|
}))
|
2016-07-02 11:54:50 +02:00
|
|
|
.then(() => res.status(201).end())
|
2016-06-18 21:53:18 +02:00
|
|
|
.catch(NameExistsError, () => {
|
2016-07-02 11:54:50 +02:00
|
|
|
res.status(403)
|
|
|
|
.json([{ msg: `A strategy named '${req.body.name}' already exists.` }])
|
|
|
|
.end();
|
2014-12-10 19:11:52 +01:00
|
|
|
})
|
2016-07-02 11:54:50 +02:00
|
|
|
.catch(ValidationError, () => res.status(400).json(req.validationErrors()))
|
2016-06-18 21:53:18 +02:00
|
|
|
.catch(err => {
|
2016-06-18 21:55:46 +02:00
|
|
|
logger.error('Could not create strategy', err);
|
2014-12-10 19:11:52 +01:00
|
|
|
res.status(500).end();
|
|
|
|
});
|
2014-10-30 21:10:48 +01:00
|
|
|
});
|
|
|
|
|
2016-07-02 11:54:50 +02:00
|
|
|
function validateStrategyName (req) {
|
2016-08-22 15:12:05 +02:00
|
|
|
return new BPromise((resolve, reject) => {
|
2014-12-10 19:11:52 +01:00
|
|
|
strategyDb.getStrategy(req.body.name)
|
2016-06-18 21:53:18 +02:00
|
|
|
.then(() => {
|
2016-06-18 21:55:46 +02:00
|
|
|
reject(new NameExistsError('Feature name already exist'));
|
2016-06-18 21:53:18 +02:00
|
|
|
}, () => {
|
2014-12-10 19:11:52 +01:00
|
|
|
resolve(req);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2014-10-30 21:10:48 +01:00
|
|
|
};
|