From 8e0bae735217abc5d0a34ac0555484289cda43bf Mon Sep 17 00:00:00 2001 From: Fredrik Strand Oseberg Date: Thu, 4 Mar 2021 13:28:31 +0100 Subject: [PATCH] feat/return feature on create (#742) * feat: return feature on create * refactor: change test to verify returning object * fix: return undefined instead of null --- src/lib/db/feature-toggle-store.js | 7 ++++++- src/lib/routes/admin-api/feature.js | 7 +++++-- src/lib/services/feature-toggle-service.js | 9 ++++++--- src/test/e2e/api/admin/feature.e2e.test.js | 9 +++++++-- 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/lib/db/feature-toggle-store.js b/src/lib/db/feature-toggle-store.js index 82717807a5..19f491bc2a 100644 --- a/src/lib/db/feature-toggle-store.js +++ b/src/lib/db/feature-toggle-store.js @@ -169,10 +169,15 @@ class FeatureToggleStore { async createFeature(data) { try { - await this.db(TABLE).insert(this.eventDataToRow(data)); + const row = await this.db(TABLE) + .insert(this.eventDataToRow(data)) + .returning(FEATURE_COLUMNS); + + return this.rowToFeature(row[0]); } catch (err) { this.logger.error('Could not insert feature, error: ', err); } + return undefined; } async updateFeature(data) { diff --git a/src/lib/routes/admin-api/feature.js b/src/lib/routes/admin-api/feature.js index 2ef28e326d..534a433af5 100644 --- a/src/lib/routes/admin-api/feature.js +++ b/src/lib/routes/admin-api/feature.js @@ -126,8 +126,11 @@ class FeatureController extends Controller { const userName = extractUser(req); try { - await this.featureService.createFeatureToggle(req.body, userName); - res.status(201).end(); + const createdFeature = await this.featureService.createFeatureToggle( + req.body, + userName, + ); + res.status(201).json(createdFeature); } catch (error) { handleErrors(res, this.logger, error); } diff --git a/src/lib/services/feature-toggle-service.js b/src/lib/services/feature-toggle-service.js index 3f92088ed9..ed06c65027 100644 --- a/src/lib/services/feature-toggle-service.js +++ b/src/lib/services/feature-toggle-service.js @@ -64,13 +64,16 @@ class FeatureToggleService { async createFeatureToggle(value, userName) { await this.validateName(value); - const feature = await featureSchema.validateAsync(value); - await this.featureToggleStore.createFeature(feature); + const featureData = await featureSchema.validateAsync(value); + const feature = await this.featureToggleStore.createFeature( + featureData, + ); await this.eventStore.store({ type: FEATURE_CREATED, createdBy: userName, - data: feature, + data: featureData, }); + return feature; } async updateToggle(updatedFeature, userName) { diff --git a/src/test/e2e/api/admin/feature.e2e.test.js b/src/test/e2e/api/admin/feature.e2e.test.js index 4feca70ab4..1e6a1cbb4f 100644 --- a/src/test/e2e/api/admin/feature.e2e.test.js +++ b/src/test/e2e/api/admin/feature.e2e.test.js @@ -47,7 +47,7 @@ test.serial('cant get feature that dose not exist', async t => { }); test.serial('creates new feature toggle', async t => { - t.plan(0); + t.plan(3); const request = await setupApp(stores); return request .post('/api/admin/features') @@ -57,7 +57,12 @@ test.serial('creates new feature toggle', async t => { strategies: [{ name: 'default' }], }) .set('Content-Type', 'application/json') - .expect(201); + .expect(201) + .expect(res => { + t.is(res.body.name, 'com.test.feature'); + t.is(res.body.enabled, false); + t.truthy(res.body.createdAt); + }); }); test.serial('creates new feature toggle with variants', async t => {