diff --git a/src/lib/db/event-store.js b/src/lib/db/event-store.js index 67ca664941..1697c56bed 100644 --- a/src/lib/db/event-store.js +++ b/src/lib/db/event-store.js @@ -24,12 +24,7 @@ class EventStore extends EventEmitter { async store(event) { try { const rows = await this.db(TABLE) - .insert({ - type: event.type, - created_by: event.createdBy, // eslint-disable-line - data: event.data, - tags: event.tags ? JSON.stringify(event.tags) : [], - }) + .insert(this.eventToDbRow(event)) .returning(EVENT_COLUMNS); const savedEvent = this.rowToEvent(rows[0]); process.nextTick(() => this.emit(event.type, savedEvent)); @@ -104,7 +99,7 @@ class EventStore extends EventEmitter { type: e.type, created_by: e.createdBy, data: e.data, - tags: e.tags ? JSON.stringify(e.tags) : [], + tags: JSON.stringify(e.tags || []), }; } } diff --git a/src/test/e2e/stores/event-store.e2e.test.js b/src/test/e2e/stores/event-store.e2e.test.js index b1ec525251..2765dfc60d 100644 --- a/src/test/e2e/stores/event-store.e2e.test.js +++ b/src/test/e2e/stores/event-store.e2e.test.js @@ -2,7 +2,10 @@ const test = require('ava'); const sinon = require('sinon'); -const { APPLICATION_CREATED } = require('../../../lib/event-type'); +const { + APPLICATION_CREATED, + FEATURE_CREATED, +} = require('../../../lib/event-type'); const dbInit = require('../helpers/database-init'); const getLogger = require('../../fixtures/no-logger'); @@ -40,6 +43,32 @@ test.serial('Should include id and createdAt when saving', async t => { t.is(seen[0].data.appName, event1.data.appName); }); +test.serial('Should include empty tags array for new event', async t => { + t.plan(2); + const event = { + type: FEATURE_CREATED, + createdBy: 'me@mail.com', + data: { + name: 'someName', + enabled: true, + strategies: [{ name: 'default' }], + }, + }; + + const promise = new Promise(resolve => { + eventStore.on(FEATURE_CREATED, storedEvent => { + t.is(storedEvent.name, event.name); + t.true(Array.isArray(storedEvent.tags), 'tags should be an array'); + resolve(); + }); + }); + + // Trigger + await eventStore.store(event); + + return promise; +}); + test.serial('Should be able to store multiple events at once', async t => { const clock = sinon.useFakeTimers(); const event1 = {