Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 43 additions & 7 deletions crud-node-mongo/hotel-backend/controllers/bookingController.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ exports.bookRoom = async (req, res) => {
const existingBooking = await Booking.findOne({
roomNumber,
$or: [
{ startTime: { $lt: endTimeDate }, endTime: { $gt: startTimeDate } },
{ startTime: { $gte: startTimeDate, $lt: endTimeDate } },
{ endTime: { $lte: endTimeDate, $gt: startTimeDate } },
],
{
startTime: { $lt: endTimeDate },//This will cover all the overlap cases.
endTime: { $gt: startTimeDate },
}
]
});

if (existingBooking) {
Expand All @@ -39,7 +40,7 @@ exports.bookRoom = async (req, res) => {

// Calculate duration and price
const duration = (endTimeDate - startTimeDate) / (1000 * 60 * 60); // Convert to hours
const room = await Room.findOne({ roomNumber });
const room = existingBooking;//this unnesessary and wasiting database time and resources
const totalPrice = duration * room.pricePerHour;

// Create new booking
Expand Down Expand Up @@ -150,7 +151,7 @@ exports.cancelBooking = async (req, res) => {
booking.status = "cancelled";
booking.refundAmount = refundAmount;

await Booking.findByIdAndDelete(bookingId);
await booking.save();// You're updating the booking's status to "cancelled" and adding a refundAmount, but then you're deleting that booking from the database right after.**

res
.status(200)
Expand Down Expand Up @@ -183,6 +184,9 @@ exports.viewBookings = async (req, res) => {
query.startTime = { $gte: startDateTime, $lte: endDateTime };
}

// Exclude cancelled bookings by default
query.status = { $ne: "cancelled" };

// Find bookings based on filters
const bookings = await Booking.find(query);

Expand All @@ -191,4 +195,36 @@ exports.viewBookings = async (req, res) => {
console.error(error);
res.status(500).json({ message: "Internal Server Error" });
}
};
};

// Report a booking
// controllers/bookingController.js
exports.reportBooking = async (req, res) => {
try{
const bookingId = req.params.id;
const {reason} = req.body;

const booking = await Booking.findbyIdAndUpdate(
bookingId,
{ isReported: true, reportReason: reason },
{ new: true}
);
if(!booking){
return res.status(404).json({message: "Booking not found"})
}
res.status(200).json({message:"Booking reported",booking});
}
catch(err){
res.status(500).json({message: "Error reporting booking",error: err.message});
}
}

//Get all reported bookings
exports.getReportedBookings = async (req, res) =>{
try{
const reported = await Booking.find({ isReported: true});
res.status(200).json(reported);
} catch(err) {
res.status(500).json({message: "Error fetching reports",error: err.message});
}
}
6 changes: 5 additions & 1 deletion crud-node-mongo/hotel-backend/models/bookingModels.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ const bookingSchema = new mongoose.Schema({
startTime: { type: Date, required: true },
roomType : {type : String, required : true},
endTime: { type: Date, required: true },
totalPrice: { type: Number, required: false },
totalPrice: { type: Number},

// New fields
isReported: {type: Boolean, default:false},
reportReason: {type: String, defult: null},
});

const Booking = mongoose.model("Booking", bookingSchema);
Expand Down
5 changes: 5 additions & 0 deletions crud-node-mongo/hotel-backend/routes/bookingRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@ router.put("/edit/:id", bookingController.editBooking);
router.delete("/cancel/:id", bookingController.cancelBooking);
router.get("/view", bookingController.viewBookings);

//Report a booking
router.post("/report/:id", bookingController.reportBooking);

//View all reported bookings
router.get("/reports",bookingController.getReportedBookings);
module.exports = router;