Permalink
Browse files

Implementing admin notification on new Post

  • Loading branch information...
1 parent b1c0687 commit cdde9c0a03f9958de5bf0e75615b3ed8d03aa317 @JedWatson committed Feb 2, 2014
Showing with 50 additions and 0 deletions.
  1. +49 −0 models/posts.js
  2. +1 −0 routes/views/createPost.js
View
49 models/posts.js
@@ -1,4 +1,5 @@
var keystone = require('keystone'),
+ async = require('async'),
Types = keystone.Field.Types;
/**
@@ -43,6 +44,54 @@ Post.relationship({ ref: 'PostComment', refPath: 'post', path: 'comments' });
/**
+ * Notifications
+ * =============
+ */
+
+Post.schema.methods.notifyAdmins = function(callback) {
+
+ var post = this;
+
+ // Method to send the notification email after data has been loaded
+ var sendEmail = function(err, results) {
+
+ if (err) return callback(err);
+
+ async.each(results.admins, function(admin, done) {
+
+ new keystone.Email('admin-notification-new-post').send({
+ admin: admin.name.first || admin.name.full,
+ author: results.author ? results.author.name.full : 'Somebody',
+ title: post.title,
+ keystoneURL: 'http://www.sydjs.com/keystone/post/' + post.id,
+ subject: 'New Post to SydJS'
+ }, {
+ to: admin,
+ from: {
+ name: 'SydJS',
+ email: 'contact@sydjs.com'
+ }
+ }, done);
+
+ }, callback);
+
+ }
+
+ // Query data in parallel
+ async.parallel({
+ author: function(next) {
+ if (!post.author) return next();
+ keystone.list('User').model.findById(post.author).exec(next);
+ },
+ admins: function(next) {
+ keystone.list('User').model.find().where('isAdmin', true).exec(next)
+ }
+ }, sendEmail);
+
+}
+
+
+/**
* Registration
* ============
*/
View
1 routes/views/createPost.js
@@ -34,6 +34,7 @@ exports = module.exports = function(req, res) {
if (err) {
locals.validationErrors = err.errors;
} else {
+ newPost.notifyAdmins();
req.flash('success', 'Your post has been added' + ((newPost.state == 'draft') ? ' and will appear on the site once it\'s been approved' : '') + '.');
return res.redirect('/blog/post/' + newPost.slug);
}

0 comments on commit cdde9c0

Please sign in to comment.