1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01:00
unleash.unleash/packages/unleash-api/lib/routes/strategy.js

92 lines
3.1 KiB
JavaScript
Raw Normal View History

2016-06-18 21:53:18 +02:00
'use strict';
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');
const version = 1;
2014-11-17 22:39:04 +01:00
module.exports = function (app, config) {
2016-06-18 21:53:18 +02:00
const strategyDb = config.strategyDb;
const eventStore = config.eventStore;
2016-06-18 21:53:18 +02:00
app.get('/strategies', (req, res) => {
strategyDb.getStrategies().then(strategies => {
res.json({ version, strategies });
2014-11-17 22:39:04 +01:00
});
});
2016-06-18 21:53:18 +02:00
app.get('/strategies/:name', (req, res) => {
strategyDb.getStrategy(req.params.name)
2016-06-18 21:53:18 +02:00
.then(strategy => {
res.json(strategy);
})
2016-06-18 21:53:18 +02:00
.catch(() => {
res.status(404).json({ error: 'Could not find strategy' });
});
});
2016-06-18 21:53:18 +02:00
app.delete('/strategies/:name', (req, res) => {
const strategyName = req.params.name;
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(() => {
res.status(200).end();
})
2016-06-18 21:53:18 +02:00
.catch(NotFoundError, () => {
res.status(404).end();
})
2016-06-18 21:53:18 +02:00
.catch(err => {
logger.error(`Could not delete strategy=${strategyName}`, err);
res.status(500).end();
});
});
2016-06-18 21:53:18 +02:00
app.post('/strategies', (req, res) => {
req.checkBody('name', 'Name is required').notEmpty();
req.checkBody('name', 'Name must match format ^[0-9a-zA-Z\\.\\-]+$').matches(/^[0-9a-zA-Z\\.\\-]+$/i);
2016-06-18 21:53:18 +02:00
const newStrategy = req.body;
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();
})
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);
res.status(500).end();
});
});
2016-07-02 11:54:50 +02:00
function validateStrategyName (req) {
return new BPromise((resolve, reject) => {
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
}, () => {
resolve(req);
});
});
}
};