members.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. const router = express.Router();
  9. router.use(passport.authenticate('jwt', { session: false }));
  10. router.use((req, res, next) => {
  11. if (req.user && req.user.id) {
  12. req.userId = req.user.id; // Pass user ID to req.userId
  13. }
  14. next();
  15. });
  16. router.post('/create', function(req, res, next) {
  17. const user = req.userId;
  18. const { title, description, dueDate, place, startDate, endDate } = req.body;
  19. if (!title || title.length === 0 || !dueDate || !dueDate || !description || !startDate || !endDate || !place) {
  20. res.status(400).json({ 'message': 'Empty parameter' });
  21. return;
  22. }
  23. if (isDatePassed(startDate, dueDate)) {
  24. res.status(400).json({ 'message': 'Due date is after start date' });
  25. return;
  26. }
  27. if (isDueDatePassed(dueDate)) {
  28. res.status(400).json({ 'message': 'Due date is in the past' });
  29. return;
  30. }
  31. // TODO check if end date is before start date
  32. if (isDatePassed(endDate, startDate)) {
  33. res.status(400).json({ 'message': 'End date is before start date' });
  34. return;
  35. }
  36. const newTask = new Appointment({ user, title, description, dueDate, place, startDate, endDate });
  37. newTask.save()
  38. .then(doc => res.json({ "id": `${doc._id}` }))
  39. .catch(err => res.status(500).json({ 'message': err }));
  40. });
  41. router.put('/modify', function (req, res, next) {
  42. const user = req.userId;
  43. const { appointmentId, title, description, dueDate, place, startDate, endDate } = req.body;
  44. if (!title || !dueDate || !dueDate || !description || !startDate || !endDate || !place || !appointmentId) {
  45. res.status(400).json({ 'error': 'Empty parameter' });
  46. return;
  47. }
  48. console.log(appointmentId);
  49. console.log(user);
  50. Appointment.updateOne(
  51. { _id: appointmentId, user: user },
  52. { $set: { title, description, dueDate, place, startDate, endDate } }
  53. )
  54. .then(result => {
  55. if (result.modifiedCount === 1) {
  56. console.log("Successfully updated the document.");
  57. res.json({ "success": true });
  58. } else {
  59. console.log("Document not updated");
  60. res.status(500).json({ 'message': 'Document has not changed' });
  61. }
  62. })
  63. .catch(err => res.status(404).json({ 'message': 'Not found' }));
  64. });
  65. router.delete('/delete', function (req, res, next) {
  66. const user = req.userId;
  67. const {appointmentId} = req.body;
  68. if (!appointmentId) {
  69. res.status(400).json({ 'message': 'Invalid parameter' });
  70. return;
  71. }
  72. Appointment.deleteOne({ _id: appointmentId, user: user })
  73. .then(result => {
  74. if (result.deletedCount === 1) {
  75. res.json({ 'success': true });
  76. } else {
  77. res.status(404).json({ 'message': 'Not found' });
  78. }
  79. })
  80. .catch(err => res.status(500).json({ 'message': err }));
  81. });
  82. router.get('/appointments', function (req, res, next) {
  83. const user = req.userId;
  84. Appointment.find({ user: user })
  85. .then(appointments => {
  86. res.json(appointments);
  87. })
  88. .catch(err => {
  89. res.status(404).json({'message': "Appointments not found"});
  90. });
  91. });
  92. router.get('/bookings', function (req, res, next) {
  93. const { appointmentId } = req.query;
  94. if (!appointmentId) {
  95. res.status(400).json({ 'message': 'Empty parameter' });
  96. return;
  97. }
  98. Booking.find({ appointment: appointmentId })
  99. .then(bookings => {
  100. if (bookings === null) {
  101. res.status(404).json({'message': "Appointment not found"});
  102. } else {
  103. res.json(bookings);
  104. }
  105. })
  106. .catch(err => {
  107. res.status(404).json({'message': "Appointment not found"});
  108. });
  109. });
  110. export { router };