From fb9db1200268152faf6890eed14535c4300494a6 Mon Sep 17 00:00:00 2001 From: Tymoteusz Czech <2625371+Tymek@users.noreply.github.com> Date: Wed, 12 Jul 2023 17:14:55 +0200 Subject: [PATCH] Fix: variants-batch (#4222) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## About the changes Fix un-awaited promise on batch variant update - reduce function allowed TS to skip Promise type. --------- Co-authored-by: Gastón Fournier --- src/lib/services/feature-toggle-service.ts | 19 ++++++++----------- .../api/admin/project/variants.e2e.test.ts | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+), 11 deletions(-) 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';