1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-11 00:08:30 +01:00
unleash.unleash/src/lib/features/project/createProjectService.ts
Nuno Góis 87d9497be9
refactor: prefer eventService.storeEvent methods (#4830)
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>
2023-09-27 14:23:05 +01:00

184 lines
6.2 KiB
TypeScript

import { Db, IUnleashConfig } from 'lib/server-impl';
import EventStore from '../../db/event-store';
import GroupStore from '../../db/group-store';
import { AccountStore } from '../../db/account-store';
import EnvironmentStore from '../../db/environment-store';
import {
AccessService,
EventService,
FavoritesService,
GroupService,
ProjectService,
} from '../../services';
import FakeGroupStore from '../../../test/fixtures/fake-group-store';
import FakeEventStore from '../../../test/fixtures/fake-event-store';
import ProjectStore from '../../db/project-store';
import FeatureToggleStore from '../../db/feature-toggle-store';
import FeatureTypeStore from '../../db/feature-type-store';
import { FeatureEnvironmentStore } from '../../db/feature-environment-store';
import ProjectStatsStore from '../../db/project-stats-store';
import {
createAccessService,
createFakeAccessService,
} from '../access/createAccessService';
import {
createFakeFeatureToggleService,
createFeatureToggleService,
} from '../feature-toggle/createFeatureToggleService';
import { FavoriteFeaturesStore } from '../../db/favorite-features-store';
import { FavoriteProjectsStore } from '../../db/favorite-projects-store';
import FakeProjectStore from '../../../test/fixtures/fake-project-store';
import FakeFeatureToggleStore from '../../../test/fixtures/fake-feature-toggle-store';
import FakeFeatureTypeStore from '../../../test/fixtures/fake-feature-type-store';
import FakeEnvironmentStore from '../../../test/fixtures/fake-environment-store';
import FakeFeatureEnvironmentStore from '../../../test/fixtures/fake-feature-environment-store';
import FakeProjectStatsStore from '../../../test/fixtures/fake-project-stats-store';
import FakeFavoriteFeaturesStore from '../../../test/fixtures/fake-favorite-features-store';
import FakeFavoriteProjectsStore from '../../../test/fixtures/fake-favorite-projects-store';
import { FakeAccountStore } from '../../../test/fixtures/fake-account-store';
import {
createFakePrivateProjectChecker,
createPrivateProjectChecker,
} from '../private-project/createPrivateProjectChecker';
import FakeFeatureTagStore from '../../../test/fixtures/fake-feature-tag-store';
export const createProjectService = (
db: Db,
config: IUnleashConfig,
): ProjectService => {
const { eventBus, getLogger, flagResolver } = config;
const eventStore = new EventStore(db, getLogger);
const projectStore = new ProjectStore(
db,
eventBus,
getLogger,
flagResolver,
);
const groupStore = new GroupStore(db);
const featureToggleStore = new FeatureToggleStore(db, eventBus, getLogger);
const featureTypeStore = new FeatureTypeStore(db, getLogger);
const accountStore = new AccountStore(db, getLogger);
const environmentStore = new EnvironmentStore(db, eventBus, getLogger);
const featureEnvironmentStore = new FeatureEnvironmentStore(
db,
eventBus,
getLogger,
);
const projectStatsStore = new ProjectStatsStore(db, eventBus, getLogger);
const accessService: AccessService = createAccessService(db, config);
const featureToggleService = createFeatureToggleService(db, config);
const favoriteFeaturesStore = new FavoriteFeaturesStore(
db,
eventBus,
getLogger,
);
const favoriteProjectsStore = new FavoriteProjectsStore(
db,
eventBus,
getLogger,
);
const eventService = new EventService(
{
eventStore,
featureTagStore: new FakeFeatureTagStore(),
},
config,
);
const favoriteService = new FavoritesService(
{
favoriteFeaturesStore,
favoriteProjectsStore,
},
config,
eventService,
);
const groupService = new GroupService(
{ groupStore, accountStore },
{ getLogger },
eventService,
);
const privateProjectChecker = createPrivateProjectChecker(db, config);
return new ProjectService(
{
projectStore,
eventStore,
featureToggleStore,
featureTypeStore,
environmentStore,
featureEnvironmentStore,
accountStore,
projectStatsStore,
},
config,
accessService,
featureToggleService,
groupService,
favoriteService,
eventService,
privateProjectChecker,
);
};
export const createFakeProjectService = (
config: IUnleashConfig,
): ProjectService => {
const { getLogger } = config;
const eventStore = new FakeEventStore();
const projectStore = new FakeProjectStore();
const groupStore = new FakeGroupStore();
const featureToggleStore = new FakeFeatureToggleStore();
const featureTypeStore = new FakeFeatureTypeStore();
const accountStore = new FakeAccountStore();
const environmentStore = new FakeEnvironmentStore();
const featureEnvironmentStore = new FakeFeatureEnvironmentStore();
const projectStatsStore = new FakeProjectStatsStore();
const accessService = createFakeAccessService(config);
const featureToggleService = createFakeFeatureToggleService(config);
const favoriteFeaturesStore = new FakeFavoriteFeaturesStore();
const favoriteProjectsStore = new FakeFavoriteProjectsStore();
const eventService = new EventService(
{
eventStore,
featureTagStore: new FakeFeatureTagStore(),
},
config,
);
const favoriteService = new FavoritesService(
{
favoriteFeaturesStore,
favoriteProjectsStore,
},
config,
eventService,
);
const groupService = new GroupService(
{ groupStore, accountStore },
{ getLogger },
eventService,
);
const privateProjectChecker = createFakePrivateProjectChecker();
return new ProjectService(
{
projectStore,
eventStore,
featureToggleStore,
featureTypeStore,
environmentStore,
featureEnvironmentStore,
accountStore,
projectStatsStore,
},
config,
accessService,
featureToggleService,
groupService,
favoriteService,
eventService,
privateProjectChecker,
);
};