-
Notifications
You must be signed in to change notification settings - Fork 0
/
uploadEarlyBirdAnnouncements.js
71 lines (62 loc) · 1.92 KB
/
uploadEarlyBirdAnnouncements.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
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 = []
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.referrer = 'hyphen-hacks_2019'
this.years_involved = '2019'
this.waiver_completed = 'true'
this.earlyBirdBonus = 'true'
this.role = 'attendee'
// this.created = input.created
}
const db = admin.firestore();
const peopleRef = db.collection('people');
const queryRef = peopleRef.where('ticket_class_name', '==', 'High school Student');
queryRef.get().then(data => {
data.forEach(snap => {
const person = snap.data()
const cleanedPerson = new Person(person)
if (emails.indexOf(cleanedPerson.email) > 0) {
console.log('email dup', cleanedPerson.email)
} else {
if (moment(person.created).isValid()) {
if (moment(person.created).isBefore('2019-8-1')) {
requestBody.push(cleanedPerson)
} else {
console.log('UR LATE')
}
} else {
console.log('UR DATE SUCKS')
}
}
emails.push(cleanedPerson.email)
})
console.log('Cleaned:', emails.length, 'people')
console.log('validated', requestBody.length, 'people')
fs.writeFile('./private/emailsUploadWaivers.json', JSON.stringify(requestBody), e => {
console.log(e)
})
fetch(endPoint, {
method: 'post',
headers: {
Authorization: 'Bearer ' + keys.sendGrid
},
body: JSON.stringify(requestBody)
}).then(e => e.json()).then(e => {
console.log(e)
fs.writeFile('./private/emailsErrorWaiver.json', JSON.stringify(e), err => {
console.log(err)
})
})
});