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 978a96741b82..6adc16ebbf39 100644 --- a/src/lib/features/feature-toggle/feature-toggle-strategies-store.ts +++ b/src/lib/features/feature-toggle/feature-toggle-strategies-store.ts @@ -343,11 +343,17 @@ class FeatureStrategiesStore implements IFeatureStrategiesStore { let selectColumns = ['features_view.*'] as (string | Raw)[]; if (this.flagResolver.isEnabled('useLastSeenRefactor')) { - query.leftJoin( - 'last_seen_at_metrics', - 'last_seen_at_metrics.environment', - 'features_view.environment_name', - ); + query.leftJoin('last_seen_at_metrics', function () { + this.on( + 'last_seen_at_metrics.environment', + '=', + 'features_view.environment_name', + ).andOn( + 'last_seen_at_metrics.feature_name', + '=', + 'features_view.name', + ); + }); // Override feature view for now selectColumns.push( 'last_seen_at_metrics.last_seen_at as env_last_seen_at', diff --git a/src/lib/features/feature-toggle/tests/feature-toggle-last-seen-at.e2e.test.ts b/src/lib/features/feature-toggle/tests/feature-toggle-last-seen-at.e2e.test.ts index 3ff5c7adb12f..3180e395d21e 100644 --- a/src/lib/features/feature-toggle/tests/feature-toggle-last-seen-at.e2e.test.ts +++ b/src/lib/features/feature-toggle/tests/feature-toggle-last-seen-at.e2e.test.ts @@ -154,3 +154,53 @@ test('response should include last seen at per environment for multiple environm expect(production.name).toBe('production'); expect(production.lastSeenAt).toEqual('2023-10-01T12:34:56.000Z'); }); + +test('response should include last seen at per environment correctly for a single toggle /api/admin/project/:projectId/features/:featureName', async () => { + const featureName = 'multiple-environment-last-seen-at-single-toggle'; + await app.createFeature(featureName); + await setupLastSeenAtTest(`${featureName}1`); + await setupLastSeenAtTest(`${featureName}2`); + await setupLastSeenAtTest(`${featureName}3`); + await setupLastSeenAtTest(`${featureName}4`); + await setupLastSeenAtTest(`${featureName}5`); + + await insertLastSeenAt( + featureName, + db.rawDatabase, + 'default', + '2023-08-01 12:30:56', + ); + + await insertLastSeenAt( + featureName, + db.rawDatabase, + 'development', + '2023-08-01 12:30:56', + ); + + await insertLastSeenAt( + featureName, + db.rawDatabase, + 'production', + '2023-08-01 12:30:56', + ); + + const { body } = await app.request + .get(`/api/admin/projects/default/features/${featureName}`) + .expect(200); + + expect(body.environments).toMatchObject([ + { + name: 'default', + lastSeenAt: '2023-08-01T12:30:56.000Z', + }, + { + name: 'development', + lastSeenAt: '2023-08-01T12:30:56.000Z', + }, + { + name: 'production', + lastSeenAt: '2023-08-01T12:30:56.000Z', + }, + ]); +}); diff --git a/src/test/e2e/helpers/test-helper.ts b/src/test/e2e/helpers/test-helper.ts index 23d4d09b274e..55295d8f19ae 100644 --- a/src/test/e2e/helpers/test-helper.ts +++ b/src/test/e2e/helpers/test-helper.ts @@ -346,10 +346,15 @@ export const insertLastSeenAt = async ( environment: string = 'default', date: string = '2023-10-01 12:34:56', ): Promise => { - await db.raw(`INSERT INTO last_seen_at_metrics (feature_name, environment, last_seen_at) + try { + await db.raw(`INSERT INTO last_seen_at_metrics (feature_name, environment, last_seen_at) VALUES ('${featureName}', '${environment}', '${date}');`); - return date; + return date; + } catch (err) { + console.log(err); + return Promise.resolve(''); + } }; export const insertFeatureEnvironmentsLastSeen = async (