Skip to content

Commit

Permalink
Merge pull request #1849 from haseebzaki-07/new_branch_4
Browse files Browse the repository at this point in the history
Add subscribe
  • Loading branch information
PriyaGhosal authored Nov 10, 2024
2 parents 25031b5 + 7eabd3a commit 387a9b3
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 25 deletions.
4 changes: 3 additions & 1 deletion backend/.env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
MONGO_URI=your_mongo_uri
JWT_SECRET=your_jwt_secret
PORT=5000
PORT=5000
ADMIN_EMAIL=
ADMIN_EMAIL_PASSWORD=
53 changes: 29 additions & 24 deletions backend/controllers/ContactController.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
const ContactUs = require("../models/ContactUs");

const FeedbackModal = require("../models/Feedback");

const SuggestionModal = require("../models/suggestion");


const nodemailer = require("nodemailer");

// Submit Contact Form
exports.submitContactForm = async (req, res) => {
const { name, email, message } = req.body;

Expand All @@ -21,66 +22,70 @@ exports.submitContactForm = async (req, res) => {
}
};

// User Feedback
exports.userfeedback = async (req, res) => {
try {
const { Name, Destination, Rating, Review, Complaint } = req.body;

// Check if all required fields are provided
if (!Name || !Destination || !Rating || !Review) {
return res.status(400).send({
return res.status(400).json({
message: "All fields are required",
});
}
const response = new FeedbackModal({

const feedbackData = new FeedbackModal({
name: Name,
destination: Destination,
rating: Rating,
review: Review,
complaint: Complaint,
});

const feedback = await response.save();
if (response) {
res.status(201).send({
success: true,
message: "feedback recorded ",
feedback,
});
}
const feedback = await feedbackData.save();
return res.status(201).json({
success: true,
message: "Feedback recorded",
feedback,
});
} catch (error) {
console.log(error);
res.status(500).send({
console.error("Error saving feedback:", error);
return res.status(500).json({
success: false,
message: "internal server error ",
message: "Internal server error",
});
}
};

exports.sendEmail = async (req, resp) => {
// Send Email
exports.sendEmail = async (req, res) => {
const transporter = nodemailer.createTransport({
service: "gmail",
auth: {
user: "your email id",
pass: "your password",
user: "your-email@gmail.com",
pass: "your-email-password",
},
});

const mailOptions = {
from: req.body.Email,
to: "your email id",
to: "your-email@gmail.com",
subject: "BuddyTrail user message",
text: `
Name: ${req.body.Name}
Phone:${req.body.Phone}
Phone: ${req.body.Phone}
Email: ${req.body.Email}
Message: ${req.body.Message}`,
Message: ${req.body.Message}
`,
};

transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.log("Error sending email: " + error);
resp.status(500).send("Error sending email");
console.error("Error sending email:", error);
return res.status(500).send("Error sending email");
} else {
console.log("Email sent: " + info.response);
resp.status(200).send("Form data sent successfully");
console.log("Email sent:", info.response);
return res.status(200).send("Form data sent successfully");
}
});
};
Expand Down
34 changes: 34 additions & 0 deletions backend/controllers/subscribeController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const nodemailer = require('nodemailer');
const dotenv = require('dotenv');
dotenv.config();

// Configure Nodemailer
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: process.env.ADMIN_EMAIL,
pass: process.env.ADMIN_EMAIL_PASSWORD,
},
});

// Subscription handler
exports.subscribe = async (req, res) => {
const { email } = req.body;

// Define the confirmation email content
const mailOptions = {
from: "BuddyTrail",
to: email,
subject: 'Thank you for subscribing BuddyTrail!',
text: `Thank you for subscribing to our newsletter! We'll keep you updated with the latest news and offers.`,
};

try {
// Send the confirmation email
await transporter.sendMail(mailOptions);
res.status(200).json({ message: 'Subscription successful! A confirmation email has been sent.' });
} catch (error) {
console.error('Error sending confirmation email:', error);
res.status(500).json({ message: 'Failed to send confirmation email. Please try again later.' });
}
};
3 changes: 3 additions & 0 deletions backend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ const connectDB = require('./config/db'); // Database connection
const authRoutes = require('./routes/authRoutes'); // Authentication routes
const ContactRoutes = require('./routes/ContactRoutes');
const RatingRoutes = require('./routes/RatingRoutes');
const SubscribeRoute = require('./routes/SubscribeRoute');
const cookieParser = require('cookie-parser'); // Middleware for parsing cookies
const config = require('./config/config'); // Config file for environment variables
const cors = require('cors'); // Middleware for Cross-Origin Resource Sharing



// Initialize express app
const app = express();

Expand Down Expand Up @@ -42,6 +44,7 @@ app.use(cookieParser()); // Enable cookie parsing
app.use('/api/auth', authRoutes);
app.use('/api/contact', ContactRoutes);
app.use('/api/rating', RatingRoutes);
app.use("/api/subscribe", SubscribeRoute )


// Server listening on configured port
Expand Down
9 changes: 9 additions & 0 deletions backend/routes/SubscribeRoute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const express = require('express');
const { subscribe } = require('../controllers/subscribeController.js');


const router = express.Router();

router.post('/', subscribe);

module.exports = router;
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3679,6 +3679,7 @@ <h2>Exclusive Deals and Offers!</h2>

<!-- Scripts -->
<script src="script.js"></script>
<script src="/script/subscribe.js"></script>
<script>
function toggleChatbot() {
const chatbotContainer = document.getElementById('chatbotContainer');
Expand Down
35 changes: 35 additions & 0 deletions script/subscribe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
document.addEventListener("DOMContentLoaded", () => {
const form = document.getElementById("newsletter-form");
const subscriptionMessage = document.getElementById("subscription-message");

form.addEventListener("submit", async (event) => {
event.preventDefault();

const email = document.getElementById("email").value;

try {
const response = await fetch("http://localhost:5000/api/subscribe/", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ email }),
});

if (response.ok) {
subscriptionMessage.textContent = "Thank you for subscribing!";
subscriptionMessage.style.color = "green";
form.reset();
} else {
const errorData = await response.json();
subscriptionMessage.textContent = errorData.message || "Subscription failed. Please try again.";
subscriptionMessage.style.color = "red";
}
} catch (error) {
console.error("Error:", error);
subscriptionMessage.textContent = "An error occurred. Please try again.";
subscriptionMessage.style.color = "red";
}
});
});

0 comments on commit 387a9b3

Please sign in to comment.