Skip to content

Instantly share code, notes, and snippets.

@MichaelBailly
Created September 17, 2014 16:37
Show Gist options
  • Save MichaelBailly/11fd757246ec58cbe6de to your computer and use it in GitHub Desktop.
Save MichaelBailly/11fd757246ec58cbe6de to your computer and use it in GitHub Desktop.
module.exports.save = function(notification, callback) {
function sendCallback(err, children, parent) {
if ( parent ) {
topic.publish(parent);
}
callback(err,children);
}
if (!notification) {
return callback(new Error('Notification can not be null'));
}
this.saveOne(notification, null, function(err, parent) {
if (err) {
return sendCallback(err);
}
if (notification.target.length === 1) {
return sendCallback(err, [parent], parent);
}
var result = [parent];
async.eachLimit(notification.target, 10, function(target, callback) {
var n = {
title: notification.title,
author: notification.author,
action: notification.action,
object: notification.object,
link: notification.link,
level: notification.level,
timestamps: parent.timestamps,
target: [target],
data: notification.data || {}
};
return saveOne(n, parent, function(err, _n) {
if (_n) {
result.push(_n);
}
// never fails
return sendCallback();
});
}, function(err) {
if (err) {
console.log('Fail to save notification');
}
return sendCallback(err, result, parent);
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment