move the switch case to notifications

this way we don't need to export all the functions
This commit is contained in:
Girish Ramakrishnan
2019-02-27 16:10:54 -08:00
parent 6a18d6918e
commit e93b95bee8
2 changed files with 39 additions and 29 deletions
+4 -21
View File
@@ -105,28 +105,11 @@ function add(action, source, data, callback) {
api(uuid.v4(), action, source, data, function (error, id) {
if (error) return callback(new EventLogError(EventLogError.INTERNAL_ERROR, error));
// decide if we want to add notifications as well
if (action === exports.ACTION_USER_ADD) {
notifications.userAdded(source.userId, id, data.user);
} else if (action === exports.ACTION_USER_REMOVE) {
notifications.userRemoved(source.userId, id, data.user);
} else if (action === exports.ACTION_USER_UPDATE && data.adminStatusChanged) {
notifications.adminChanged(source.userId, id, data.user);
} else if (action === exports.ACTION_APP_OOM) {
notifications.oomEvent(id, data.app ? data.app.id : data.containerId, { app: data.app, details: data });
} else if (action === exports.ACTION_APP_DOWN) {
notifications.appDied(id, data.app);
} else if (action === exports.ACTION_APP_UP) {
notifications.appUp(id, data.app);
} else if (action === exports.ACTION_APP_TASK_CRASH) {
notifications.apptaskCrash(id, data.appId, data.crashLogFile);
} else if (action === exports.ACTION_PROCESS_CRASH) {
notifications.processCrash(id, data.processName, data.crashLogFile);
} else {
// no notification
}
notifications.onEvent(id, action, source, data, function (error) {
if (error) return callback(new EventLogError(EventLogError.INTERNAL_ERROR, error));
callback(null, { id: id });
callback(null, { id: id });
});
});
}
+35 -8
View File
@@ -9,15 +9,9 @@ exports = module.exports = {
ack: ack,
getAllPaged: getAllPaged,
onEvent: onEvent,
// specialized notifications
userAdded: userAdded,
userRemoved: userRemoved,
adminChanged: adminChanged,
oomEvent: oomEvent,
appUp: appUp,
appDied: appDied,
processCrash: processCrash,
apptaskCrash: apptaskCrash,
backupConfigWarning: backupConfigWarning,
diskSpaceWarning: diskSpaceWarning,
mailStatusWarning: mailStatusWarning,
@@ -29,6 +23,7 @@ var assert = require('assert'),
config = require('./config.js'),
DatabaseError = require('./databaseerror.js'),
debug = require('debug')('box:notifications'),
eventlog = require('./eventlog.js'),
mailer = require('./mailer.js'),
notificationdb = require('./notificationdb.js'),
safe = require('safetydance'),
@@ -328,3 +323,35 @@ function diskSpaceWarning(message) {
if (error) console.error(error);
});
}
function onEvent(id, action, source, data, callback) {
assert.strictEqual(typeof id, 'string');
assert.strictEqual(typeof action, 'string');
assert.strictEqual(typeof source, 'object');
assert.strictEqual(typeof data, 'object');
assert.strictEqual(typeof callback, 'function');
// decide if we want to add notifications as well
if (action === eventlog.ACTION_USER_ADD) {
userAdded(source.userId, id, data.user);
} else if (action === eventlog.ACTION_USER_REMOVE) {
userRemoved(source.userId, id, data.user);
} else if (action === eventlog.ACTION_USER_UPDATE && data.adminStatusChanged) {
adminChanged(source.userId, id, data.user);
} else if (action === eventlog.ACTION_APP_OOM) {
oomEvent(id, data.app ? data.app.id : data.containerId, { app: data.app, details: data });
} else if (action === eventlog.ACTION_APP_DOWN) {
appDied(id, data.app);
} else if (action === eventlog.ACTION_APP_UP) {
appUp(id, data.app);
} else if (action === eventlog.ACTION_APP_TASK_CRASH) {
apptaskCrash(id, data.appId, data.crashLogFile);
} else if (action === eventlog.ACTION_PROCESS_CRASH) {
processCrash(id, data.processName, data.crashLogFile);
} else {
// no notification
}
callback();
}