-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.js
62 lines (53 loc) · 1.43 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
const AWS = require('aws-sdk');
var lambda = new AWS.Lambda();
const formatResponse = () => {
return {
statusCode: 200,
body: JSON.stringify({ message: 'Success' }),
};
};
const formatErrorResponse = (error, statusCode = 500) => {
return {
statusCode,
body: JSON.stringify({
error,
}),
};
};
const isValidEmail = (email) => {
const re =
/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(String(email).toLowerCase());
};
const isValidEmailList = (emails) => {
let valid = true;
for (const email of emails) {
if (!isValidEmail(email)) {
valid = false;
break;
}
}
return valid;
};
exports.handler = async (event) => {
const body = JSON.parse(event.body);
const attachments = body.attachments;
const emails = body.emails;
const isValid = isValidEmailList(emails);
if (!isValid) {
return formatErrorResponse('Invalid emails', 400);
}
console.log('Start to zip');
// invoke lambda function to zip file and send email to recipients
const params = {
FunctionName: 'lambda-zip-file-and-send-email',
InvocationType: 'Event',
Payload: JSON.stringify({
attachments,
emails,
}),
};
const lambdaResponse = await lambda.invoke(params).promise();
console.log('lambda response', lambdaResponse);
return formatResponse();
};