2017-06-28 10:20:22 +02:00
|
|
|
'use strict';
|
|
|
|
|
2018-11-30 11:11:12 +01:00
|
|
|
const Controller = require('../controller');
|
2017-06-28 10:20:22 +02:00
|
|
|
|
|
|
|
const {
|
|
|
|
FEATURE_CREATED,
|
|
|
|
FEATURE_UPDATED,
|
|
|
|
FEATURE_ARCHIVED,
|
|
|
|
} = require('../../event-type');
|
|
|
|
const NameExistsError = require('../../error/name-exists-error');
|
2018-12-12 10:09:38 +01:00
|
|
|
const { handleErrors } = require('./util');
|
2017-06-28 10:20:22 +02:00
|
|
|
const extractUser = require('../../extract-user');
|
2018-12-19 10:36:56 +01:00
|
|
|
const {
|
|
|
|
UPDATE_FEATURE,
|
|
|
|
DELETE_FEATURE,
|
|
|
|
CREATE_FEATURE,
|
|
|
|
} = require('../../permissions');
|
2018-12-12 10:09:38 +01:00
|
|
|
const { featureShema, nameSchema } = require('./feature-schema');
|
2017-06-28 10:20:22 +02:00
|
|
|
const version = 1;
|
|
|
|
|
2018-11-30 11:11:12 +01:00
|
|
|
class FeatureController extends Controller {
|
2018-12-19 14:50:01 +01:00
|
|
|
constructor(config) {
|
|
|
|
super(config);
|
|
|
|
this.featureToggleStore = config.stores.featureToggleStore;
|
|
|
|
this.eventStore = config.stores.eventStore;
|
2019-04-30 21:14:23 +02:00
|
|
|
this.logger = config.getLogger('/admin-api/feature.js');
|
2018-11-30 11:11:12 +01:00
|
|
|
|
|
|
|
this.get('/', this.getAllToggles);
|
2018-12-19 10:36:56 +01:00
|
|
|
this.post('/', this.createToggle, CREATE_FEATURE);
|
2018-11-30 11:11:12 +01:00
|
|
|
this.get('/:featureName', this.getToggle);
|
2018-12-19 10:36:56 +01:00
|
|
|
this.put('/:featureName', this.updateToggle, UPDATE_FEATURE);
|
|
|
|
this.delete('/:featureName', this.deleteToggle, DELETE_FEATURE);
|
2018-11-30 11:11:12 +01:00
|
|
|
this.post('/validate', this.validate);
|
2018-12-19 10:36:56 +01:00
|
|
|
this.post('/:featureName/toggle', this.toggle, UPDATE_FEATURE);
|
2019-03-07 22:19:48 +01:00
|
|
|
this.post('/:featureName/toggle/on', this.toggleOn, UPDATE_FEATURE);
|
|
|
|
this.post('/:featureName/toggle/off', this.toggleOff, UPDATE_FEATURE);
|
2018-11-30 11:11:12 +01:00
|
|
|
}
|
2017-06-28 10:20:22 +02:00
|
|
|
|
2018-11-30 11:11:12 +01:00
|
|
|
async getAllToggles(req, res) {
|
|
|
|
const features = await this.featureToggleStore.getFeatures();
|
|
|
|
res.json({ version, features });
|
|
|
|
}
|
2017-06-28 10:20:22 +02:00
|
|
|
|
2018-11-30 11:11:12 +01:00
|
|
|
async getToggle(req, res) {
|
|
|
|
try {
|
2018-12-12 10:09:38 +01:00
|
|
|
const name = req.params.featureName;
|
|
|
|
const feature = await this.featureToggleStore.getFeature(name);
|
2018-11-30 11:11:12 +01:00
|
|
|
res.json(feature).end();
|
|
|
|
} catch (err) {
|
|
|
|
res.status(404).json({ error: 'Could not find feature' });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async validate(req, res) {
|
2018-12-01 12:03:47 +01:00
|
|
|
const name = req.body.name;
|
2017-06-28 10:20:22 +02:00
|
|
|
|
2018-11-30 11:11:12 +01:00
|
|
|
try {
|
2020-01-02 19:23:52 +01:00
|
|
|
await nameSchema.validateAsync({ name });
|
2018-12-01 12:03:47 +01:00
|
|
|
await this.validateUniqueName(name);
|
2018-11-30 11:11:12 +01:00
|
|
|
res.status(201).end();
|
|
|
|
} catch (error) {
|
2019-04-30 21:14:23 +02:00
|
|
|
handleErrors(res, this.logger, error);
|
2018-11-30 11:11:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-01 12:03:47 +01:00
|
|
|
// TODO: cleanup this validation
|
|
|
|
async validateUniqueName(name) {
|
|
|
|
let msg;
|
|
|
|
try {
|
|
|
|
const definition = await this.featureToggleStore.hasFeature(name);
|
|
|
|
msg = definition.archived
|
|
|
|
? 'An archived toggle with that name already exist'
|
|
|
|
: 'A toggle with that name already exist';
|
|
|
|
} catch (error) {
|
|
|
|
// No conflict, everything ok!
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Interntional throw here!
|
|
|
|
throw new NameExistsError(msg);
|
2017-06-28 10:20:22 +02:00
|
|
|
}
|
|
|
|
|
2018-12-01 12:03:47 +01:00
|
|
|
async createToggle(req, res) {
|
|
|
|
const toggleName = req.body.name;
|
2017-06-28 10:20:22 +02:00
|
|
|
const userName = extractUser(req);
|
|
|
|
|
2018-11-30 11:11:12 +01:00
|
|
|
try {
|
2018-12-01 12:03:47 +01:00
|
|
|
await this.validateUniqueName(toggleName);
|
2020-01-02 19:23:52 +01:00
|
|
|
const value = await featureShema.validateAsync(req.body);
|
2018-11-30 11:11:12 +01:00
|
|
|
await this.eventStore.store({
|
|
|
|
type: FEATURE_CREATED,
|
|
|
|
createdBy: userName,
|
2020-01-02 19:23:52 +01:00
|
|
|
data: value,
|
2018-11-30 11:11:12 +01:00
|
|
|
});
|
|
|
|
res.status(201).end();
|
|
|
|
} catch (error) {
|
2019-04-30 21:14:23 +02:00
|
|
|
handleErrors(res, this.logger, error);
|
2018-11-30 11:11:12 +01:00
|
|
|
}
|
|
|
|
}
|
2017-06-28 10:20:22 +02:00
|
|
|
|
2018-11-30 11:11:12 +01:00
|
|
|
async updateToggle(req, res) {
|
2017-06-28 10:20:22 +02:00
|
|
|
const featureName = req.params.featureName;
|
|
|
|
const userName = extractUser(req);
|
|
|
|
const updatedFeature = req.body;
|
|
|
|
|
|
|
|
updatedFeature.name = featureName;
|
|
|
|
|
2018-11-30 11:11:12 +01:00
|
|
|
try {
|
|
|
|
await this.featureToggleStore.getFeature(featureName);
|
2020-01-02 19:23:52 +01:00
|
|
|
await featureShema.validateAsync(updatedFeature);
|
2018-11-30 11:11:12 +01:00
|
|
|
await this.eventStore.store({
|
|
|
|
type: FEATURE_UPDATED,
|
|
|
|
createdBy: userName,
|
|
|
|
data: updatedFeature,
|
|
|
|
});
|
|
|
|
res.status(200).end();
|
|
|
|
} catch (error) {
|
2019-04-30 21:14:23 +02:00
|
|
|
handleErrors(res, this.logger, error);
|
2018-11-30 11:11:12 +01:00
|
|
|
}
|
|
|
|
}
|
2017-06-28 10:20:22 +02:00
|
|
|
|
2019-03-07 22:19:48 +01:00
|
|
|
// Kept to keep backward compatability
|
2018-11-30 11:11:12 +01:00
|
|
|
async toggle(req, res) {
|
2019-03-07 22:19:48 +01:00
|
|
|
try {
|
|
|
|
const name = req.params.featureName;
|
|
|
|
const feature = await this.featureToggleStore.getFeature(name);
|
|
|
|
const enabled = !feature.enabled;
|
|
|
|
this._toggle(enabled, req, res);
|
|
|
|
} catch (error) {
|
2019-04-30 21:14:23 +02:00
|
|
|
handleErrors(res, this.logger, error);
|
2019-03-07 22:19:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async toggleOn(req, res) {
|
|
|
|
this._toggle(true, req, res);
|
|
|
|
}
|
|
|
|
|
|
|
|
async toggleOff(req, res) {
|
|
|
|
this._toggle(false, req, res);
|
|
|
|
}
|
|
|
|
|
|
|
|
async _toggle(enabled, req, res) {
|
2017-06-28 10:20:22 +02:00
|
|
|
const featureName = req.params.featureName;
|
|
|
|
const userName = extractUser(req);
|
|
|
|
|
2018-11-30 11:11:12 +01:00
|
|
|
try {
|
|
|
|
const feature = await this.featureToggleStore.getFeature(
|
|
|
|
featureName
|
|
|
|
);
|
2017-06-28 10:20:22 +02:00
|
|
|
|
2019-03-07 22:19:48 +01:00
|
|
|
feature.enabled = enabled;
|
2018-11-30 11:11:12 +01:00
|
|
|
await this.eventStore.store({
|
|
|
|
type: FEATURE_UPDATED,
|
|
|
|
createdBy: userName,
|
|
|
|
data: feature,
|
|
|
|
});
|
2019-03-07 22:19:48 +01:00
|
|
|
res.json(feature).end();
|
2018-11-30 11:11:12 +01:00
|
|
|
} catch (error) {
|
2019-04-30 21:14:23 +02:00
|
|
|
handleErrors(res, this.logger, error);
|
2018-11-30 11:11:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async deleteToggle(req, res) {
|
2017-06-28 10:20:22 +02:00
|
|
|
const featureName = req.params.featureName;
|
|
|
|
const userName = extractUser(req);
|
|
|
|
|
2018-11-30 11:11:12 +01:00
|
|
|
try {
|
|
|
|
await this.featureToggleStore.getFeature(featureName);
|
|
|
|
await this.eventStore.store({
|
|
|
|
type: FEATURE_ARCHIVED,
|
|
|
|
createdBy: userName,
|
|
|
|
data: {
|
|
|
|
name: featureName,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
res.status(200).end();
|
|
|
|
} catch (error) {
|
2019-04-30 21:14:23 +02:00
|
|
|
handleErrors(res, this.logger, error);
|
2018-11-30 11:11:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-06-28 10:20:22 +02:00
|
|
|
|
2018-11-30 11:11:12 +01:00
|
|
|
module.exports = FeatureController;
|