Skip to content

Commit

Permalink
Merge branch 'main' into Feat/ImageUpload-Cloudinary
Browse files Browse the repository at this point in the history
  • Loading branch information
harshbhar0629 authored Nov 1, 2024
2 parents 7c305f2 + f5617e3 commit 18271a7
Show file tree
Hide file tree
Showing 16 changed files with 349 additions and 123 deletions.
75 changes: 36 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,17 +290,17 @@ We extend our heartfelt gratitude to all the amazing contributors who have made
</a>
</td>
<td align="center">
<a href="https://github.com/itznayan">
<img src="https://avatars.githubusercontent.com/u/136584376?v=4" width="100;" alt="itznayan"/>
<a href="https://github.com/tejasbenibagde">
<img src="https://avatars.githubusercontent.com/u/124677750?v=4" width="100;" alt="tejasbenibagde"/>
<br />
<sub><b>Mahera Nayan</b></sub>
<sub><b>Tejas Benibagde</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/tejasbenibagde">
<img src="https://avatars.githubusercontent.com/u/124677750?v=4" width="100;" alt="tejasbenibagde"/>
<a href="https://github.com/itznayan">
<img src="https://avatars.githubusercontent.com/u/136584376?v=4" width="100;" alt="itznayan"/>
<br />
<sub><b>Tejas Benibagde</b></sub>
<sub><b>Mahera Nayan</b></sub>
</a>
</td>
<td align="center">
Expand All @@ -311,40 +311,40 @@ We extend our heartfelt gratitude to all the amazing contributors who have made
</a>
</td>
<td align="center">
<a href="https://github.com/NilanchalaPanda">
<img src="https://avatars.githubusercontent.com/u/110488337?v=4" width="100;" alt="NilanchalaPanda"/>
<a href="https://github.com/meghanakn22">
<img src="https://avatars.githubusercontent.com/u/172406754?v=4" width="100;" alt="meghanakn22"/>
<br />
<sub><b>Nilanchal</b></sub>
<sub><b>meghanakn22</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/Amnyadav">
<img src="https://avatars.githubusercontent.com/u/127370497?v=4" width="100;" alt="Amnyadav"/>
<a href="https://github.com/VinayLodhi1712">
<img src="https://avatars.githubusercontent.com/u/135756009?v=4" width="100;" alt="VinayLodhi1712"/>
<br />
<sub><b>Aman Yadav</b></sub>
<sub><b>Vinay Anand Lodhi</b></sub>
</a>
</td>
</tr>
<tr>
<td align="center">
<a href="https://github.com/VinayLodhi1712">
<img src="https://avatars.githubusercontent.com/u/135756009?v=4" width="100;" alt="VinayLodhi1712"/>
<a href="https://github.com/Amnyadav">
<img src="https://avatars.githubusercontent.com/u/127370497?v=4" width="100;" alt="Amnyadav"/>
<br />
<sub><b>Vinay Anand Lodhi</b></sub>
<sub><b>Aman Yadav</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/meghanakn22">
<img src="https://avatars.githubusercontent.com/u/172406754?v=4" width="100;" alt="meghanakn22"/>
<a href="https://github.com/NilanchalaPanda">
<img src="https://avatars.githubusercontent.com/u/110488337?v=4" width="100;" alt="NilanchalaPanda"/>
<br />
<sub><b>meghanakn22</b></sub>
<sub><b>Nilanchal</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/Sawan-Kushwah">
<img src="https://avatars.githubusercontent.com/u/138680328?v=4" width="100;" alt="Sawan-Kushwah"/>
<a href="https://github.com/Suhas-Koheda">
<img src="https://avatars.githubusercontent.com/u/72063139?v=4" width="100;" alt="Suhas-Koheda"/>
<br />
<sub><b>Sawan kushwah </b></sub>
<sub><b>Suhas Koheda</b></sub>
</a>
</td>
<td align="center">
Expand All @@ -355,21 +355,28 @@ We extend our heartfelt gratitude to all the amazing contributors who have made
</a>
</td>
<td align="center">
<a href="https://github.com/Suhas-Koheda">
<img src="https://avatars.githubusercontent.com/u/72063139?v=4" width="100;" alt="Suhas-Koheda"/>
<a href="https://github.com/Sawan-Kushwah">
<img src="https://avatars.githubusercontent.com/u/138680328?v=4" width="100;" alt="Sawan-Kushwah"/>
<br />
<sub><b>Suhas Koheda</b></sub>
<sub><b>Sawan kushwah </b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/CoderFleet">
<img src="https://avatars.githubusercontent.com/u/87255169?v=4" width="100;" alt="CoderFleet"/>
<br />
<sub><b>Rudransh Pratap Singh</b></sub>
</a>
</td>
</tr>
<tr>
<td align="center">
<a href="https://github.com/Jay-1409">
<img src="https://avatars.githubusercontent.com/u/166749819?v=4" width="100;" alt="Jay-1409"/>
<br />
<sub><b>Jay shah</b></sub>
</a>
</td>
</tr>
<tr>
<td align="center">
<a href="https://github.com/vishnuprasad2004">
<img src="https://avatars.githubusercontent.com/u/116942066?v=4" width="100;" alt="vishnuprasad2004"/>
Expand Down Expand Up @@ -405,21 +412,14 @@ We extend our heartfelt gratitude to all the amazing contributors who have made
<sub><b>Abhijit Motekar</b></sub>
</a>
</td>
</tr>
<tr>
<td align="center">
<a href="https://github.com/Navneetdadhich">
<img src="https://avatars.githubusercontent.com/u/156535853?v=4" width="100;" alt="Navneetdadhich"/>
<br />
<sub><b>Navneet Dadhich</b></sub>
</a>
</td>
</tr>
<tr>
<td align="center">
<a href="https://github.com/CoderFleet">
<img src="https://avatars.githubusercontent.com/u/87255169?v=4" width="100;" alt="CoderFleet"/>
<br />
<sub><b>Rudransh Pratap Singh</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/AE-Hertz">
Expand Down Expand Up @@ -697,7 +697,4 @@ Stay updated and engage with our community on social media:
- [LinkedIn](https://www.linkedin.com/in/ramakrushna-biswal/)
- [Email](mailto:[email protected])

We are always here to help you! Don’t hesitate to connect with us and be part of the PlayCafe journey.



We are always here to help you! Don’t hesitate to connect with us and be part of the PlayCafe journey.
42 changes: 18 additions & 24 deletions backend/controller/customer.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,54 +102,48 @@ async function loginCustomer(req, res) {
const customerLoginSchema = z.object({
email: z.string().email("Invalid email address"),
password: z.string().min(6, "Password must be at least 6 characters long"),
rememberMe: z.boolean().optional(),
});


const validation = customerLoginSchema.safeParse(req.body);
if (!validation.success) {
return res.status(400).json({ error: validation.error.errors });
}

try {
const customer = await Customer.findOne({ email: req.body.email });

const { email, password, rememberMe } = req.body;
const customer = await Customer.findOne({ email });

if (!customer) {
return res.status(401).json({ error: "Invalid email or password" });
}
if (!customer.isVerified) {
return res.status(403).json({ error: "Account not verified. Please verify your email." });
}

const validPassword = await bcrypt.compare(req.body.password, customer.password);

const validPassword = await bcrypt.compare(password, customer.password);
if (!validPassword) {
return res.status(401).json({ error: "Invalid email or password" });
}

const payload = {
sub: customer._id,
name: customer.name, // Optional
role: "customer", // Optional
email: customer.email, // Optional
};

const token = jwt.sign(
payload,
process.env.JWT_SECRET,
{ expiresIn: "1h" } // Expires in 1 hour
);

req.session.user = {
id: customer._id,
sub: customer._id,
name: customer.name,
role: "customer",
email: customer.email,
};

const token = jwt.sign(payload, process.env.JWT_SECRET, {
expiresIn: rememberMe ? "7d" : "1h", // Set token expiry based on rememberMe option
});

res.cookie("authToken", token, {
maxAge: 1000 * 60 * 60,
httpOnly: true,
secure: true,
maxAge: rememberMe ? 7 * 24 * 60 * 60 * 1000 : 60 * 60 * 1000, // 7 days or 1 hour
httpOnly: true,
secure: process.env.NODE_ENV === "production",
sameSite: "strict",
});

return res.json({
message: "Login successful",
token,
Expand All @@ -162,12 +156,12 @@ async function loginCustomer(req, res) {
});
} catch (error) {
console.error("Error during login:", error);

res.status(500).json({ error: "Internal server error" });
}
}



async function resetPassword(req, res) {
const customerResetPasswordSchema = z.object({
email: z.string().email("Invalid email address"),
Expand Down
76 changes: 69 additions & 7 deletions backend/controller/reservation.controller.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
const { z } = require("zod");
const Reservation = require("../models/reservation.model");
const Customer = require("../models/customer.model"); // Import Customer model
const logger = require("../config/logger");
const { sendReservationConfirmation } = require("../config/nodemailer"); // Import your email function
const { sendReservationConfirmation } = require("../config/nodemailer"); // Import 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
email: z.string().email(), // Include email validation
userId: z.string().optional(), // Make userId optional for validation
})
.strict(); // Disallow unknown keys

// Controller to create a reservation
async function createReservation(req, res) {
try {
const validationResult = reservationSchema.safeParse(req.body);
Expand All @@ -28,13 +31,29 @@ async function createReservation(req, res) {
errors: validationResult.error.errors,
});
}
const userId = req.params.id

// Create the reservation in the database
const reservation = await Reservation.create(validationResult.data);
const { email, date, guests, time } = validationResult.data;

// Find the customer by userId if provided
const customer = await Customer.findById(userId);
if (!customer) {
return res.status(404).json({
success: false,
message: "Customer not found.",
});
}

// Create the reservation in the database with userId reference
const reservation = await Reservation.create({
guests,
date,
time,
customer: customer._id, // Associate with customer
});

// Send a confirmation email
try {
const { email, date, guests, time } = validationResult.data;
await sendReservationConfirmation(email, {
reservationDate: date,
guests,
Expand All @@ -45,10 +64,10 @@ async function createReservation(req, res) {
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
// Email error does not block reservation creation
}

// Send the success response
// Respond with success
res.status(201).json({
success: true,
message: "Reservation created successfully",
Expand All @@ -68,6 +87,49 @@ async function createReservation(req, res) {
}
}

// Controller to fetch all reservations for a specific user
async function fetchUserReservations(req, res) {
try {
const userId = req.params.id; // Extract user ID from route parameters

if (!userId) {
return res.status(400).json({
success: false,
message: "User ID is required.",
});
}

// Find all reservations associated with the customer
const reservations = await Reservation.find({ customer: userId }).populate("customer", "name email");

if (reservations.length === 0) {
logger.info(`No reservations found for user ID: ${userId}`);
return res.status(404).json({
success: false,
message: "No reservations found for this user.",
});
}

res.status(200).json({
success: true,
message: "Reservations retrieved successfully.",
data: reservations,
});
} catch (error) {
logger.error("Error fetching user reservations:", {
message: error.message,
stack: error.stack,
userId: req.params.id,
});

res.status(500).json({
success: false,
message: "An error occurred while fetching reservations.",
});
}
}

module.exports = {
createReservation,
fetchUserReservations
};
3 changes: 3 additions & 0 deletions backend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,20 @@ const app = express();
const port = process.env.PORT || 3000;
const session = require("express-session");
const MongoStore = require("connect-mongo");

const fileUpload = require("express-fileupload");
const { cloudinaryConnect } = require("./config/cloudinary");

// CORS configuration
const corsOptions = {
origin: ["http://localhost:5173", "https://play-cafe.vercel.app"],
credentials: true,
optionsSuccessStatus: 200,
};

app.use(cors(corsOptions));


app.use(express.json());
app.use('/api', newsletterRoute);
app.use(
Expand Down
Loading

0 comments on commit 18271a7

Please sign in to comment.