| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- /**
- * Import required dependencies.
- */
- import express from 'express';
- import Appointment from "../schemas/appointmentSchema.js";
- import Booking from "../schemas/bookingSchema.js";
- import {isDueDatePassed, isInputValid, isValidEmail} from "../utils.js";
- /**
- * Create an Express router for handling appointment-related routes.
- */
- const router = express.Router();
- /**
- * Create a new appointment and associate it with a booking.
- */
- router.post('/create', async function(req, res, next) {
- const { appointmentId, time, inputs } = req.body;
- if (!appointmentId || !time || !inputs) {
- console.log(appointmentId);
- console.log(time);
- console.log(inputs);
- res.status(400).json({ 'message': 'Parameters are incomplete' });
- return;
- }
- // TODO validate times
- // advanced input validation
- // get constraints from database
- try {
- const existingAppointment = await Appointment.findOne({ _id: appointmentId });
- if (!existingAppointment) {
- return res.status(404).json({ message: 'appointment does not exist' });
- }
-
- const bookingDate = new Date(Date.parse(time));
- if (!existingAppointment.times.find(input => input.date.toString() === bookingDate.toString() && input.available === true)) {
- res.status(400).json({ 'message': 'This appointment time is not available' });
- return;
- }
- if (!isInputValid(inputs, existingAppointment.inputs)) {
- res.status(400).json({ 'message': 'Custom inputs are invalid' });
- return;
- }
- if (isDueDatePassed(existingAppointment["dueDate"])) {
- return res.status(401).json({ message: 'appointment registration is closed' });
- }
- const duplicateBooking = await Booking.findOne({ appointment: appointmentId, time, inputs: inputs })
- .catch(err => {
- console.log(err);
- });
- if (duplicateBooking) {
- return res.status(404).json({ message: 'already registered' });
- }
- let times = existingAppointment.times;
- const index = times.findIndex(element => element.date.toString() === bookingDate.toString());
- if (index === -1) {
- return res.status(404).json({ message: 'registration time not found' });
- }
- times[index].available = false;
- await Appointment.updateOne(
- { _id: appointmentId },
- { $set: { times } }
- )
- .then(result => {
- if (result.modifiedCount === 1) {
- console.log("Successfully updated the document.");
- } else {
- console.log("Document not updated");
- return res.status(500).json({ 'message': 'Document has not changed' });
- }
- })
- .catch(err => {
- return res.status(404).json({ 'message': 'Not found' })
- });
- const newBooking = new Booking({ appointment: appointmentId, time, inputs });
- newBooking.save()
- .then(doc => res.json({ "success": true }))
- .catch(err => res.status(500).json({ 'error': err }));
- } catch (err) {
- // Handle errors
- console.error(err);
- return res.status(500).json({ error: 'Internal Server Error' });
- }
- });
- /**
- * Retrieve an appointment by ID.
- */
- router.get('/', function (req, res, next) {
- const {appointmentId, isUser} = req.query;
- Appointment.findOne({ _id: appointmentId })
- .then(appointment => {
- if (appointment === null) {
- res.status(404).json({'message': "Appointment not found"});
- } else {
- // Check if a registered user accesses this endpoint
- if (isDueDatePassed(appointment["dueDate"]) && !isUser) {
- return res.status(410).json({ message: 'appointment registration is closed' });
- } else {
- res.json(appointment);
- }
- }
- })
- .catch(err => {
- res.status(404).json({'message': "Appointment not found"});
- });
- });
- export { router };
|