members.js 4.4 KB

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