diff --git a/src/notificationdb.js b/src/notificationdb.js index 94d01e56f..5904f53f8 100644 --- a/src/notificationdb.js +++ b/src/notificationdb.js @@ -3,6 +3,7 @@ exports = module.exports = { get: get, add: add, + upsert: upsert, update: update, del: del, listByUserIdPaged: listByUserIdPaged @@ -37,6 +38,30 @@ function add(notification, callback) { }); } +// will clear the ack flag +// matches by userId and title +function upsert(notification, callback) { + assert.strictEqual(typeof notification, 'object'); + assert.strictEqual(typeof callback, 'function'); + + database.query('SELECT * from notifications WHERE userId = ? AND title = ?', [ notification.userId, notification.title ], function (error, result) { + if (error) return callback(new DatabaseError(DatabaseError.INTERNAL_ERROR, error)); + if (result.length === 0) return add(notification, callback); + + postProcess(result[0]); + + var data = { + acknowledged: false, + eventId: notification.eventId, + message: notification.message, + action: notification.action, + creationTime: new Date() + }; + + update(result[0].id, data, callback); + }); +} + function update(id, data, callback) { assert.strictEqual(typeof id, 'string'); assert.strictEqual(typeof data, 'object'); @@ -54,7 +79,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); + return callback(null, id); }); } diff --git a/src/test/database-test.js b/src/test/database-test.js index 9455c6715..0ac060118 100644 --- a/src/test/database-test.js +++ b/src/test/database-test.js @@ -256,10 +256,10 @@ describe('database', function () { }); it('update succeeds', function (done) { - notificationdb.update(NOTIFICATION_1.id, { acknowledged: true }, function (error) { + notificationdb.update(NOTIFICATION_1.id, { acknowledged: true }, function (error, result) { expect(error).to.equal(null); - notificationdb.get(NOTIFICATION_1.id, function (error, result) { + notificationdb.get(result, function (error, result) { expect(error).to.equal(null); expect(result.id).to.equal(NOTIFICATION_1.id); expect(result.title).to.equal(NOTIFICATION_1.title); @@ -296,12 +296,49 @@ describe('database', function () { }); it('can add notification without eventId', function (done) { - notificationdb.add(NOTIFICATION_3, function (error) { + notificationdb.add(NOTIFICATION_3, function (error, result) { expect(error).to.equal(null); + expect(result).to.be.a('string'); + + // stash for further use + NOTIFICATION_3.id = result; done(); }); }); + + it('can upsert acked notification without eventId', function (done) { + notificationdb.update(NOTIFICATION_3.id, { acknowledged: true }, function (error, result) { + expect(error).to.equal(null); + expect(result).to.be.a('string'); + + // check message also gets updated + NOTIFICATION_3.message = 'new message'; + + notificationdb.get(NOTIFICATION_3.id, function (error, result) { + expect(error).to.equal(null); + + var oldCreationTime = result.creationTime; + + // wait to verify the creationTime update + setTimeout(function () { + notificationdb.upsert(NOTIFICATION_3, function (error, result) { + expect(error).to.equal(null); + expect(result).to.equal(NOTIFICATION_3.id); + + notificationdb.get(NOTIFICATION_3.id, function (error, result) { + expect(error).to.equal(null); + expect(result.acknowledged).to.equal(false); + expect(result.message).to.equal(NOTIFICATION_3.message); + expect(result.creationTime).to.be.greaterThan(oldCreationTime); + + done(); + }); + }); + }, 1000); + }); + }); + }); }); describe('domains', function () {