diff --git a/backend/controller/feedback.controller.js b/backend/controller/feedback.controller.js index b46eaddc..dc296335 100644 --- a/backend/controller/feedback.controller.js +++ b/backend/controller/feedback.controller.js @@ -1,8 +1,9 @@ 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(), @@ -10,15 +11,24 @@ const feedbackSchema = z.object({ 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", @@ -26,15 +36,18 @@ async function createFeedback(req, res) { }); } + 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", @@ -42,13 +55,33 @@ async function createFeedback(req, res) { } } + +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": "john@1212.com", -// "feedback": "This is a dummy feedback" -// } diff --git a/frontend/src/components/Pages/Event.jsx b/frontend/src/components/Pages/Event.jsx index e27d335f..9596269e 100644 --- a/frontend/src/components/Pages/Event.jsx +++ b/frontend/src/components/Pages/Event.jsx @@ -253,7 +253,7 @@ function Event() { {events.map((event) => (
-
+

{event.title}

{event.description} diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 00000000..a0c27c60 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,6 @@ +{ + "name": "PlayCafe", + "lockfileVersion": 3, + "requires": true, + "packages": {} +}