-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
165 lines (152 loc) · 4.69 KB
/
app.js
File metadata and controls
165 lines (152 loc) · 4.69 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
require("dotenv").config();
var Mailgen = require("mailgen");
const MAILGUN_API_KEY = process.env.MAILGUN_API_KEY;
const MAILGUN_DOMAIN = process.env.MAILGUN_DOMAIN;
var mailgun = require("mailgun-js")({
apiKey: MAILGUN_API_KEY,
domain: MAILGUN_DOMAIN,
});
// Configure mailgen by setting a theme and your product info
const mailGenerator = new Mailgen({
theme: "default",
product: {
// Appears in header & footer of e-mails
name: "SmartFIN",
link: "https://ourfin.tech/",
// Optional product logo
// logo: 'https://mailgen.js/img/logo.png'
},
});
exports.aggregated_email = (event, context) => {
const requestBody = JSON.parse(event.body);
const txnTbank = requestBody.tbank;
const txnOcbc = requestBody.ocbc;
const txnDbs = requestBody.dbs;
var dataTbank = []
for (var i = 0; i < txnTbank.length; i++) {
sign = txnTbank[i].transactionType === "301" ? "-": ""; // 301 is debit, 200 is credit (?) idk lol
dataTbank.push(
{
date: txnTbank[i].transactionDate,
description: txnTbank[i].narrative,
amount: `${sign}${txnTbank[i].transactionAmount}`,
}
)
}
var dataOcbc = []
for (var i = 0; i < txnOcbc.length; i++) {
sign = txnOcbc[i].transactionType === "301" ? "-": ""; // 301 is debit, 200 is credit (?) idk lol
dataOcbc.push(
{
date: txnOcbc[i].transactionDate,
description: txnOcbc[i].narrative,
amount: `${sign}${txnOcbc[i].transactionAmount}`,
}
)
}
var dataDbs = []
for (var i = 0; i < txnDbs.length; i++) {
sign = txnDbs[i].transactionType === "301" ? "-": ""; // 301 is debit, 200 is credit (?) idk lol
dataDbs.push(
{
date: txnDbs[i].transactionDate,
description: txnDbs[i].narrative,
amount: `${sign}${txnDbs[i].transactionAmount}`,
}
)
}
const email = {
body: {
name: "JJ Goi",
intro:
"Here's your weekly aggregated transactions from all the different bank accounts you've linked with SmartFIN.",
table: [
{
// Optionally, add a title to each table.
title: "tBank",
data: dataTbank,
columns: {
// Optionally, customize the column widths
customWidth: {
date: "20%",
amount: "15%",
},
// Optionally, change column text alignment
customAlignment: {
amount: "right",
},
},
},
{
// Optionally, add a title to each table.
title: "OCBC",
data: dataOcbc,
columns: {
// Optionally, customize the column widths
customWidth: {
date: "20%",
amount: "15%",
},
// Optionally, change column text alignment
customAlignment: {
amount: "right",
},
},
},
{
// Optionally, add a title to each table.
title: "DBS",
data: dataDbs,
columns: {
// Optionally, customize the column widths
customWidth: {
date: "20%",
amount: "15%",
},
// Optionally, change column text alignment
customAlignment: {
amount: "right",
},
},
},
],
action: {
instructions:
"To update your notification settings, you can click on the button below.",
button: {
color: "#22BC66", // Optional action button color
text: "Update",
link: "https://ourfin.tech/recipes/add/aggregated_email",
},
},
outro:
"Need help, or have questions? Just reply to this email, we'd love to help.",
},
};
// Generate an HTML email with the provided contents
var emailBody = mailGenerator.generate(email);
// Generate the plaintext version of the e-mail (for clients that do not support HTML)
var emailText = mailGenerator.generatePlaintext(email);
// Send
var mailOptions = {
from: "SmartFIN <hello@ourfin.tech>",
to: requestBody.email,
subject: "[SmartFIN] Your weekly aggregated account transactions",
text: emailText,
html: emailBody,
};
mailgun.messages().send(mailOptions, function (error, body) {
console.log(body);
});
console.log(
`${requestBody.email} sent email for task smartfin.aggregated_email`
);
return {
statusCode: 200,
body: JSON.stringify(
`{"status": 200, "message": "Email has been sent out for smartfin.aggregated_email."}`
),
};
// Optionally, preview the generated HTML e-mail by writing it to a local file
// require("fs").writeFileSync("preview.html", emailBody, "utf8");
};