const Booking = require('../models/Booking'); const { createNotification } = require('./notificationController'); // @desc Create a new booking // @route POST /api/bookings // @access Private (Customer) const createBooking = async (req, res) => { try { const { photographerId, date, startTime, duration, totalPrice, sessionNotes } = req.body; const customerId = req.user._id; const booking = await Booking.create({ photographerId, customerId, date, startTime, duration, totalPrice, sessionNotes }); if (booking) { // Trigger Notification to Photographer await createNotification( photographerId, 'booking', `New booking request from ${req.user.firstName} for ${date}`, '/dashboard' ); // Real-time Emit via Socket (req.io initialized in index.js) if (req.io) { req.io.to(photographerId.toString()).emit('receiveNotification', { type: 'booking', content: `New booking request from ${req.user.firstName}` }); } res.status(201).json(booking); } else { res.status(400).json({ message: 'Invalid booking data' }); } } catch (error) { res.status(500).json({ message: error.message }); } }; // @desc Get user's bookings // @route GET /api/bookings // @access Private const getUserBookings = async (req, res) => { try { let bookings; if (req.user.role === 'customer') { bookings = await Booking.find({ customerId: req.user._id }).populate('photographerId', 'firstName lastName email profilePicture'); } else { bookings = await Booking.find({ photographerId: req.user._id }).populate('customerId', 'firstName lastName email profilePicture'); } res.json(bookings); } catch (error) { res.status(500).json({ message: error.message }); } }; const updateBookingStatus = async (req, res) => { try { const { status } = req.body; const booking = await Booking.findById(req.params.id); if (!booking) { return res.status(404).json({ message: 'Booking not found' }); } if (booking.photographerId.toString() !== req.user._id.toString()) { return res.status(401).json({ message: 'Not authorized to update this booking' }); } booking.status = status || booking.status; const updatedBooking = await booking.save(); // Trigger Notification to Customer await createNotification( booking.customerId, 'booking', `Your booking status has been updated to: ${status}`, '/dashboard' ); if (req.io) { req.io.to(booking.customerId.toString()).emit('receiveNotification', { type: 'booking', content: `Booking ${status}` }); } res.json(updatedBooking); } catch (error) { res.status(500).json({ message: error.message }); } }; module.exports = { createBooking, getUserBookings, updateBookingStatus };