-
Notifications
You must be signed in to change notification settings - Fork 0
/
finalWaiverReminderEmail.js
126 lines (113 loc) · 3.31 KB
/
finalWaiverReminderEmail.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
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
const keys = require('./private/api-keys.json')
const admin = require("firebase-admin");
const fetch = require('node-fetch');
const fs = require('fs')
const moment = require('moment')
const endPoint = 'https://api.sendgrid.com/v3/contactdb/recipients'
let requestBody = []
let emails = []
const production = true
const rejectedPeople = require('./private/rejected').rejected
console.log(rejectedPeople.length)
const testingEmails = ['rsf.sho@gmail.com', 'ben.grant@hyphen-hacks.com']
//const testingEmails = []
admin.initializeApp({
credential: admin.credential.cert(keys.firebase),
databaseURL: "https://hyphen-hacks-2019.firebaseio.com"
});
function Person(input) {
this.email = input.email
this.name = input.name
this.firstName = input.profile.first_name
this.referrer = 'hyphen-hacks_2019'
this.years_involved = '2019'
this.waiver_completed = input.waiverStatus === 2
this.id = input.id
if (rejectedPeople.indexOf(Number(input.id)) === -1) {
this.accepted = true
} else {
this.accepted = false
console.log('Denied', input.name)
}
if (input.ticket_class_name === 'High school Student') {
this.role = 'attendee'
this.school = input.answers[1].answer
} else if (input.ticket_class_name === 'Mentor/Judge') {
if (input.answers[14].answer === "Judge") {
this.role = "judge"
} else {
this.role = "mentor"
}
} else {
this.role = 'volunteer'
}
}
const db = admin.firestore();
const peopleRef = db.collection('people');
const allPeople = {}
const allPeopleArray = []
function sendEmail(email, templateID, fullName, inputData) {
const mailBody = {
"personalizations": [
{
"to": [
{
"email": email,
"name": fullName
}
],
"dynamic_template_data": inputData
}
],
"from": {
"email": "noreply@hyphen-hacks.com",
"name": "Ronan at Hyphen Hacks"
},
"reply_to": {
"email": "support@hyphen-hacks.com",
"name": "Ronan"
},
"template_id": templateID,
"tracking_settings": {
"click_tracking": {
'enable': true
}
}
};
// log.info(JSON.stringify(mailBody))\
if (production === true || testingEmails.indexOf(email) > -1) {
console.log(`sending to ${email}`)
fetch('https://api.sendgrid.com/v3/mail/send', {
method: 'post',
headers: {
'Authorization': 'Bearer ' + keys.sendGrid,
"content-type": "application/json"
},
body: JSON.stringify(mailBody)
}).then(() => {
console.log('sent to', email)
}).catch(e => {
console.log(e)
});
}
}
peopleRef.get().then(data => {
data.forEach(snap => {
const person = snap.data()
const cleanedPerson = new Person(person)
const waiverLink = `https://waivers.hyphen-hacks.com/#/p/${cleanedPerson.id}`;
if (cleanedPerson.accepted) {
if (!cleanedPerson.waiver_completed) {
allPeople[cleanedPerson.id] = cleanedPerson
allPeopleArray.push(cleanedPerson)
sendEmail(cleanedPerson.email, 'd-0b11c43baa2740d1b2dbb591172e71c9', cleanedPerson.name, {
name: cleanedPerson.firstName, link: waiverLink
})
}
}
})
fs.writeFile('./private/finalEmailWaivers.json', JSON.stringify(allPeople), (e) => {
console.log(e)
console.log('written', allPeopleArray.length, 'people')
})
})