Ver código fonte

appointment scheduling wip

Liontix 11 meses atrás
pai
commit
4faaf91af2

+ 10 - 0
public/html/404.html

@@ -0,0 +1,10 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <title>Error 404</title>
+</head>
+<body>
+<h1>Error 404: NOT FOUND</h1>
+</body>
+</html>

+ 10 - 0
public/html/410.html

@@ -0,0 +1,10 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <title>Gone</title>
+</head>
+<body>
+<h1>appointment registration is closed</h1>
+</body>
+</html>

+ 7 - 2
public/html/schedule.html

@@ -5,10 +5,15 @@
     <meta name="viewport" content="width=device-width, initial-scale=1.0">
     <title>Attending Appointment</title>
     <link rel="stylesheet" href="../stylesheets/schedule.css">
+    <script src="../javascripts/schedule.js" async></script>
 </head>
 <body>
 <div class="container">
     <h1>Attending an appointment</h1>
+    <h3 id="title"></h3>
+    <h4 id="place"></h4>
+    <p id="description"></p>
+
     <form id="schedule-form">
         <div class="input-group">
             <label for="email">Email</label>
@@ -25,12 +30,12 @@
         <!-- Preset Due Date -->
         <div class="input-group">
             <label for="due-date">Due Date</label>
-            <input type="text" id="due-date" name="dueDate" value="2024-12-20" readonly>
+            <input type="text" id="due-date" name="dueDate" readonly>
         </div>
         <!-- Preset Appointment Date -->
         <div class="input-group">
             <label for="appointment-date">Appointment Date</label>
-            <input type="text" id="appointment-date" name="appointmentDate" value="2024-12-25" readonly>
+            <input type="text" id="appointment-date" name="appointmentDate" readonly>
         </div>
         <!-- Preset Time Span -->
         <div class="input-group">

+ 67 - 0
public/javascripts/schedule.js

@@ -0,0 +1,67 @@
+"use strict";
+
+window.onload = async function () {
+    await getAppointment();
+}
+
+// Function to format time as HH:MM
+function formatTime(date) {
+    const hours = date.getHours().toString().padStart(2, '0');  // Get hours and ensure two digits
+    const minutes = date.getMinutes().toString().padStart(2, '0');  // Get minutes and ensure two digits
+    return `${hours}:${minutes}`;
+}
+
+// Function to get time span (from 11:00 to 12:00 format)
+function getTimeSpan(start, end) {
+    const startTime = formatTime(start);
+    const endTime = formatTime(end);
+    return `${startTime} to ${endTime}`;
+}
+
+async function getAppointment() {
+    const urlParams = new URLSearchParams(window.location.search);
+    const appointment = urlParams.get('appointment');
+    if (!appointment) {
+        window.location.replace("/html/404.html");
+    }
+
+    let options = {method: 'GET', headers: {'User-Agent': 'insomnia/10.0.0'}};
+
+    const response = await fetch(`/api/schedule?appointmentId=${appointment}`, options)
+        .catch(err => console.error('error:' + err));
+
+    if (response.status === 404) {
+        window.location.replace("/html/404.html");
+    }
+
+    if (response.status === 410) {
+        window.location.replace("/html/410.html");
+    }
+    const js = await response.json();
+    displayAppointmentDetails(js);
+}
+
+function displayAppointmentDetails(appointment) {
+    // Update the innerHTML of the elements to display the appointment details
+    document.getElementById('appointment-date').innerText = appointment._id;
+    document.getElementById('title').innerText = appointment.title;
+    document.getElementById('description').innerText = appointment.description;
+
+    // Format the dates to a more user-friendly format
+    const dueDate = new Date(appointment.dueDate).toLocaleDateString();
+    const startDate = new Date(appointment.startDate);
+    const endDate = new Date(appointment.endDate);
+    const startDateFormatted = startDate.toLocaleString();
+    const endDateFormatted = endDate.toLocaleString();
+
+    console.log(dueDate);
+    console.log(startDate);
+    console.log(endDate);
+
+    document.getElementById('due-date').value = dueDate;
+    document.getElementById('appointment-date').value = startDateFormatted;
+    document.getElementById('time-span').value = getTimeSpan(startDate, endDate);
+
+    // Set place
+    document.getElementById('place').innerText = appointment.place;
+}

+ 10 - 1
routes/appointments.js

@@ -2,6 +2,7 @@ import express from 'express';
 import Appointment from "../schemas/appointmentSchema.js";
 import Booking from "../schemas/bookingSchema.js";
 import {isDueDatePassed, isValidEmail} from "../utils.js";
+import {app} from "../app.js";
 const router = express.Router();
 
 router.post('/create', async function(req, res, next) {
@@ -56,7 +57,15 @@ router.get('/', function (req, res, next) {
     const {appointmentId} = req.query;
     Appointment.findOne({ _id: appointmentId })
         .then(appointment => {
-            res.json(appointment);
+            if (appointment === null) {
+                res.status(404).json({'message': "Appointment not found"});
+            } else {
+                if (isDueDatePassed(appointment["dueDate"])) {
+                    return res.status(410).json({ message: 'appointment registration is closed' });
+                } else {
+                    res.json(appointment);
+                }
+            }
         })
         .catch(err => {
             res.status(404).json({'message': "Appointment not found"});

+ 6 - 2
routes/members.js

@@ -103,8 +103,12 @@ router.get('/bookings', function (req, res, next) {
     }
 
     Booking.find({ appointment: appointmentId })
-        .then(tasks => {
-            res.json(tasks);
+        .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"});

+ 1 - 0
utils.js

@@ -10,4 +10,5 @@ function isDueDatePassed(dueDate) {
     return dueDateTime < now;
 }
 
+
 export {isValidEmail, isDueDatePassed};