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,7 +1046,12 @@ class FeatureToggleService {
featureName,
);
if (hasEnvironment) {
if (!hasEnvironment) {
throw new NotFoundError(
`Could not find environment ${environment} for feature: ${featureName}`,
);
}
if (enabled) {
const strategies = await this.getStrategiesForEnvironment(
project,
@ -1047,15 +1059,6 @@ class FeatureToggleService {
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),
{
@ -1065,9 +1068,6 @@ class FeatureToggleService {
},
createdBy,
);
} else {
throw new NoAccessError(CREATE_FEATURE_STRATEGY);
}
}
}
const updatedEnvironmentStatus =
@ -1079,9 +1079,7 @@ class FeatureToggleService {
const feature = await this.featureToggleStore.get(featureName);
if (updatedEnvironmentStatus > 0) {
const tags = await this.tagStore.getAllTagsForFeature(
featureName,
);
const tags = await this.tagStore.getAllTagsForFeature(featureName);
await this.eventStore.store(
new FeatureEnvironmentEvent({
enabled,
@ -1096,11 +1094,6 @@ class FeatureToggleService {
return feature;
}
throw new NotFoundError(
`Could not find environment ${environment} for feature: ${featureName}`,
);
}
// @deprecated
async storeFeatureUpdatedEventLegacy(
featureName: string,
@ -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;