2016-06-18 21:53:18 +02:00
|
|
|
'use strict';
|
2016-10-26 10:43:11 +02:00
|
|
|
|
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 ValidationError = require('../error/validation-error.js');
|
|
|
|
const NotFoundError = require('../error/notfound-error');
|
|
|
|
const validateRequest = require('../error/validate-request');
|
|
|
|
const extractUser = require('../extract-user');
|
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) => {
|
|
|
|
switch (error.constructor) {
|
|
|
|
case NotFoundError:
|
|
|
|
return res
|
|
|
|
.status(404)
|
|
|
|
.end();
|
|
|
|
case NameExistsError:
|
|
|
|
return res
|
|
|
|
.status(403)
|
|
|
|
.json([{ msg: `A strategy named '${req.body.name}' already exists.` }])
|
|
|
|
.end();
|
|
|
|
case ValidationError:
|
|
|
|
return res
|
|
|
|
.status(400)
|
|
|
|
.json(req.validationErrors())
|
|
|
|
.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-06-18 21:55:46 +02:00
|
|
|
type: eventType.strategyDeleted,
|
|
|
|
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) => {
|
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-11-05 10:16:48 +01:00
|
|
|
.then(() => eventStore.store({
|
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-11-05 15:08:00 +01:00
|
|
|
.catch(error => handleError(req, res, error));
|
2014-10-30 21:10:48 +01:00
|
|
|
});
|
|
|
|
|
2016-07-02 11:54:50 +02:00
|
|
|
function validateStrategyName (req) {
|
2016-11-05 15:08:00 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
2016-11-05 10:16:48 +01:00
|
|
|
strategyStore.getStrategy(req.body.name)
|
2016-11-05 15:08:00 +01:00
|
|
|
.then(() => reject(new NameExistsError('Feature name already exist')))
|
|
|
|
.catch(() => resolve(req));
|
2014-12-10 19:11:52 +01:00
|
|
|
});
|
|
|
|
}
|
2014-10-30 21:10:48 +01:00
|
|
|
};
|