1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-31 00:16:47 +01:00

CR applier should be able to create strategy (#2597)

This commit is contained in:
Mateusz Kwasniewski 2022-12-05 12:39:13 +01:00 committed by GitHub
parent a65a47e3b5
commit b9290dd5ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1015,13 +1015,21 @@ class FeatureToggleService {
user?: User,
): Promise<FeatureToggle> {
await this.stopWhenChangeRequestsEnabled(project, environment);
if (enabled) {
await this.stopWhenCannotCreateStrategies(
project,
environment,
featureName,
user,
);
}
return this.unprotectedUpdateEnabled(
project,
featureName,
environment,
enabled,
createdBy,
user,
);
}
@ -1031,7 +1039,6 @@ class FeatureToggleService {
environment: string,
enabled: boolean,
createdBy: string,
user?: User,
): Promise<FeatureToggle> {
const hasEnvironment =
await this.featureEnvironmentStore.featureHasEnvironment(
@ -1039,66 +1046,52 @@ class FeatureToggleService {
featureName,
);
if (hasEnvironment) {
if (enabled) {
const strategies = await this.getStrategiesForEnvironment(
if (!hasEnvironment) {
throw new NotFoundError(
`Could not find environment ${environment} for feature: ${featureName}`,
);
}
if (enabled) {
const strategies = await this.getStrategiesForEnvironment(
project,
featureName,
environment,
);
if (strategies.length === 0) {
await this.unprotectedCreateStrategy(
getDefaultStrategy(featureName),
{
environment,
projectId: project,
featureName,
},
createdBy,
);
}
}
const updatedEnvironmentStatus =
await this.featureEnvironmentStore.setEnvironmentEnabledStatus(
environment,
featureName,
enabled,
);
const feature = await this.featureToggleStore.get(featureName);
if (updatedEnvironmentStatus > 0) {
const tags = await this.tagStore.getAllTagsForFeature(featureName);
await this.eventStore.store(
new FeatureEnvironmentEvent({
enabled,
project,
featureName,
environment,
);
if (strategies.length === 0) {
const canAddStrategies =
user &&
(await this.accessService.hasPermission(
user,
CREATE_FEATURE_STRATEGY,
project,
environment,
));
if (canAddStrategies) {
await this.unprotectedCreateStrategy(
getDefaultStrategy(featureName),
{
environment,
projectId: project,
featureName,
},
createdBy,
);
} else {
throw new NoAccessError(CREATE_FEATURE_STRATEGY);
}
}
}
const updatedEnvironmentStatus =
await this.featureEnvironmentStore.setEnvironmentEnabledStatus(
environment,
featureName,
enabled,
);
const feature = await this.featureToggleStore.get(featureName);
if (updatedEnvironmentStatus > 0) {
const tags = await this.tagStore.getAllTagsForFeature(
featureName,
);
await this.eventStore.store(
new FeatureEnvironmentEvent({
enabled,
project,
featureName,
environment,
createdBy,
tags,
}),
);
}
return feature;
createdBy,
tags,
}),
);
}
throw new NotFoundError(
`Could not find environment ${environment} for feature: ${featureName}`,
);
return feature;
}
// @deprecated
@ -1394,6 +1387,40 @@ class FeatureToggleService {
);
}
}
private async stopWhenCannotCreateStrategies(
project: string,
environment: string,
featureName: string,
user: User,
) {
const hasEnvironment =
await this.featureEnvironmentStore.featureHasEnvironment(
environment,
featureName,
);
if (hasEnvironment) {
const strategies = await this.getStrategiesForEnvironment(
project,
featureName,
environment,
);
if (strategies.length === 0) {
const canAddStrategies =
user &&
(await this.accessService.hasPermission(
user,
CREATE_FEATURE_STRATEGY,
project,
environment,
));
if (!canAddStrategies) {
throw new NoAccessError(CREATE_FEATURE_STRATEGY);
}
}
}
}
}
export default FeatureToggleService;