mirror of
https://github.com/Unleash/unleash.git
synced 2025-07-17 13:46:47 +02:00
fix: move application logic to service (#3846)
This commit is contained in:
parent
7555f5beba
commit
1dba9d092b
@ -887,13 +887,16 @@ export default class ProjectFeaturesController extends Controller {
|
|||||||
req.body.segmentIds = [];
|
req.body.segmentIds = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
const updatedStrategy = await this.featureService.updateStrategy(
|
const updatedStrategy = await this.startTransaction(async (tx) =>
|
||||||
strategyId,
|
this.transactionalFeatureToggleService(tx).updateStrategy(
|
||||||
req.body,
|
strategyId,
|
||||||
{ environment, projectId, featureName },
|
req.body,
|
||||||
userName,
|
{ environment, projectId, featureName },
|
||||||
req.user,
|
userName,
|
||||||
|
req.user,
|
||||||
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
res.status(200).json(updatedStrategy);
|
res.status(200).json(updatedStrategy);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -907,28 +910,16 @@ export default class ProjectFeaturesController extends Controller {
|
|||||||
const strategy = await this.featureService.getStrategy(strategyId);
|
const strategy = await this.featureService.getStrategy(strategyId);
|
||||||
|
|
||||||
const { newDocument } = applyPatch(strategy, patch);
|
const { newDocument } = applyPatch(strategy, patch);
|
||||||
const updatedStrategy = await this.featureService.updateStrategy(
|
const updatedStrategy = await this.startTransaction(async (tx) =>
|
||||||
strategyId,
|
this.transactionalFeatureToggleService(tx).updateStrategy(
|
||||||
newDocument,
|
strategyId,
|
||||||
{ environment, projectId, featureName },
|
newDocument,
|
||||||
userName,
|
{ environment, projectId, featureName },
|
||||||
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,
|
|
||||||
userName,
|
userName,
|
||||||
);
|
req.user,
|
||||||
}
|
),
|
||||||
|
);
|
||||||
|
|
||||||
res.status(200).json(updatedStrategy);
|
res.status(200).json(updatedStrategy);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -510,6 +510,29 @@ class FeatureToggleService {
|
|||||||
return this.unprotectedUpdateStrategy(id, updates, context, userName);
|
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(
|
async unprotectedUpdateStrategy(
|
||||||
id: string,
|
id: string,
|
||||||
updates: Partial<IFeatureStrategy>,
|
updates: Partial<IFeatureStrategy>,
|
||||||
@ -565,6 +588,12 @@ class FeatureToggleService {
|
|||||||
tags,
|
tags,
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
await this.optionallyDisableFeature(
|
||||||
|
featureName,
|
||||||
|
environment,
|
||||||
|
projectId,
|
||||||
|
userName,
|
||||||
|
);
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
throw new NotFoundError(`Could not find strategy with id ${id}`);
|
throw new NotFoundError(`Could not find strategy with id ${id}`);
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
import dbInit, { ITestDb } from '../../helpers/database-init';
|
import dbInit, { ITestDb } from '../../helpers/database-init';
|
||||||
import getLogger from '../../../fixtures/no-logger';
|
import getLogger from '../../../fixtures/no-logger';
|
||||||
import { IUnleashTest, setupApp } from '../../helpers/test-helper';
|
import {
|
||||||
|
IUnleashTest,
|
||||||
|
setupAppWithCustomConfig,
|
||||||
|
} from '../../helpers/test-helper';
|
||||||
import {
|
import {
|
||||||
IConstraint,
|
IConstraint,
|
||||||
IFeatureToggleClient,
|
IFeatureToggleClient,
|
||||||
@ -180,7 +183,7 @@ const createTestSegments = async () => {
|
|||||||
|
|
||||||
beforeAll(async () => {
|
beforeAll(async () => {
|
||||||
db = await dbInit('segments', getLogger);
|
db = await dbInit('segments', getLogger);
|
||||||
app = await setupApp(db.stores);
|
app = await setupAppWithCustomConfig(db.stores, {}, db.rawDatabase);
|
||||||
});
|
});
|
||||||
|
|
||||||
afterAll(async () => {
|
afterAll(async () => {
|
||||||
|
Loading…
Reference in New Issue
Block a user