2016-06-18 21:53:18 +02:00
|
|
|
'use strict';
|
2016-10-26 10:43:11 +02:00
|
|
|
|
2016-06-20 10:27:19 +02:00
|
|
|
const logger = require('../logger');
|
2016-10-27 20:51:21 +02:00
|
|
|
const eventType = require('../event-type');
|
|
|
|
const ValidationError = require('../error/validation-error');
|
|
|
|
const validateRequest = require('../error/validate-request');
|
2014-12-17 21:56:27 +01:00
|
|
|
|
2016-11-05 15:08:00 +01:00
|
|
|
const handleErrors = (req, res, error) => {
|
|
|
|
switch (error.constructor) {
|
|
|
|
case ValidationError:
|
|
|
|
return res
|
|
|
|
.status(400)
|
|
|
|
.json(req.validationErrors())
|
|
|
|
.end();
|
|
|
|
default:
|
|
|
|
logger.error('Server failed executing request', 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 { featureToggleStore, eventStore } = config.stores;
|
2016-05-01 22:53:09 +02:00
|
|
|
|
2016-06-18 21:53:18 +02:00
|
|
|
app.get('/archive/features', (req, res) => {
|
2016-11-05 10:16:48 +01:00
|
|
|
featureToggleStore.getArchivedFeatures().then(archivedFeatures => {
|
2016-06-18 21:53:18 +02:00
|
|
|
res.json({ features: archivedFeatures });
|
2014-12-17 21:56:27 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-06-18 21:53:18 +02:00
|
|
|
app.post('/archive/revive', (req, res) => {
|
2014-12-17 21:56:27 +01:00
|
|
|
req.checkBody('name', 'Name is required').notEmpty();
|
|
|
|
|
|
|
|
validateRequest(req)
|
2016-11-05 10:16:48 +01:00
|
|
|
.then(() => eventStore.store({
|
2016-06-18 21:55:46 +02:00
|
|
|
type: eventType.featureRevived,
|
|
|
|
createdBy: req.connection.remoteAddress,
|
|
|
|
data: req.body,
|
2016-07-02 11:54:50 +02:00
|
|
|
}))
|
|
|
|
.then(() => res.status(200).end())
|
2016-11-05 15:08:00 +01:00
|
|
|
.catch(error => handleErrors(req, res, error));
|
2014-12-17 21:56:27 +01:00
|
|
|
});
|
|
|
|
};
|