-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphotos_lambda.js
More file actions
134 lines (108 loc) · 3.67 KB
/
photos_lambda.js
File metadata and controls
134 lines (108 loc) · 3.67 KB
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
// const AWS = require('aws-sdk');
const {
getCustomerByEmail,
getTableName,
AWS
} = require('./common');
async function handler(event, context) {
console.log('photos handler event', event);
for(let record of event.Records) {
try {
await processRecord(record);
} catch(e) {
console.warn('could not process record... skipping', e);
}
}
}
async function processRecord(record) {
console.info('processing record...', record);
const {
bucketName, fileName
} = extractFileInfo(record);
console.info(`handling file ${fileName} at ${bucketName}`);
const customerEmail = parseCustomerEmail(fileName);
const customer = await getCustomerByEmail(customerEmail);
if(customer == undefined) {
console.log('customer not found..');
return;
}
console.log('found customer: ', JSON.stringify(customer));
const photoLocation = `s3://${bucketName}/${decodeURIComponent(fileName)}`
await updatePhotoLocation(customer, photoLocation);
await sendWelcomeEmail(customer, photoLocation);
}
async function updatePhotoLocation(customer, photoLocation) {
const customerId = customer.id.S;
console.info(`updating photo location to ${photoLocation} for customer ${customerId}`);
const dynamoClient = new AWS.DynamoDB();
const params = {
TableName: await getTableName(),
Key: {
id: { 'S': customerId }
},
UpdateExpression: "set photo_location=:p",
ExpressionAttributeValues: {
":p": { 'S': photoLocation }
},
ReturnValues: "UPDATED_NEW",
}
const updateResponse = await dynamoClient
.updateItem(params)
.promise();
console.info('update photo response: ', updateResponse);
return updateResponse;
}
async function sendWelcomeEmail(customer, photoLocation) {
const customerEmail = customer.email.S;
const customerName = `${customer.lastname.S}, ${customer.firstname.S}`;
console.info(`sending welcome e-mail`);
const senderName = process.env.SENDER_NAME || 'DevOps Academy';
const senderMail = process.env.SENDER_EMAIL;
if(senderMail == undefined) {
console.warn("You must specify SENDER_NAME and SENDER_EMAIL to send the welcome e-mail.");
return;
}
const bodyHtml = `
<html>
<body>
<h1>Hello ${customerName}</h1>
<p>This is a confirmation e-mail that your photo was uploated to <strong>${photoLocation}</strong>.</p>
</body>
</html>`;
const sender = `${senderName} <${senderMail}>`;
const Charset = "UTF-8";
try {
const sesClient = new AWS.SESV2();
const response = await sesClient.sendEmail({
FromEmailAddress: sender,
Destination: {
ToAddresses: [ customerEmail ]
},
Content: {
Simple: {
Body: {
Html: {
Charset,
Data: bodyHtml
}
},
Subject: {
Charset,
Data: '[devops-academy] Photo Upload Confirmation'
}
}
}
}).promise();
console.info(`Email sent! Message Id: ${response.MessageId} `)
} catch(e) {
console.warn('Error on sending e-mail', e);
}
}
const extractFileInfo = record => ({
bucketName: record.s3.bucket.name,
fileName: record.s3.object.key
});
const parseCustomerEmail = fileName => decodeURIComponent(fileName.replace(/\.[^.]*$/, ""));
module.exports = {
handler
}