-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
212 lines (168 loc) · 4.81 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
"use strict";
var util = require("util");
var EventEmitter = require("events").EventEmitter;
var nodemailer = require('nodemailer');
var smtpPool = require('nodemailer-smtp-pool');
var mongoose = require('mongoose');
var EmailOutbox = require('./models/mailerOutbox');
var EmailSent = require('./models/mailerSent');
var async = require('async');
var _ = require('lodash');
var Mailer = function(databaseConfig, transportConfig) {
var self = this;
var transporter;
var addedMails = [];
var localEmitter = new EventEmitter();
var shouldCloseDatabase = false;
function Mailer() {
EventEmitter.call(this);
};
var noop = function() {};
var wireEvents = function() {
// If the Node process ends, close the Mongoose connection
process.on('SIGINT', function() {
mongoose.connection.close(function() {
console.log('Mongoose default connection disconnected through app termination', 'error');
process.exit(0);
});
});
mongoose.connection.on('open', function(ref) {
console.log('Succeeded connected to: ' + databaseConfig);
});
mongoose.connection.on('error', function(err) {
console.log('ERROR connecting to: %s %s', databaseConfig, err);
});
// When the connection is disconnected
mongoose.connection.on('disconnected', function() {
console.log('Mongoose default connection disconnected');
});
};
var init = function() {
if (!databaseConfig) {
throw new Error("database configuration is missing !");
}
// connect to DB
mongoose.connect(databaseConfig);
// initialize transporter
if (transportConfig) {
var mailConfig = transportConfig.config;
if (transportConfig.transportType === 'smtp') {
transporter = nodemailer.createTransport(smtpPool(mailConfig));
} else {
transporter = nodemailer.createTransport(mailConfig);
}
}
// trigger onInit
self.emit('onInit');
};
var getConfig = function() {
var config = {
mailerConfig: transportConfig,
dbConnectionString: databaseConfig
};
return config;
};
var prepareMail = function(mailData, callback) {
// Make sure a callback is defined.
callback = (callback || noop);
if (mailData) {
addedMails.push(mailData);
}
callback(addedMails);
// Return this object reference to allow for method chaining.
return (this);
};
var getPreparedMails = function() {
return addedMails;
};
var savePreparedMails = function(callback) {
if (addedMails.length) {
EmailOutbox.collection.insert(addedMails, function(err) {
if (err) {
callback(err);
}
// clear array
addedMails.length = 0;
callback(null);
});
} else {
callback(null);
}
return (this);
};
var sendMails = function(callback) {
callback = (callback || noop);
var counter = 0;
var sentMails = [];
var successIDMails = [];
var falseIDMails = [];
var sendCounter = 0;
async.waterfall([
function(callback) {
EmailOutbox.find({}, function(err, emails) {
if (err) return callback(err);
callback(null, emails);
});
},
function(emails, callback) {
_.each(emails, function(email) {
if (transporter) {
transporter.sendMail(email, function(err, info) {
sendCounter++;
console.log(info.response);
callback(null, email);
});
}
});
},
function(email, callback) {
var sentMail = new EmailSent();
sentMail.from = email.from;
sentMail.to = email.to;
sentMail.subject = email.subject;
sentMail.text = email.text;
sentMail.html = email.html;
sentMail.status = true;
sentMail.attachments = email.attachments;
sentMail.sent = new Date();
sentMail.save(function(err) {
callback(null, email);
});
},
function(outboxEmail, callback) {
var id = outboxEmail._id;
EmailOutbox.findByIdAndRemove(id, function(err, user) {
if (err) {
console.log(err);
return callback(err);
}
callback();
});
}
], function(err) {
if (err) return callback(err);
self.emit('onCompleted');
// call the caller
callback();
});
};
var close = function(callback) {
mongoose.connection.close(function() {
process.exit(0);
callback();
});
};
wireEvents();
init();
return {
getConfig: getConfig,
prepareMail: prepareMail,
getPreparedMails: getPreparedMails,
saveMails: savePreparedMails,
send: sendMails,
close: close
}
};
// extend the EventEmitter class
util.inherits(Mailer, EventEmitter);
module.exports = Mailer;