import express from 'express'; import passport from "passport"; import {mongoose} from "mongoose"; import Appointment from "../schemas/appointmentSchema.js"; import Booking from "../schemas/bookingSchema.js"; import {isDatePassed, isDueDatePassed} from "../utils.js"; /** * Router for handling appointments. */ const router = express.Router(); /** * Middleware to authenticate with passport */ router.use(passport.authenticate('jwt', { session: false })); /** * Middleware to pass user ID to req.userId if it exists in the request. */ router.use((req, res, next) => { if (req.user && req.user.id) { req.userId = req.user.id; // Pass user ID to req.userId } next(); }); /** * Create a new appointment. */ router.post('/create', function(req, res, next) { const user = req.userId; const { title, description, dueDate, place, startDate, endDate } = req.body; if (!title || title.length === 0 || !dueDate || !description || !startDate || !endDate || !place) { res.status(400).json({ 'message': 'Empty parameter' }); return; } if (isDatePassed(startDate, dueDate)) { res.status(400).json({ 'message': 'Due date is after start date' }); return; } if (isDueDatePassed(dueDate)) { res.status(400).json({ 'message': 'Due date is in the past' }); return; } if (isDatePassed(endDate, startDate)) { res.status(400).json({ 'message': 'End date is before start date' }); return; } const newTask = new Appointment({ user, title, description, dueDate, place, startDate, endDate }); newTask.save() .then(doc => res.json({ "id": doc._id })) .catch(err => res.status(500).json({ 'message': err })); }); /** * Modify an existing appointment. */ router.post('/modify', function (req, res, next) { const user = req.userId; const { appointmentId, title, description, dueDate, place, startDate, endDate } = req.body; if (!title || !dueDate || !description || !startDate || !endDate || !place || !appointmentId) { res.status(400).json({ 'error': 'Empty parameter' }); return; } console.log(appointmentId); console.log(user); Appointment.updateOne( { _id: appointmentId, user: user }, { $set: { title, description, dueDate, place, startDate, endDate } } ) .then(result => { if (result.modifiedCount === 1) { console.log("Successfully updated the document."); res.json({ "id": appointmentId }); } else { console.log("Document not updated"); res.status(500).json({ 'message': 'Document has not changed' }); } }) .catch(err => res.status(404).json({ 'message': 'Not found' })); }); /** * Delete an appointment. */ router.delete('/delete', function (req, res, next) { const user = req.userId; const {appointmentId} = req.body; if (!appointmentId) { res.status(400).json({ 'message': 'Invalid parameter' }); return; } Appointment.deleteOne({ _id: appointmentId, user: user }) .then(result => { if (result.deletedCount === 1) { res.json({ 'success': true }); } else { res.status(404).json({ 'message': 'Not found' }); } }) .catch(err => res.status(500).json({ 'message': err })); }); /** * Get all appointments for the authenticated user. */ router.get('/appointments', function (req, res, next) { const user = req.userId; Appointment.find({ user: user }) .then(appointments => { res.json(appointments); }) .catch(err => { res.status(404).json({'message': "Appointments not found"}); }); }); /** * Get all bookings for a specific appointment. */ router.get('/bookings', function (req, res, next) { const { appointmentId } = req.query; if (!appointmentId) { res.status(400).json({ 'message': 'Empty parameter' }); return; } Booking.find({ appointment: appointmentId }) .then(bookings => { if (bookings === null) { res.status(404).json({'message': "Appointment not found"}); } else { res.json(bookings); } }) .catch(err => { res.status(404).json({'message': "Appointment not found"}); }); }); export { router };