This repository has been archived by the owner on Oct 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.js
84 lines (66 loc) · 2.12 KB
/
handler.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
'use strict';
const AWS = require('aws-sdk');
const uuid = require('uuid');
module.exports.submitContactForm = async (event, context) => {
console.log("EVENT: ", event);
const sns = new AWS.SNS();
const messageData = JSON.parse(event.body);
console.log("Message: ", messageData);
let message = "Hello, world!";
let statusCode = 200;
let response = {
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": true
},
};
if (typeof messageData.name != 'string' || messageData.name.length === 0) {
statusCode = 400;
message = "Name is required";
}
if (typeof messageData.email != 'string' || messageData.email.length === 0) {
statusCode = 400;
message = "Email address is required";
}
if (typeof messageData.message != 'string' || messageData.message.length === 0) {
statusCode = 400;
message = "A message is required";
}
const snsMessage = {
TopicArn: process.env.CONTACT_TOPIC_ARN,
Subject: "Message from Contactotron",
Message: `
Contact Form Message
From: ${messageData.name} <${messageData.email}>
Message:
${messageData.message}
`
};
const result = await sns.publish(snsMessage).promise();
console.log("PUBLISH RESULT: ", result);
// { ResponseMetadata: { RequestId: '25f27025-32f8-59ad-8f53-248fc5a553d8' },
// MessageId: 'cf6561fb-09d6-57f7-9ab7-3551fe8bad82' }
response.statusCode = statusCode;
response.body = JSON.stringify({ message: message });
console.log("RESPONSE: ", JSON.stringify(response));
return response;
};
module.exports.saveContactMessage = async (event, context) => {
console.log("SNS EVENT: ", event);
const dynamoDB = new AWS.DynamoDB();
const snsObject = event.Records[0].Sns;
const dbItem = {
TableName: process.env.CONTACT_TABLE_NAME,
Item: {
'id': {S: uuid.v4()},
'Timestamp': {S: snsObject.Timestamp},
'MessageId': {S: snsObject.MessageId},
'Message': {S: snsObject.Message},
'Subject': {S: snsObject.Subject}
}
};
console.log("SAVING: ", dbItem);
await dynamoDB.putItem(dbItem).promise();
console.log("SAVED");
return;
}