From da5e18233c89f532bd128b58847445e4ea457acb Mon Sep 17 00:00:00 2001 From: Mateusz Kwasniewski Date: Tue, 1 Aug 2023 11:20:59 +0200 Subject: [PATCH] feat: segments service DI (#4376) --- src/lib/features/index.ts | 1 + .../features/segment/createSegmentService.ts | 47 +++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 src/lib/features/segment/createSegmentService.ts diff --git a/src/lib/features/index.ts b/src/lib/features/index.ts index b257f4ea44..e2ef8a921f 100644 --- a/src/lib/features/index.ts +++ b/src/lib/features/index.ts @@ -3,3 +3,4 @@ export * from './export-import-toggles/createExportImportService'; export * from './feature-toggle/createFeatureToggleService'; export * from './project/createProjectService'; export * from './change-request-access-service/createChangeRequestAccessReadModel'; +export * from './segment/createSegmentService'; diff --git a/src/lib/features/segment/createSegmentService.ts b/src/lib/features/segment/createSegmentService.ts new file mode 100644 index 0000000000..4fe3af204d --- /dev/null +++ b/src/lib/features/segment/createSegmentService.ts @@ -0,0 +1,47 @@ +import { Db, IUnleashConfig } from 'lib/server-impl'; +import EventStore from '../../db/event-store'; +import { SegmentService } from '../../services'; +import FakeEventStore from '../../../test/fixtures/fake-event-store'; +import { ISegmentService } from '../../segments/segment-service-interface'; +import FeatureStrategiesStore from '../../db/feature-strategy-store'; +import SegmentStore from '../../db/segment-store'; +import FakeSegmentStore from '../../../test/fixtures/fake-segment-store'; +import FakeFeatureStrategiesStore from '../../../test/fixtures/fake-feature-strategies-store'; + +export const createSegmentService = ( + db: Db, + config: IUnleashConfig, +): ISegmentService => { + const { eventBus, getLogger, flagResolver } = config; + const eventStore = new EventStore(db, getLogger); + const segmentStore = new SegmentStore( + db, + eventBus, + getLogger, + flagResolver, + ); + const featureStrategiesStore = new FeatureStrategiesStore( + db, + eventBus, + getLogger, + flagResolver, + ); + + return new SegmentService( + { segmentStore, featureStrategiesStore, eventStore }, + config, + ); +}; + +export const createFakeSegmentService = ( + config: IUnleashConfig, +): ISegmentService => { + const eventStore = new FakeEventStore(); + const segmentStore = new FakeSegmentStore(); + const featureStrategiesStore = new FakeFeatureStrategiesStore(); + + return new SegmentService( + { segmentStore, featureStrategiesStore, eventStore }, + config, + ); +};