members.js 4.5 KB

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