From 8c2a052a683a2a09ea113e67c59804ab1591a5c2 Mon Sep 17 00:00:00 2001 From: Jaanus Sellin Date: Thu, 9 Nov 2023 10:58:34 +0200 Subject: [PATCH] fix: project overview and search returning just single tag (#5305) --- .../feature-search/feature.search.e2e.test.ts | 20 +++++++++++++ .../feature-toggle-strategies-store.ts | 30 +++++++++---------- 2 files changed, 34 insertions(+), 16 deletions(-) diff --git a/src/lib/features/feature-search/feature.search.e2e.test.ts b/src/lib/features/feature-search/feature.search.e2e.test.ts index 6388c23c66..a638b2c98d 100644 --- a/src/lib/features/feature-search/feature.search.e2e.test.ts +++ b/src/lib/features/feature-search/feature.search.e2e.test.ts @@ -228,6 +228,26 @@ test('should search matching features by tag', async () => { }); }); +test('should return all feature tags', async () => { + await app.createFeature('my_feature_a'); + await app.addTag('my_feature_a', { type: 'simple', value: 'my_tag' }); + await app.addTag('my_feature_a', { type: 'simple', value: 'second_tag' }); + + const { body } = await searchFeatures({}); + + expect(body).toMatchObject({ + features: [ + { + name: 'my_feature_a', + tags: [ + { type: 'simple', value: 'my_tag' }, + { type: 'simple', value: 'second_tag' }, + ], + }, + ], + }); +}); + test('should return empty features', async () => { const { body } = await searchFeatures({ query: '' }); expect(body).toMatchObject({ features: [] }); diff --git a/src/lib/features/feature-toggle/feature-toggle-strategies-store.ts b/src/lib/features/feature-toggle/feature-toggle-strategies-store.ts index 0aa473441e..be3902ea71 100644 --- a/src/lib/features/feature-toggle/feature-toggle-strategies-store.ts +++ b/src/lib/features/feature-toggle/feature-toggle-strategies-store.ts @@ -105,18 +105,6 @@ function mapInput(input: IFeatureStrategy): IFeatureStrategiesTable { }; } -const getUniqueRows = (rows: any[]) => { - const seen = {}; - return rows.filter((row) => { - const key = `${row.environment}-${row.feature_name}`; - if (seen[key]) { - return false; - } - seen[key] = true; - return true; - }); -}; - const sortEnvironments = (overview: IFeatureOverview) => { return Object.values(overview).map((data: IFeatureOverview) => ({ ...data, @@ -733,7 +721,7 @@ class FeatureStrategiesStore implements IFeatureStrategiesStore { const rows = await query; if (rows.length > 0) { - const overview = this.getFeatureOverviewData(getUniqueRows(rows)); + const overview = this.getFeatureOverviewData(rows); const features = sortEnvironments(overview); return { features, @@ -855,7 +843,7 @@ class FeatureStrategiesStore implements IFeatureStrategiesStore { query = query.select(selectColumns); const rows = await query; if (rows.length > 0) { - const overview = this.getFeatureOverviewData(getUniqueRows(rows)); + const overview = this.getFeatureOverviewData(rows); return sortEnvironments(overview); } return []; @@ -864,9 +852,18 @@ class FeatureStrategiesStore implements IFeatureStrategiesStore { getFeatureOverviewData(rows): IFeatureOverview { return rows.reduce((acc, row) => { if (acc[row.feature_name] !== undefined) { - acc[row.feature_name].environments.push( - FeatureStrategiesStore.getEnvironment(row), + const environmentExists = acc[ + row.feature_name + ].environments.some( + (existingEnvironment) => + existingEnvironment.name === row.environment, ); + if (!environmentExists) { + acc[row.feature_name].environments.push( + FeatureStrategiesStore.getEnvironment(row), + ); + } + if (this.isNewTag(acc[row.feature_name], row)) { this.addTag(acc[row.feature_name], row); } @@ -882,6 +879,7 @@ class FeatureStrategiesStore implements IFeatureStrategiesStore { impressionData: row.impression_data, environments: [FeatureStrategiesStore.getEnvironment(row)], }; + if (this.isNewTag(acc[row.feature_name], row)) { this.addTag(acc[row.feature_name], row); }