| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- import express from 'express';
- import passport from "passport";
- import {mongoose} from "mongoose";
- import Appointment from "../schemas/appointmentSchema.js";
- import Booking from "../schemas/bookingSchema.js";
- import {appointmentTimeSchema, isDueDatePassed, isValidAppointmentTimes, isValidCustomInput, validateDates} from "../utils.js";
- /**
- * Router for handling appointments.
- */
- const router = express.Router();
- /**
- * Middleware to authenticate with passport
- */
- router.use(passport.authenticate('jwt', { session: false }));
- /**
- * Middleware to pass user ID to req.userId if it exists in the request.
- */
- router.use((req, res, next) => {
- if (req.user && req.user.id) {
- req.userId = req.user.id; // Pass user ID to req.userId
- }
- next();
- });
- /**
- * Create a new appointment.
- */
- router.post('/create', function(req, res, next) {
- const user = req.userId;
- const { title, description, dueDate, place, duration, times, inputs } = req.body;
- if (!title || title.length === 0 || !dueDate || !description || !duration || !place || !times || !inputs) {
- res.status(400).json({ 'message': 'Empty parameter' });
- return;
- }
- ///
- if (isDueDatePassed(dueDate)) {
- res.status(400).json({ 'message': 'Due date is invalid or in the past' });
- return;
- }
- // validate times
- if (!isValidAppointmentTimes(times)) {
- res.status(400).json({ 'message': 'Appointment times are invalid' });
- return;
- }
- if (!isValidCustomInput(inputs)) {
- res.status(400).json({ 'message': 'Custom inputs are invalid' });
- return;
- }
- const newTask = new Appointment({ user, title, description, dueDate, place, duration, times, inputs });
- newTask.save()
- .then(doc => res.json({ "id": doc._id }))
- .catch(err => res.status(500).json({ 'message': err }));
- });
- /**
- * Modify an existing appointment.
- */
- router.post('/modify', function (req, res, next) {
- const user = req.userId;
- const { appointmentId, title, description, dueDate, place, times, inputs } = req.body;
- if (!title || !dueDate || !description || !startDate || !endDate || !times || !inputs) {
- res.status(400).json({ 'error': 'Empty parameter' });
- return;
- }
- if (isDueDatePassed(dueDate)) {
- res.status(400).json({ 'error': 'Invalid due date' });
- return;
- }
- Appointment.updateOne(
- { _id: appointmentId, user: user },
- { $set: { title, description, dueDate, place, times, inputs } }
- )
- .then(result => {
- if (result.modifiedCount === 1) {
- console.log("Successfully updated the document.");
- res.json({ "id": appointmentId });
- } else {
- console.log("Document not updated");
- res.status(500).json({ 'message': 'Document has not changed' });
- }
- })
- .catch(err => res.status(404).json({ 'message': 'Not found' }));
- });
- /**
- * Delete an appointment.
- */
- router.delete('/delete', function (req, res, next) {
- const user = req.userId;
- const {appointmentId} = req.body;
- if (!appointmentId) {
- res.status(400).json({ 'message': 'Invalid parameter' });
- return;
- }
- Appointment.deleteOne({ _id: appointmentId, user: user })
- .then(result => {
- if (result.deletedCount === 1) {
- res.json({ 'success': true });
- } else {
- res.status(404).json({ 'message': 'Not found' });
- }
- })
- .catch(err => res.status(500).json({ 'message': err }));
- });
- /**
- * Get all appointments for the authenticated user.
- */
- router.get('/appointments', function (req, res, next) {
- const user = req.userId;
- Appointment.find({ user: user })
- .then(appointments => {
- res.json(appointments);
- })
- .catch(err => {
- res.status(404).json({'message': "Appointments not found"});
- });
- });
- /**
- * Get all bookings for a specific appointment.
- */
- router.get('/bookings', function (req, res, next) {
- const { appointmentId } = req.query;
- if (!appointmentId) {
- res.status(400).json({ 'message': 'Empty parameter' });
- return;
- }
- Booking.find({ appointment: appointmentId })
- .then(bookings => {
- if (bookings === null) {
- res.status(404).json({'message': "Appointment not found"});
- } else {
- res.json(bookings);
- }
- })
- .catch(err => {
- res.status(404).json({'message': "Appointment not found"});
- });
- });
- export { router };
|