diff --git a/src/notificationdb.js b/src/notificationdb.js index eb3e640e3..d442b5edc 100644 --- a/src/notificationdb.js +++ b/src/notificationdb.js @@ -2,8 +2,8 @@ exports = module.exports = { get: get, + getByUserIdAndTitle: getByUserIdAndTitle, add: add, - upsert: upsert, update: update, del: del, listByUserIdPaged: listByUserIdPaged @@ -38,24 +38,18 @@ function add(notification, callback) { }); } -function upsert(notification, callback) { - assert.strictEqual(typeof notification, 'object'); +function getByUserIdAndTitle(userId, title, callback) { + assert.strictEqual(typeof userId, 'string'); + assert.strictEqual(typeof title, 'string'); assert.strictEqual(typeof callback, 'function'); - database.query('SELECT * from notifications WHERE userId = ? AND title = ?', [ notification.userId, notification.title ], function (error, result) { + database.query('SELECT ' + NOTIFICATION_FIELDS + ' from notifications WHERE userId = ? AND title = ? ORDER BY creationTime LIMIT 1', [ userId, title ], function (error, results) { if (error) return callback(new DatabaseError(DatabaseError.INTERNAL_ERROR, error)); - if (result.length === 0) return add(notification, callback); + if (results.length === 0) return callback(new DatabaseError(DatabaseError.NOT_FOUND)); - postProcess(result[0]); + postProcess(results[0]); - var data = { - acknowledged: notification.acknowledged, - eventId: notification.eventId, - message: notification.message, - creationTime: new Date() - }; - - update(result[0].id, data, callback); + callback(null, results[0]); }); } @@ -76,7 +70,7 @@ function update(id, data, callback) { if (error) return callback(new DatabaseError(DatabaseError.INTERNAL_ERROR, error)); if (result.affectedRows !== 1) return callback(new DatabaseError(DatabaseError.NOT_FOUND)); - return callback(null, id); + callback(null); }); } diff --git a/src/notifications.js b/src/notifications.js index 7c5e8e553..bb921112c 100644 --- a/src/notifications.js +++ b/src/notifications.js @@ -295,19 +295,29 @@ function upsert(userId, eventId, title, message, callback) { assert.strictEqual(typeof message, 'string'); assert.strictEqual(typeof callback, 'function'); - debug(`upsert: userId=${userId} title=${title} message=${message}`); + const acknowledged = !message; - notificationdb.upsert({ + const data = { userId: userId, eventId: eventId, title: title, message: message, - acknowledged: !message - }, function (error, result) { - if (error && error.reason === DatabaseError.NOT_FOUND) return callback(new NotificationsError(NotificationsError.NOT_FOUND, error.message)); - if (error) return callback(new NotificationsError(NotificationsError.INTERNAL_ERROR, error)); + acknowledged: acknowledged + }; - callback(null, { id: result }); + notificationdb.getByUserIdAndTitle(userId, title, function (error, result) { + if (error && error.reason !== DatabaseError.NOT_FOUND) return callback(new NotificationsError(NotificationsError.INTERNAL_ERROR, error)); + + if (!result && acknowledged) return callback(); // do not add acked alerts + + let updateFunc = !result ? notificationdb.add.bind(null, data) : notificationdb.update.bind(null, result.id, data); + + updateFunc(function (error) { + if (error && error.reason === DatabaseError.NOT_FOUND) return callback(new NotificationsError(NotificationsError.NOT_FOUND, error.message)); + if (error) return callback(new NotificationsError(NotificationsError.INTERNAL_ERROR, error)); + + callback(null); + }); }); }