-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailsRoutes.js
160 lines (139 loc) · 5.33 KB
/
mailsRoutes.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
const express = require('express');
const router = express.Router();
const mailSender = require('./config/mailSender');
router.post("/announce_publishment", async (req, res) => {
const translate = req.__;
try {
const { publisherName, publisherEmail, publisherPhoneNumber, publishmentNote } = req.body;
if (!publisherName || !publisherEmail || !publisherPhoneNumber || !publishmentNote)
return res.status(400).json({
message: "Missing required fields error",
errors: [!publisherName && "publisherName", !publisherEmail && "publisherEmail", !publisherPhoneNumber && "publisherPhoneNumber", !publishmentNote && "publishmentNote"]
.filter(a => !!a)
.map(attr => ({
"message": `Required '${attr}' field is missing`
}))
});
const mailOptions = {
to: 'info@myapp.com',
from: `MailSender <info@mailsender.com>`,
subject: "New Version",
template: 'publishmentAnnounce',
context: {
subject: 'New Version Announcment',
appName: "Designed Mail Sender",
verisonName: "Hero Version (1.2.0)",
webPageName: "HUAWEI AppGallery",
publishmentInformations: {
publisherName: publisherName,
publisherEmail: publisherEmail,
publisherPhoneNumber: publisherPhoneNumber,
publishmentNote: publishmentNote,
}
}
};
const result = await mailSender(mailOptions);
if (!result) {
return res.status(400).json({
message: translate("Sending %s failed.", translate("Version Announcment"))
});
}
return res.status(200).json({
status: 200,
message: translate("%s successfully sent.", translate("Version Announcment"))
});
} catch (error) {
console.error({ error });
res.status(400).json({
message: translate("Sending %s failed.", translate("Version Announcment"))
});
}
})
router.post("/forgot_password", async (req, res) => {
const translate = req.__;
try {
const { email } = req.body;
if (!email)
return res.status(400).json({
message: "Missing required fields error",
errors: [!email && "email"]
.filter(a => !!a)
.map(attr => ({
"message": `Required '${attr}' field is missing`
}))
});
const userName = "Name from database";
const verifyToken = "Generate a token as a temp password.";
const mailOptions = {
to: 'email',
from: `MailSender <info@mailsender.com>`,
subject: "Forgot Password",
template: 'forgotPassword',
context: {
subject: 'Forgot Password',
userName: userName,
appName: "mailsender",
hostName: "http://mailsender.com", //req.headers.host
verifyToken: verifyToken,
}
};
const result = await mailSender(mailOptions);
if (!result) {
return res.status(400).json({
message: translate("Sending %s failed.", translate("Forgot Password"))
});
}
return res.status(200).json({
status: 200,
message: translate("%s successfully sent.", translate("Forgot Password"))
});
} catch (error) {
console.error({ error });
res.status(400).json({
message: translate("Sending %s failed.", translate("Forgot Password"))
});
}
})
router.post("/password_changed", async (req, res) => {
const translate = req.__;
try {
const { userName } = req.body;
if (!userName)
return res.status(400).json({
message: "Missing required fields error",
errors: [!userName && "userName"]
.filter(a => !!a)
.map(attr => ({
"message": `Required '${attr}' field is missing`
}))
});
const mailOptions = {
to: 'email',
from: `MailSender <info@mailsender.com>`,
subject: "Password Changed Warning",
template: 'forgotPassword',
context: {
subject: 'Password Changed Warning',
userName: userName,
appName: "mailsender",
webContactFormPath:"http://mymailsenderapp.com/contact"
}
};
const result = await mailSender(mailOptions);
if (!result) {
return res.status(400).json({
message: translate("Sending %s failed.", translate("Password Changed Warning"))
});
}
return res.status(200).json({
status: 200,
message: translate("%s successfully sent.", translate("Password Changed Warning"))
});
} catch (error) {
console.error({ error });
res.status(400).json({
message: translate("Sending %s failed.", translate("Password Changed Warning"))
});
}
})
module.exports = router;