-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
165 lines (141 loc) · 5.42 KB
/
app.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
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: "Flex Jr",
link: "https://www.flexjr.one/",
logo: "https://flexjr-assets.s3.ap-southeast-1.amazonaws.com/flex-logo-dark.png",
},
});
exports.subscription_status = (event, context) => {
const requestBody = JSON.parse(event.body);
const status = requestBody.status;
if (status == "success") {
const email = {
body: {
name: requestBody.name,
intro: "Thank you for upgrading!",
action: {
instructions: "To manage your company’s subscriptions and invoices, you can click on the button below.",
button: {
color: "#22BC66", // Optional action button color
text: "Manage Subscriptions",
link: "https://app.flexjr.one/flex/organization/subscriptions/manage",
},
},
outro: "Need help, or have questions? Just reply to this email, we’re happy to answer your questions!",
},
};
// 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: "Flex <[email protected]>",
to: requestBody.email,
subject: "Your Flex subscription has been upgraded!",
text: emailText,
html: emailBody,
};
mailgun.messages().send(mailOptions, function(error, body) {
console.log(body);
});
} else if (status == "failed") {
const email = {
body: {
name: requestBody.name,
intro: "We were not able to charge your Flex Visa card for the subscription, so you’ve not been charged. You can try again later, and if the issue still persists, you can contact us.",
action: {
instructions: "To manage your company’s subscriptions, you can click on the button below.",
button: {
color: "#22BC66", // Optional action button color
text: "Manage Subscriptions",
link: "https://app.flexjr.one/flex/organization/subscriptions/manage",
},
},
outro: "Need help, or have questions? Just reply to this email, we’re happy to answer your questions!",
},
};
// 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: "Flex <[email protected]>",
to: requestBody.email,
subject: "You’ve not been charged for your Flex subscription",
text: emailText,
html: emailBody,
};
mailgun.messages().send(mailOptions, function(error, body) {
console.log(body);
});
}
console.info(
`${requestBody.email} sent email for task flexjr.subscription_status`
);
return {
statusCode: 200,
body: JSON.stringify(
`{"status": 200, "message": "Email has been sent out for flexjr.subscription_status"}`
),
};
// Optionally, preview the generated HTML e-mail by writing it to a local file
// require("fs").writeFileSync("preview.html", emailBody, "utf8");
};
// exports.subscription_request = (event, context) => {
// const requestBody = JSON.parse(event.body);
// const status = requestBody.status;
// const email = {
// body: {
// name: requestBody.name,
// intro: "Your employee has requested for an upgrade!",
// action: {
// instructions: "To manage your company’s subscriptions and invoices, you can click on the button below.",
// button: {
// color: "#22BC66", // Optional action button color
// text: "Manage Subscriptions",
// link: "https://app.flexjr.one/flex/organization/subscriptions/manage",
// },
// },
// outro: "Need help, or have questions? Just reply to this email, we’re happy to answer your questions!",
// },
// };
// // 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: "Flex <[email protected]>",
// to: requestBody.email,
// subject: "Your Flex subscription has been upgraded!",
// text: emailText,
// html: emailBody,
// };
// mailgun.messages().send(mailOptions, function(error, body) {
// console.log(body);
// });
// console.info(
// `${requestBody.email} sent email for task flexjr.subscription_status`
// );
// return {
// statusCode: 200,
// body: JSON.stringify(
// `{"status": 200, "message": "Email has been sent out for flexjr.subscription_status"}`
// ),
// };
// // Optionally, preview the generated HTML e-mail by writing it to a local file
// // require("fs").writeFileSync("preview.html", emailBody, "utf8");
// };