members.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import express from 'express';
  2. import passport from "passport";
  3. import {mongoose} from "mongoose";
  4. import Appointment from "../schemas/appointmentSchema.js";
  5. import Booking from "../schemas/bookingSchema.js";
  6. import {validateDates} from "../utils.js";
  7. /**
  8. * Router for handling appointments.
  9. */
  10. const router = express.Router();
  11. /**
  12. * Middleware to authenticate with passport
  13. */
  14. router.use(passport.authenticate('jwt', { session: false }));
  15. /**
  16. * Middleware to pass user ID to req.userId if it exists in the request.
  17. */
  18. router.use((req, res, next) => {
  19. if (req.user && req.user.id) {
  20. req.userId = req.user.id; // Pass user ID to req.userId
  21. }
  22. next();
  23. });
  24. /**
  25. * Create a new appointment.
  26. */
  27. router.post('/create', function(req, res, next) {
  28. const user = req.userId;
  29. const { title, description, dueDate, place, startDate, endDate } = req.body;
  30. if (!title || title.length === 0 || !dueDate || !description || !startDate || !endDate || !place) {
  31. res.status(400).json({ 'message': 'Empty parameter' });
  32. return;
  33. }
  34. if (!validateDates(res, startDate, endDate, dueDate)) {
  35. return;
  36. }
  37. const newTask = new Appointment({ user, title, description, dueDate, place, startDate, endDate });
  38. newTask.save()
  39. .then(doc => res.json({ "id": doc._id }))
  40. .catch(err => res.status(500).json({ 'message': err }));
  41. });
  42. /**
  43. * Modify an existing appointment.
  44. */
  45. router.post('/modify', function (req, res, next) {
  46. const user = req.userId;
  47. const { appointmentId, title, description, dueDate, place, startDate, endDate } = req.body;
  48. if (!title || !dueDate || !description || !startDate || !endDate || !place || !appointmentId) {
  49. res.status(400).json({ 'error': 'Empty parameter' });
  50. return;
  51. }
  52. if (!validateDates(res, startDate, endDate, dueDate)) {
  53. return;
  54. }
  55. Appointment.updateOne(
  56. { _id: appointmentId, user: user },
  57. { $set: { title, description, dueDate, place, startDate, endDate } }
  58. )
  59. .then(result => {
  60. if (result.modifiedCount === 1) {
  61. console.log("Successfully updated the document.");
  62. res.json({ "id": appointmentId });
  63. } else {
  64. console.log("Document not updated");
  65. res.status(500).json({ 'message': 'Document has not changed' });
  66. }
  67. })
  68. .catch(err => res.status(404).json({ 'message': 'Not found' }));
  69. });
  70. /**
  71. * Delete an appointment.
  72. */
  73. router.delete('/delete', function (req, res, next) {
  74. const user = req.userId;
  75. const {appointmentId} = req.body;
  76. if (!appointmentId) {
  77. res.status(400).json({ 'message': 'Invalid parameter' });
  78. return;
  79. }
  80. Appointment.deleteOne({ _id: appointmentId, user: user })
  81. .then(result => {
  82. if (result.deletedCount === 1) {
  83. res.json({ 'success': true });
  84. } else {
  85. res.status(404).json({ 'message': 'Not found' });
  86. }
  87. })
  88. .catch(err => res.status(500).json({ 'message': err }));
  89. });
  90. /**
  91. * Get all appointments for the authenticated user.
  92. */
  93. router.get('/appointments', function (req, res, next) {
  94. const user = req.userId;
  95. Appointment.find({ user: user })
  96. .then(appointments => {
  97. res.json(appointments);
  98. })
  99. .catch(err => {
  100. res.status(404).json({'message': "Appointments not found"});
  101. });
  102. });
  103. /**
  104. * Get all bookings for a specific appointment.
  105. */
  106. router.get('/bookings', function (req, res, next) {
  107. const { appointmentId } = req.query;
  108. if (!appointmentId) {
  109. res.status(400).json({ 'message': 'Empty parameter' });
  110. return;
  111. }
  112. Booking.find({ appointment: appointmentId })
  113. .then(bookings => {
  114. if (bookings === null) {
  115. res.status(404).json({'message': "Appointment not found"});
  116. } else {
  117. res.json(bookings);
  118. }
  119. })
  120. .catch(err => {
  121. res.status(404).json({'message': "Appointment not found"});
  122. });
  123. });
  124. export { router };