File size: 3,325 Bytes
0f8617c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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 };