Skip to content

Commit

Permalink
Add feedback mail
Browse files Browse the repository at this point in the history
  • Loading branch information
haseebzaki-07 committed Oct 23, 2024
1 parent 9682169 commit cc94f55
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 16 deletions.
61 changes: 47 additions & 14 deletions backend/controller/feedback.controller.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,87 @@
const { z } = require("zod");
const { Feedback } = require("../models/feedback.model");
const logger = require("../config/logger"); // Import your logger
const logger = require("../config/logger");
const nodemailer = require("nodemailer");


// Define the Zod schema for feedback validation
const feedbackSchema = z.object({
name: z.string().min(2).max(100),
email: z.string().email(),
feedback: z.string().min(10),
rating: z.number().min(1).max(5),
});


const transporter = nodemailer.createTransport({
service: "gmail",
auth: {
user: process.env.EMAIL_USER,
pass: process.env.EMAIL_PASS,
},
});

async function createFeedback(req, res) {
try {
const validationResult = feedbackSchema.safeParse(req.body);

if (!validationResult.success) {
logger.error("Validation error:", {
errors: validationResult.error.errors, // Log the detailed validation errors
body: req.body, // Optionally log the request body for context
}); // Use logger for validation errors
errors: validationResult.error.errors,
body: req.body,
});
return res.status(400).json({
success: false,
message: "Validation failed",
errors: validationResult.error.errors,
});
}


const feedback = await Feedback.create(validationResult.data);

await sendThankYouEmail(feedback);

res.status(201).json({
success: true,
message: "Feedback created successfully",
message: "Feedback created successfully and email sent",
data: feedback,
});
} catch (error) {
logger.error("Error creating feedback:", error); // Log the error using Winston
logger.error("Error creating feedback:", error);
res.status(500).json({
success: false,
message: "An error occurred while creating the feedback",
});
}
}


async function sendThankYouEmail(feedback) {
const mailOptions = {
from: process.env.EMAIL_USER, // Sender email
to: feedback.email, // Receiver email
subject: "Thank you for your feedback!",
text: `Hi ${feedback.name},
Thank you for your valuable feedback. Here are the details:
- Feedback: ${feedback.feedback}
- Rating: ${feedback.rating}/5
We appreciate you taking the time to share your thoughts with us!
Best regards,
Play Cafe`,
};

try {
await transporter.sendMail(mailOptions);
logger.info(`Thank-you email sent to ${feedback.email}`);
} catch (error) {
logger.error("Error sending email:", error);
}
}

module.exports = {
createFeedback,
};

// Dummy API call for feedback
// {
// "name": "John Doe",
// "email": "[email protected]",
// "feedback": "This is a dummy feedback"
// }
4 changes: 2 additions & 2 deletions frontend/src/components/Pages/Event.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ function Event() {
{events.map((event) => (
<div
key={event._id}
className="grid grid-cols-1 md:grid-cols-2 gap-10 lg:grid-cols-1 xl:grid-cols-2 md:px-6 lg:px-4 xl:px-0"
className="grid grid-cols-1 md:grid-cols-2 gap-10 lg:grid-cols-1 xl:grid-cols-2 md:px-6 lg:px-4 xl:px-0 "
>
<div className="w-full m-10 mx-auto lg:mx-0 md:mx-0">
<img
Expand All @@ -263,7 +263,7 @@ function Event() {
className="h-[400px] w-full"
/>
</div>
<div className="w-full lg:m-10 md:m-10">
<div className="w-full lg:m-10 md:m-10 ">
<h1 className="text-4xl font-semibold">{event.title}</h1>
<h4 className="text-xl text-muted text-slate-700 italic mt-2 leading-8">
{event.description}
Expand Down
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cc94f55

Please sign in to comment.