From 3ef7fd2420ef7d8e65fb314cd40177a7fe7a054b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivar=20Conradi=20=C3=98sthus?= Date: Thu, 28 Oct 2021 15:48:07 +0200 Subject: [PATCH] fix: legacy feature stale on/off needs to return full toggle --- src/lib/routes/admin-api/feature.ts | 90 ++++++++++++----------------- 1 file changed, 38 insertions(+), 52 deletions(-) diff --git a/src/lib/routes/admin-api/feature.ts b/src/lib/routes/admin-api/feature.ts index 2701fc0544..b0d21c8e35 100644 --- a/src/lib/routes/admin-api/feature.ts +++ b/src/lib/routes/admin-api/feature.ts @@ -22,11 +22,9 @@ import { DEFAULT_ENV } from '../../util/constants'; const version = 1; class FeatureController extends Controller { - private logger: Logger; + private tagService: FeatureTagService; - private featureTagService: FeatureTagService; - - private featureService2: FeatureToggleServiceV2; + private service: FeatureToggleServiceV2; constructor( config: IUnleashConfig, @@ -39,9 +37,8 @@ class FeatureController extends Controller { >, ) { super(config); - this.featureTagService = featureTagService; - this.featureService2 = featureToggleServiceV2; - this.logger = config.getLogger('/admin-api/feature.ts'); + this.tagService = featureTagService; + this.service = featureToggleServiceV2; this.get('/', this.getAllToggles); this.post('/', this.createToggle, CREATE_FEATURE); @@ -94,7 +91,7 @@ class FeatureController extends Controller { async getAllToggles(req: Request, res: Response): Promise { const query = await this.prepQuery(req.query); - const features = await this.featureService2.getFeatureToggles(query); + const features = await this.service.getFeatureToggles(query); res.json({ version, features }); } @@ -104,21 +101,19 @@ class FeatureController extends Controller { res: Response, ): Promise { const name = req.params.featureName; - const feature = await this.featureService2.getFeatureToggleLegacy(name); + const feature = await this.service.getFeatureToggleLegacy(name); res.json(feature).end(); } async listTags(req: Request, res: Response): Promise { - const tags = await this.featureTagService.listTags( - req.params.featureName, - ); + const tags = await this.tagService.listTags(req.params.featureName); res.json({ version, tags }); } async addTag(req: IAuthRequest, res: Response): Promise { const { featureName } = req.params; const userName = extractUsername(req); - const tag = await this.featureTagService.addTag( + const tag = await this.tagService.addTag( featureName, req.body, userName, @@ -130,18 +125,14 @@ class FeatureController extends Controller { async removeTag(req: IAuthRequest, res: Response): Promise { const { featureName, type, value } = req.params; const userName = extractUsername(req); - await this.featureTagService.removeTag( - featureName, - { type, value }, - userName, - ); + await this.tagService.removeTag(featureName, { type, value }, userName); res.status(200).end(); } async validate(req: Request, res: Response): Promise { const { name } = req.body; - await this.featureService2.validateName(name); + await this.service.validateName(name); res.status(200).end(); } @@ -151,14 +142,14 @@ class FeatureController extends Controller { const validatedToggle = await featureSchema.validateAsync(toggle); const { enabled } = validatedToggle; - const createdFeature = await this.featureService2.createFeatureToggle( + const createdFeature = await this.service.createFeatureToggle( validatedToggle.project, validatedToggle, userName, ); const strategies = await Promise.all( toggle.strategies.map(async (s) => - this.featureService2.createStrategy( + this.service.createStrategy( s, createdFeature.project, createdFeature.name, @@ -166,7 +157,7 @@ class FeatureController extends Controller { ), ), ); - await this.featureService2.updateEnabled( + await this.service.updateEnabled( createdFeature.project, createdFeature.name, DEFAULT_ENV, @@ -188,23 +179,17 @@ class FeatureController extends Controller { updatedFeature.name = featureName; - const projectId = await this.featureService2.getProjectId( - updatedFeature.name, - ); + const projectId = await this.service.getProjectId(updatedFeature.name); const value = await featureSchema.validateAsync(updatedFeature); - await this.featureService2.updateFeatureToggle( - projectId, - value, - userName, - ); + await this.service.updateFeatureToggle(projectId, value, userName); - await this.featureService2.removeAllStrategiesForEnv(featureName); + await this.service.removeAllStrategiesForEnv(featureName); if (updatedFeature.strategies) { await Promise.all( updatedFeature.strategies.map(async (s) => - this.featureService2.createStrategy( + this.service.createStrategy( s, projectId, featureName, @@ -213,7 +198,7 @@ class FeatureController extends Controller { ), ); } - await this.featureService2.updateEnabled( + await this.service.updateEnabled( projectId, updatedFeature.name, DEFAULT_ENV, @@ -221,11 +206,10 @@ class FeatureController extends Controller { userName, ); - const feature = - await this.featureService2.storeFeatureUpdatedEventLegacy( - featureName, - userName, - ); + const feature = await this.service.storeFeatureUpdatedEventLegacy( + featureName, + userName, + ); res.status(200).json(feature); } @@ -235,14 +219,14 @@ class FeatureController extends Controller { async toggle(req: IAuthRequest, res: Response): Promise { const userName = extractUsername(req); const { featureName } = req.params; - const projectId = await this.featureService2.getProjectId(featureName); - const feature = await this.featureService2.toggle( + const projectId = await this.service.getProjectId(featureName); + const feature = await this.service.toggle( projectId, featureName, DEFAULT_ENV, userName, ); - await this.featureService2.storeFeatureUpdatedEventLegacy( + await this.service.storeFeatureUpdatedEventLegacy( featureName, userName, ); @@ -252,15 +236,15 @@ class FeatureController extends Controller { async toggleOn(req: IAuthRequest, res: Response): Promise { const { featureName } = req.params; const userName = extractUsername(req); - const projectId = await this.featureService2.getProjectId(featureName); - const feature = await this.featureService2.updateEnabled( + const projectId = await this.service.getProjectId(featureName); + const feature = await this.service.updateEnabled( projectId, featureName, DEFAULT_ENV, true, userName, ); - await this.featureService2.storeFeatureUpdatedEventLegacy( + await this.service.storeFeatureUpdatedEventLegacy( featureName, userName, ); @@ -270,15 +254,15 @@ class FeatureController extends Controller { async toggleOff(req: IAuthRequest, res: Response): Promise { const { featureName } = req.params; const userName = extractUsername(req); - const projectId = await this.featureService2.getProjectId(featureName); - const feature = await this.featureService2.updateEnabled( + const projectId = await this.service.getProjectId(featureName); + const feature = await this.service.updateEnabled( projectId, featureName, DEFAULT_ENV, false, userName, ); - await this.featureService2.storeFeatureUpdatedEventLegacy( + await this.service.storeFeatureUpdatedEventLegacy( featureName, userName, ); @@ -288,22 +272,24 @@ class FeatureController extends Controller { async staleOn(req: IAuthRequest, res: Response): Promise { const { featureName } = req.params; const userName = extractUsername(req); - await this.featureService2.updateStale(featureName, true, userName); - res.status(200).end(); + await this.service.updateStale(featureName, true, userName); + const feature = await this.service.getFeatureToggleLegacy(featureName); + res.json(feature); } async staleOff(req: IAuthRequest, res: Response): Promise { const { featureName } = req.params; const userName = extractUsername(req); - await this.featureService2.updateStale(featureName, false, userName); - res.status(200).end(); + await this.service.updateStale(featureName, false, userName); + const feature = await this.service.getFeatureToggleLegacy(featureName); + res.json(feature); } async archiveToggle(req: IAuthRequest, res: Response): Promise { const { featureName } = req.params; const userName = extractUsername(req); - await this.featureService2.archiveToggle(featureName, userName); + await this.service.archiveToggle(featureName, userName); res.status(200).end(); } }