diff --git a/migrations/20181217140321-notifications-add-table.js b/migrations/20181217140321-notifications-add-table.js index 0c2476fe2..8c4303a71 100644 --- a/migrations/20181217140321-notifications-add-table.js +++ b/migrations/20181217140321-notifications-add-table.js @@ -2,14 +2,16 @@ exports.up = function(db, callback) { var cmd = 'CREATE TABLE notifications(' + - 'id int NOT NULL AUTO_INCREMENT,' + - 'userId VARCHAR(128) NOT NULL,' + - 'title VARCHAR(512) NOT NULL,' + - 'message TEXT,' + - 'action VARCHAR(512) NOT NULL,' + - 'acknowledged BOOLEAN DEFAULT false,' + - 'creationTime TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,' + - 'PRIMARY KEY (id))'; + 'id int NOT NULL AUTO_INCREMENT,' + + 'userId VARCHAR(128) NOT NULL,' + + 'eventId VARCHAR(128) NOT NULL,' + + 'title VARCHAR(512) NOT NULL,' + + 'message TEXT,' + + 'action VARCHAR(512) NOT NULL,' + + 'acknowledged BOOLEAN DEFAULT false,' + + 'creationTime TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,' + + 'FOREIGN KEY(eventId) REFERENCES eventlog(id),' + + 'PRIMARY KEY (id)) CHARACTER SET utf8 COLLATE utf8_bin'; db.runSql(cmd, function (error) { if (error) console.error(error); diff --git a/migrations/schema.sql b/migrations/schema.sql index 58f0d8db4..4620e89b1 100644 --- a/migrations/schema.sql +++ b/migrations/schema.sql @@ -215,12 +215,14 @@ CREATE TABLE IF NOT EXISTS tasks( CREATE TABLE IF NOT EXISTS notifications( id int NOT NULL AUTO_INCREMENT, userId VARCHAR(128) NOT NULL, + eventId VARCHAR(128) NOT NULL, title VARCHAR(512) NOT NULL, message TEXT, action VARCHAR(512) NOT NULL, acknowledged BOOLEAN DEFAULT false, creationTime TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (eventId) REFERENCES eventlog(id), PRIMARY KEY (id) ); diff --git a/src/notificationdb.js b/src/notificationdb.js index b4d012a13..908f48941 100644 --- a/src/notificationdb.js +++ b/src/notificationdb.js @@ -12,7 +12,7 @@ let assert = require('assert'), database = require('./database.js'), DatabaseError = require('./databaseerror'); -const NOTIFICATION_FIELDS = [ 'id', 'userId', 'title', 'message', 'action', 'creationTime', 'acknowledged' ]; +const NOTIFICATION_FIELDS = [ 'id', 'userId', 'eventId', 'title', 'message', 'action', 'creationTime', 'acknowledged' ]; function postProcess(result) { assert.strictEqual(typeof result, 'object'); @@ -26,8 +26,8 @@ function add(notification, callback) { assert.strictEqual(typeof notification, 'object'); assert.strictEqual(typeof callback, 'function'); - const query = 'INSERT INTO notifications (userId, title, message, action) VALUES (?, ?, ?, ?)'; - const args = [ notification.userId, notification.title, notification.message, notification.action ]; + const query = 'INSERT INTO notifications (userId, eventId, title, message, action) VALUES (?, ?, ?, ?)'; + const args = [ notification.userId, notification.eventId, notification.title, notification.message, notification.action ]; database.query(query, args, function (error, result) { if (error) return callback(new DatabaseError(DatabaseError.INTERNAL_ERROR, error));