-
-
Notifications
You must be signed in to change notification settings - Fork 103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Newsletter saved to db and a thank you mail is sent to email #174
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -11,6 +11,40 @@ const transporter = nodemailer.createTransport({ | |||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
// Function to send newsletter subscription confirmation via email | ||||||||||||||||||||||||||||||||||||||
exports.sendSubscriptionConfirmation = async (email) => { | ||||||||||||||||||||||||||||||||||||||
// Construct the email content | ||||||||||||||||||||||||||||||||||||||
const emailText = ` | ||||||||||||||||||||||||||||||||||||||
Dear Customer, | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
Thank you for subscribing to our newsletter! We're excited to have you on board. | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
You will now receive regular updates about our latest boardgame collections, special offers, and upcoming events. | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
If you have any questions or feedback, feel free to reach out. | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
Best regards, | ||||||||||||||||||||||||||||||||||||||
PlayCafe Team | ||||||||||||||||||||||||||||||||||||||
`; | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
try { | ||||||||||||||||||||||||||||||||||||||
await transporter.sendMail({ | ||||||||||||||||||||||||||||||||||||||
from: process.env.EMAIL_USER, | ||||||||||||||||||||||||||||||||||||||
to: email, | ||||||||||||||||||||||||||||||||||||||
subject: "Thank You for Subscribing!", | ||||||||||||||||||||||||||||||||||||||
text: emailText, | ||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||
logger.info('Newsletter subscription confirmation sent successfully via email', { email }); | ||||||||||||||||||||||||||||||||||||||
} catch (error) { | ||||||||||||||||||||||||||||||||||||||
logger.error('Failed to send newsletter subscription confirmation email', { error, email }); | ||||||||||||||||||||||||||||||||||||||
if (error.code === 'ECONNREFUSED') { | ||||||||||||||||||||||||||||||||||||||
throw new Error('Failed to connect to email server. Please try again later.'); | ||||||||||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||||||||||
throw new Error(`Failed to send subscription confirmation email: ${error.message}`); | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
Comment on lines
+37
to
+44
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Enhance error handling and logging The current error handling is good, but there's room for improvement in terms of providing more context and potentially handling different types of errors. Consider the following enhancements:
Here's an example of how you could modify the code: - logger.error('Failed to send newsletter subscription confirmation email', { error, email });
+ logger.error('Failed to send newsletter subscription confirmation email', { error: error.toString(), stack: error.stack, email });
if (error.code === 'ECONNREFUSED') {
- throw new Error('Failed to connect to email server. Please try again later.');
+ throw new Error(`Failed to connect to email server for ${email}. Please try again later.`);
+ } else if (error.code === 'EAUTH') {
+ throw new Error(`Authentication failed when sending email to ${email}. Please check email service credentials.`);
} else {
- throw new Error(`Failed to send subscription confirmation email: ${error.message}`);
+ throw new Error(`Failed to send subscription confirmation email to ${email}: ${error.message}`);
} These changes provide more context in the logs and error messages, which can be crucial for debugging and monitoring the application's email functionality. 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
// Function to send reservation confirmation via email | ||||||||||||||||||||||||||||||||||||||
exports.sendReservationConfirmation = async (email, reservationDetails) => { | ||||||||||||||||||||||||||||||||||||||
const { reservationDate, guests, time } = reservationDetails; | ||||||||||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
const NewsletterEmail = require('../models/newsletter.model'); // Import the Mongoose model | ||
const { sendSubscriptionConfirmation } = require('../config/nodemailer'); // Import the mailer function | ||
|
||
// Controller for handling newsletter subscriptions | ||
exports.subscribeToNewsletter = async (req, res) => { | ||
const { email } = req.body; | ||
|
||
if (!email) { | ||
return res.status(400).json({ error: 'Email is required' }); | ||
} | ||
Comment on lines
+8
to
+10
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Consider adding email format validation. While the code checks if an email is provided, it doesn't validate the email format. Consider adding a regex check or using a validation library to ensure the email is in a valid format before proceeding with the subscription process. Here's a simple regex check you could add: const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!email || !emailRegex.test(email)) {
return res.status(400).json({ error: 'Please provide a valid email address' });
} |
||
|
||
try { | ||
// Check if the email already exists in the database | ||
const existingEmail = await NewsletterEmail.findOne({ email }); | ||
if (existingEmail) { | ||
return res.status(400).json({ error: 'This email is already subscribed.' }); | ||
} | ||
|
||
// Save the email to the database | ||
const newEmail = new NewsletterEmail({ email }); | ||
await newEmail.save(); | ||
|
||
try { | ||
await sendSubscriptionConfirmation(email); | ||
} catch (error) { | ||
console.error('Error sending confirmation email:', error); | ||
return res.status(500).json({ error: 'Subscription successful, but there was an error sending the confirmation email.' }); | ||
} | ||
|
||
return res.status(201).json({ message: 'Subscription successful! A confirmation email has been sent.' }); | ||
} catch (error) { | ||
console.error('Error subscribing to newsletter:', error); | ||
return res.status(500).json({ error: 'Error subscribing to the newsletter.' }); | ||
} | ||
Comment on lines
+31
to
+34
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Improve error handling with more specific error messages. While the current error handling catches and logs errors, it returns a generic message for all types of errors. This might make it difficult to debug issues in production. Consider categorizing errors and returning more specific messages: } catch (error) {
console.error('Error subscribing to newsletter:', error);
if (error.name === 'ValidationError') {
return res.status(400).json({ error: 'Invalid email format.' });
} else if (error.code === 11000) {
return res.status(400).json({ error: 'This email is already subscribed.' });
} else {
return res.status(500).json({ error: 'An unexpected error occurred. Please try again later.' });
}
} This will provide more helpful feedback to both users and developers. |
||
}; |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,19 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
const mongoose = require('mongoose'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Define the schema for newsletter emails | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
const NewsletterEmailSchema = new mongoose.Schema({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
email: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
type: String, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
required: true, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
unique: true, // Ensure no duplicate emails | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
trim: true, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
match: [/.+\@.+\..+/, 'Please enter a valid email address'], // Simple email validation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
subscribedAt: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
type: Date, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
default: Date.now, // Automatically set the date of subscription | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+4
to
+16
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Consider enhancing email validation. The schema structure looks good overall. However, the email validation regex ( Here's a suggested improvement for the email validation: email: {
type: String,
required: true,
unique: true,
trim: true,
- match: [/.+\@.+\..+/, 'Please enter a valid email address'],
+ match: [/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/, 'Please enter a valid email address'],
}, This regex pattern provides a more comprehensive check for email format validity. 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Export the model | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
module.exports = mongoose.model('NewsletterEmail', NewsletterEmailSchema); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const express = require("express"); | ||
const { subscribeToNewsletter } = require("../controller/newsletter.controller"); // Import the controller | ||
const router = express.Router(); | ||
require("dotenv").config(); | ||
|
||
|
||
|
||
router.post("/subscribe", subscribeToNewsletter); | ||
|
||
module.exports = router; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Consider using HTML formatting for the email content
The email content is well-structured and informative. However, plain text emails may not be as visually appealing or engaging for users.
Consider using HTML formatting for the email content to improve readability and allow for more attractive styling. Here's an example of how you could modify the code:
This change would allow for better formatting and potentially include images or styled elements in the future.
📝 Committable suggestion