1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-05-08 01:15:49 +02:00

fix: move application logic to service (#3846)

This commit is contained in:
Mateusz Kwasniewski 2023-05-23 16:41:52 +02:00 committed by GitHub
parent 7555f5beba
commit 1dba9d092b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 52 additions and 29 deletions

View File

@ -887,13 +887,16 @@ export default class ProjectFeaturesController extends Controller {
req.body.segmentIds = [];
}
const updatedStrategy = await this.featureService.updateStrategy(
strategyId,
req.body,
{ environment, projectId, featureName },
userName,
req.user,
const updatedStrategy = await this.startTransaction(async (tx) =>
this.transactionalFeatureToggleService(tx).updateStrategy(
strategyId,
req.body,
{ environment, projectId, featureName },
userName,
req.user,
),
);
res.status(200).json(updatedStrategy);
}
@ -907,28 +910,16 @@ export default class ProjectFeaturesController extends Controller {
const strategy = await this.featureService.getStrategy(strategyId);
const { newDocument } = applyPatch(strategy, patch);
const updatedStrategy = await this.featureService.updateStrategy(
strategyId,
newDocument,
{ environment, projectId, featureName },
userName,
req.user,
);
const feature = await this.featureService.getFeature({ featureName });
const env = feature.environments.find((e) => e.name === environment);
const hasOnlyDisabledStrategies = env!.strategies.every(
(strat) => strat.disabled,
);
if (hasOnlyDisabledStrategies) {
await this.featureService.updateEnabled(
projectId,
featureName,
environment,
false,
const updatedStrategy = await this.startTransaction(async (tx) =>
this.transactionalFeatureToggleService(tx).updateStrategy(
strategyId,
newDocument,
{ environment, projectId, featureName },
userName,
);
}
req.user,
),
);
res.status(200).json(updatedStrategy);
}

View File

@ -510,6 +510,29 @@ class FeatureToggleService {
return this.unprotectedUpdateStrategy(id, updates, context, userName);
}
async optionallyDisableFeature(
featureName: string,
environment: string,
projectId: string,
userName: string,
): Promise<void> {
const feature = await this.getFeature({ featureName });
const env = feature.environments.find((e) => e.name === environment);
const hasOnlyDisabledStrategies = env?.strategies.every(
(strategy) => strategy.disabled,
);
if (hasOnlyDisabledStrategies) {
await this.unprotectedUpdateEnabled(
projectId,
featureName,
environment,
false,
userName,
);
}
}
async unprotectedUpdateStrategy(
id: string,
updates: Partial<IFeatureStrategy>,
@ -565,6 +588,12 @@ class FeatureToggleService {
tags,
}),
);
await this.optionallyDisableFeature(
featureName,
environment,
projectId,
userName,
);
return data;
}
throw new NotFoundError(`Could not find strategy with id ${id}`);

View File

@ -1,6 +1,9 @@
import dbInit, { ITestDb } from '../../helpers/database-init';
import getLogger from '../../../fixtures/no-logger';
import { IUnleashTest, setupApp } from '../../helpers/test-helper';
import {
IUnleashTest,
setupAppWithCustomConfig,
} from '../../helpers/test-helper';
import {
IConstraint,
IFeatureToggleClient,
@ -180,7 +183,7 @@ const createTestSegments = async () => {
beforeAll(async () => {
db = await dbInit('segments', getLogger);
app = await setupApp(db.stores);
app = await setupAppWithCustomConfig(db.stores, {}, db.rawDatabase);
});
afterAll(async () => {