-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsendTimeslots.js
93 lines (81 loc) · 2.75 KB
/
sendTimeslots.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
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
require("dotenv").config();
const fs = require("fs");
const csv = require("csv-parser");
const date = require("date-and-time");
const sgMail = require("@sendgrid/mail");
const sgClient = require("@sendgrid/client");
const { pruneContacts } = require("./util");
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
sgClient.setApiKey(process.env.SENDGRID_API_KEY);
Date.prototype.addHours = function (h) {
this.setTime(this.getTime() + h * 60 * 60 * 1000);
return this;
};
const contacts = [];
const PRUNE = true;
const IS_HIGHSCHOOL = false;
const SEND_EMAIL = false;
fs.createReadStream("finaldata.csv")
.pipe(csv())
.on("data", (data) => contacts.push(data))
.on("end", async () => {
const uniqueContacts = PRUNE
? await pruneContacts(contacts, sgClient)
: contacts;
uniqueContacts.forEach((contact) => {
const email = contact["Email Address"];
const firstName = contact["Your Full Name"].split(" ")[0];
const studentTimezone = parseInt(contact["Time Zone"]);
const hasTimezone = !Number.isNaN(studentTimezone);
const defaultSchoolName = IS_HIGHSCHOOL
? "Quaranteen University Academy"
: "Quaranteen University";
const schoolName =
contact["School"] === "Unknown" ? defaultSchoolName : contact["School"];
const schoolStartDate = new Date(contact["School Start Time UTC"]);
const schoolStartTime = date.format(schoolStartDate, "hh:mm:ss A");
const schoolStartTimeLocal = hasTimezone
? date.format(schoolStartDate.addHours(studentTimezone), "hh:mm:ss A")
: null;
const studentStartDate = new Date(contact["Start Time UTC"]);
const studentStartTime = date.format(studentStartDate, "hh:mm:ss A");
const studentStartTimeLocal = hasTimezone
? date.format(studentStartDate.addHours(studentTimezone), "hh:mm:ss A")
: null;
const emailData = {
firstName,
hasTimezone,
studentTimezone,
schoolName,
schoolStartTime,
studentStartTime,
schoolStartTimeLocal,
studentStartTimeLocal,
};
const msg = {
to: email,
from: process.env.FROM_ADDRESS,
replyTo: process.env.REPLY_TO_ADDRESS,
templateId: process.env.TEMPLATE_ID,
dynamic_template_data: emailData,
asm: {
group_id: process.env.ASM_GROUP_ID,
},
};
if (SEND_EMAIL) {
sgMail
.send(msg)
.then(() => {
console.log(`${email}: Success!`);
})
.catch((error) => {
console.error(`${email}: Failure!`, error);
if (error.response) {
console.error(error.response.body);
}
});
} else {
console.log(msg);
}
});
});