From 087bf5ecd1e763f175fc9e1e5b7592b7fa676d2c Mon Sep 17 00:00:00 2001 From: Arindam <17arindambera@gmail.com> Date: Mon, 7 Oct 2024 04:58:37 +0530 Subject: [PATCH 1/4] reservation email working --- backend/config/nodemailer.js | 45 +++++++++++++++ backend/controller/reservation.controller.js | 23 +++++++- backend/package.json | 1 + frontend/src/App.jsx | 7 ++- frontend/src/components/Pages/Register.jsx | 6 +- .../src/components/Shared/AuthContext.jsx | 15 +++++ frontend/src/components/Shared/Navbar.jsx | 57 ++++++++++++++----- 7 files changed, 133 insertions(+), 21 deletions(-) create mode 100644 backend/config/nodemailer.js create mode 100644 frontend/src/components/Shared/AuthContext.jsx diff --git a/backend/config/nodemailer.js b/backend/config/nodemailer.js new file mode 100644 index 00000000..b1e7e47d --- /dev/null +++ b/backend/config/nodemailer.js @@ -0,0 +1,45 @@ +require("dotenv").config(); +const nodemailer = require("nodemailer"); + +// 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, + }); + console.log("Reservation confirmation sent successfully via email"); + } catch (error) { + console.error("Error sending reservation confirmation email:", error); + throw new Error("Failed to send reservation confirmation email"); + } +}; \ No newline at end of file diff --git a/backend/controller/reservation.controller.js b/backend/controller/reservation.controller.js index 4ee00658..09a84b9e 100644 --- a/backend/controller/reservation.controller.js +++ b/backend/controller/reservation.controller.js @@ -1,12 +1,14 @@ 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 }); async function createReservation(req, res) { @@ -25,11 +27,26 @@ async function createReservation(req, res) { }); } + // Create the reservation in the database const reservation = await Reservation.create(validationResult.data); + // 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, + stack: emailError.stack, + }); + // 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", + message: "Reservation created successfully, confirmation email sent", data: reservation, }); } catch (error) { @@ -48,4 +65,4 @@ async function createReservation(req, res) { module.exports = { createReservation, -}; +}; \ No newline at end of file diff --git a/backend/package.json b/backend/package.json index 2275182a..cf18051e 100644 --- a/backend/package.json +++ b/backend/package.json @@ -16,6 +16,7 @@ "dotenv": "^16.4.5", "express": "^4.21.0", "mongoose": "^8.7.0", + "nodemailer": "^6.9.15", "validator": "^13.12.0", "winston": "^3.14.2", "zod": "^3.23.8" diff --git a/frontend/src/App.jsx b/frontend/src/App.jsx index 4d22da23..588f8639 100644 --- a/frontend/src/App.jsx +++ b/frontend/src/App.jsx @@ -4,11 +4,12 @@ import './App.css'; import Navbar from '../src/components/Shared/Navbar'; import Footer from "../src/components/Shared/Footer" import { Outlet } from 'react-router-dom'; - +import { AuthProvider } from './components/Shared/AuthContext'; import {KindeProvider} from "@kinde-oss/kinde-auth-react"; function App() { return ( +