-
Notifications
You must be signed in to change notification settings - Fork 1
/
notificationScheduler.js
60 lines (57 loc) · 2.32 KB
/
notificationScheduler.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
const schedule = require('node-schedule');
const { transporter } = require('./email-config');
const { GMAIL_USERNAME } = require('./config');
const User = require('./models/user');
const Notification = require('./models/notifications');
const notificationScheduler = {
add: function(id, scheduledDateTime, display, emailNotification, title, body, receiverEmail, hostEmail = GMAIL_USERNAME, smtpClient = transporter) {
const that = this;
schedule.scheduleJob(id, scheduledDateTime, async function(){
if (emailNotification) {
//Schedule email delivery
await smtpClient.sendMail({
from: hostEmail,
to: receiverEmail,
subject: title,
html: body
}, function (err, info) {
if(err)
console.log(err)
else
console.log(`Email notification for job id: ${id} sent - ${info.messageId}`);
});
}
const { getSocketIO } = require('./app');
const io = getSocketIO();
await User.findOne({'local.email': receiverEmail}).then((user) => {
if (io.sockets.connected[user.socketId]) {
io.sockets.connected[user.socketId]
.emit('notification', {title: title, body: body, display: display});
}
})
console.log(`Scheduled job id: ${id} complete`);
await Notification.findByIdAndRemove(id).then(function(){
that.remove(id);
console.log(`Removed scheduled job id: ${id}`);
})
});
console.log(`Added schedule: ${id}`);
},
addAll: function(notifications){
for (let notification of notifications){
this.add(
notification.id, notification.scheduledDateTime, notification.display, notification.emailNotification,
notification.title, notification.body, notification.userEmail);
}
},
remove: function(id) {
schedule.cancelJob(id);
console.log(`Canceled schedule: ${id}`);
},
removeAll: function(){
for(let key in schedule.scheduledJobs){
this.remove(key);
}
}
}
module.exports = { notificationScheduler };