2018-12-17 16:37:19 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
exports = module.exports = {
|
|
|
|
|
NotificationsError: NotificationsError,
|
|
|
|
|
|
|
|
|
|
add: add,
|
|
|
|
|
get: get,
|
|
|
|
|
ack: ack,
|
2019-01-04 17:13:52 +01:00
|
|
|
getAllPaged: getAllPaged,
|
2018-12-17 17:35:19 +01:00
|
|
|
|
|
|
|
|
// specialized notifications
|
|
|
|
|
userAdded: userAdded
|
2018-12-17 16:37:19 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var assert = require('assert'),
|
2018-12-17 17:35:19 +01:00
|
|
|
async = require('async'),
|
2018-12-17 16:37:19 +01:00
|
|
|
DatabaseError = require('./databaseerror.js'),
|
|
|
|
|
debug = require('debug')('box:notifications'),
|
2018-12-17 17:35:19 +01:00
|
|
|
mailer = require('./mailer.js'),
|
2018-12-17 16:37:19 +01:00
|
|
|
notificationdb = require('./notificationdb.js'),
|
2018-12-17 17:35:19 +01:00
|
|
|
users = require('./users.js'),
|
2018-12-17 16:37:19 +01:00
|
|
|
util = require('util');
|
|
|
|
|
|
2018-12-17 17:35:19 +01:00
|
|
|
const NOOP_CALLBACK = function (error) { if (error) debug(error); };
|
|
|
|
|
|
2018-12-17 16:37:19 +01:00
|
|
|
function NotificationsError(reason, errorOrMessage) {
|
|
|
|
|
assert.strictEqual(typeof reason, 'string');
|
|
|
|
|
assert(errorOrMessage instanceof Error || typeof errorOrMessage === 'string' || typeof errorOrMessage === 'undefined');
|
|
|
|
|
|
|
|
|
|
Error.call(this);
|
|
|
|
|
Error.captureStackTrace(this, this.constructor);
|
|
|
|
|
|
|
|
|
|
this.name = this.constructor.name;
|
|
|
|
|
this.reason = reason;
|
|
|
|
|
if (typeof errorOrMessage === 'undefined') {
|
|
|
|
|
this.message = reason;
|
|
|
|
|
} else if (typeof errorOrMessage === 'string') {
|
|
|
|
|
this.message = errorOrMessage;
|
|
|
|
|
} else {
|
|
|
|
|
this.message = 'Internal error';
|
|
|
|
|
this.nestedError = errorOrMessage;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
util.inherits(NotificationsError, Error);
|
|
|
|
|
NotificationsError.INTERNAL_ERROR = 'Internal Error';
|
|
|
|
|
NotificationsError.NOT_FOUND = 'Not Found';
|
|
|
|
|
|
2018-12-17 17:35:19 +01:00
|
|
|
function add(userId, title, message, action, callback) {
|
2018-12-17 16:37:19 +01:00
|
|
|
assert.strictEqual(typeof userId, 'string');
|
|
|
|
|
assert.strictEqual(typeof title, 'string');
|
|
|
|
|
assert.strictEqual(typeof message, 'string');
|
|
|
|
|
assert.strictEqual(typeof action, 'string');
|
2018-12-17 17:40:53 +01:00
|
|
|
assert.strictEqual(typeof callback, 'function');
|
2018-12-17 16:37:19 +01:00
|
|
|
|
2018-12-17 17:35:19 +01:00
|
|
|
debug('add: ', userId, title, action);
|
2018-12-17 16:37:19 +01:00
|
|
|
|
|
|
|
|
notificationdb.add({
|
|
|
|
|
userId: userId,
|
|
|
|
|
title: title,
|
|
|
|
|
message: message,
|
|
|
|
|
action: action
|
|
|
|
|
}, function (error, result) {
|
|
|
|
|
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');
|
|
|
|
|
|
|
|
|
|
debug('get: ', id);
|
|
|
|
|
|
|
|
|
|
notificationdb.get(id, function (error, result) {
|
|
|
|
|
if (error && error.reason === DatabaseError.NOT_FOUND) return callback(new NotificationsError(NotificationsError.NOT_FOUND));
|
|
|
|
|
if (error) return callback(new NotificationsError(NotificationsError.INTERNAL_ERROR, error));
|
|
|
|
|
|
|
|
|
|
callback(null, result);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function ack(id, callback) {
|
|
|
|
|
assert.strictEqual(typeof id, 'string');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
debug('ack: ', id);
|
|
|
|
|
|
|
|
|
|
notificationdb.update(id, { acknowledged: true }, function (error) {
|
|
|
|
|
if (error && error.reason === DatabaseError.NOT_FOUND) return callback(new NotificationsError(NotificationsError.NOT_FOUND));
|
|
|
|
|
if (error) return callback(new NotificationsError(NotificationsError.INTERNAL_ERROR, error));
|
|
|
|
|
|
|
|
|
|
callback(null);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if acknowledged === null we return all, otherwise yes or no based on acknowledged as a boolean
|
2019-01-04 17:13:52 +01:00
|
|
|
function getAllPaged(userId, acknowledged, page, perPage, callback) {
|
2018-12-17 16:37:19 +01:00
|
|
|
assert.strictEqual(typeof userId, 'string');
|
|
|
|
|
assert(acknowledged === null || typeof acknowledged === 'boolean');
|
|
|
|
|
assert.strictEqual(typeof page, 'number');
|
|
|
|
|
assert.strictEqual(typeof perPage, 'number');
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
notificationdb.listByUserIdPaged(userId, page, perPage, function (error, result) {
|
|
|
|
|
if (error) return callback(new NotificationsError(NotificationsError.INTERNAL_ERROR, error));
|
|
|
|
|
|
|
|
|
|
if (acknowledged === null) return callback(null, result);
|
|
|
|
|
|
|
|
|
|
callback(null, result.filter(function (r) { return r.acknowledged === acknowledged; }));
|
|
|
|
|
});
|
|
|
|
|
}
|
2018-12-17 17:35:19 +01:00
|
|
|
|
|
|
|
|
function userAdded(user, callback) {
|
|
|
|
|
assert.strictEqual(typeof user, 'object');
|
|
|
|
|
assert(typeof callback === 'undefined' || typeof callback === 'function');
|
|
|
|
|
|
|
|
|
|
callback = callback || NOOP_CALLBACK;
|
|
|
|
|
|
|
|
|
|
users.getAllAdmins(function (error, result) {
|
|
|
|
|
if (error) return callback(new NotificationsError(NotificationsError.INTERNAL_ERROR, error));
|
|
|
|
|
|
|
|
|
|
async.each(result, function (admin, callback) {
|
|
|
|
|
mailer.userAdded(admin.email, user);
|
|
|
|
|
add(admin.id, 'User added', `User ${user.fallbackEmail} was added`, '', callback);
|
|
|
|
|
}, callback);
|
|
|
|
|
});
|
|
|
|
|
}
|