members.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 {isDatePassed, isDueDatePassed} 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 (isDatePassed(startDate, dueDate)) {
  35. res.status(400).json({ 'message': 'Due date is after start date' });
  36. return;
  37. }
  38. if (isDueDatePassed(dueDate)) {
  39. res.status(400).json({ 'message': 'Due date is in the past' });
  40. return;
  41. }
  42. if (isDatePassed(endDate, startDate)) {
  43. res.status(400).json({ 'message': 'End date is before start date' });
  44. return;
  45. }
  46. const newTask = new Appointment({ user, title, description, dueDate, place, startDate, endDate });
  47. newTask.save()
  48. .then(doc => res.json({ "id": doc._id }))
  49. .catch(err => res.status(500).json({ 'message': err }));
  50. });
  51. /**
  52. * Modify an existing appointment.
  53. */
  54. router.post('/modify', function (req, res, next) {
  55. const user = req.userId;
  56. const { appointmentId, title, description, dueDate, place, startDate, endDate } = req.body;
  57. if (!title || !dueDate || !description || !startDate || !endDate || !place || !appointmentId) {
  58. res.status(400).json({ 'error': 'Empty parameter' });
  59. return;
  60. }
  61. console.log(appointmentId);
  62. console.log(user);
  63. Appointment.updateOne(
  64. { _id: appointmentId, user: user },
  65. { $set: { title, description, dueDate, place, startDate, endDate } }
  66. )
  67. .then(result => {
  68. if (result.modifiedCount === 1) {
  69. console.log("Successfully updated the document.");
  70. res.json({ "id": appointmentId });
  71. } else {
  72. console.log("Document not updated");
  73. res.status(500).json({ 'message': 'Document has not changed' });
  74. }
  75. })
  76. .catch(err => res.status(404).json({ 'message': 'Not found' }));
  77. });
  78. /**
  79. * Delete an appointment.
  80. */
  81. router.delete('/delete', function (req, res, next) {
  82. const user = req.userId;
  83. const {appointmentId} = req.body;
  84. if (!appointmentId) {
  85. res.status(400).json({ 'message': 'Invalid parameter' });
  86. return;
  87. }
  88. Appointment.deleteOne({ _id: appointmentId, user: user })
  89. .then(result => {
  90. if (result.deletedCount === 1) {
  91. res.json({ 'success': true });
  92. } else {
  93. res.status(404).json({ 'message': 'Not found' });
  94. }
  95. })
  96. .catch(err => res.status(500).json({ 'message': err }));
  97. });
  98. /**
  99. * Get all appointments for the authenticated user.
  100. */
  101. router.get('/appointments', function (req, res, next) {
  102. const user = req.userId;
  103. Appointment.find({ user: user })
  104. .then(appointments => {
  105. res.json(appointments);
  106. })
  107. .catch(err => {
  108. res.status(404).json({'message': "Appointments not found"});
  109. });
  110. });
  111. /**
  112. * Get all bookings for a specific appointment.
  113. */
  114. router.get('/bookings', function (req, res, next) {
  115. const { appointmentId } = req.query;
  116. if (!appointmentId) {
  117. res.status(400).json({ 'message': 'Empty parameter' });
  118. return;
  119. }
  120. Booking.find({ appointment: appointmentId })
  121. .then(bookings => {
  122. if (bookings === null) {
  123. res.status(404).json({'message': "Appointment not found"});
  124. } else {
  125. res.json(bookings);
  126. }
  127. })
  128. .catch(err => {
  129. res.status(404).json({'message': "Appointment not found"});
  130. });
  131. });
  132. export { router };