-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathindex.js
355 lines (302 loc) · 9.78 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
const AWS = require('aws-sdk')
const validator = require('validator')
const uuidv4 = require('uuid/v4')
const nodemailer = require('nodemailer')
const smtpTransport = require('nodemailer-smtp-transport')
const docClient = new AWS.DynamoDB.DocumentClient()
const sns = new AWS.SNS()
const sqs = new AWS.SQS()
exports.listGigs = (event, context, callback) => {
const queryParams = {
TableName: 'gig'
}
docClient.scan(queryParams, (err, data) => {
if (err) {
console.error(err)
return callback(null, {
statusCode: 500,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify({error: 'Internal Server Error'})
})
}
const response = {
statusCode: 200,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify({gigs: data.Items})
}
return callback(null, response)
})
}
exports.gig = (event, context, callback) => {
const gigSlug = event.pathParameters.slug
const queryParams = {
Key: {
slug: gigSlug
},
TableName: 'gig'
}
docClient.get(queryParams, (err, data) => {
if (err) {
console.error(err)
return callback(null, {
statusCode: 500,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify({error: 'Internal Server Error'})
})
}
// item not found, return 404
if (!data.Item) {
return callback(null, {
statusCode: 404,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify({error: 'Gig not found'})
})
}
const response = {
statusCode: 200,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify(data.Item)
}
return callback(null, response)
})
}
exports.purchaseTicket = (event, context, callback) => {
// receives a JSON in the event.body containing:
// - gig: needs to be an existing gig
// - name: non empty string
// - email: valid email
// - cardNumber: valid credit card number
// - cardExpiryMonth: required (int between 1 and 12)
// - cardExpiryYear: required (int between 2018 and 2024) (month and year in the future)
// - cardCVC: required (valid cvc)
// - disclaimerAccepted: required (true)
//
// Must return a validation error (400 Bad request) with the following object:
// {error: "Invalid request", errors: [{field: "fieldName", message: "error message"}]}
//
// or, in case of success a 202 (Accepted) with body { success: true }
let data
// parses the input
try {
data = JSON.parse(event.body)
} catch (err) {
return callback(null, {
statusCode: 400,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify({error: 'Invalid content, expected valid JSON'})
})
}
// validates every field
const errors = []
// gig: needs to be an existing gig
if (!data.gig) {
errors.push({field: 'gig', message: 'field is mandatory'})
// validating if the gig exists in DynamoDB is left as an exercise
}
// name: non empty string
if (!data.name) {
errors.push({field: 'name', message: 'field is mandatory'})
}
// email: valid email
if (!data.email) {
errors.push({field: 'email', message: 'field is mandatory'})
} else if (!validator.isEmail(data.email)) {
errors.push({field: 'email', message: 'field is not a valid email'})
}
// cardNumber: valid credit card number
if (!data.cardNumber) {
errors.push({field: 'cardNumber', message: 'field is mandatory'})
} else if (!validator.isCreditCard(data.cardNumber)) {
errors.push({field: 'cardNumber', message: 'field is not a valid credit card number'})
}
// cardExpiryMonth: required (int between 1 and 12)
if (!data.cardExpiryMonth) {
errors.push({field: 'cardExpiryMonth', message: 'field is mandatory'})
} else if (!validator.isInt(String(data.cardExpiryMonth), {min: 1, max: 12})) {
errors.push({field: 'cardExpiryMonth', message: 'field must be an integer in range [1,12]'})
}
// cardExpiryYear: required (month and year in the future)
if (!data.cardExpiryYear) {
errors.push({field: 'cardExpiryYear', message: 'field is mandatory'})
} else if (!validator.isInt(String(data.cardExpiryYear), {min: 2018, max: 2024})) {
errors.push({field: 'cardExpiryYear', message: 'field must be an integer in range [2018,2024]'})
}
// validating that expiry is in the future is left as exercise
// (consider using a library like moment.js)
// cardCVC: required (valid cvc)
if (!data.cardCVC) {
errors.push({field: 'cardCVC', message: 'field is mandatory'})
} else if (!String(data.cardCVC).match(/^[0-9]{3,4}$/)) {
errors.push({field: 'cardCVC', message: 'field must be a valid CVC'})
}
// disclaimerAccepted: required (true)
if (data.disclaimerAccepted !== true) {
errors.push({field: 'disclaimerAccepted', message: 'field must be true'})
}
// if there are errors, return a 400 with the list of errors
if (errors.length) {
return callback(null, {
statusCode: 400,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify({error: 'Invalid Request', errors})
})
}
// fetch gig from DynamoDB
const queryParams = {
Key: {
slug: data.gig
},
TableName: 'gig'
}
docClient.get(queryParams, (err, dynamoData) => {
if (err) {
console.error(err)
return callback(null, {
statusCode: 500,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify({error: 'Internal Server Error'})
})
}
// item not found, return 404
if (!dynamoData.Item) {
return callback(null, {
statusCode: 400,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify({error: 'Invalid gig'})
})
}
const gig = dynamoData.Item
// creates a ticket object
const ticket = {
id: uuidv4(),
createdAt: Date.now(),
name: data.name,
email: data.email,
gig: data.gig
}
// fires an sns message with gig and ticket
sns.publish({
TopicArn: process.env.SNS_TOPIC_ARN,
Message: JSON.stringify({ticket, gig})
}, (err, data) => {
if (err) {
console.error(err)
return callback(null, {
statusCode: 500,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify({error: 'Internal Server Error'})
})
}
// if everything went well return a 202 (accepted)
return callback(null, {
statusCode: 202,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify({success: true})
})
})
})
}
exports.sendMailWorker = (event, context, callback) => {
const receiveMessageParams = {
QueueUrl: process.env.SQS_QUEUE_URL,
MaxNumberOfMessages: 1
}
sqs.receiveMessage(receiveMessageParams, (err, data) => {
if (err) {
console.error(err)
return callback(err)
}
if (!data.Messages) {
console.log('no messages to process')
return callback(null, 'no messages to process')
}
const message = data.Messages[0]
// extract message data from sqs message
// the double JSON parse is because the sqs message contains the original sns message
// in the body, so we are basically extracting the data from the `Message` attribute
// of the original sns message.
const messageData = JSON.parse(JSON.parse(message.Body).Message)
const transporter = nodemailer.createTransport(smtpTransport({
host: process.env.SMTP_HOST,
port: process.env.SMTP_PORT,
auth: {
user: process.env.SMTP_USERNAME,
pass: process.env.SMTP_PASSWORD
}
}))
const subject = `Your ticket for ${messageData.gig.bandName} in ${messageData.gig.city}`
const content = `
Hey ${messageData.ticket.name},
you are going to see ${messageData.gig.bandName} in ${messageData.gig.city}!
This is the secret code that will give you access to our time travel collection point:
---
${messageData.ticket.id}
---
Be sure to show it to our staff at entrance.
Collection point is placed in ${messageData.gig.collectionPoint}.
Be sure to be there on ${messageData.gig.date} at ${messageData.gig.collectionTime}
We already look forward (or maybe backward) to having you there, it's going to be epic!
— Your friendly Ticketless staff
PS: remember that is forbidden to place bets or do any other action that might substantially
increase your net worth while time travelling. Travel safe!
`
const mailOptions = {
from: process.env.SMTP_SENDER_ADDRESS,
to: messageData.ticket.email,
subject,
text: content
}
transporter.sendMail(mailOptions, (err, info) => {
if (err) {
console.error(err)
return callback(err)
}
// delete message from queue
const deleteMessageParams = {
QueueUrl: process.env.SQS_QUEUE_URL,
ReceiptHandle: message.ReceiptHandle
}
sqs.deleteMessage(deleteMessageParams, (err, data) => {
if (err) {
console.error(err)
return callback(err)
}
console.log('1 message processed successfully')
return callback(null, 'Completed')
})
})
})
}