-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
116 lines (112 loc) · 3.25 KB
/
index.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
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
const aws = require("aws-sdk");
var ddb = new aws.DynamoDB({ apiVersion: "2012-08-10" });
var ses = new aws.SES();
aws.config.update({
region: "us-east-1"
});
var docClient = new aws.DynamoDB.DocumentClient();
exports.emailService = function(event, context, callback) {
let message = event.Records[0].Sns.Message;
let messageJson = JSON.parse(message);
let messageDataJson = JSON.parse(messageJson.data);
console.log("Test Message: " + messageJson.data);
console.log("Test Link: " + messageDataJson.link);
console.log("Test Id: " + messageDataJson.Email);
console.log("Source"+ messageDataJson.sourceE);
let Link = JSON.stringify(messageDataJson.link);
let currentTime = new Date().getTime();
let ttl = 1*60*1000;
let expirationTime = (currentTime + ttl).toString();
var emailParams = {
Destination: {
/* required */
ToAddresses: [
messageDataJson.Email
]
},
Message: {
/* required */
Body: {
Text: {
Charset: "UTF-8",
Data: Link
}
},
Subject: {
Charset: "UTF-8",
Data: "User Recipe Link"
}
},
Source: messageDataJson.sourceE
};
let putParams = {
TableName: "csye6225",
Item: {
id: { S: messageDataJson.Email },
link: {S: Link},
ttl: { N: expirationTime }
}
};
let queryParams = {
TableName: 'csye6225',
Key: {
'id': {S: messageDataJson.Email}
},
};
// first get item and check if email exists
//if does not exist put item and send email,
//if exists check if ttl > currentTime,
// if ttl is greater than current time do nothing,
// else send email
ddb.getItem(queryParams, (err, data) => {
if(err) console.log(err)
else{
// console.log('getItemttl: '+JSON.stringify(data, null, 2));
console.log(data.Item)
let jsonData = JSON.stringify(data)
console.log(jsonData)
let parsedJson = JSON.parse(jsonData);
console.log(parsedJson)
if (data.Item == null) {
ddb.putItem(putParams, (err, data) => {
if (err) console.log(err);
else {
console.log(data);
console.log('sent from 1st function')
var sendPromise = ses.sendEmail(emailParams).promise();
sendPromise
.then(function(data) {
console.log(data.MessageId);
})
.catch(function(err) {
console.error(err, err.stack);
});
}
});
}
else {
let curr = new Date().getTime();
let ttl = Number(parsedJson.Item.ttl.N);
console.log(typeof curr + ' '+ curr);
console.log(typeof ttl+ ' '+ ttl);
if (curr > ttl) {
ddb.putItem(putParams, (err, data) => {
if (err) console.log(err);
else {
console.log(data);
console.log('sent from 1st function')
var sendPromise = ses.sendEmail(emailParams).promise();
sendPromise
.then(function(data) {
console.log(data.MessageId);
})
.catch(function(err) {
console.error(err, err.stack);
});
}
});
}
}
}
});
};