-
Notifications
You must be signed in to change notification settings - Fork 4
/
contact.js
70 lines (58 loc) · 1.48 KB
/
contact.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
const Mailgun = require('mailgun-js');
const Airtable = require('airtable');
const sendThankYouEmail = async ({ email }) => {
return new Promise((resolve, reject) => {
console.log('Sending the email');
const { MG_API_KEY: apiKey, MG_DOMAIN: domain } = process.env;
const mailgun = Mailgun({
apiKey,
domain
});
const mailData = {
from: 'Stefan Judis <no-reply@stefanjudis.com>',
to: email,
subject: 'Thank you for your interest',
text: "I'll come back to you asap!"
};
mailgun.messages().send(mailData, err => {
if (err) return reject(err);
resolve();
});
});
};
const saveUser = async ({ name, email, message }) => {
return new Promise((resolve, reject) => {
const { AT_API_KEY: apiKey, AT_BASE, AT_TABLE } = process.env;
Airtable.configure({
apiKey
});
const base = Airtable.base(AT_BASE);
base(AT_TABLE).create({ name, email, message }, err => {
if (err) return reject(err);
resolve();
});
});
};
exports.handler = async event => {
try {
const data = JSON.parse(event.body);
await sendThankYouEmail(data);
if (data.receiveUpdates) {
await saveUser(data);
}
// send a thank you email
// sign person
return {
statusCode: 200,
body: JSON.stringify({
message: "Let's become serverless conductors!!!"
})
};
} catch (e) {
console.log(e);
return {
statusCode: 500,
body: e.mssage
};
}
};