-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.js
61 lines (49 loc) · 1.53 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
/**
*
* +----------------------------------+
* | Thiago Vacare |
* | 01/2018 |
* +----------------------------------+
*
* */
'use strict';
const AWS = require('aws-sdk');
const Promise = require('bluebird');
const {getSQSMessage} = require('./helper');
const {deleteSQSMessage} = require('./helper');
const {sendSNSEmail} = require('./helper');
const {getSESTemplate} = require('./helper');
require('dotenv').config();
AWS.config.update({
region: process.env.AWS_REGION
});
AWS.config.setPromisesDependency(Promise);
module.exports.sender = (event, context, callback) => {
const SES = new AWS.SES();
const SQS = new AWS.SQS();
return getSQSMessage(SQS)
.then(msg => {
console.log('Reading message', msg);
if (msg.Messages) {
let [message] = msg.Messages;
if (message && message.Body) {
const body = JSON.parse(message.Body);
return getSESTemplate(SES, body.template, body.context)
.then(template => {
template = template || '';
sendSNSEmail(body.mailTo, body.subject, template);
return message;
});
}
}
})
.then(message => {
if (message) {
const receiptHandle = message.ReceiptHandle;
deleteSQSMessage(SQS, receiptHandle)
}
})
.catch(err => {
throw new Error(err)
});
};