2016-06-18 21:53:18 +02:00
|
|
|
'use strict';
|
|
|
|
const Promise = require("bluebird");
|
|
|
|
const logger = require('../logger');
|
|
|
|
const eventType = require('../eventType');
|
|
|
|
const NameExistsError = require('../error/NameExistsError');
|
|
|
|
const NotFoundError = require('../error/NotFoundError');
|
|
|
|
const ValidationError = require('../error/ValidationError');
|
|
|
|
const validateRequest = require('../error/validateRequest');
|
|
|
|
const extractUser = require('../extractUser');
|
2014-10-20 15:12:30 +02:00
|
|
|
|
2016-05-01 22:53:09 +02:00
|
|
|
module.exports = function (app, config) {
|
2016-06-18 21:53:18 +02:00
|
|
|
const featureDb = config.featureDb;
|
|
|
|
const eventStore = config.eventStore;
|
2016-05-01 22:53:09 +02:00
|
|
|
|
2016-06-18 21:53:18 +02:00
|
|
|
app.get('/features', (req, res) => {
|
|
|
|
featureDb.getFeatures().then(features => {
|
|
|
|
res.json({ features });
|
2014-10-21 13:10:15 +02:00
|
|
|
});
|
2014-10-20 15:12:30 +02:00
|
|
|
});
|
|
|
|
|
2016-06-18 21:53:18 +02:00
|
|
|
app.get('/features/:featureName', (req, res) => {
|
2014-11-14 16:58:05 +01:00
|
|
|
featureDb.getFeature(req.params.featureName)
|
2016-06-18 21:53:18 +02:00
|
|
|
.then(feature => {
|
2016-04-24 22:41:37 +02:00
|
|
|
res.json(feature);
|
|
|
|
})
|
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 feature' });
|
2014-11-14 16:58:05 +01:00
|
|
|
});
|
2014-10-20 15:12:30 +02:00
|
|
|
});
|
|
|
|
|
2016-06-18 21:53:18 +02:00
|
|
|
app.post('/features', (req, res) => {
|
2014-11-01 11:47:21 +01:00
|
|
|
req.checkBody('name', 'Name is required').notEmpty();
|
|
|
|
req.checkBody('name', 'Name must match format ^[a-zA-Z\\.\\-]+$').matches(/^[a-zA-Z\\.\\-]+$/i);
|
|
|
|
|
2014-12-10 19:11:52 +01:00
|
|
|
validateRequest(req)
|
2014-12-10 18:49:08 +01:00
|
|
|
.then(validateUniqueName)
|
2016-06-18 21:53:18 +02:00
|
|
|
.then(() => eventStore.create({
|
|
|
|
type: eventType.featureCreated,
|
|
|
|
createdBy: extractUser(req),
|
|
|
|
data: req.body
|
|
|
|
}))
|
|
|
|
.then(() => {
|
2014-12-10 18:45:02 +01:00
|
|
|
res.status(201).end();
|
|
|
|
})
|
2016-06-18 21:53:18 +02:00
|
|
|
.catch(NameExistsError, () => {
|
2014-12-17 21:56:27 +01:00
|
|
|
res.status(403).json([{
|
2016-06-18 21:53:18 +02:00
|
|
|
msg: `A feature named '${req.body.name}' already exists. It could be archived.`
|
2014-12-17 21:56:27 +01:00
|
|
|
}]).end();
|
2014-12-10 18:45:02 +01:00
|
|
|
})
|
2016-06-18 21:53:18 +02:00
|
|
|
.catch(ValidationError, () => {
|
2014-12-10 18:45:02 +01:00
|
|
|
res.status(400).json(req.validationErrors());
|
|
|
|
})
|
2016-06-18 21:53:18 +02:00
|
|
|
.catch(err => {
|
2014-12-10 18:45:02 +01:00
|
|
|
logger.error("Could not create feature toggle", err);
|
|
|
|
res.status(500).end();
|
|
|
|
});
|
2016-04-24 22:41:37 +02:00
|
|
|
});
|
2014-10-20 15:12:30 +02:00
|
|
|
|
2016-06-18 21:53:18 +02:00
|
|
|
app.put('/features/:featureName', (req, res) => {
|
|
|
|
const featureName = req.params.featureName;
|
|
|
|
const userName = extractUser(req);
|
|
|
|
const updatedFeature = req.body;
|
2014-11-14 12:56:23 +01:00
|
|
|
|
|
|
|
updatedFeature.name = featureName;
|
|
|
|
|
|
|
|
featureDb.getFeature(featureName)
|
2016-06-18 21:53:18 +02:00
|
|
|
.then(() => eventStore.create({
|
|
|
|
type: eventType.featureUpdated,
|
|
|
|
createdBy: userName,
|
|
|
|
data: updatedFeature
|
|
|
|
}))
|
|
|
|
.then(() => {
|
2014-12-10 18:45:02 +01:00
|
|
|
res.status(200).end();
|
|
|
|
})
|
2016-06-18 21:53:18 +02:00
|
|
|
.catch(NotFoundError, () => {
|
2014-10-22 15:52:43 +02:00
|
|
|
res.status(404).end();
|
2014-12-10 18:45:02 +01:00
|
|
|
})
|
2016-06-18 21:53:18 +02:00
|
|
|
.catch(err => {
|
|
|
|
logger.error(`Could not update feature toggle=${featureName}`, err);
|
2014-12-10 18:45:02 +01:00
|
|
|
res.status(500).end();
|
2014-11-14 12:56:23 +01:00
|
|
|
});
|
2014-10-21 15:28:10 +02:00
|
|
|
});
|
2014-12-10 18:49:08 +01:00
|
|
|
|
2016-06-18 21:53:18 +02:00
|
|
|
app.delete('/features/:featureName', (req, res) => {
|
|
|
|
const featureName = req.params.featureName;
|
|
|
|
const userName = extractUser(req);
|
2014-12-15 22:40:07 +01:00
|
|
|
|
|
|
|
featureDb.getFeature(featureName)
|
2016-06-18 21:53:18 +02:00
|
|
|
.then(() => eventStore.create({
|
|
|
|
type: eventType.featureArchived,
|
|
|
|
createdBy: userName,
|
|
|
|
data: {
|
|
|
|
name: featureName
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
.then(() => {
|
2014-12-15 22:40:07 +01:00
|
|
|
res.status(200).end();
|
|
|
|
})
|
2016-06-18 21:53:18 +02:00
|
|
|
.catch(NotFoundError, () => {
|
2014-12-15 22:40:07 +01:00
|
|
|
res.status(404).end();
|
|
|
|
})
|
2016-06-18 21:53:18 +02:00
|
|
|
.catch(err => {
|
|
|
|
logger.error(`Could not archive feature=${featureName}`, err);
|
2014-12-15 22:40:07 +01:00
|
|
|
res.status(500).end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-12-10 18:49:08 +01:00
|
|
|
function validateUniqueName(req) {
|
2016-06-18 21:53:18 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
2014-12-10 18:49:08 +01:00
|
|
|
featureDb.getFeature(req.body.name)
|
2016-06-18 21:53:18 +02:00
|
|
|
.then(() => {
|
2014-12-10 18:49:08 +01:00
|
|
|
reject(new NameExistsError("Feature name already exist"));
|
2016-06-18 21:53:18 +02:00
|
|
|
}, () => {
|
2014-12-10 18:49:08 +01:00
|
|
|
resolve(req);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2014-10-20 15:12:30 +02:00
|
|
|
};
|