-
-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
298 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }); | ||
} | ||
|
||
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.' }); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
}); | ||
|
||
// Export the model | ||
module.exports = mongoose.model('NewsletterEmail', NewsletterEmailSchema); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.