appointments.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. * Import required dependencies.
  3. */
  4. import express from 'express';
  5. import Appointment from "../schemas/appointmentSchema.js";
  6. import Booking from "../schemas/bookingSchema.js";
  7. import {isDueDatePassed, isInputValid, isValidEmail} from "../utils.js";
  8. /**
  9. * Create an Express router for handling appointment-related routes.
  10. */
  11. const router = express.Router();
  12. /**
  13. * Create a new appointment and associate it with a booking.
  14. */
  15. router.post('/create', async function(req, res, next) {
  16. const { appointmentId, time, inputs } = req.body;
  17. if (!appointmentId || !time || !inputs) {
  18. console.log(appointmentId);
  19. console.log(time);
  20. console.log(inputs);
  21. res.status(400).json({ 'message': 'Parameters are incomplete' });
  22. return;
  23. }
  24. // TODO validate times
  25. // advanced input validation
  26. // get constraints from database
  27. try {
  28. const existingAppointment = await Appointment.findOne({ _id: appointmentId });
  29. if (!existingAppointment) {
  30. return res.status(404).json({ message: 'appointment does not exist' });
  31. }
  32. console.log(existingAppointment.times);
  33. // existingAppointment.times.find(input => input.date === "2025-01-27T06:00:00.000Z")
  34. if (existingAppointment.times.find(input => input.date === "2025-01-27T06:00:00.000Z")) {
  35. console.log("done");
  36. }
  37. if (!isInputValid(inputs, existingAppointment.inputs)) {
  38. res.status(400).json({ 'message': 'Custom inputs are invalid' });
  39. return;
  40. }
  41. if (isDueDatePassed(existingAppointment["dueDate"])) {
  42. return res.status(401).json({ message: 'appointment registration is closed' });
  43. }
  44. const duplicateBooking = await Booking.findOne({ appointment: appointmentId, time, inputs: inputs })
  45. .catch(err => {
  46. console.log(err);
  47. });
  48. if (duplicateBooking) {
  49. return res.status(404).json({ message: 'already registered' });
  50. }
  51. const newBooking = new Booking({ appointment: appointmentId, time, inputs });
  52. newBooking.save()
  53. .then(doc => res.json({ "success": true }))
  54. .catch(err => res.status(500).json({ 'error': err }));
  55. } catch (err) {
  56. // Handle errors
  57. console.error(err);
  58. return res.status(500).json({ error: 'Internal Server Error' });
  59. }
  60. });
  61. /**
  62. * Retrieve an appointment by ID.
  63. */
  64. router.get('/', function (req, res, next) {
  65. const {appointmentId, isUser} = req.query;
  66. Appointment.findOne({ _id: appointmentId })
  67. .then(appointment => {
  68. if (appointment === null) {
  69. res.status(404).json({'message': "Appointment not found"});
  70. } else {
  71. // Check if a registered user accesses this endpoint
  72. if (isDueDatePassed(appointment["dueDate"]) && !isUser) {
  73. return res.status(410).json({ message: 'appointment registration is closed' });
  74. } else {
  75. res.json(appointment);
  76. }
  77. }
  78. })
  79. .catch(err => {
  80. res.status(404).json({'message': "Appointment not found"});
  81. });
  82. });
  83. export { router };