diff --git a/src/notifications.js b/src/notifications.js index b8236a010..185a6a092 100644 --- a/src/notifications.js +++ b/src/notifications.js @@ -4,6 +4,7 @@ exports = module.exports = { NotificationsError: NotificationsError, add: add, + upsert: upsert, get: get, ack: ack, getAllPaged: getAllPaged, @@ -15,7 +16,9 @@ exports = module.exports = { oomEvent: oomEvent, appDied: appDied, processCrash: processCrash, - apptaskCrash: apptaskCrash + apptaskCrash: apptaskCrash, + backupConfigWarning: backupConfigWarning, + diskSpaceWarning: diskSpaceWarning }; var assert = require('assert'), @@ -75,6 +78,30 @@ function add(userId, eventId, title, message, action, callback) { }); } +function upsert(userId, eventId, title, message, action, callback) { + assert.strictEqual(typeof userId, 'string'); + assert.strictEqual(typeof eventId, 'string'); + assert.strictEqual(typeof title, 'string'); + assert.strictEqual(typeof message, 'string'); + assert.strictEqual(typeof action, 'string'); + assert.strictEqual(typeof callback, 'function'); + + debug('upsert: ', userId, title, action); + + notificationdb.upsert({ + userId: userId, + eventId: eventId, + title: title, + message: message, + action: action + }, 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)); + + callback(null, { id: result }); + }); +} + function get(id, callback) { assert.strictEqual(typeof id, 'string'); assert.strictEqual(typeof callback, 'function'); @@ -243,3 +270,24 @@ function apptaskCrash(eventId, appId, crashLogFile) { if (error) console.error(error); }); } + +function backupConfigWarning(message) { + assert.strictEqual(typeof message, 'string'); + + actionForAllAdmins([], function (admin, callback) { + upsert(admin.id, null, 'Backup Configuration', message, '/#/backups', callback); + }, function (error) { + if (error) console.error(error); + }); +} + +function diskSpaceWarning(message) { + assert.strictEqual(typeof message, 'string'); + + actionForAllAdmins([], function (admin, callback) { + mailer.outOfDiskSpace(admin.email, message); + upsert(admin.id, null, 'Out of Disk Space', message, '/#/graphs', callback); + }, function (error) { + if (error) console.error(error); + }); +}