1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-28 00:06:53 +01:00
unleash.unleash/lib/routes/strategy.js

114 lines
3.6 KiB
JavaScript
Raw Normal View History

2016-06-18 21:53:18 +02:00
'use strict';
2016-10-26 10:43:11 +02:00
2016-12-12 21:44:21 +01:00
const joi = require('joi');
2016-10-27 20:51:21 +02:00
const eventType = require('../event-type');
2016-06-20 10:27:19 +02:00
const logger = require('../logger');
2016-10-27 20:51:21 +02:00
const NameExistsError = require('../error/name-exists-error');
const extractUser = require('../extract-user');
2016-12-12 21:44:21 +01:00
const strategySchema = require('./strategy-schema');
const version = 1;
2014-11-17 22:39:04 +01:00
const handleError = (req, res, error) => {
2016-12-12 21:44:21 +01:00
switch (error.name) {
case 'NotFoundError':
return res
.status(404)
.end();
2016-12-12 21:44:21 +01:00
case 'NameExistsError':
return res
.status(403)
.json([{ msg: `A strategy named '${req.body.name}' already exists.` }])
.end();
2016-12-12 21:44:21 +01:00
case 'ValidationError':
return res
.status(400)
2016-12-12 21:44:21 +01:00
.json(error)
.end();
default:
logger.error('Could perfom operation', error);
return res
.status(500)
.end();
}
};
module.exports = function (app, config) {
const { strategyStore, eventStore } = config.stores;
2016-06-18 21:53:18 +02:00
app.get('/strategies', (req, res) => {
2016-11-05 10:16:48 +01:00
strategyStore.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) => {
2016-11-05 10:16:48 +01:00
strategyStore.getStrategy(req.params.name)
.then(strategy => res.json(strategy).end())
.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;
2016-11-05 10:16:48 +01:00
strategyStore.getStrategy(strategyName)
.then(() => eventStore.store({
2016-12-09 14:50:30 +01:00
type: eventType.STRATEGY_DELETED,
2016-06-18 21:55:46 +02:00
createdBy: extractUser(req),
data: {
name: strategyName,
},
}))
.then(() => res.status(200).end())
.catch(error => handleError(req, res, error));
});
2016-06-18 21:53:18 +02:00
app.post('/strategies', (req, res) => {
2016-12-12 21:44:21 +01:00
const data = req.body;
validateInput(data)
.then(validateStrategyName)
2016-12-12 21:44:21 +01:00
.then((newStrategy) => eventStore.store({
2016-12-09 14:50:30 +01:00
type: eventType.STRATEGY_CREATED,
2016-06-18 21:55:46 +02:00
createdBy: extractUser(req),
data: newStrategy,
}))
2016-07-02 11:54:50 +02:00
.then(() => res.status(201).end())
.catch(error => handleError(req, res, error));
});
app.put('/strategies/:strategyName', (req, res) => {
const strategyName = req.params.strategyName;
const updatedStrategy = req.body;
updatedStrategy.name = strategyName;
strategyStore.getStrategy(strategyName)
.then(() => validateInput(updatedStrategy))
.then(() => eventStore.store({
type: eventType.STRATEGY_UPDATED,
createdBy: extractUser(req),
data: updatedStrategy,
}))
.then(() => res.status(200).end())
.catch(error => handleError(req, res, error));
});
2016-12-12 21:44:21 +01:00
function validateStrategyName (data) {
return new Promise((resolve, reject) => {
2016-12-12 21:44:21 +01:00
strategyStore.getStrategy(data.name)
.then(() => reject(new NameExistsError('Feature name already exist')))
2016-12-12 21:44:21 +01:00
.catch(() => resolve(data));
});
}
function validateInput (data) {
return new Promise((resolve, reject) => {
joi.validate(data, strategySchema, (err, cleaned) => {
if (err) {
return reject(err);
}
return resolve(cleaned);
});
});
}
};