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');
|
2016-09-05 16:50:52 +02:00
|
|
|
const version = 1;
|
2014-11-17 22:39:04 +01:00
|
|
|
|
2016-11-05 15:08:00 +01:00
|
|
|
const handleError = (req, res, error) => {
|
2016-12-12 21:44:21 +01:00
|
|
|
switch (error.name) {
|
|
|
|
case 'NotFoundError':
|
2016-11-05 15:08:00 +01:00
|
|
|
return res
|
|
|
|
.status(404)
|
|
|
|
.end();
|
2016-12-12 21:44:21 +01:00
|
|
|
case 'NameExistsError':
|
2016-11-05 15:08:00 +01:00
|
|
|
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':
|
2016-11-05 15:08:00 +01:00
|
|
|
return res
|
|
|
|
.status(400)
|
2016-12-12 21:44:21 +01:00
|
|
|
.json(error)
|
2016-11-05 15:08:00 +01:00
|
|
|
.end();
|
|
|
|
default:
|
|
|
|
logger.error('Could perfom operation', error);
|
|
|
|
return res
|
|
|
|
.status(500)
|
|
|
|
.end();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-05-01 22:53:09 +02:00
|
|
|
module.exports = function (app, config) {
|
2016-11-05 14:08:47 +01:00
|
|
|
const { strategyStore, eventStore } = config.stores;
|
2016-05-01 22:53:09 +02:00
|
|
|
|
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 => {
|
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) => {
|
2016-11-05 10:16:48 +01:00
|
|
|
strategyStore.getStrategy(req.params.name)
|
2016-11-05 15:08:00 +01:00
|
|
|
.then(strategy => res.json(strategy).end())
|
|
|
|
.catch(() => 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
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
}))
|
2016-11-05 15:08:00 +01:00
|
|
|
.then(() => res.status(200).end())
|
|
|
|
.catch(error => handleError(req, res, error));
|
2014-12-08 20:56:22 +01:00
|
|
|
});
|
|
|
|
|
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)
|
2014-12-10 19:11:52 +01:00
|
|
|
.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())
|
2016-11-05 15:08:00 +01:00
|
|
|
.catch(error => handleError(req, res, error));
|
2014-10-30 21:10:48 +01:00
|
|
|
});
|
|
|
|
|
2016-12-12 21:44:21 +01:00
|
|
|
function validateStrategyName (data) {
|
2016-11-05 15:08:00 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
2016-12-12 21:44:21 +01:00
|
|
|
strategyStore.getStrategy(data.name)
|
2016-11-05 15:08:00 +01:00
|
|
|
.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);
|
|
|
|
});
|
2014-12-10 19:11:52 +01:00
|
|
|
});
|
|
|
|
}
|
2014-10-30 21:10:48 +01:00
|
|
|
};
|