From 3bfafcf87ef6657c3f87f9ec8bfcec095be6b811 Mon Sep 17 00:00:00 2001 From: David Leek Date: Thu, 22 Feb 2024 13:44:47 +0100 Subject: [PATCH] chore: remove redundant check for project names in db (#6311) Skips the fetching project names from db part in the proxy-repository that was done when the token had ALL_PROJECTS --- .../client-feature-toggle-store.ts | 12 ++++++++++-- .../fakes/fake-feature-strategies-store.ts | 16 ++++++++++------ src/lib/proxy/proxy-repository.ts | 13 ++----------- 3 files changed, 22 insertions(+), 19 deletions(-) diff --git a/src/lib/features/client-feature-toggles/client-feature-toggle-store.ts b/src/lib/features/client-feature-toggles/client-feature-toggle-store.ts index e35e47f11c..d9caf205bb 100644 --- a/src/lib/features/client-feature-toggles/client-feature-toggle-store.ts +++ b/src/lib/features/client-feature-toggles/client-feature-toggle-store.ts @@ -11,7 +11,12 @@ import { ITag, PartialDeep, } from '../../types'; -import { DEFAULT_ENV, ensureStringValue, mapValues } from '../../util'; +import { + ALL_PROJECTS, + DEFAULT_ENV, + ensureStringValue, + mapValues, +} from '../../util'; import EventEmitter from 'events'; import FeatureToggleStore from '../feature-toggle/feature-toggle-store'; import { Db } from '../../db/db'; @@ -166,7 +171,10 @@ export default class FeatureToggleClientStore .whereIn(['tag_type', 'tag_value'], featureQuery.tag); query = query.whereIn('features.name', tagQuery); } - if (featureQuery.project) { + if ( + featureQuery.project && + !featureQuery.project.includes(ALL_PROJECTS) + ) { query = query.whereIn('project', featureQuery.project); } if (featureQuery.namePrefix) { diff --git a/src/lib/features/feature-toggle/fakes/fake-feature-strategies-store.ts b/src/lib/features/feature-toggle/fakes/fake-feature-strategies-store.ts index ccb7057b58..3e282d304d 100644 --- a/src/lib/features/feature-toggle/fakes/fake-feature-strategies-store.ts +++ b/src/lib/features/feature-toggle/fakes/fake-feature-strategies-store.ts @@ -10,6 +10,7 @@ import { import NotFoundError from '../../../error/notfound-error'; import { IFeatureStrategiesStore } from '../types/feature-toggle-strategies-store-type'; import { IFeatureProjectUserParams } from '../feature-toggle-controller'; +import { ALL_PROJECTS } from '../../../util'; interface ProjectEnvironment { projectName: string; @@ -182,17 +183,20 @@ export default class FakeFeatureStrategiesStore if (featureQuery.namePrefix) { if (featureQuery.project) { return ( - toggle.name.startsWith(featureQuery.namePrefix) && - featureQuery.project.some((project) => - project.includes(toggle.project), - ) + (toggle.name.startsWith(featureQuery.namePrefix) && + featureQuery.project.some((project) => + project.includes(toggle.project), + )) || + featureQuery.project.includes(ALL_PROJECTS) ); } return toggle.name.startsWith(featureQuery.namePrefix); } if (featureQuery.project) { - return featureQuery.project.some((project) => - project.includes(toggle.project), + return ( + featureQuery.project.some((project) => + project.includes(toggle.project), + ) || featureQuery.project.includes(ALL_PROJECTS) ); } return toggle.archived === archived; diff --git a/src/lib/proxy/proxy-repository.ts b/src/lib/proxy/proxy-repository.ts index e55ec91815..e80863750b 100644 --- a/src/lib/proxy/proxy-repository.ts +++ b/src/lib/proxy/proxy-repository.ts @@ -11,7 +11,7 @@ import { mapFeaturesForClient, mapSegmentsForClient, } from '../features/playground/offline-unleash-client'; -import { ALL_ENVS, ALL_PROJECTS } from '../util/constants'; +import { ALL_ENVS } from '../util/constants'; import { UnleashEvents } from 'unleash-client'; import { Logger } from '../logger'; import ConfigurationRevisionService, { @@ -151,7 +151,7 @@ export class ProxyRepository private async featuresForToken(): Promise { return mapFeaturesForClient( await this.services.featureToggleServiceV2.getClientFeatures({ - project: await this.projectIdsForToken(), + project: this.token.projects, environment: this.environmentNameForToken(), }), ); @@ -163,15 +163,6 @@ export class ProxyRepository ); } - private async projectIdsForToken(): Promise { - if (this.token.projects.includes(ALL_PROJECTS)) { - const allProjects = await this.stores.projectStore.getAll(); - return allProjects.map((project) => project.id); - } - - return this.token.projects; - } - private environmentNameForToken(): string { if (this.token.environment === ALL_ENVS) { return 'default';