mirror of
https://github.com/Unleash/unleash.git
synced 2025-01-31 00:16:47 +01:00
87d9497be9
https://linear.app/unleash/issue/2-1403/consider-refactoring-the-way-tags-are-fetched-for-the-events This adds 2 methods to `EventService`: - `storeEvent`; - `storeEvents`; This allows us to run event-specific logic inside these methods. In the case of this PR, this means fetching the feature tags in case the event contains a `featureName` and there are no tags specified in the event. This prevents us from having to remember to fetch the tags in order to store feature-related events except for very specific cases, like the deletion of a feature - You can't fetch tags for a feature that no longer exists, so in that case we need to pre-fetch the tags before deleting the feature. This also allows us to do any event-specific post-processing to the event before reaching the DB layer. In general I think it's also nicer that we reference the event service instead of the event store directly. There's a lot of changes and a lot of files touched, but most of it is boilerplate to inject the `eventService` where needed instead of using the `eventStore` directly. Hopefully this will be a better approach than https://github.com/Unleash/unleash/pull/4729 --------- Co-authored-by: Gastón Fournier <gaston@getunleash.io>
197 lines
7.6 KiB
TypeScript
197 lines
7.6 KiB
TypeScript
import {
|
|
AccessService,
|
|
EventService,
|
|
FeatureToggleService,
|
|
GroupService,
|
|
} from '../../services';
|
|
import FeatureStrategiesStore from '../../db/feature-strategy-store';
|
|
import FeatureToggleStore from '../../db/feature-toggle-store';
|
|
import FeatureToggleClientStore from '../../db/feature-toggle-client-store';
|
|
import ProjectStore from '../../db/project-store';
|
|
import { FeatureEnvironmentStore } from '../../db/feature-environment-store';
|
|
import ContextFieldStore from '../../db/context-field-store';
|
|
import GroupStore from '../../db/group-store';
|
|
import { AccountStore } from '../../db/account-store';
|
|
import { AccessStore } from '../../db/access-store';
|
|
import RoleStore from '../../db/role-store';
|
|
import EnvironmentStore from '../../db/environment-store';
|
|
import { Db } from '../../db/db';
|
|
import { IUnleashConfig } from '../../types';
|
|
import FakeEventStore from '../../../test/fixtures/fake-event-store';
|
|
import FakeFeatureStrategiesStore from '../../../test/fixtures/fake-feature-strategies-store';
|
|
import FakeFeatureToggleStore from '../../../test/fixtures/fake-feature-toggle-store';
|
|
import FakeFeatureToggleClientStore from '../../../test/fixtures/fake-feature-toggle-client-store';
|
|
import FakeProjectStore from '../../../test/fixtures/fake-project-store';
|
|
import FakeFeatureEnvironmentStore from '../../../test/fixtures/fake-feature-environment-store';
|
|
import FakeContextFieldStore from '../../../test/fixtures/fake-context-field-store';
|
|
import FakeGroupStore from '../../../test/fixtures/fake-group-store';
|
|
import { FakeAccountStore } from '../../../test/fixtures/fake-account-store';
|
|
import FakeAccessStore from '../../../test/fixtures/fake-access-store';
|
|
import FakeRoleStore from '../../../test/fixtures/fake-role-store';
|
|
import FakeEnvironmentStore from '../../../test/fixtures/fake-environment-store';
|
|
import EventStore from '../../db/event-store';
|
|
import {
|
|
createChangeRequestAccessReadModel,
|
|
createFakeChangeRequestAccessService,
|
|
} from '../change-request-access-service/createChangeRequestAccessReadModel';
|
|
import {
|
|
createFakeSegmentService,
|
|
createSegmentService,
|
|
} from '../segment/createSegmentService';
|
|
import StrategyStore from '../../db/strategy-store';
|
|
import FakeStrategiesStore from '../../../test/fixtures/fake-strategies-store';
|
|
import {
|
|
createFakePrivateProjectChecker,
|
|
createPrivateProjectChecker,
|
|
} from '../private-project/createPrivateProjectChecker';
|
|
import { DependentFeaturesReadModel } from '../dependent-features/dependent-features-read-model';
|
|
import { FakeDependentFeaturesReadModel } from '../dependent-features/fake-dependent-features-read-model';
|
|
import FeatureTagStore from '../../db/feature-tag-store';
|
|
import FakeFeatureTagStore from '../../../test/fixtures/fake-feature-tag-store';
|
|
|
|
export const createFeatureToggleService = (
|
|
db: Db,
|
|
config: IUnleashConfig,
|
|
): FeatureToggleService => {
|
|
const { getLogger, eventBus, flagResolver } = config;
|
|
const featureStrategiesStore = new FeatureStrategiesStore(
|
|
db,
|
|
eventBus,
|
|
getLogger,
|
|
flagResolver,
|
|
);
|
|
const featureToggleStore = new FeatureToggleStore(db, eventBus, getLogger);
|
|
const featureToggleClientStore = new FeatureToggleClientStore(
|
|
db,
|
|
eventBus,
|
|
getLogger,
|
|
flagResolver,
|
|
);
|
|
const projectStore = new ProjectStore(
|
|
db,
|
|
eventBus,
|
|
getLogger,
|
|
flagResolver,
|
|
);
|
|
const featureEnvironmentStore = new FeatureEnvironmentStore(
|
|
db,
|
|
eventBus,
|
|
getLogger,
|
|
);
|
|
const contextFieldStore = new ContextFieldStore(
|
|
db,
|
|
getLogger,
|
|
flagResolver,
|
|
);
|
|
const groupStore = new GroupStore(db);
|
|
const strategyStore = new StrategyStore(db, getLogger);
|
|
const accountStore = new AccountStore(db, getLogger);
|
|
const accessStore = new AccessStore(db, eventBus, getLogger);
|
|
const featureTagStore = new FeatureTagStore(db, eventBus, getLogger);
|
|
const roleStore = new RoleStore(db, eventBus, getLogger);
|
|
const environmentStore = new EnvironmentStore(db, eventBus, getLogger);
|
|
const eventStore = new EventStore(db, getLogger);
|
|
const eventService = new EventService(
|
|
{ eventStore, featureTagStore },
|
|
{ getLogger },
|
|
);
|
|
const groupService = new GroupService(
|
|
{ groupStore, accountStore },
|
|
{ getLogger },
|
|
eventService,
|
|
);
|
|
const accessService = new AccessService(
|
|
{ accessStore, accountStore, roleStore, environmentStore, groupStore },
|
|
{ getLogger, flagResolver },
|
|
groupService,
|
|
);
|
|
const segmentService = createSegmentService(db, config);
|
|
const changeRequestAccessReadModel = createChangeRequestAccessReadModel(
|
|
db,
|
|
config,
|
|
);
|
|
|
|
const privateProjectChecker = createPrivateProjectChecker(db, config);
|
|
|
|
const dependentFeaturesReadModel = new DependentFeaturesReadModel(db);
|
|
|
|
const featureToggleService = new FeatureToggleService(
|
|
{
|
|
featureStrategiesStore,
|
|
featureToggleStore,
|
|
featureToggleClientStore,
|
|
projectStore,
|
|
featureTagStore,
|
|
featureEnvironmentStore,
|
|
contextFieldStore,
|
|
strategyStore,
|
|
},
|
|
{ getLogger, flagResolver },
|
|
segmentService,
|
|
accessService,
|
|
eventService,
|
|
changeRequestAccessReadModel,
|
|
privateProjectChecker,
|
|
dependentFeaturesReadModel,
|
|
);
|
|
return featureToggleService;
|
|
};
|
|
|
|
export const createFakeFeatureToggleService = (
|
|
config: IUnleashConfig,
|
|
): FeatureToggleService => {
|
|
const { getLogger, flagResolver } = config;
|
|
const eventStore = new FakeEventStore();
|
|
const strategyStore = new FakeStrategiesStore();
|
|
const featureStrategiesStore = new FakeFeatureStrategiesStore();
|
|
const featureToggleStore = new FakeFeatureToggleStore();
|
|
const featureToggleClientStore = new FakeFeatureToggleClientStore();
|
|
const projectStore = new FakeProjectStore();
|
|
const featureEnvironmentStore = new FakeFeatureEnvironmentStore();
|
|
const contextFieldStore = new FakeContextFieldStore();
|
|
const groupStore = new FakeGroupStore();
|
|
const accountStore = new FakeAccountStore();
|
|
const accessStore = new FakeAccessStore();
|
|
const featureTagStore = new FakeFeatureTagStore();
|
|
const roleStore = new FakeRoleStore();
|
|
const environmentStore = new FakeEnvironmentStore();
|
|
const eventService = new EventService(
|
|
{ eventStore, featureTagStore },
|
|
{ getLogger },
|
|
);
|
|
const groupService = new GroupService(
|
|
{ groupStore, accountStore },
|
|
{ getLogger },
|
|
eventService,
|
|
);
|
|
const accessService = new AccessService(
|
|
{ accessStore, accountStore, roleStore, environmentStore, groupStore },
|
|
{ getLogger, flagResolver },
|
|
groupService,
|
|
);
|
|
const segmentService = createFakeSegmentService(config);
|
|
const changeRequestAccessReadModel = createFakeChangeRequestAccessService();
|
|
const fakePrivateProjectChecker = createFakePrivateProjectChecker();
|
|
const dependentFeaturesReadModel = new FakeDependentFeaturesReadModel();
|
|
const featureToggleService = new FeatureToggleService(
|
|
{
|
|
featureStrategiesStore,
|
|
featureToggleStore,
|
|
featureToggleClientStore,
|
|
projectStore,
|
|
featureTagStore,
|
|
featureEnvironmentStore,
|
|
contextFieldStore,
|
|
strategyStore,
|
|
},
|
|
{ getLogger, flagResolver },
|
|
segmentService,
|
|
accessService,
|
|
eventService,
|
|
changeRequestAccessReadModel,
|
|
fakePrivateProjectChecker,
|
|
dependentFeaturesReadModel,
|
|
);
|
|
return featureToggleService;
|
|
};
|