From a91876790e5e0f113aafb0ba1ab5448fd7c38fcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nuno=20G=C3=B3is?= Date: Tue, 25 Feb 2025 13:41:41 +0000 Subject: [PATCH] chore: deprecate and undeprecate protected environments (#9360) https://linear.app/unleash/issue/2-3320/allow-users-to-deprecate-and-undeprecate-protected-environments Allows users to deprecate and undeprecate protected environments. ![image](https://github.com/user-attachments/assets/621684d3-21e3-4f58-b6b5-2d2731c9fd9e) --- .../EnvironmentActionCellPopover.tsx | 2 +- .../features/project-environments/environment-service.ts | 2 +- .../project-environments/environment-store-type.ts | 1 + .../features/project-environments/environment-store.ts | 8 ++++++++ .../project-environments/fake-environment-store.ts | 8 ++++++++ src/test/e2e/api/admin/environment.test.ts | 9 +++++++++ 6 files changed, 28 insertions(+), 2 deletions(-) diff --git a/frontend/src/component/environments/EnvironmentTable/EnvironmentActionCell/EnvironmentActionCellPopover/EnvironmentActionCellPopover.tsx b/frontend/src/component/environments/EnvironmentTable/EnvironmentActionCell/EnvironmentActionCellPopover/EnvironmentActionCellPopover.tsx index 77b645b1e2..1f2a56b132 100644 --- a/frontend/src/component/environments/EnvironmentTable/EnvironmentActionCell/EnvironmentActionCellPopover/EnvironmentActionCellPopover.tsx +++ b/frontend/src/component/environments/EnvironmentTable/EnvironmentActionCell/EnvironmentActionCellPopover/EnvironmentActionCellPopover.tsx @@ -140,7 +140,7 @@ export const EnvironmentActionCellPopover = ({ onDeprecateToggle(); handleClose(); }} - disabled={!hasAccess || environment.protected} + disabled={!hasAccess} > { const exists = await this.environmentStore.exists(name); if (exists) { - return this.environmentStore.updateProperty(name, 'enabled', value); + return this.environmentStore.toggle(name, value); } throw new NotFoundError(`Could not find environment ${name}`); } diff --git a/src/lib/features/project-environments/environment-store-type.ts b/src/lib/features/project-environments/environment-store-type.ts index 75d2dc97e2..16aa710934 100644 --- a/src/lib/features/project-environments/environment-store-type.ts +++ b/src/lib/features/project-environments/environment-store-type.ts @@ -17,6 +17,7 @@ export interface IEnvironmentStore extends Store { field: string, value: string | number | boolean, ): Promise; + toggle(name: string, enabled: boolean): Promise; updateSortOrder(id: string, value: number): Promise; importEnvironments(environments: IEnvironment[]): Promise; delete(name: string): Promise; diff --git a/src/lib/features/project-environments/environment-store.ts b/src/lib/features/project-environments/environment-store.ts index 3da9522b00..196f046a2f 100644 --- a/src/lib/features/project-environments/environment-store.ts +++ b/src/lib/features/project-environments/environment-store.ts @@ -309,6 +309,14 @@ export default class EnvironmentStore implements IEnvironmentStore { .where({ name: id }); } + async toggle(name: string, enabled: boolean): Promise { + await this.db(TABLE) + .update({ + enabled, + }) + .where({ name }); + } + async update( env: Pick, name: string, diff --git a/src/lib/features/project-environments/fake-environment-store.ts b/src/lib/features/project-environments/fake-environment-store.ts index b8e3deb55c..2c0aaab7d0 100644 --- a/src/lib/features/project-environments/fake-environment-store.ts +++ b/src/lib/features/project-environments/fake-environment-store.ts @@ -92,6 +92,14 @@ export default class FakeEnvironmentStore implements IEnvironmentStore { return Promise.resolve(); } + async toggle(name: string, enabled: boolean): Promise { + const environment = this.environments.find( + (env: IEnvironment) => env.name === name, + ); + if (environment) environment.enabled = enabled; + return Promise.resolve(); + } + async connectProject( // eslint-disable-next-line @typescript-eslint/no-unused-vars environment: string, diff --git a/src/test/e2e/api/admin/environment.test.ts b/src/test/e2e/api/admin/environment.test.ts index e7db829b73..3d1230dad2 100644 --- a/src/test/e2e/api/admin/environment.test.ts +++ b/src/test/e2e/api/admin/environment.test.ts @@ -159,3 +159,12 @@ test('Getting a non existing environment yields 404', async () => { .get('/api/admin/environments/this-does-not-exist') .expect(404); }); + +test('Can deprecate and undeprecate protected environments', async () => { + await app.request + .post(`/api/admin/environments/${DEFAULT_ENV}/off`) + .expect(204); + await app.request + .post(`/api/admin/environments/${DEFAULT_ENV}/on`) + .expect(204); +});