members.js 3.6 KB

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