This repository was archived by the owner on Oct 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreminderapp.js
60 lines (51 loc) · 1.85 KB
/
reminderapp.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 { Storage } = require('@google-cloud/storage');
const storage = new Storage();
const bucket = storage.bucket(process.env.BUCKETNAME);
const fileName = process.env.FILENAME;
const remoteFile = bucket.file(fileName);
const csv = require('csv-parser');
const moment = require('moment');
const sgMail = require('@sendgrid/mail');
exports.appProcessor = (event, context) => {
var today = moment().date();
var currentMonth = moment().month() + 1;
var flag = true;
remoteFile.createReadStream()
.pipe(csv())
.on('data', (row) => {
var bday = moment(row.Birthday, "MM-DD-YYYY");
bdaydate = bday.date() - 1;
bdaymonth = bday.month() + 1;
var annday = moment(row.Anniversary, "MM-DD-YYYY");
anndaydate = annday.date() - 1;
anndaymonth = annday.month() + 1;
if (today == bdaydate && currentMonth == bdaymonth) {
sendEmail(row.Name, "bday")
flag = false;
}
if (today == anndaydate && currentMonth == anndaymonth) {
sendEmail(row.Name, "aniv")
flag = false;
}
}).on('end', () => {
if (flag) {
sendEmail("No Events scheduled for Tomorrow", "");
}
});
function sendEmail(name, remindertype) {
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
if (remindertype == "bday") {
type = " has a Birthday tomorrow";
} else if (remindertype == "aniv") {
type = " has an Anniversary tomorrow";
}
const msg = {
to: process.env.TO_ADDR,
from: process.env.FROM_ADDR,
subject: process.env.SUBJECT,
text: name + type,
html: '<strong>' + name + type + '</strong>',
};
sgMail.send(msg);
}
};