Repository navigation
Expand file tree
/
Copy pathhandler.ts
More file actions
64 lines (50 loc) · 1.65 KB
/
Copy pathhandler.ts
File metadata and controls
64 lines (50 loc) · 1.65 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
import * as mongoose from 'mongoose';
import { sendEmailNotification } from './src/sendEmailForNewPost
68B3
9;;
export const sendEmailForNewPost = async (event) => {
console.log('Received event:', JSON.stringify(event, null, 2));
if (
event.discussionName === undefined ||
event.discussionLink === undefined ||
event.postContent === undefined ||
event.authorName === undefined ||
event.userIds === undefined
) {
return { message: 'Some data is missing from body of POST request', event };
}
console.log(
event.discussionName,
event.discussionLink,
event.postContent,
event.authorName,
event.userIds,
);
const dev = process.env.NODE_ENV !== 'production';
const MONGO_URL = dev ? process.env.MONGO_URL_TEST : process.env.MONGO_URL;
await mongoose.connect(MONGO_URL, { useNewUrlParser: true, useFindAndModify: false });
const { discussionName, discussionLink, postContent, authorName, userIds } = event;
try {
await sendEmailNotification({
productionUrlApp: process.env.PRODUCTION_URL_APP,
discussionName,
discussionLink,
postContent,
authorName,
userIds,
});
} catch (error) {
console.error(error.stack);
return { error: error.message, event };
} finally {
await mongoose.disconnect();
}
return { message: 'Email was sent successfully!', event };
// return {
// statusCode: 200,
// body: JSON.stringify({
// message: 'Go Serverless v1.0! Your function executed successfully!',
// input: event,
// }),
// };
// Use this code if you don't use the http event with the LAMBDA-PROXY integration
};
// set up and test API Gateway