-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
172 lines (142 loc) · 5.76 KB
/
index.js
File metadata and controls
172 lines (142 loc) · 5.76 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
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
const { MongoClient } = require("mongodb");
const dotenv = require("dotenv");
const nodemailer = require("nodemailer");
const config = require("./config");
dotenv.config();
MongoClient.connect(process.env.MONGODB_URL).then(async client => {
const messages = [];
const { host, port, auth, secure } = await config();
const transport = nodemailer.createTransport({
host,
port,
secure,
auth,
tls: {
rejectUnauthorized: false
}
});
transport.verify(async (error, success) => {
if (error) {
console.error(error);
} else {
const db = client.db(process.env.MONGODB_NAME);
const notifications = db.collection("notifications");
const users = db.collection("users");
let notifCursor = notifications.watch();
const { subject, appUrl, language, amountMessages } = await config();
const sendMail = mailOptions => {
transport.sendMail(mailOptions, (error, info) => {
if (error) {
return console.error(error);
}
console.log('Message sent: %s', info.messageId);
if (process.env.NODE_ENV != 'production')
console.log('Preview URL: ' + nodemailer.getTestMessageUrl(info));
});
}
const concatHTMLMessages = (htmlMessages) => {
let finalHtml = '<html> <div dir="rtl"> <h2> שלום רב! </h2> <br> <b> התזכורות הבאות נוצרו לך במערכת AmanBoard </b> <hr> <br>';
for (let i = 0; i < htmlMessages.length; i++) {
finalHtml += htmlMessages[i] + '<br><hr><br>';
}
return finalHtml + '</html>';
}
const sendAmountOfMails = amount => {
let count = Math.min(amount, messages.length);
for (let i = 0; i < count; i++) {
const option = messages.pop();
const { to, from, subject } = option;
const allOptions = [option];
let messageArray = [option];
while (messageArray.length > 0) {
messageArray = messages.splice(messages.findIndex(message => message.to === to), 1);
allOptions = [...allOptions, ...messageArray];
}
const htmlMessages = allOptions.map(option => option.html);
const finalOption = {
to,
from,
subject,
html: concatHTMLMessages(htmlMessages)
}
sendMail(finalOption);
}
if (messages.length > 0)
console.log(`${messages.length} messages left in Queue`);
}
// set amount of mails to send over one minute
setInterval(() => sendAmountOfMails(amountMessages), 60000);
notifCursor.on("change", async ({ fullDocument }) => {
if (fullDocument) {
let { from, notifTo, title, boardId, action } = fullDocument;
if (notifTo) {
const user = await users.findOne({ _id: notifTo });
const mailOptions = {
from: auth.user,
to: user.email || user.mail,
subject,
text: createTextMessage(language, from, title, action, boardId, appUrl),
html: createHTMLMessage(language, from, title, action, boardId, appUrl)
};
messages.push(mailOptions);
}
}
});
}
});
});
const createTextMessage = (language, ...args) => textMessages[language](...args);
const englishTextMessage = (from, title, action, boardId, appUrl) => {
return `Hey There!
The user: ${from} has modified a board you are watching.
Board Title: ${title}
${from} did the following change: ${action}
Link to Board: ${appUrl}/b/${boardId}
`;
}
const hebrewTextMessage = (from, title, action, boardId, appUrl) => {
return `
שלום רב!
המשתמש: ${from} ערך לוח עליו הפעלת צפייה.
כותרת הלוח הינה: ${title}
${from} עשה את הפעולה הבאה: ${action}
קישור ללוח הרלוונטי: ${appUrl}/b/${boardId}
`
}
const textMessages = {
'english': englishTextMessage,
'hebrew': hebrewTextMessage
}
const createHTMLMessage = (language, ...args) => htmlMessages[language](...args);
const englishHtmlMessage = (from, title, action, boardId, appUrl) => {
return `
<html>
<h2> Hi There! </h2>
<br>
<p> The user: ${from} has modified a board you are watching. </p>
<p> Board Title: ${title} </p>
<p> ${from} did the following change: ${action} </p>
<br>
<a href="${appUrl}/b/${boardId}"> Link to Board </a>
</html>
`;
}
const hebrewHtmlMessage = (from, title, action, boardId, appUrl) => {
return `
<html>
<div dir="rtl">
<h2> שלום רב! </h2>
<br>
<p> המשתמש: ${from} ערך לוח עליו הפעלת צפייה. </p>
<p> כותרת הלוח הינה: ${title} </p>
<p> ${from} עשה את הפעולה הבאה: ${action} </p>
<br>
<a href="${appUrl}/b/${boardId}"> קישור ללוח הרלוונטי </a>
</div>
</html>
`;
}
const htmlMessages = {
'english': englishHtmlMessage,
'hebrew': hebrewHtmlMessage
}