From f3df7269cb640ed99e752bd67f5128bb57fd9f59 Mon Sep 17 00:00:00 2001 From: Mateusz Kwasniewski Date: Mon, 4 Mar 2024 15:48:30 +0100 Subject: [PATCH] refactor: segment read model used in proxy-repository (#6421) --- src/lib/db/index.ts | 2 ++ src/lib/proxy/proxy-repository.ts | 9 ++++++--- src/lib/services/index.ts | 1 - src/lib/services/proxy-service.ts | 6 ++++-- src/lib/types/stores.ts | 3 +++ src/test/fixtures/store.ts | 2 ++ 6 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/lib/db/index.ts b/src/lib/db/index.ts index 869e5bcb41..1e9a5bdb80 100644 --- a/src/lib/db/index.ts +++ b/src/lib/db/index.ts @@ -42,6 +42,7 @@ import LastSeenStore from '../features/metrics/last-seen/last-seen-store'; import FeatureSearchStore from '../features/feature-search/feature-search-store'; import { InactiveUsersStore } from '../users/inactive/inactive-users-store'; import { TrafficDataUsageStore } from '../features/traffic-data-usage/traffic-data-usage-store'; +import { SegmentReadModel } from '../features/segment/segment-read-model'; export const createStores = ( config: IUnleashConfig, @@ -145,6 +146,7 @@ export const createStores = ( featureSearchStore: new FeatureSearchStore(db, eventBus, getLogger), inactiveUsersStore: new InactiveUsersStore(db, eventBus, getLogger), trafficDataUsageStore: new TrafficDataUsageStore(db, getLogger), + segmentReadModel: new SegmentReadModel(db), }; }; diff --git a/src/lib/proxy/proxy-repository.ts b/src/lib/proxy/proxy-repository.ts index 742b6e4951..1780d4b436 100644 --- a/src/lib/proxy/proxy-repository.ts +++ b/src/lib/proxy/proxy-repository.ts @@ -21,11 +21,14 @@ import { PROXY_FEATURES_FOR_TOKEN_TIME } from '../metric-events'; type Config = Pick; -type Stores = Pick; +type Stores = Pick< + IUnleashStores, + 'projectStore' | 'eventStore' | 'segmentReadModel' +>; type Services = Pick< IUnleashServices, - 'featureToggleServiceV2' | 'segmentService' | 'configurationRevisionService' + 'featureToggleServiceV2' | 'configurationRevisionService' >; export class ProxyRepository @@ -164,7 +167,7 @@ export class ProxyRepository private async segmentsForToken(): Promise { return mapSegmentsForClient( - await this.services.segmentService.getAll(), + await this.stores.segmentReadModel.getAll(), ); } diff --git a/src/lib/services/index.ts b/src/lib/services/index.ts index 58c7b97963..c339887837 100644 --- a/src/lib/services/index.ts +++ b/src/lib/services/index.ts @@ -288,7 +288,6 @@ export const createServices = ( const proxyService = new ProxyService(config, stores, { featureToggleServiceV2, clientMetricsServiceV2, - segmentService, settingService, configurationRevisionService, }); diff --git a/src/lib/services/proxy-service.ts b/src/lib/services/proxy-service.ts index 7187d681af..fb6d2f55bf 100644 --- a/src/lib/services/proxy-service.ts +++ b/src/lib/services/proxy-service.ts @@ -23,12 +23,14 @@ type Config = Pick< 'getLogger' | 'frontendApi' | 'frontendApiOrigins' | 'eventBus' >; -type Stores = Pick; +type Stores = Pick< + IUnleashStores, + 'projectStore' | 'eventStore' | 'segmentReadModel' +>; type Services = Pick< IUnleashServices, | 'featureToggleServiceV2' - | 'segmentService' | 'clientMetricsServiceV2' | 'settingService' | 'configurationRevisionService' diff --git a/src/lib/types/stores.ts b/src/lib/types/stores.ts index d3d8b86a53..2f635bab84 100644 --- a/src/lib/types/stores.ts +++ b/src/lib/types/stores.ts @@ -39,6 +39,7 @@ import { ILastSeenStore } from '../features/metrics/last-seen/types/last-seen-st import { IFeatureSearchStore } from '../features/feature-search/feature-search-store-type'; import { IInactiveUsersStore } from '../users/inactive/types/inactive-users-store-type'; import { ITrafficDataUsageStore } from '../features/traffic-data-usage/traffic-data-usage-store-type'; +import { ISegmentReadModel } from '../features/segment/segment-read-model-type'; export interface IUnleashStores { accessStore: IAccessStore; @@ -82,6 +83,7 @@ export interface IUnleashStores { featureSearchStore: IFeatureSearchStore; inactiveUsersStore: IInactiveUsersStore; trafficDataUsageStore: ITrafficDataUsageStore; + segmentReadModel: ISegmentReadModel; } export { @@ -124,4 +126,5 @@ export { ILastSeenStore, IFeatureSearchStore, ITrafficDataUsageStore, + ISegmentReadModel, }; diff --git a/src/test/fixtures/store.ts b/src/test/fixtures/store.ts index 314142d4f6..9e23ff8d9f 100644 --- a/src/test/fixtures/store.ts +++ b/src/test/fixtures/store.ts @@ -42,6 +42,7 @@ import { FakeLastSeenStore } from '../../lib/features/metrics/last-seen/fake-las import FakeFeatureSearchStore from '../../lib/features/feature-search/fake-feature-search-store'; import { FakeInactiveUsersStore } from '../../lib/users/inactive/fakes/fake-inactive-users-store'; import { FakeTrafficDataUsageStore } from '../../lib/features/traffic-data-usage/fake-traffic-data-usage-store'; +import { FakeSegmentReadModel } from '../../lib/features/segment/fake-segment-read-model'; const db = { select: () => ({ @@ -93,6 +94,7 @@ const createStores: () => IUnleashStores = () => { featureSearchStore: new FakeFeatureSearchStore(), inactiveUsersStore: new FakeInactiveUsersStore(), trafficDataUsageStore: new FakeTrafficDataUsageStore(), + segmentReadModel: new FakeSegmentReadModel(), }; };