I'm trying to setup email notification using node's events
module. I'm not able to make it work, as in its not doing when email
event is emitted. Not sure whats wrong. Could you please help me debug this code. Thanks in advance!
email.js
var express = require('express');
var nodemailer = require('nodemailer');
var events = require('events');
var util = require('util');
var _ = require('lodash');
var Notify = function() {
events.EventEmitter.call(this);
var transporter = nodemailer.createTransport();
this.eventNames = {
newBug: 'new bug',
updatedBug: 'updated bug',
commentedOnBug: 'commented on bug',
updatedAndCommentedOnBug: 'updated and commented on bug',
};
this.notifier = function(eventName, data) {
this.on('email', function() {
console.log('email notifier......');
})
};
this.notify = function(eventName, data) {
console.log('notify...');
this.emit('email', data);
};
this.send = function(eventName, data) {
var email = {
from: 'new_bugtrack@example.com',
to: '',
subject: '',
text: ''
};
email.to = data.assignTo.email || '';
switch (eventName) {
case this.eventNames.newBug:
email.subject = 'Created new bug ' + data.id
email.text = data.title
break;
case this.eventNames.updatedBug:
email.subject = _.last(data.changeHistory).updatedBy.name + ' updated bug-' + data.id
email.text = data._.last(data.changeHistory).change
break;
case this.eventNames.commentedOnBug:
email.subject = _.last(data.changeHistory).updatedBy.name + ' commented on bug-' + data.id
email.text = data._.last(data.changeHistory).comment
break;
case this.eventNames.updatedAndCommentedOnBug:
email.subject = _.last(data.changeHistory).updatedBy.name + 'updated and commented on bug-' + data.id
email.text = _.last(data.changeHistory).change + '-----' + _.last(data.changeHistory).comment
break;
default:
break;
}
if (process.env.NODE_ENV === "development" || process.env.NODE_ENV === "test") {
email.to = 'example@example.com';
}
console.log('email emit activated');
transporter.sendMail(email);
};
};
util.inherits(Notify, events.EventEmitter);
// dont't send emails in dev/test env
if (process.env.NODE_ENV === "development" || process.env.NODE_ENV === "test") {
Notify.send = function(eventName, data) {
console.log('emailed data', JSON.stringify(data, null, 2));
};
}
module.exports.Notify = Notify;
bug.controller.js
var marklogic = require('marklogic');
var conn = require('../../config/db-config.js').connection;
var db = marklogic.createDatabaseClient(conn);
var Notify = require('../../email/email').Notify;
var email = new Notify();
console.log('notify', email);
// just a simple function for demo
exports.count = function(req, res) {
db.documents.query(
q.where(
q.collection('bugs')
)
.slice(1, 1)
.withOptions({
debug: true,
categories: 'metadata'
})
).result(function(response) {
email.notify(email.eventNames.newBug, {a: 2})
res.status(200).json({
count: response[0].total
});
});
};
...
...
...
I see the log statement notify
being printed from notify()
function but not email notifier
which is hooked to email
event