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 logger = require('../../logger')('/admin-api/archive.js');
|
||||||
const { FEATURE_REVIVED } = require('../../event-type');
|
const { FEATURE_REVIVED } = require('../../event-type');
|
||||||
const ValidationError = require('../../error/validation-error');
|
|
||||||
const validateRequest = require('../../error/validate-request');
|
|
||||||
const extractUser = require('../../extract-user');
|
const extractUser = require('../../extract-user');
|
||||||
|
|
||||||
const handleErrors = (req, res, error) => {
|
class ArchiveController {
|
||||||
switch (error.constructor) {
|
constructor({ featureToggleStore, eventStore }) {
|
||||||
case ValidationError:
|
const router = Router();
|
||||||
return res
|
this._router = router;
|
||||||
.status(400)
|
this.featureToggleStore = featureToggleStore;
|
||||||
.json(req.validationErrors())
|
this.eventStore = eventStore;
|
||||||
.end();
|
|
||||||
default:
|
router.get('/features', this.getArchivedFeatures.bind(this));
|
||||||
logger.error('Server failed executing request', error);
|
router.post('/revive/:name', this.reviveFeatureToggle.bind(this));
|
||||||
return res.status(500).end();
|
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
module.exports.router = function(config) {
|
async getArchivedFeatures(req, res) {
|
||||||
const { featureToggleStore, eventStore } = config.stores;
|
const features = await this.featureToggleStore.getArchivedFeatures();
|
||||||
const router = Router();
|
res.json({ features });
|
||||||
|
}
|
||||||
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 reviveFeatureToggle(req, res) {
|
||||||
const userName = extractUser(req);
|
const userName = extractUser(req);
|
||||||
|
|
||||||
validateRequest(req)
|
try {
|
||||||
.then(() =>
|
await this.eventStore.store({
|
||||||
eventStore.store({
|
type: FEATURE_REVIVED,
|
||||||
type: FEATURE_REVIVED,
|
createdBy: userName,
|
||||||
createdBy: userName,
|
data: { name: req.params.name },
|
||||||
data: { name: req.params.name },
|
});
|
||||||
})
|
res.status(200).end();
|
||||||
)
|
} catch (error) {
|
||||||
.then(() => res.status(200).end())
|
logger.error('Server failed executing request', error);
|
||||||
.catch(error => handleErrors(req, res, 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].data.name, name);
|
||||||
t.is(events[0].createdBy, 'unknown');
|
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 { Router } = require('express');
|
||||||
|
|
||||||
const features = require('./feature.js');
|
const features = require('./feature.js');
|
||||||
const featureArchive = require('./archive.js');
|
const ArchiveController = require('./archive.js');
|
||||||
const events = require('./event.js');
|
const events = require('./event.js');
|
||||||
const strategies = require('./strategy');
|
const strategies = require('./strategy');
|
||||||
const metrics = require('./metrics');
|
const metrics = require('./metrics');
|
||||||
@ -17,7 +17,7 @@ class AdminApi {
|
|||||||
|
|
||||||
router.get('/', (req, res) => res.json(apiDef));
|
router.get('/', (req, res) => res.json(apiDef));
|
||||||
router.use('/features', features.router(config));
|
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('/strategies', strategies.router(config));
|
||||||
router.use('/events', events.router(config));
|
router.use('/events', events.router(config));
|
||||||
router.use('/metrics', metrics.router(config));
|
router.use('/metrics', metrics.router(config));
|
||||||
|
Loading…
Reference in New Issue
Block a user