-
-
Notifications
You must be signed in to change notification settings - Fork 103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Email is sent on successful reservation #121
Changes from all commits
087bf5e
9fe8451
d6e8dcc
959b2fd
964ef7b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
MONGO_URI=enter_your_mongo_uri | ||
MONGO_URI=enter_your_mongo_uri | ||
EMAIL_USER=your_gmail | ||
EMAIL_PASS=your_16_digit_pass |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
require("dotenv").config(); | ||
const nodemailer = require("nodemailer"); | ||
const logger = require('./logger'); | ||
|
||
// Create a Nodemailer transporter using SMTP | ||
const transporter = nodemailer.createTransport({ | ||
service: "gmail", // or your preferred email service | ||
auth: { | ||
user: process.env.EMAIL_USER, | ||
pass: process.env.EMAIL_PASS, | ||
}, | ||
}); | ||
|
||
// Function to send reservation confirmation via email | ||
exports.sendReservationConfirmation = async (email, reservationDetails) => { | ||
const { reservationDate, guests, time } = reservationDetails; | ||
|
||
// Construct the email content | ||
const emailText = ` | ||
Dear Customer, | ||
|
||
We are pleased to confirm your reservation. Here are the details of your reservation: | ||
|
||
Reservation Date: ${reservationDate} | ||
Number of Guests: ${guests} | ||
Reservation Time: ${time} | ||
|
||
Thank you for choosing our service. We look forward to hosting you. | ||
|
||
Best regards, | ||
PlayCafe | ||
`; | ||
|
||
try { | ||
await transporter.sendMail({ | ||
from: process.env.EMAIL_USER, | ||
to: email, | ||
subject: "Reservation Confirmation", | ||
text: emailText, | ||
}); | ||
logger.info('Reservation confirmation sent successfully via email', { email }); | ||
} catch (error) { | ||
logger.error('Failed to send reservation confirmation email', { error, email }); | ||
if (error.code === 'ECONNREFUSED') { | ||
throw new Error('Failed to connect to email server. Please try again later.'); | ||
} else { | ||
throw new Error(`Failed to send reservation confirmation email: ${error.message}`); | ||
} | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,15 @@ | ||
const { z } = require("zod"); | ||
const Reservation = require("../models/reservation.model"); | ||
const logger = require("../config/logger"); // Import your logger | ||
const logger = require("../config/logger"); | ||
const { sendReservationConfirmation } = require("../config/nodemailer"); // Import your email function | ||
|
||
// Define the Zod schema for reservation validation | ||
const reservationSchema = z.object({ | ||
guests: z.string(), | ||
date: z.string(), | ||
time: z.string(), | ||
}); | ||
email: z.string().email(), // Include email validation in the schema | ||
}).strict(); // Disallow unknown keys | ||
|
||
async function createReservation(req, res) { | ||
try { | ||
|
@@ -25,8 +27,22 @@ async function createReservation(req, res) { | |
}); | ||
} | ||
|
||
// Create the reservation in the database | ||
const reservation = await Reservation.create(validationResult.data); | ||
Comment on lines
+30
to
31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Potential security risk: Prevent extra unwanted fields in request data The current schema may allow extra fields in Apply this diff to enforce strict schema validation: -const reservationSchema = z.object({
+const reservationSchema = z.object({
guests: z.string(),
date: z.string(),
time: z.string(),
email: z.string().email(),
+}).strict(); // Disallow unknown keys
|
||
|
||
// Send a confirmation email | ||
try { | ||
const { email, date, guests, time } = validationResult.data; | ||
await sendReservationConfirmation(email, { reservationDate: date, guests, time }); | ||
logger.info(`Reservation confirmation email sent to ${email}`); | ||
} catch (emailError) { | ||
logger.error("Error sending reservation confirmation email:", { | ||
message: emailError.message, | ||
}); | ||
// Email error should not block the main reservation process, so no need to return a failure response | ||
} | ||
|
||
// Send the success response | ||
res.status(201).json({ | ||
success: true, | ||
message: "Reservation created successfully", | ||
|
@@ -48,4 +64,4 @@ async function createReservation(req, res) { | |
|
||
module.exports = { | ||
createReservation, | ||
}; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { createContext, useState, useContext } from "react"; | ||
|
||
const AuthContext = createContext(); | ||
|
||
export const AuthProvider = ({ children }) => { | ||
const [email, setEmail] = useState(null); // Store user's email | ||
|
||
return ( | ||
<AuthContext.Provider value={{ email, setEmail }}> | ||
{children} | ||
</AuthContext.Provider> | ||
); | ||
}; | ||
|
||
export const useAuth = () => useContext(AuthContext); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Consider internationalizing the email content.
The email content is currently hardcoded in English. For a potentially global user base, consider implementing internationalization (i18n) for the email content. This would allow for sending emails in the user's preferred language.
Example implementation: