-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemail.txt
429 lines (400 loc) · 14.6 KB
/
email.txt
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/**
* @module email
*/
require('dotenv').config()
const nodemailer = require('nodemailer')
const moment = require('moment')
const fs = require('fs')
const path = require('path')
const HTML_TEMPLATE = fs.readFileSync(
path.resolve(__dirname, './email-template.html'),
'utf-8',
)
const HTML_TEMPLATE_3M = fs.readFileSync(
path.resolve(__dirname, './email-template-offline-3months.html'),
'utf-8',
)
const HTML_TEMPLATE_UPDATE = fs.readFileSync(
path.resolve(__dirname, './email-template-update.html'),
'utf-8',
)
const logger = require('./logger')
// to send the email using sendgrid service
// const sgMail = require('@sendgrid/mail')
// sgMail.setApiKey(process.env.SENDGRID_API_KEY)
// testing with nodemailer
const transport = nodemailer.createTransport({
host: process.env.SMTP_HOST,
port: process.env.SMTP_PORT,
})
/**
* @function sendNoticeEmails to send each received offline validator contact an email.
*
* Takes an array of contacts and wraps each of them in a Promise that resolves into the response of the function transport.sendMail()
*
* @param {Array<Object>} contactsArray from module contacts.js function getContactDetails
* @returns {Promise} initially a single wrapped promise, returns an array of results. see Promise.all().
*/
exports.sendNoticeEmails = (contactsArray, ccContacts) => {
let promises = []
for (let i = 0; i < contactsArray.length; i++) {
console.log('contactsArraycontactsArray', contactsArray[i])
// promises.push(
// later(7000 * i) // Avoid sending too much at once
// .then(() => sendNoticeEmail(contactsArray[i], ccContacts)),
// )
}
return Promise.all(promises)
}
/**
* @function to send a notice email to each of the contacts of an offline validator.
* If there are multiple contacts, sends to all of them at once
*
* @param {Object} contactArray array of contacts of an institution.
*
* @example
* [{
haupt: 'N',
institution: "ABC university"
address: "0xadF2sA1b26Dcb1A723591c144deeb6633E5A79b",
title: 'Mrs.',
academicTitle: '',
firstName: 'Alice',
lastName: 'Smith',
email: 'alice[at]abc.edu',
comments: 'comments',
lastOnline: 2020-11-04T11:06:50.000Z
},
{
haupt: 'N',
institution: "ABC university"
address: "0xadF2sA1b26Dcb1A723591c144deeb6633E5A79b",
title: 'Mr.',
academicTitle: '',
firstName: 'John',
lastName: 'Doe',
email: 'john[at]abc.edu',
comments: 'comments',
lastOnline: 2020-11-04T11:06:50.000Z
},
]
*
*/
function sendNoticeEmail(contactArray, ccContacts) {
logger.log('Sending notice email to: ', contactArray)
let { institution, address, lastOnline } = contactArray[0] // Same for all contacts
let fullNames = '',
emails = [] // Different if multiple contacts
for (contact of contactArray) {
let { academicTitle, firstName, lastName, email } = contact
fullNames +=
(academicTitle ? ' ' + academicTitle : '') + ` ${firstName} ${lastName}, ` // use academic title if exist
emails.push(email)
}
let lastOnlineDateString = moment(lastOnline).format('MMMM Do YYYY')
let lastOnlineTimeString = moment(lastOnline).format('hh:mm a [Germany time]') // moment uses server's local time. Our servers are in Germany.
const properties = {
fullNames,
institution,
address,
lastOnlineDateString,
lastOnlineTimeString,
}
const message = {
from: `bloxberg Validator Monitoring <[email protected]>`,
to: emails,
cc: ccContacts,
subject: 'bloxberg Validator Offline',
html: applyTemplate(HTML_TEMPLATE, properties),
}
console.log('messagemessagemessagemessagemessagemessage', message)
// return setTimeout(() => Promise.resolve (`Email sent to ${institution}: ${email}`), Math.random() * 1000 * 2) // Debug with randomly resolved Promises.
// return transport.sendMail(message)
}
// new sendNoticeEmails start //
/**
* @function sendNoticeEmails to send each received offline validator contact an email.
*
* Takes an array of contacts and wraps each of them in a Promise that resolves into the response of the function transport.sendMail()
*
* @param {Array<Object>} contactsArray from module contacts.js function getContactDetails
* @returns {Promise} initially a single wrapped promise, returns an array of results. see Promise.all().
*/
exports.sendNoticeEmailsNew = (contactsArray, ccContacts) => {
let promises = []
for (let i = 0; i < contactsArray.length; i++) {
let fullNames = ''
let finalContactData = {}
for (contact of contactsArray[i]) {
let {
academicTitle,
firstName,
lastName,
email,
institution,
address,
lastOnline,
} = contact
fullNames +=
(academicTitle ? ' ' + academicTitle : '') +
` ${firstName} ${lastName}, ` // use academic title if exist
let lastOnlineDateString = moment(lastOnline).format('MMMM Do YYYY')
let lastOnlineTimeString = moment(lastOnline).format(
'hh:mm a [Germany time]',
)
finalContactData.fullNames = fullNames
finalContactData.institution = institution
finalContactData.address = address
finalContactData.lastOnlineDateString = lastOnlineDateString
finalContactData.lastOnlineTimeString = lastOnlineTimeString
promises.push(
later(10000 * i) // Avoid sending too much at once
.then(() => sendNoticeEmail(finalContactData, ccContacts, email)),
)
}
}
return Promise.all(promises)
}
/**
* @function to send a notice email to each of the contacts of an offline validator.
* If there are multiple contacts, sends to all of them at once
*
* @param {Object} contactArray array of contacts of an institution.
*
* @example
* [{
haupt: 'N',
institution: "ABC university"
address: "0xadF2sA1b26Dcb1A723591c144deeb6633E5A79b",
title: 'Mrs.',
academicTitle: '',
firstName: 'Alice',
lastName: 'Smith',
email: 'alice[at]abc.edu',
comments: 'comments',
lastOnline: 2020-11-04T11:06:50.000Z
},
{
haupt: 'N',
institution: "ABC university"
address: "0xadF2sA1b26Dcb1A723591c144deeb6633E5A79b",
title: 'Mr.',
academicTitle: '',
firstName: 'John',
lastName: 'Doe',
email: 'john[at]abc.edu',
comments: 'comments',
lastOnline: 2020-11-04T11:06:50.000Z
},
]
*
*/
function sendNoticeEmail(contactArray, ccContacts, toEmail) {
// logger.log('Sending notice email to: ', contactArray)
const message = {
from: `bloxberg Validator Monitoring <[email protected]>`,
to: toEmail,
cc: ccContacts,
subject: 'bloxberg Validator Offline',
html: applyTemplate(HTML_TEMPLATE, contactArray),
}
console.log('messagemessagemessagemessagemessagemessage', message)
// return setTimeout(() => Promise.resolve (`Email sent to ${institution}: ${email}`), Math.random() * 1000 * 2) // Debug with randomly resolved Promises.
// return transport.sendMail(message)
}
// new sendNoticeEmails end //
function later(delay) {
return new Promise(function (resolve) {
setTimeout(resolve, delay)
})
}
/**
* Function to send error emails to admins when the script fails.
*
* @param {Array} emails - array of email strings as receivers
* @param {Error} error - the thrown Error object
*/
exports.sendErrorEmails = (emails, error) => {
logger.log('Sending error emails to ' + emails.join() + 'about: ' + error)
const message = {
from: `bloxberg Validator Monitoring <[email protected]>`,
to: emails,
subject: '❗ ERROR: bloxberg Validator Offline',
text: `When running the script the following error is encountered\n\n
${error.message}\n\n
${error.stack}`,
}
// return setTimeout(() => Promise.resolve(`Email sent to ${institution}: ${email}`), Math.random() * 1000 * 2) // Debug with randomly resolved Promises.
// return sgMail.send(message)
}
/**
* Function to send error emails to bloxberg admins when the script fails.
*
* @param {Array} emails - array of email strings as receivers
* @param {Array} notFoundContacts - array of validator objects without a found contact.
* @example
* [
* {address: 0x1fd210....321, institution: undefined},
* {address: 0x2fe3ad....da2, institution: Some University},
* ]
*/
exports.sendNotFoundEmails = (notFoundContacts, admins) => {
if (notFoundContacts.length < 1) return Promise.resolve()
logger.log(
'Sending not found emails to ' +
emails.join() +
' for the addresses: ' +
notFoundContacts.map((contact) => contact.address).join(),
)
const message = {
from: `bloxberg Validator Monitoring <[email protected]>`,
to: admins,
subject: '❗ Contact Not Found: bloxberg Validator Offline',
text:
`The following validators do not have an assigned contact and
hence were not able to be contacted. If the institution name is undefined,
it means an associated institution for the address couldn't be found in Cobra input.
Otherwise the institution has an associated address but is missing any contacts. \n\n` +
notFoundContacts
.map((contact) => {
return `Institution: ${contact.institution} \n\n
Address: ${contact.address}\n\n`
})
.join(),
}
// return setTimeout(() => Promise.resolve(`Email sent to ${institution}: ${email}`), Math.random() * 1000 * 2) // Debug with randomly resolved Promises.
// return sgMail.send(message)
}
// Replaces properties in the template with {{ double parantheses }} with values in the parameter object.
// from: https://stackoverflow.com/questions/29831810/how-to-fill-information-into-a-template-text-file
function applyTemplate(template, properties) {
var returnValue = ''
var templateFragments = template.split('{{')
returnValue += templateFragments[0]
for (var i = 1; i < templateFragments.length; i++) {
var fragmentSections = templateFragments[i].split('}}', 2)
returnValue += properties[fragmentSections[0]]
returnValue += fragmentSections[1]
}
return returnValue
}
/**
* @function send3mNoticeEmail to send each received offline validator contact an email if they are offline for more than 3 months.
*
* Takes an array of contacts and wraps each of them in a Promise that resolves into the response of the function transport.sendMail()
*
* @param {Array<Object>} contactsArray from module contacts.js function getContactDetails
* @returns {Promise} initially a single wrapped promise, returns an array of results. see Promise.all().
*/
exports.send3mNoticeEmail = (contactsArray, ccContacts) => {
let promises = []
for (let i = 0; i < contactsArray.length; i++) {
promises.push(
later(5000 * i) // Avoid sending too much at once
.then(() => send3mNoticeEmail(contactsArray[i], ccContacts)),
)
}
return Promise.all(promises)
}
function later(delay) {
return new Promise(function (resolve) {
setTimeout(resolve, delay)
})
}
/**
* @function to send a notice email to each of the contacts of an offline validator for 3 months.
* If there are multiple contacts, sends to all of them at once
*
* @param {Object} contactArray array of contacts of an institution.
*
*/
function send3mNoticeEmail(contactArray, ccContacts) {
logger.log('Sending notice email to: ', contactArray)
let { institution, address, lastOnline } = contactArray[0] // Same for all contacts
let fullNames = '',
emails = [] // Different if multiple contacts
for (contact of contactArray) {
let { academicTitle, firstName, lastName, email } = contact
fullNames +=
(academicTitle ? ' ' + academicTitle : '') + ` ${firstName} ${lastName}, ` // use academic title if exist
emails.push(email)
}
let lastOnlineDateString = moment(lastOnline).format('MMMM Do YYYY')
let lastOnlineTimeString = moment(lastOnline).format('hh:mm a [Germany time]') // moment uses server's local time. Our servers are in Germany.
const properties = {
fullNames,
institution,
address,
lastOnlineDateString,
lastOnlineTimeString,
}
const message = {
from: `bloxberg Validator Monitoring <[email protected]>`,
to: emails,
cc: ccContacts,
subject: 'bloxberg Validator Offline Escalation',
html: applyTemplate(HTML_TEMPLATE_3M, properties),
}
// return setTimeout(() => Promise.resolve(`Email sent to ${institution}: ${email}`), Math.random() * 1000 * 2) // Debug with randomly resolved Promises.
return transport.sendMail(message)
}
/**
* @function sendUpdateNoticeEmail to send each validator to update details in blockchain.
*
* Takes an array of contacts and wraps each of them in a Promise that resolves into the response of the function transport.sendMail()
*
* @param {Array<Object>} contactsArray from module contacts.js function getContactDetails
* @returns {Promise} initially a single wrapped promise, returns an array of results. see Promise.all().
*/
exports.sendUpdateNoticeEmail = (contactsArray, ccContacts) => {
let promises = []
for (let i = 0; i < contactsArray.length; i++) {
promises.push(
later(5000 * i) // Avoid sending too much at once
.then(() => sendUpdateNoticeEmail(contactsArray[i], ccContacts)),
)
}
return Promise.all(promises)
}
function later(delay) {
return new Promise(function (resolve) {
setTimeout(resolve, delay)
})
}
/**
* @function to send a notice email to each of the contacts to update the information.
* If there are multiple contacts, sends to all of them at once
*
* @param {Object} contactArray array of contacts of an institution.
*
*/
function sendUpdateNoticeEmail(contactArray, ccContacts) {
logger.log('Sending notice email to: ', contactArray)
let { institution, address, lastOnline } = contactArray[0] // Same for all contacts
let fullNames = '',
emails = [] // Different if multiple contacts
for (contact of contactArray) {
let { academicTitle, firstName, lastName, email } = contact
fullNames +=
(academicTitle ? ' ' + academicTitle : '') + ` ${firstName} ${lastName}, ` // use academic title if exist
emails.push(email)
}
let lastOnlineDateString = moment(lastOnline).format('MMMM Do YYYY')
let lastOnlineTimeString = moment(lastOnline).format('hh:mm a [Germany time]') // moment uses server's local time. Our servers are in Germany.
const properties = {
fullNames,
institution,
address,
lastOnlineDateString,
lastOnlineTimeString,
}
const message = {
from: `bloxberg Validator Monitoring <[email protected]>`,
to: emails,
cc: ccContacts,
subject: 'Please update your validator address',
html: applyTemplate(HTML_TEMPLATE_UPDATE, properties),
}
// return setTimeout(() => Promise.resolve(`Email sent to ${institution}: ${email}`), Math.random() * 1000 * 2) // Debug with randomly resolved Promises.
return transport.sendMail(message)
}