diff --git a/lib/routes/admin-api/archive.js b/lib/routes/admin-api/archive.js index ef2675374b..91cfbd5aaf 100644 --- a/lib/routes/admin-api/archive.js +++ b/lib/routes/admin-api/archive.js @@ -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; diff --git a/lib/routes/admin-api/archive.test.js b/lib/routes/admin-api/archive.test.js index 366371e5e7..15eac5ab3f 100644 --- a/lib/routes/admin-api/archive.test.js +++ b/lib/routes/admin-api/archive.test.js @@ -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); +}); diff --git a/lib/routes/admin-api/index.js b/lib/routes/admin-api/index.js index fb95844be8..5920c51790 100644 --- a/lib/routes/admin-api/index.js +++ b/lib/routes/admin-api/index.js @@ -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));