Skip to content

Commit

Permalink
feat: updated default strategy event (#5462)
Browse files Browse the repository at this point in the history
  • Loading branch information
kwasniew committed Nov 28, 2023
1 parent f0c28dd commit f6bc418
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 24 deletions.
8 changes: 4 additions & 4 deletions src/lib/routes/admin-api/project/environments.ts
Expand Up @@ -102,15 +102,15 @@ 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({
tags: ['Projects'],
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',
),
Expand Down Expand Up @@ -161,14 +161,14 @@ export default class EnvironmentsController extends Controller {
res.status(200).end();
}

async addDefaultStrategyToProjectEnvironment(
async updateDefaultStrategyForProjectEnvironment(
req: Request<IProjectEnvironmentParams, CreateFeatureStrategySchema>,
res: Response<CreateFeatureStrategySchema>,
): Promise<void> {
const { projectId, environment } = req.params;
const strategy = req.body;

const saved = await this.environmentService.addDefaultStrategy(
const saved = await this.environmentService.updateDefaultStrategy(
environment,
projectId,
strategy,
Expand Down
18 changes: 16 additions & 2 deletions src/lib/services/environment-service.ts
@@ -1,4 +1,5 @@
import {
DEFAULT_STRATEGY_UPDATED,
IEnvironment,
IEnvironmentStore,
IFeatureEnvironmentStore,
Expand Down Expand Up @@ -126,21 +127,34 @@ export default class EnvironmentService {
}
}

async addDefaultStrategy(
async updateDefaultStrategy(
environment: string,
projectId: string,
strategy: CreateFeatureStrategySchema,
username: string = 'unknown',
): Promise<CreateFeatureStrategySchema> {
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(
Expand Down
2 changes: 2 additions & 0 deletions src/lib/types/events.ts
Expand Up @@ -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;
Expand Down Expand Up @@ -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];

Expand Down
38 changes: 20 additions & 18 deletions src/test/e2e/api/admin/project/environments.e2e.test.ts
Expand Up @@ -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 =
Expand All @@ -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,
});
});

Expand Down

0 comments on commit f6bc418

Please sign in to comment.