-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathbookingController.js
More file actions
230 lines (192 loc) · 6.78 KB
/
Copy pathbookingController.js
File metadata and controls
230 lines (192 loc) · 6.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// controllers/bookingController.js
const { Booking, Room } = require("../models/bookingModels");
// Book a room
exports.bookRoom = async (req, res) => {
try {
const { userEmail, roomNumber, roomType, startTime, endTime } = req.body;
// Convert start time and end time to Date objects
const startTimeDate = new Date(startTime);
const endTimeDate = new Date(endTime);
const existingRoom = await Room.findOne({ roomNumber, roomType });
// console.log(startTimeDate)
if (!existingRoom) {
// alert("Room Already Booked");
console.log("help");
return res.status(400).json({ message: "Invalid Room Details" });
}
// console.log("here");
// Check if room is available
const existingBooking = await Booking.findOne({
roomNumber,
$or: [
{
startTime: { $lt: endTimeDate },//This will cover all the overlap cases.
endTime: { $gt: startTimeDate },
}
]
});
if (existingBooking) {
// console.log("hello");
return res
.status(400)
.json({ message: "Room is not available for the specified time slot" });
}
// Calculate duration and price
const duration = (endTimeDate - startTimeDate) / (1000 * 60 * 60); // Convert to hours
const room = existingBooking;//this unnesessary and wasiting database time and resources
const totalPrice = duration * room.pricePerHour;
// Create new booking
const booking = new Booking({
userEmail,
roomNumber,
roomType,
startTime: startTimeDate,
endTime: endTimeDate,
totalPrice,
});
await booking.save();
res.status(201).json({ message: "Room booked successfully", booking });
} catch (error) {
console.error(error);
res.status(500).json({ message: "Internal Server Error" });
}
};
// Edit a booking
// controllers/bookingController.js
exports.editBooking = async (req, res) => {
try {
const bookingId = req.params.id;
const { userEmail, roomNumber, startTime, endTime } = req.body;
// Find the existing booking
const booking = await Booking.findById(bookingId);
if (!booking) {
return res.status(404).json({ message: "Booking not found" });
}
// Convert start time and end time to Date objects
const startTimeDate = new Date(startTime);
const endTimeDate = new Date(endTime);
// Check if the new time slot is available
const existingBooking = await Booking.findOne({
roomNumber,
_id: { $ne: bookingId },
$or: [
{ startTime: { $lt: endTimeDate }, endTime: { $gt: startTimeDate } },
{ startTime: { $gte: startTimeDate, $lt: endTimeDate } },
{ endTime: { $lte: endTimeDate, $gt: startTimeDate } },
],
});
if (existingBooking) {
return res
.status(400)
.json({ message: "Room is not available for the specified time slot" });
}
// Calculate duration and price
const duration = (endTimeDate - startTimeDate) / (1000 * 60 * 60); // Convert to hours
const room = await Room.findOne({ roomNumber });
const totalPrice = duration * room.pricePerHour;
// Update booking details
booking.userEmail = userEmail;
booking.roomNumber = roomNumber;
booking.startTime = startTimeDate;
booking.endTime = endTimeDate;
booking.totalPrice = totalPrice;
await booking.save();
res.status(200).json({ message: "Booking updated successfully", booking });
} catch (error) {
console.error(error);
res.status(500).json({ message: "Internal Server Error" });
}
};
// Cancel a booking
exports.cancelBooking = async (req, res) => {
try {
const bookingId = req.params.id;
// Find the booking by ID
const booking = await Booking.findById(bookingId);
if (!booking) {
return res.status(404).json({ message: "Booking not found" });
}
// Calculate time difference in milliseconds
const currentTime = new Date();
const timeDifference = booking.startTime - currentTime;
// Define cancellation policy thresholds
const within24Hours = 24 * 60 * 60 * 1000; // 24 hours in milliseconds
const within48Hours = 48 * 60 * 60 * 1000; // 48 hours in milliseconds
let refundAmount = 0;
if (timeDifference > within48Hours) {
// Full refund if more than 48 hours left
refundAmount = booking.totalPrice;
} else if (timeDifference > within24Hours) {
// 50% refund if between 24 and 48 hours left
refundAmount = booking.totalPrice / 2;
}
// Update booking status and refund amount
booking.status = "cancelled";
booking.refundAmount = refundAmount;
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)
.json({ message: "Booking cancelled successfully", refundAmount });
} catch (error) {
console.error(error);
res.status(500).json({ message: "Internal Server Error" });
}
};
// View all bookings
// controllers/bookingController.js
exports.viewBookings = async (req, res) => {
try {
const { roomNumber, roomType, startTime, endTime } = req.query;
// Construct query object based on provided filters
const query = {};
if (roomNumber) {
query.roomNumber = roomNumber;
}
if (roomType) {
query.roomType = roomType;
}
if (startTime && endTime) {
// Convert startTime and endTime strings to Date objects
const startDateTime = new Date(startTime);
const endDateTime = new Date(endTime);
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);
res.status(200).json({ bookings });
} catch (error) {
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});
}
}