-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_handler.js
More file actions
69 lines (53 loc) · 1.37 KB
/
db_handler.js
File metadata and controls
69 lines (53 loc) · 1.37 KB
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
var MongoClient = require('mongodb').MongoClient;
var assert = require('assert');
var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport('smtps://robfz729%40gmail.com:pass@smtp.gmail.com');
var url = 'mongodb://localhost:27017/arqui';
var collection_name = 'email_sender_test';
var send_email = function(doc, dest) {
var mailOptions = {
from: 'Tasky',
to: dest,
subject: 'Remainder',
text: 'Remainder',
html: '<b>Remainder</b>'
};
transporter.sendMail(mailOptions, function(error, info){
if(error){
return console.log(error);
}
console.log('Message sent: ' + info.response);
});
}
var need_send = function(doc) {
// Tell if notification needs to be sent
return true;
}
var get_user = function(doc) {
// Get the task's owner email
return 'roberto.fierros.zepeda@live.com';
}
var send_notifications = function(db, callback) {
var cursor = db.collection(collection_name).find();
cursor.each(function(err, doc) {
assert.equal(err, null);
if (doc != null) {
if (need_send(doc)) {
send_email(doc, get_user(doc));
}
} else {
callback();
}
});
}
var service = function() {
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
send_notifications(db, function() {
db.close();
});
});
}
setInterval(function() {
service();
}, 5000);