diff --git a/src/lib/services/feature-toggle-service.ts b/src/lib/services/feature-toggle-service.ts index 65ec3fde46..d359764e60 100644 --- a/src/lib/services/feature-toggle-service.ts +++ b/src/lib/services/feature-toggle-service.ts @@ -1837,17 +1837,14 @@ class FeatureToggleService { ): Promise { await variantsArraySchema.validateAsync(newVariants); const fixedVariants = this.fixVariantWeights(newVariants); - const oldVariants: { [env: string]: IVariant[] } = environments.reduce( - async (result, environment) => { - result[environment] = await this.featureEnvironmentStore.get({ - featureName, - environment, - }); - return result; - }, - {}, - ); - + const oldVariants: { [env: string]: IVariant[] } = {}; + for (const env of environments) { + const featureEnv = await this.featureEnvironmentStore.get({ + featureName, + environment: env, + }); + oldVariants[env] = featureEnv.variants || []; + } await this.eventStore.batchStore( environments.map( (environment) => diff --git a/src/test/e2e/api/admin/project/variants.e2e.test.ts b/src/test/e2e/api/admin/project/variants.e2e.test.ts index 4327135ee5..6f7762d08e 100644 --- a/src/test/e2e/api/admin/project/variants.e2e.test.ts +++ b/src/test/e2e/api/admin/project/variants.e2e.test.ts @@ -305,6 +305,25 @@ test('Can push variants to multiple environments', async () => { }); }); +test("Returns proper error if project and/or feature toggle doesn't exist", async () => { + await app.request + .put( + `/api/admin/projects/nonexistent/features/undefined/variants-batch`, + ) + .send({ + variants: [ + { + name: 'new-variant-1', + stickiness: 'default', + weight: 500, + weightType: WeightType.VARIABLE, + }, + ], + environments: ['development', 'production'], + }) + .expect(404); +}); + test('Can add variant for a feature', async () => { const featureName = 'feature-variants-patch-add'; const variantName = 'fancy-variant-patch';