Skip to content

Commit

Permalink
feat: Basic bulk update implementation (#3794)
Browse files Browse the repository at this point in the history
  • Loading branch information
kwasniew committed May 17, 2023
1 parent 6d47316 commit 6b41cf0
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
30 changes: 28 additions & 2 deletions src/lib/routes/admin-api/project/project-features.ts
Expand Up @@ -723,7 +723,20 @@ export default class ProjectFeaturesController extends Controller {
>,
res: Response<void>,
): Promise<void> {
res.status(405).end();
const { environment, projectId } = req.params;
const { shouldActivateDisabledStrategies } = req.query;
const { features } = req.body;

await this.featureService.bulkUpdateEnabled(
projectId,
features,
environment,
true,
extractUsername(req),
req.user,
shouldActivateDisabledStrategies === 'true',
);
res.status(200).end();
}

async bulkToggleFeaturesEnvironmentOff(
Expand All @@ -735,7 +748,20 @@ export default class ProjectFeaturesController extends Controller {
>,
res: Response<void>,
): Promise<void> {
res.status(405).end();
const { environment, projectId } = req.params;
const { shouldActivateDisabledStrategies } = req.query;
const { features } = req.body;

await this.featureService.bulkUpdateEnabled(
projectId,
features,
environment,
false,
extractUsername(req),
req.user,
shouldActivateDisabledStrategies === 'true',
);
res.status(200).end();
}

async toggleFeatureEnvironmentOff(
Expand Down
24 changes: 24 additions & 0 deletions src/lib/services/feature-toggle-service.ts
Expand Up @@ -1257,6 +1257,30 @@ class FeatureToggleService {
);
}

async bulkUpdateEnabled(
project: string,
featureNames: string[],
environment: string,
enabled: boolean,
createdBy: string,
user?: User,
shouldActivateDisabledStrategies = false,
): Promise<void> {
await Promise.all(
featureNames.map((featureName) =>
this.updateEnabled(
project,
featureName,
environment,
enabled,
createdBy,
user,
shouldActivateDisabledStrategies,
),
),
);
}

async updateEnabled(
project: string,
featureName: string,
Expand Down
18 changes: 16 additions & 2 deletions src/test/e2e/api/admin/project/features.e2e.test.ts
Expand Up @@ -413,14 +413,28 @@ test('Can bulk enable/disable environment for feature with strategies', async ()
)
.send({ features: [featureName] })
.set('Content-Type', 'application/json')
.expect(405);
.expect(200);
await app.getProjectFeatures(project, featureName).expect((res) => {
const enabledFeatureEnv = res.body.environments.find(
(e) => e.name === envName,
);
expect(enabledFeatureEnv).toBeTruthy();
expect(enabledFeatureEnv.enabled).toBe(true);
});

await app.request
.post(
`/api/admin/projects/${project}/bulk_features/environments/${envName}/off`,
)
.send({ features: [featureName] })
.expect(405);
.expect(200);
await app.getProjectFeatures(project, featureName).expect((res) => {
const disabledFeatureEnv = res.body.environments.find(
(e) => e.name === envName,
);
expect(disabledFeatureEnv).toBeTruthy();
expect(disabledFeatureEnv.enabled).toBe(false);
});
});

test("Trying to get a project that doesn't exist yields 404", async () => {
Expand Down

0 comments on commit 6b41cf0

Please sign in to comment.