1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-25 00:07:47 +01:00

feat/return feature on create (#742)

* feat: return feature on create

* refactor: change test to verify returning object

* fix: return undefined instead of null
This commit is contained in:
Fredrik Strand Oseberg 2021-03-04 13:28:31 +01:00 committed by GitHub
parent ec3d6e95bc
commit 8e0bae7352
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 8 deletions

View File

@ -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) {

View File

@ -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);
}

View File

@ -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) {

View File

@ -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 => {