-
Notifications
You must be signed in to change notification settings - Fork 56
Backend work for Smart Room Booking & Clash Detection #233
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a0bccbc
Backend work for Smart Room Booking & Clash Detection
gmarav05 89b6667
Add seedRooms function to seeding process
harshitap1305 3528b0f
Update seed.js
harshitap1305 130d285
Merge branch 'OpenLake:main' into main
gmarav05 0b1621f
Address clash detection and booking review issues
gmarav05 98387b7
Update backend session/auth config and lockfile
gmarav05 742b265
Address latest CodeRabbit review findings
gmarav05 e5c7333
Update event overlap conflicts and centralize review roles
gmarav05 43ade98
Update date parsing to use UTC methods
harshitap1305 488dd97
Update index.js
harshitap1305 0285aa6
Refactor passport serialization and deserialization
harshitap1305 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
|
|
||
| const { Room, RoomBooking, Event, User } = require('../models/schema'); | ||
|
|
||
| exports.createRoom = async (req, res) => { | ||
| try { | ||
| const { name, capacity, location, amenities } = req.body; | ||
| const room = new Room({ name, capacity, location, amenities }); | ||
| await room.save(); | ||
| res.status(201).json({ message: 'Room created', room }); | ||
| } catch (err) { | ||
| if (err.code === 11000) { | ||
| return res.status(409).json({ message: 'Room name already exists' }); | ||
| } | ||
| res.status(500).json({ message: 'Error creating room', error: err.message }); | ||
| } | ||
| }; | ||
|
|
||
|
|
||
| exports.getAllRooms = async (_req, res) => { | ||
| try { | ||
| const rooms = await Room.find({ is_active: true }); | ||
| res.json(rooms); | ||
| } catch (err) { | ||
| res.status(500).json({ message: 'Error fetching rooms' }); | ||
| } | ||
| }; | ||
|
|
||
|
|
||
| exports.bookRoom = async (req, res) => { | ||
| try { | ||
| const { roomId, eventId, date, startTime, endTime, purpose } = req.body; | ||
| // Check for clash | ||
| const clash = await RoomBooking.findOne({ | ||
| room: roomId, | ||
| status: { $in: ['Pending', 'Approved'] }, | ||
| $or: [ | ||
| { startTime: { $lt: endTime }, endTime: { $gt: startTime } }, | ||
| ], | ||
| }); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| if (clash) { | ||
| return res.status(409).json({ message: 'Room clash detected', conflictingBooking: clash }); | ||
| } | ||
| const booking = new RoomBooking({ | ||
| room: roomId, | ||
| event: eventId, | ||
| date, | ||
| startTime, | ||
| endTime, | ||
| purpose, | ||
| bookedBy: req.user._id, | ||
| }); | ||
| await booking.save(); | ||
| res.status(201).json({ message: 'Room booked (pending approval)', booking }); | ||
| } catch (err) { | ||
| res.status(500).json({ message: 'Error booking room', error: err.message }); | ||
| } | ||
| }; | ||
|
|
||
|
|
||
| exports.getAvailability = async (req, res) => { | ||
| try { | ||
| const { date, roomId } = req.query; | ||
| const query = { date: new Date(date) }; | ||
| if (roomId) query.room = roomId; | ||
| const bookings = await RoomBooking.find(query).populate('room event bookedBy'); | ||
| res.json(bookings); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| } catch (err) { | ||
| res.status(500).json({ message: 'Error fetching availability' }); | ||
| } | ||
| }; | ||
|
|
||
|
|
||
| exports.updateBookingStatus = async (req, res) => { | ||
| try { | ||
| const { id } = req.params; | ||
| const { status } = req.body; | ||
| if (!['Approved', 'Rejected'].includes(status)) { | ||
| return res.status(400).json({ message: 'Invalid status' }); | ||
| } | ||
| const booking = await RoomBooking.findByIdAndUpdate( | ||
| id, | ||
| { status, reviewedBy: req.user._id, updated_at: new Date() }, | ||
| { new: true } | ||
| ); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| if (!booking) return res.status(404).json({ message: 'Booking not found' }); | ||
| res.json({ message: 'Booking status updated', booking }); | ||
| } catch (err) { | ||
| res.status(500).json({ message: 'Error updating booking status' }); | ||
| } | ||
| }; | ||
|
|
||
|
|
||
| exports.cancelBooking = async (req, res) => { | ||
| try { | ||
| const { id } = req.params; | ||
| const booking = await RoomBooking.findById(id); | ||
| if (!booking) return res.status(404).json({ message: 'Booking not found' }); | ||
|
|
||
| if ( | ||
| String(booking.bookedBy) !== String(req.user._id) && | ||
| !['PRESIDENT', 'GENSEC_SCITECH', 'GENSEC_ACADEMIC', 'GENSEC_CULTURAL', 'GENSEC_SPORTS', 'CLUB_COORDINATOR'].includes(req.user.role) | ||
| ) { | ||
| return res.status(403).json({ message: 'Forbidden' }); | ||
| } | ||
| booking.status = 'Cancelled'; | ||
| booking.updated_at = new Date(); | ||
| await booking.save(); | ||
| res.json({ message: 'Booking cancelled', booking }); | ||
| } catch (err) { | ||
| res.status(500).json({ message: 'Error cancelling booking' }); | ||
| } | ||
| }; | ||
|
|
||
| exports.getBookings = async (req, res) => { | ||
| try { | ||
| const { roomId, date, status } = req.query; | ||
| const query = {}; | ||
| if (roomId) query.room = roomId; | ||
| if (date) query.date = new Date(date); | ||
| if (status) query.status = status; | ||
| const bookings = await RoomBooking.find(query).populate('room event bookedBy'); | ||
| res.json(bookings); | ||
| } catch (err) { | ||
| res.status(500).json({ message: 'Error fetching bookings' }); | ||
| } | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| const express = require('express'); | ||
| const router = express.Router(); | ||
| const isAuthenticated = require('../middlewares/isAuthenticated'); | ||
| const authorizeRole = require('../middlewares/authorizeRole'); | ||
| const { ROLE_GROUPS, ROLES } = require('../utils/roles'); | ||
| const roomBookingController = require('../controllers/roomBookingController'); | ||
|
|
||
| // Create a new room (admin only) | ||
| router.post('/create-room', isAuthenticated, authorizeRole(ROLE_GROUPS.ADMIN), roomBookingController.createRoom); | ||
|
|
||
| // Get all rooms | ||
| router.get('/rooms', isAuthenticated, roomBookingController.getAllRooms); | ||
|
|
||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| // Book a room (admin only) | ||
| router.post('/book', isAuthenticated, authorizeRole(ROLE_GROUPS.ADMIN), roomBookingController.bookRoom); | ||
|
|
||
| // Get room availability | ||
| router.get('/availability', isAuthenticated, roomBookingController.getAvailability); | ||
|
|
||
| // Get bookings (filterable) | ||
| router.get('/bookings', isAuthenticated, roomBookingController.getBookings); | ||
|
|
||
| // Update booking status (approve/reject) | ||
| router.put('/bookings/:id/status', isAuthenticated, authorizeRole([ | ||
| ROLES.PRESIDENT, | ||
| ROLES.GENSEC_SCITECH, | ||
| ROLES.GENSEC_ACADEMIC, | ||
| ROLES.GENSEC_CULTURAL, | ||
| ROLES.GENSEC_SPORTS, | ||
| ]), roomBookingController.updateBookingStatus); | ||
|
|
||
| // Cancel a booking | ||
| router.delete('/bookings/:id', isAuthenticated, roomBookingController.cancelBooking); | ||
|
|
||
| module.exports = router; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.