-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
263 lines (224 loc) · 6.52 KB
/
index.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
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
let responseHeaders = new Headers({
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, HEAD, POST, OPTIONS',
'Access-Control-Allow-Headers': '*',
'content-type': 'application/json;charset=UTF-8',
})
async function findGuest(submission) {
const email = submission.email
const params = {
filterByFormula: '{Email}="' + email + '"',
}
const url =
'https://api.airtable.com/v0/appiPHOcdGktaFlGE/Guests?' +
new URLSearchParams(params)
let headers = new Headers()
headers.append('Authorization', AIRTABLE_API_KEY)
headers.append('Content-Type', 'application/json')
const requestOptions = {
method: 'GET',
headers: headers,
redirect: 'follow',
}
const response = await fetch(url, requestOptions)
const rj = await response.json()
const records = rj.records
// console.log(JSON.stringify(rj))
return records
}
async function sendEmail(submission) {
const url = 'https://api.sendgrid.com/v3/mail/send'
let headers = new Headers()
headers.append('Authorization', SENDGRID_API_KEY)
headers.append('Content-Type', 'application/json')
const data = {
personalizations: [
{
to: [
{
email: '[email protected]',
name: 'Philip Johnson',
},
],
},
],
from: {
email: '[email protected]',
name: 'alerts',
},
replyTo: {
email: '[email protected]',
name: 'Philip Johnson',
},
subject: submission.email + ' has replied to your save the date',
content: [
{
type: 'text/plain',
value: JSON.stringify(submission),
},
],
}
const requestOptions = {
method: 'POST',
headers: headers,
body: JSON.stringify(data),
redirect: 'follow',
}
try {
const response = await fetch(url, requestOptions)
const rj = await response.text
return rj
} catch (e) {
console.log(JSON.stringify(e))
}
}
async function createGuest(submission) {
const email = submission.email
const url = 'https://api.airtable.com/v0/appiPHOcdGktaFlGE/Guests'
let headers = new Headers()
headers.append('Authorization', AIRTABLE_API_KEY)
headers.append('Content-Type', 'application/json')
const data = {
records: [
{
fields: {
Email: email,
'Guest Names': submission.firstName + ' ' + submission.lastName,
'Indicated Liklihood': submission.selectedOption,
},
},
],
}
const requestOptions = {
method: 'POST',
headers: headers,
body: JSON.stringify(data),
redirect: 'follow',
}
const response = await fetch(url, requestOptions)
const rj = await response.json()
const records = rj.records
// console.log(JSON.stringify(rj))
return records
}
async function checkExistingResponse(records) {
const existingResponses = records.filter(
record => typeof record.fields['Indicated Liklihood'] == 'number',
)
return existingResponses
}
async function createAddress(submission) {
const data = {
records: [
{
fields: {
'Address 1': submission.streetAddress,
'Address 2': submission.streetAddress2,
City: submission.city,
Region: submission.region,
'Postal Code': submission.postalCode,
Country: submission.country,
},
},
],
}
// console.log(data)
const url = 'https://api.airtable.com/v0/appiPHOcdGktaFlGE/Mailing%20Address'
let headers = new Headers()
headers.append('Authorization', AIRTABLE_API_KEY)
headers.append('Content-Type', 'application/json')
const requestOptions = {
method: 'POST',
headers: headers,
body: JSON.stringify(data),
redirect: 'follow',
}
const response = await fetch(url, requestOptions)
const rj = await response.json()
// print(JSON.stringify(rj))
const recordIds = rj.records.map(record => record.id)
// console.log(JSON.stringify(recordIds))
return recordIds
}
async function updateRSVP(records, newAddress, submission) {
const recordsUpdate = records.map(record => {
return {
id: record.id,
fields: {
'Indicated Liklihood': submission.selectedOption,
'Mailing Address': newAddress,
},
}
})
const data = {
records: recordsUpdate,
}
// console.log(JSON.stringify(data))
const url = 'https://api.airtable.com/v0/appiPHOcdGktaFlGE/Guests'
let headers = new Headers()
headers.append('Authorization', AIRTABLE_API_KEY)
headers.append('Content-Type', 'application/json')
const requestOptions = {
method: 'PATCH',
headers: headers,
body: JSON.stringify(data),
redirect: 'follow',
}
const response = await fetch(url, requestOptions)
const rj = await response.json()
// console.log(JSON.stringify(rj))
return rj
}
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
/**
* Respond with hello worker text
* @param {Request} request
*/
async function handleRequest(request) {
const requestUrl = new URL(request.url)
const path = requestUrl.pathname
const submission = await request.json()
console.log(JSON.stringify(submission))
// console.log(JSON.stringify(emailResult))
// Root Path Logic
if (path == '/') {
// console.log(JSON.stringify(request))
await sendEmail(submission)
// console.log(JSON.stringify(newAddress))
const guests = await findGuest(submission)
if (guests.length < 1) {
console.log('Guest not found. Creating new guest')
const newGuest = await createGuest(submission)
const newAddress = await createAddress(submission)
// console.log(JSON.stringify(newGuest), JSON.stringify(newAddress))
const updates = await updateRSVP(newGuest, newAddress, submission)
} else {
console.log('Found existing guest')
const newAddress = await createAddress(submission)
const updates = await updateRSVP(guests, newAddress, submission)
}
return new Response('true', {
status: 200,
headers: responseHeaders,
})
}
if (path == '/response') {
const guests = await findGuest(submission)
const existingResponses = await checkExistingResponse(guests)
const ej = JSON.stringify(existingResponses)
return new Response(existingResponses.length > 0 ? ej : false, {
status: 200,
headers: responseHeaders,
})
}
if (path == '/guest') {
const guests = await findGuest(submission)
const ej = JSON.stringify(guests)
return new Response(guests.length > 0 ? ej : false, {
status: 200,
headers: responseHeaders,
})
}
}