From f6bc418bdd00f1737ed7bef6d02f92dba62002a9 Mon Sep 17 00:00:00 2001 From: Mateusz Kwasniewski Date: Tue, 28 Nov 2023 14:59:20 +0100 Subject: [PATCH] feat: updated default strategy event (#5462) --- .../routes/admin-api/project/environments.ts | 8 ++-- src/lib/services/environment-service.ts | 18 ++++++++- src/lib/types/events.ts | 2 + .../admin/project/environments.e2e.test.ts | 38 ++++++++++--------- 4 files changed, 42 insertions(+), 24 deletions(-) diff --git a/src/lib/routes/admin-api/project/environments.ts b/src/lib/routes/admin-api/project/environments.ts index 03f4026297..e5cec10793 100644 --- a/src/lib/routes/admin-api/project/environments.ts +++ b/src/lib/routes/admin-api/project/environments.ts @@ -102,7 +102,7 @@ export default class EnvironmentsController extends Controller { this.route({ method: 'post', path: `${PREFIX}/:environment/default-strategy`, - handler: this.addDefaultStrategyToProjectEnvironment, + handler: this.updateDefaultStrategyForProjectEnvironment, permission: UPDATE_PROJECT, middleware: [ openApiService.validPath({ @@ -110,7 +110,7 @@ export default class EnvironmentsController extends Controller { operationId: 'addDefaultStrategyToProjectEnvironment', summary: 'Set environment-default strategy', description: - 'Adds a default strategy for this environment. Unleash will use this strategy by default when enabling a toggle. Use the wild card "*" for `:environment` to add to all environments. ', + 'Sets a default strategy for this environment. Unleash will use this strategy by default when enabling a toggle. Use the wild card "*" for `:environment` to add to all environments. ', requestBody: createRequestSchema( 'createFeatureStrategySchema', ), @@ -161,14 +161,14 @@ export default class EnvironmentsController extends Controller { res.status(200).end(); } - async addDefaultStrategyToProjectEnvironment( + async updateDefaultStrategyForProjectEnvironment( req: Request, res: Response, ): Promise { const { projectId, environment } = req.params; const strategy = req.body; - const saved = await this.environmentService.addDefaultStrategy( + const saved = await this.environmentService.updateDefaultStrategy( environment, projectId, strategy, diff --git a/src/lib/services/environment-service.ts b/src/lib/services/environment-service.ts index 4e9360b0a2..303c954e9b 100644 --- a/src/lib/services/environment-service.ts +++ b/src/lib/services/environment-service.ts @@ -1,4 +1,5 @@ import { + DEFAULT_STRATEGY_UPDATED, IEnvironment, IEnvironmentStore, IFeatureEnvironmentStore, @@ -126,21 +127,34 @@ export default class EnvironmentService { } } - async addDefaultStrategy( + async updateDefaultStrategy( environment: string, projectId: string, strategy: CreateFeatureStrategySchema, + username: string = 'unknown', ): Promise { if (strategy.name !== 'flexibleRollout') { throw new BadDataError( 'Only "flexibleRollout" strategy can be used as a default strategy for an environment', ); } - return this.projectStore.updateDefaultStrategy( + const previousDefaultStrategy = + await this.projectStore.getDefaultStrategy(projectId, environment); + const defaultStrategy = await this.projectStore.updateDefaultStrategy( projectId, environment, strategy, ); + await this.eventService.storeEvent({ + type: DEFAULT_STRATEGY_UPDATED, + project: projectId, + environment, + createdBy: username, + preData: previousDefaultStrategy, + data: defaultStrategy, + }); + + return defaultStrategy; } async overrideEnabledProjects( diff --git a/src/lib/types/events.ts b/src/lib/types/events.ts index 841a69b9c1..f37f27b0bb 100644 --- a/src/lib/types/events.ts +++ b/src/lib/types/events.ts @@ -111,6 +111,7 @@ export const SETTING_DELETED = 'setting-deleted' as const; export const PROJECT_ENVIRONMENT_ADDED = 'project-environment-added' as const; export const PROJECT_ENVIRONMENT_REMOVED = 'project-environment-removed' as const; +export const DEFAULT_STRATEGY_UPDATED = 'default-strategy-updated' as const; export const CLIENT_METRICS = 'client-metrics' as const; export const CLIENT_REGISTER = 'client-register' as const; @@ -297,6 +298,7 @@ export const IEventTypes = [ BANNER_DELETED, PROJECT_ENVIRONMENT_ADDED, PROJECT_ENVIRONMENT_REMOVED, + DEFAULT_STRATEGY_UPDATED, ] as const; export type IEventType = (typeof IEventTypes)[number]; diff --git a/src/test/e2e/api/admin/project/environments.e2e.test.ts b/src/test/e2e/api/admin/project/environments.e2e.test.ts index 12ab09cd97..5d2b2cd43d 100644 --- a/src/test/e2e/api/admin/project/environments.e2e.test.ts +++ b/src/test/e2e/api/admin/project/environments.e2e.test.ts @@ -113,19 +113,21 @@ test('Should not remove environment from project if project only has one environ }); test('Should add default strategy to environment', async () => { + const defaultStrategy = { + name: 'flexibleRollout', + constraints: [], + parameters: { + rollout: '50', + stickiness: 'customAppName', + groupId: 'stickytoggle', + }, + }; + await app.request .post( `/api/admin/projects/default/environments/default/default-strategy`, ) - .send({ - name: 'flexibleRollout', - constraints: [], - parameters: { - rollout: '50', - stickiness: 'customAppName', - groupId: 'stickytoggle', - }, - }) + .send(defaultStrategy) .expect(200); const envs = @@ -134,15 +136,15 @@ test('Should add default strategy to environment', async () => { expect(envs).toHaveLength(1); expect(envs[0]).toStrictEqual({ environment: 'default', - defaultStrategy: { - name: 'flexibleRollout', - constraints: [], - parameters: { - rollout: '50', - stickiness: 'customAppName', - groupId: 'stickytoggle', - }, - }, + defaultStrategy, + }); + const { body } = await app.getRecordedEvents(); + expect(body.events[0]).toMatchObject({ + type: 'default-strategy-updated', + project: 'default', + environment: 'default', + data: defaultStrategy, + preData: null, }); });