2021-09-14 19:58:48 +02:00
|
|
|
import { Response } from 'express';
|
2021-04-22 10:07:10 +02:00
|
|
|
import { IUnleashConfig } from '../../types/option';
|
|
|
|
import { IUnleashServices } from '../../types/services';
|
2021-05-06 14:11:56 +02:00
|
|
|
import { Logger } from '../../logger';
|
2021-02-17 15:24:43 +01:00
|
|
|
|
2021-05-06 14:11:56 +02:00
|
|
|
import Controller from '../controller';
|
2017-06-28 10:20:22 +02:00
|
|
|
|
2021-09-14 19:58:48 +02:00
|
|
|
import { extractUsername } from '../../util/extract-user';
|
2021-07-07 10:46:50 +02:00
|
|
|
import { DELETE_FEATURE, UPDATE_FEATURE } from '../../types/permissions';
|
2021-11-12 13:15:51 +01:00
|
|
|
import FeatureToggleService from '../../services/feature-toggle-service';
|
2021-09-14 19:58:48 +02:00
|
|
|
import { IAuthRequest } from '../unleash-types';
|
2017-06-28 10:20:22 +02:00
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
export default class ArchiveController extends Controller {
|
2021-05-06 14:11:56 +02:00
|
|
|
private readonly logger: Logger;
|
|
|
|
|
2021-11-12 13:15:51 +01:00
|
|
|
private featureService: FeatureToggleService;
|
2021-05-06 14:11:56 +02:00
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
constructor(
|
|
|
|
config: IUnleashConfig,
|
|
|
|
{
|
2021-07-07 10:46:50 +02:00
|
|
|
featureToggleServiceV2,
|
|
|
|
}: Pick<IUnleashServices, 'featureToggleServiceV2'>,
|
2021-04-22 10:07:10 +02:00
|
|
|
) {
|
2018-12-19 14:50:01 +01:00
|
|
|
super(config);
|
2019-04-30 21:14:23 +02:00
|
|
|
this.logger = config.getLogger('/admin-api/archive.js');
|
2021-07-07 10:46:50 +02:00
|
|
|
this.featureService = featureToggleServiceV2;
|
2018-11-30 08:24:22 +01:00
|
|
|
|
2018-11-30 10:11:36 +01:00
|
|
|
this.get('/features', this.getArchivedFeatures);
|
2021-05-06 14:11:56 +02:00
|
|
|
this.delete('/:featureName', this.deleteFeature, DELETE_FEATURE);
|
2021-04-23 13:29:24 +02:00
|
|
|
this.post(
|
|
|
|
'/revive/:featureName',
|
|
|
|
this.reviveFeatureToggle,
|
|
|
|
UPDATE_FEATURE,
|
|
|
|
);
|
2017-06-28 10:20:22 +02:00
|
|
|
}
|
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
|
|
async getArchivedFeatures(req, res): Promise<void> {
|
2021-08-13 10:36:19 +02:00
|
|
|
const features = await this.featureService.getMetadataForAllFeatures(
|
|
|
|
true,
|
|
|
|
);
|
|
|
|
res.json({ version: 2, features });
|
2018-11-30 08:24:22 +01:00
|
|
|
}
|
2017-06-28 10:20:22 +02:00
|
|
|
|
2021-05-06 14:11:56 +02:00
|
|
|
async deleteFeature(
|
2021-09-14 19:58:48 +02:00
|
|
|
req: IAuthRequest<any, { featureName: string }, any, any>,
|
2021-05-06 14:11:56 +02:00
|
|
|
res: Response,
|
|
|
|
): Promise<void> {
|
|
|
|
const { featureName } = req.params;
|
2021-09-14 19:58:48 +02:00
|
|
|
const user = extractUsername(req);
|
2021-08-13 10:36:19 +02:00
|
|
|
await this.featureService.deleteFeature(featureName, user);
|
|
|
|
res.status(200).end();
|
2021-05-06 14:11:56 +02:00
|
|
|
}
|
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
2021-09-14 19:58:48 +02:00
|
|
|
async reviveFeatureToggle(req: IAuthRequest, res: Response): Promise<void> {
|
|
|
|
const userName = extractUsername(req);
|
2021-04-23 13:29:24 +02:00
|
|
|
const { featureName } = req.params;
|
2021-08-13 10:36:19 +02:00
|
|
|
await this.featureService.reviveToggle(featureName, userName);
|
|
|
|
res.status(200).end();
|
2018-11-30 08:24:22 +01:00
|
|
|
}
|
|
|
|
}
|
2018-06-25 18:34:23 +02:00
|
|
|
|
2018-11-30 08:24:22 +01:00
|
|
|
module.exports = ArchiveController;
|