forked from Nova-reward/Nova-Rewards
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemailService.js
More file actions
320 lines (302 loc) · 10.9 KB
/
Copy pathemailService.js
File metadata and controls
320 lines (302 loc) · 10.9 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
const nodemailer = require('nodemailer');
const {
SMTP_HOST,
SMTP_PORT,
SMTP_USER,
SMTP_PASSWORD,
EMAIL_FROM,
SENDGRID_API_KEY,
} = require('./configService');
const { asyncLocalStorage, getLogger } = require('../lib/logger');
const {
createEmailLog,
markEmailSent,
markEmailDelivered,
markEmailFailed,
} = require('../db/emailLogRepository');
/**
* Email service for sending transactional emails.
* Requirements: #184
*/
// Create SMTP transporter for local development
const smtpTransporter = nodemailer.createTransport({
host: SMTP_HOST,
port: SMTP_PORT,
secure: SMTP_PORT === 465,
auth: SMTP_USER && SMTP_PASSWORD ? {
user: SMTP_USER,
pass: SMTP_PASSWORD,
} : undefined,
});
/**
* Sends an email using the appropriate transport (SendGrid or SMTP).
* @param {object} options
* @param {string} options.to - Recipient email
* @param {string} options.subject - Email subject
* @param {string} options.html - Email HTML content
* @param {string} options.emailType - Email type for logging
* @returns {Promise<{success: boolean, logId?: number, error?: string}>}
*/
async function sendEmail({ to, subject, html, emailType }) {
let logId;
try {
// Create email log entry
const log = await createEmailLog({
recipientEmail: to,
emailType,
subject,
});
logId = log.id;
// Use SendGrid if API key is available, otherwise use SMTP
if (SENDGRID_API_KEY) {
// SendGrid API implementation
const store = asyncLocalStorage.getStore();
const correlationId = store && store.correlationId;
const response = await fetch('https://api.sendgrid.com/v3/mail/send', {
method: 'POST',
headers: {
'Authorization': `Bearer ${SENDGRID_API_KEY}`,
'Content-Type': 'application/json',
...(correlationId ? { 'x-correlation-id': correlationId } : {}),
},
body: JSON.stringify({
personalizations: [{ to: [{ email: to }] }],
from: { email: EMAIL_FROM },
subject,
content: [{ type: 'text/html', value: html }],
}),
});
if (!response.ok) {
const error = await response.text();
throw new Error(`SendGrid API error: ${error}`);
}
await markEmailSent(logId);
return { success: true, logId };
} else {
// SMTP transport
await smtpTransporter.sendMail({
from: EMAIL_FROM,
to,
subject,
html,
});
await markEmailSent(logId);
return { success: true, logId };
}
} catch (error) {
const logger = getLogger();
logger.error({ err: error }, 'Error sending email');
if (logId) {
await markEmailFailed(logId, error.message);
}
return { success: false, logId, error: error.message };
}
}
/**
* Sends a redemption confirmation email.
* Requirements: #184
*
* @param {object} params
* @param {string} params.to - Recipient email
* @param {string} params.userName - User's display name or wallet address
* @param {string} params.rewardName - Name of the redeemed reward
* @param {number} params.pointsSpent - Points deducted
* @param {number} params.redemptionId - Redemption record ID
* @returns {Promise<{success: boolean, logId?: number, error?: string}>}
*/
async function sendRedemptionConfirmation({ to, userName, rewardName, pointsSpent, redemptionId }) {
const subject = 'NovaRewards - Redemption Confirmation';
const html = `
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: Arial, sans-serif; line-height: 1.6; color: #333; }
.container { max-width: 600px; margin: 0 auto; padding: 20px; }
.header { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 30px; text-align: center; }
.content { background: #f9f9f9; padding: 30px; }
.footer { text-align: center; padding: 20px; color: #666; font-size: 12px; }
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>Redemption Confirmed!</h1>
</div>
<div class="content">
<p>Hi ${userName},</p>
<p>Your redemption has been successfully processed.</p>
<p><strong>Reward:</strong> ${rewardName}</p>
<p><strong>Points spent:</strong> ${pointsSpent}</p>
<p><strong>Redemption ID:</strong> #${redemptionId}</p>
<p>Thank you for being a valued NovaRewards member!</p>
</div>
<div class="footer">
<p>© ${new Date().getFullYear()} NovaRewards. All rights reserved.</p>
</div>
</div>
</body>
</html>
`;
return sendEmail({ to, subject, html, emailType: 'redemption_confirmation' });
}
/**
* Sends a milestone achieved email.
* Requirements: #184
*
* @param {object} params
* @param {string} params.to - Recipient email
* @param {string} params.userName - User's name
* @param {string} params.milestoneName - Milestone name
* @param {string|number} params.rewardAmount - Reward amount
* @returns {Promise<{success: boolean, logId?: number, error?: string}>}
*/
async function sendMilestoneAchieved({ to, userName, milestoneName, rewardAmount }) {
const subject = 'NovaRewards - Milestone Achieved!';
const html = `
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: Arial, sans-serif; line-height: 1.6; color: #333; }
.container { max-width: 600px; margin: 0 auto; padding: 20px; }
.header { background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%); color: white; padding: 30px; text-align: center; }
.content { background: #f9f9f9; padding: 30px; }
.footer { text-align: center; padding: 20px; color: #666; font-size: 12px; }
.milestone { background: #fff; border-left: 4px solid #f5576c; padding: 15px; margin: 20px 0; }
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>🎉 Congratulations!</h1>
</div>
<div class="content">
<p>Hi ${userName},</p>
<p>You've achieved a new milestone!</p>
<div class="milestone">
<h3>${milestoneName}</h3>
<p><strong>Reward:</strong> ${rewardAmount} NOVA</p>
</div>
<p>Keep up the great work and continue earning rewards!</p>
</div>
<div class="footer">
<p>© ${new Date().getFullYear()} NovaRewards. All rights reserved.</p>
</div>
</div>
</body>
</html>
`;
return sendEmail({ to, subject, html, emailType: 'milestone_achieved' });
}
/**
* Sends a welcome email.
* Requirements: #184
*
* @param {object} params
* @param {string} params.to - Recipient email
* @param {string} params.userName - User's name
* @param {string} [params.referralCode] - Optional referral code
* @returns {Promise<{success: boolean, logId?: number, error?: string}>}
*/
async function sendWelcome({ to, userName, referralCode }) {
const subject = 'Welcome to NovaRewards!';
const referralSection = referralCode ? `
<div class="referral">
<h3>Share & Earn</h3>
<p>Share your referral code with friends and earn bonus points:</p>
<p><strong>${referralCode}</strong></p>
</div>
` : '';
const html = `
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: Arial, sans-serif; line-height: 1.6; color: #333; }
.container { max-width: 600px; margin: 0 auto; padding: 20px; }
.header { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 30px; text-align: center; }
.content { background: #f9f9f9; padding: 30px; }
.footer { text-align: center; padding: 20px; color: #666; font-size: 12px; }
.referral { background: #fff; border-left: 4px solid #667eea; padding: 15px; margin: 20px 0; }
.button { display: inline-block; padding: 12px 30px; background: #667eea; color: white; text-decoration: none; border-radius: 5px; margin: 20px 0; }
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>Welcome to NovaRewards!</h1>
</div>
<div class="content">
<p>Hi ${userName},</p>
<p>Welcome to NovaRewards! We're excited to have you on board.</p>
<p>Start earning rewards by completing tasks, referring friends, and participating in campaigns.</p>
${referralSection}
<p>Get started by exploring your dashboard and discovering all the ways to earn NOVA tokens!</p>
</div>
<div class="footer">
<p>© ${new Date().getFullYear()} NovaRewards. All rights reserved.</p>
</div>
</div>
</body>
</html>
`;
return sendEmail({ to, subject, html, emailType: 'welcome' });
}
/**
* Sends a password reset email.
* Requirements: #184
*
* @param {object} params
* @param {string} params.to - Recipient email
* @param {string} params.userName - User's name
* @param {string} params.resetLink - Password reset link
* @returns {Promise<{success: boolean, logId?: number, error?: string}>}
*/
async function sendPasswordReset({ to, userName, resetLink }) {
const subject = 'NovaRewards - Password Reset Request';
const html = `
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: Arial, sans-serif; line-height: 1.6; color: #333; }
.container { max-width: 600px; margin: 0 auto; padding: 20px; }
.header { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 30px; text-align: center; }
.content { background: #f9f9f9; padding: 30px; }
.footer { text-align: center; padding: 20px; color: #666; font-size: 12px; }
.button { display: inline-block; padding: 12px 30px; background: #667eea; color: white; text-decoration: none; border-radius: 5px; margin: 20px 0; }
.warning { background: #fff3cd; border: 1px solid #ffc107; padding: 15px; margin: 20px 0; border-radius: 5px; }
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>Password Reset Request</h1>
</div>
<div class="content">
<p>Hi ${userName},</p>
<p>We received a request to reset your password. Click the button below to create a new password:</p>
<p style="text-align: center;">
<a href="${resetLink}" class="button">Reset Password</a>
</p>
<div class="warning">
<p><strong>Note:</strong> This link will expire in 24 hours. If you didn't request a password reset, please ignore this email.</p>
</div>
</div>
<div class="footer">
<p>© ${new Date().getFullYear()} NovaRewards. All rights reserved.</p>
</div>
</div>
</body>
</html>
`;
return sendEmail({ to, subject, html, emailType: 'password_reset' });
}
module.exports = {
sendEmail,
sendRedemptionConfirmation,
sendMilestoneAchieved,
sendWelcome,
sendPasswordReset,
};