mirror of
https://github.com/Unleash/unleash.git
synced 2025-01-25 00:07:47 +01:00
chore(modernize): Admin ArchiveController
This commit is contained in:
parent
a7382419d5
commit
be5cf15cf8
@ -4,49 +4,43 @@ const { Router } = require('express');
|
||||
|
||||
const logger = require('../../logger')('/admin-api/archive.js');
|
||||
const { FEATURE_REVIVED } = require('../../event-type');
|
||||
const ValidationError = require('../../error/validation-error');
|
||||
const validateRequest = require('../../error/validate-request');
|
||||
const extractUser = require('../../extract-user');
|
||||
|
||||
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();
|
||||
class ArchiveController {
|
||||
constructor({ featureToggleStore, eventStore }) {
|
||||
const router = Router();
|
||||
this._router = router;
|
||||
this.featureToggleStore = featureToggleStore;
|
||||
this.eventStore = eventStore;
|
||||
|
||||
router.get('/features', this.getArchivedFeatures.bind(this));
|
||||
router.post('/revive/:name', this.reviveFeatureToggle.bind(this));
|
||||
}
|
||||
};
|
||||
|
||||
module.exports.router = function(config) {
|
||||
const { featureToggleStore, eventStore } = config.stores;
|
||||
const router = Router();
|
||||
|
||||
router.get('/features', (req, res) => {
|
||||
featureToggleStore.getArchivedFeatures().then(archivedFeatures => {
|
||||
res.json({ features: archivedFeatures });
|
||||
});
|
||||
});
|
||||
|
||||
router.post('/revive/:name', (req, res) => {
|
||||
req.checkParams('name', 'Name is required').notEmpty();
|
||||
async getArchivedFeatures(req, res) {
|
||||
const features = await this.featureToggleStore.getArchivedFeatures();
|
||||
res.json({ features });
|
||||
}
|
||||
|
||||
async reviveFeatureToggle(req, res) {
|
||||
const userName = extractUser(req);
|
||||
|
||||
validateRequest(req)
|
||||
.then(() =>
|
||||
eventStore.store({
|
||||
type: FEATURE_REVIVED,
|
||||
createdBy: userName,
|
||||
data: { name: req.params.name },
|
||||
})
|
||||
)
|
||||
.then(() => res.status(200).end())
|
||||
.catch(error => handleErrors(req, res, error));
|
||||
});
|
||||
try {
|
||||
await this.eventStore.store({
|
||||
type: FEATURE_REVIVED,
|
||||
createdBy: userName,
|
||||
data: { name: req.params.name },
|
||||
});
|
||||
res.status(200).end();
|
||||
} catch (error) {
|
||||
logger.error('Server failed executing request', error);
|
||||
return res.status(500).end();
|
||||
}
|
||||
}
|
||||
|
||||
return router;
|
||||
};
|
||||
router() {
|
||||
return this._router;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ArchiveController;
|
||||
|
@ -86,3 +86,9 @@ test('should create event when reviving toggle', async t => {
|
||||
t.is(events[0].data.name, name);
|
||||
t.is(events[0].createdBy, 'unknown');
|
||||
});
|
||||
|
||||
test('should require toggle name when reviving', t => {
|
||||
t.plan(0);
|
||||
const { request, base } = getSetup();
|
||||
return request.post(`${base}/api/admin/archive/revive/`).expect(404);
|
||||
});
|
||||
|
@ -3,7 +3,7 @@
|
||||
const { Router } = require('express');
|
||||
|
||||
const features = require('./feature.js');
|
||||
const featureArchive = require('./archive.js');
|
||||
const ArchiveController = require('./archive.js');
|
||||
const events = require('./event.js');
|
||||
const strategies = require('./strategy');
|
||||
const metrics = require('./metrics');
|
||||
@ -17,7 +17,7 @@ class AdminApi {
|
||||
|
||||
router.get('/', (req, res) => res.json(apiDef));
|
||||
router.use('/features', features.router(config));
|
||||
router.use('/archive', featureArchive.router(config));
|
||||
router.use('/archive', new ArchiveController(config.stores).router());
|
||||
router.use('/strategies', strategies.router(config));
|
||||
router.use('/events', events.router(config));
|
||||
router.use('/metrics', metrics.router(config));
|
||||
|
Loading…
Reference in New Issue
Block a user