-
-
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.
Merge pull request #390 from haseebzaki-07/new_branch_3
Feat: Add feedback mail
- Loading branch information
Showing
3 changed files
with
55 additions
and
16 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
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" | ||
// } |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.