diff --git a/backend/Controllers/doctorController.js b/backend/Controllers/doctorController.js index 0fbcb67..6a2d032 100644 --- a/backend/Controllers/doctorController.js +++ b/backend/Controllers/doctorController.js @@ -69,7 +69,7 @@ exports.selectPatient = async (req, res) => { }; exports.viewAllPatients = async (req, res) => { - const doctorUsername = req.user.username; //modifed to be used in scedule follow up + const doctorUsername = req.user.username; //modifed to be used in scedule follow up try { const doctor = await Doctor.findOne({ username: doctorUsername }).populate('registeredPatients'); res.status(200).send(doctor.registeredPatients); @@ -170,43 +170,41 @@ exports.filterDoctors = async (req, res) => { } }; - //-------------- SPRINT 2 ------------------- - - exports.addAvailableSlot = async (req, res) => { - - try { - - const {slotDate} = req.query; - - if(!slotDate) return res.status(400).json({ message: 'Enter the slot time and date.'}); - - if(slotDate < new Date()) res.status(400).json({ message: 'Date and time has already passed.'}); - - const doctor = await Doctor.findOneAndUpdate( - { username: req.user.username }, - {$pull: { availableSlots: { $lt: new Date() } }}, - { new: true } - ); - - const updatedDoctor = await Doctor.updateOne({ username: req.user.username }, { $addToSet: { availableSlots: slotDate } }); - - res.status(200).json(updatedDoctor); - } catch (error) { - res.status(500).json({ message: error.message }); - } - }; - - exports.viewDoctorAppointments = async (req,res) => { - - try{ - const appointments = await Appointment.find({doctor: req.user.username}).exec(); - filterAppointments(req, res, appointments); - - } catch(err){ - res.status(500).json({ message: err.message }); - } - }; +//-------------- SPRINT 2 ------------------- + +exports.addAvailableSlot = async (req, res) => { + try { + const { slotDate } = req.query; + + if (!slotDate) return res.status(400).json({ message: 'Enter the slot time and date.' }); + + if (slotDate < new Date()) res.status(400).json({ message: 'Date and time has already passed.' }); + + const doctor = await Doctor.findOneAndUpdate( + { username: req.user.username }, + { $pull: { availableSlots: { $lt: new Date() } } }, + { new: true } + ); + + const updatedDoctor = await Doctor.updateOne( + { username: req.user.username }, + { $addToSet: { availableSlots: slotDate } } + ); + + res.status(200).json(updatedDoctor); + } catch (error) { + res.status(500).json({ message: error.message }); + } +}; +exports.viewDoctorAppointments = async (req, res) => { + try { + const appointments = await Appointment.find({ doctor: req.user.username }).exec(); + filterAppointments(req, res, appointments); + } catch (err) { + res.status(500).json({ message: err.message }); + } +}; exports.acceptContract = async (req, res) => { try { @@ -237,22 +235,23 @@ exports.acceptContract = async (req, res) => { }; exports.addAvailableSlot = async (req, res) => { - try { + const { slotDate } = req.query; - const {slotDate} = req.query; - - if(!slotDate) return res.status(400).json({ message: 'Enter the slot time and date.'}); + if (!slotDate) return res.status(400).json({ message: 'Enter the slot time and date.' }); - if(slotDate < new Date()) res.status(400).json({ message: 'Date and time has already passed.'}); + if (slotDate < new Date()) res.status(400).json({ message: 'Date and time has already passed.' }); const doctor = await Doctor.findOneAndUpdate( { username: req.user.username }, - {$pull: { availableSlots: { $lt: new Date() } }}, + { $pull: { availableSlots: { $lt: new Date() } } }, { new: true } ); - const updatedDoctor = await Doctor.updateOne({ username: req.user.username }, { $addToSet: { availableSlots: slotDate } }); + const updatedDoctor = await Doctor.updateOne( + { username: req.user.username }, + { $addToSet: { availableSlots: slotDate } } + ); res.status(200).json(updatedDoctor); } catch (error) { @@ -262,7 +261,6 @@ exports.addAvailableSlot = async (req, res) => { exports.scheduleFollowUp = async (req, res) => { try { - const { patientId, dateTime } = req.body; const [date, time] = dateTime.split('T'); @@ -270,7 +268,7 @@ exports.scheduleFollowUp = async (req, res) => { const doctorId = req.user._id; // Use populate to get the registered patients' data - + const doctor = await Doctor.findById(doctorId); const registeredPatients = await doctor.populate('registeredPatients'); @@ -280,7 +278,6 @@ exports.scheduleFollowUp = async (req, res) => { return res.status(404).json({ error: 'Doctor not found' }); } - // Find the patient by id const patient = await Patient.findById(patientId); @@ -290,9 +287,7 @@ exports.scheduleFollowUp = async (req, res) => { } // Check if patient is registered with the doctor - const isPatientRegistered = doctor.registeredPatients.some( - (registeredPatient) => registeredPatient.id === patientId - ); + const isPatientRegistered = doctor.registeredPatients.some(registeredPatient => registeredPatient.id === patientId); if (!isPatientRegistered) { return res.status(404).json({ error: 'Patient not registered with this doctor' }); @@ -305,7 +300,6 @@ exports.scheduleFollowUp = async (req, res) => { date: date, time: time, status: 'upcoming', - }); //remove the slot from available slots TODO const updatedDoctor = await Doctor.findOneAndUpdate( @@ -338,7 +332,7 @@ exports.viewAvailableSlots = async (req, res) => { exports.getPatientMedicalHistory = async (req, res) => { try { - const {patientId} = req.query; + const { patientId } = req.query; const patient = await Patient.findById(patientId); @@ -354,3 +348,18 @@ exports.getPatientMedicalHistory = async (req, res) => { res.status(500).json({ message: 'Error fetching medical history' }); } }; + +exports.viewAllPrescriptionsByDoctor = async (req, res) => { + try { + // Find the doctor + const doctorId = req.user._id; + + // Find all prescriptions made by the doctor + const prescriptions = await Prescription.find({ doctor: doctorId }); + + res.status(200).json({ success: true, prescriptions }); + } catch (error) { + console.error('Error fetching prescriptions:', error); + res.status(500).json({ success: false, message: 'Internal server error' }); + } +}; diff --git a/backend/Controllers/patientController.js b/backend/Controllers/patientController.js index 50142e7..c751ef0 100644 --- a/backend/Controllers/patientController.js +++ b/backend/Controllers/patientController.js @@ -124,9 +124,11 @@ exports.viewAllPatients = async (req, res) => { exports.getAllPrescriptionsForPatient = async (req, res) => { try { - const username = req.query.username; - const patient = await Patient.findOne({ username: username }); - const patientId = patient.id; + const patientId = req.user._id; + const patient = await Patient.findById(patientId); + if (!patient) { + return res.status(404).json({ message: 'Patient not found' }); + } const prescriptions = await Prescription.find({ patient: patientId }).populate('doctor'); diff --git a/backend/Routes/doctorRoutes.js b/backend/Routes/doctorRoutes.js index 31ce887..0e920e9 100644 --- a/backend/Routes/doctorRoutes.js +++ b/backend/Routes/doctorRoutes.js @@ -8,7 +8,9 @@ router.route('/register').post(doctorController.registerDoctor); router.route('/updateDoctor').put(doctorController.updateDoctor); -router.route('/viewPatients').get(authMiddlewares.protect, authMiddlewares.restrictTo('doctor'),doctorController.viewAllPatients); +router + .route('/viewPatients') + .get(authMiddlewares.protect, authMiddlewares.restrictTo('doctor'), doctorController.viewAllPatients); router.route('/searchPatientsByName').get(authMiddlewares.protect, doctorController.searchPatientsByName); @@ -16,24 +18,42 @@ router.route('/selectPatient').get(doctorController.selectPatient); router.route('/doctordetails/:username').get(doctorController.viewDoctorDetails); -router.route('/viewAllDoctors').get(authMiddlewares.protect, authMiddlewares.restrictTo('doctor', 'patient'), doctorController.viewAllDoctors); +router + .route('/viewAllDoctors') + .get(authMiddlewares.protect, authMiddlewares.restrictTo('doctor', 'patient'), doctorController.viewAllDoctors); router.route('/viewPatientInfo').get(doctorController.viewPatientInfo); router.route('/filterDoctors').get(doctorController.filterDoctors); -router.route('/addAvailableSlot').put(authMiddlewares.protect, authMiddlewares.restrictTo('doctor'), doctorController.addAvailableSlot); +router + .route('/addAvailableSlot') + .put(authMiddlewares.protect, authMiddlewares.restrictTo('doctor'), doctorController.addAvailableSlot); -router.route('/viewDoctorAppointments').get(authMiddlewares.protect, authMiddlewares.restrictTo('doctor'),doctorController.viewDoctorAppointments); +router + .route('/viewDoctorAppointments') + .get(authMiddlewares.protect, authMiddlewares.restrictTo('doctor'), doctorController.viewDoctorAppointments); router.route('/acceptContract').put(doctorController.acceptContract); -router.route('/scheduleFollowUp').put(authMiddlewares.protect, authMiddlewares.restrictTo('doctor'),doctorController.scheduleFollowUp); +router + .route('/scheduleFollowUp') + .put(authMiddlewares.protect, authMiddlewares.restrictTo('doctor'), doctorController.scheduleFollowUp); -router.route('/viewAvailableSlots').get(authMiddlewares.protect, authMiddlewares.restrictTo('doctor'),doctorController.viewAvailableSlots); +router + .route('/viewAvailableSlots') + .get(authMiddlewares.protect, authMiddlewares.restrictTo('doctor'), doctorController.viewAvailableSlots); -router.route('/addAvailableSlot').put(authMiddlewares.protect, authMiddlewares.restrictTo('doctor'),doctorController.addAvailableSlot); +router + .route('/addAvailableSlot') + .put(authMiddlewares.protect, authMiddlewares.restrictTo('doctor'), doctorController.addAvailableSlot); -router.route('/getPatientMedicalHistory').get(authMiddlewares.protect, authMiddlewares.restrictTo('doctor'),doctorController.getPatientMedicalHistory); +router + .route('/getPatientMedicalHistory') + .get(authMiddlewares.protect, authMiddlewares.restrictTo('doctor'), doctorController.getPatientMedicalHistory); + +router + .route('/viewAllPrescriptionsByDoctor') + .get(authMiddlewares.protect, authMiddlewares.restrictTo('doctor'), doctorController.viewAllPrescriptionsByDoctor); module.exports = router; diff --git a/backend/Routes/patientRoutes.js b/backend/Routes/patientRoutes.js index 6fc72dc..4d30c99 100644 --- a/backend/Routes/patientRoutes.js +++ b/backend/Routes/patientRoutes.js @@ -7,7 +7,9 @@ const { protect, restrictTo } = require('../Middlewares/authMiddlewares'); const router = express.Router(); router.route('/register').post(patientController.registerPatient); -router.route('/getAllPrescriptionsForPatient').get(patientController.getAllPrescriptionsForPatient); +router + .route('/getAllPrescriptionsForPatient') + .get(protect, restrictTo('patient'), patientController.getAllPrescriptionsForPatient); router.route('/getPrescriptionById').get(patientController.getPrescriptionById); router.route('/filterPrescriptions').get(patientController.filterPrescriptions); router.route('/viewDoctors/:username').get(patientController.viewAllDoctors); diff --git a/frontend/src/App.jsx b/frontend/src/App.jsx index 0f0b24a..0ee0f45 100644 --- a/frontend/src/App.jsx +++ b/frontend/src/App.jsx @@ -37,6 +37,8 @@ import MedicalHistoryViewer from "./components/MedicalHistoryViewer"; import MedicalHistoryDoctorViewer from "./components/MedicalHistoryDoctorViewer"; import "./App.css"; import ScheduleFollowUpForm from "./components/ScheduleFollowUp"; +import ViewDoctorPrescriptions from "./components/ViewDoctorPrescriptions"; +import ViewPatientPrescription from "./components/ViewPatientPrescription"; function App() { return ( @@ -103,6 +105,17 @@ function App() { element={} /> + + } /> + + + } + /> + ); diff --git a/frontend/src/components/DoctorHome.jsx b/frontend/src/components/DoctorHome.jsx index 547952b..298d859 100644 --- a/frontend/src/components/DoctorHome.jsx +++ b/frontend/src/components/DoctorHome.jsx @@ -79,6 +79,11 @@ function DoctorHome() { View Patient Medical History +
  • + + View All Prescriptions + +
  • Logout diff --git a/frontend/src/components/PatientHome.jsx b/frontend/src/components/PatientHome.jsx index e9c9278..588a759 100644 --- a/frontend/src/components/PatientHome.jsx +++ b/frontend/src/components/PatientHome.jsx @@ -63,6 +63,9 @@ function PatientHome() {
  • View Medical History
  • +
  • + View All Prescriptions +
  • Logout diff --git a/frontend/src/components/PrescriptionList.jsx b/frontend/src/components/PrescriptionList.jsx index 6051a9d..e72467b 100644 --- a/frontend/src/components/PrescriptionList.jsx +++ b/frontend/src/components/PrescriptionList.jsx @@ -1,111 +1,55 @@ -import { useState } from "react"; -import axios from "axios"; -import PrescriptionDetails from "./PrescriptionDetails"; +import React, { useEffect, useState } from 'react'; +import axios from 'axios'; +import { Link } from "react-router-dom"; -const PrescriptionList = () => { - const [prescriptions, setPrescriptions] = useState([]); - const [selectedPrescription, setSelectedPrescription] = useState(null); - const [username, setUsername] = useState(""); - const [filterDate, setFilterDate] = useState(""); - const [filterDoctor, setFilterDoctor] = useState(""); - const [filterStatus, setFilterStatus] = useState("all"); // "all" means no filtering - - const handleFetchPrescriptions = async (e) => { - e.preventDefault(); // Prevent the default form submission - - try { - const response = await axios.get( - `http://localhost:8000/api/v1/patients/getAllPrescriptionsForPatient?username=${username}` - ); - setPrescriptions(response.data); - } catch (error) { - console.error("Error fetching prescriptions:", error); - } - }; - - const handlePrescriptionSelect = (prescription) => { - setSelectedPrescription(prescription); - }; - const handleFilterPrescriptions = async () => { - try { - const response = await axios.get( - `http://localhost:8000/api/v1/patients/filterPrescriptions?date=${filterDate}&doctorUsername=${filterDoctor}&filled=${filterStatus}` // Include patient username - ); - setPrescriptions(response.data); - } catch (error) { - console.error("Error filtering prescriptions:", error); - } - }; +const PatientPrescriptions = () => { + const [prescriptions, setPrescriptions] = useState([]); + const [loading, setLoading] = useState(true); + const [error, setError] = useState(null); + + useEffect(() => { + const fetchPrescriptions = async () => { + try { + // Make a GET request to your backend endpoint + const response = await axios.get('http://localhost:8000/api/v1/patients/getAllPrescriptionsForPatient'); + + // Extract the data from the response + setPrescriptions(response.data); + } catch (error) { + console.error('Error fetching prescriptions:', error); + setError('Internal server error'); + } finally { + setLoading(false); + } + }; + + // Call the fetchPrescriptions function + fetchPrescriptions(); + }, []); // Empty dependency array means this effect runs once when the component mounts + + if (loading) { + return

    Loading...

    ; + } + + if (error) { + return

    Error: {error}

    ; + } return (
    -

    Your Prescriptions

    -
    - - setUsername(e.target.value)} - /> - -
    - -
    { - e.preventDefault(); - handleFilterPrescriptions(); - }} - > - - - setFilterDate(e.target.value)} - /> - - setFilterDoctor(e.target.value)} - /> - - - -
    - - - - - - - - - - {prescriptions.map((prescription) => ( - - - - - ))} - -
    DateStatus
    - - {prescription.filled ? "Filled" : "Not Filled"}
    - - +

    Patient Prescriptions

    +
      + {prescriptions.map((prescription) => ( +
    • + Medicines: {prescription.medicines.map((med) => `${med.name} (${med.dosage}, ${med.duration})`).join(', ')}
      + Notes: {prescription.notes}
      + Filled: {prescription.filled ? 'Yes' : 'No'} +
    • + ))} +
    ); }; -export default PrescriptionList; +export default PatientPrescriptions; diff --git a/frontend/src/components/ViewDoctorPrescriptions.jsx b/frontend/src/components/ViewDoctorPrescriptions.jsx new file mode 100644 index 0000000..b17c060 --- /dev/null +++ b/frontend/src/components/ViewDoctorPrescriptions.jsx @@ -0,0 +1,62 @@ +import React, { useEffect, useState } from 'react'; +import axios from 'axios'; +import { Link } from "react-router-dom"; + + +const PrescriptionList = () => { + const [prescriptions, setPrescriptions] = useState([]); + const [loading, setLoading] = useState(true); + const [error, setError] = useState(null); + + useEffect(() => { + const fetchPrescriptions = async () => { + try { + // Make a GET request to your backend endpoint + const response = await axios.get('http://localhost:8000/api/v1/doctors/viewAllPrescriptionsByDoctor', { withCredentials: true }); + + // Extract the data from the response + const { success, prescriptions } = response.data; + + if (success) { + setPrescriptions(prescriptions); + } else { + setError('Failed to fetch prescriptions'); + } + } catch (error) { + console.error('Error fetching prescriptions:', error); + setError('Internal server error'); + } finally { + setLoading(false); + } + }; + + // Call the fetchPrescriptions function + fetchPrescriptions(); + }, []); // Empty dependency array means this effect runs once when the component mounts + + if (loading) { + return

    Loading...

    ; + } + + if (error) { + return

    Error: {error}

    ; + } + + return ( +
    +

    Prescriptions

    +
      + {prescriptions.map((prescription) => ( +
    • + Medicines: {prescription.medicines.map((med) => `${med.name} (${med.dosage}, ${med.duration})`).join(', ')}
      + Notes: {prescription.notes}
      + Filled: {prescription.filled ? 'Yes' : 'No'} +
      +
    • + ))} +
    +
    + ); +}; + +export default PrescriptionList; diff --git a/frontend/src/components/ViewPatientPrescription.jsx b/frontend/src/components/ViewPatientPrescription.jsx new file mode 100644 index 0000000..d34f1df --- /dev/null +++ b/frontend/src/components/ViewPatientPrescription.jsx @@ -0,0 +1,57 @@ +import React, { useEffect, useState } from 'react'; +import axios from 'axios'; + +const PatientPrescriptions = () => { + const [prescriptions, setPrescriptions] = useState([]); + const [loading, setLoading] = useState(true); + const [error, setError] = useState(null); + + useEffect(() => { + const fetchPrescriptions = async () => { + try { + // Make a GET request to your backend endpoint + const response = await axios.get('http://localhost:8000/api/v1/patients/getAllPrescriptionsForPatient', { withCredentials: true }); + + // Extract the data from the response + setPrescriptions(response.data); + } catch (error) { + console.error('Error fetching prescriptions:', error); + setError('Internal server error'); + } finally { + setLoading(false); + } + }; + + // Call the fetchPrescriptions function + fetchPrescriptions(); + }, []); // Empty dependency array means this effect runs once when the component mounts + + if (loading) { + return

    Loading...

    ; + } + + if (error) { + return

    Error: {error}

    ; + } + + return ( +
    +

    Patient Prescriptions

    +
      + {prescriptions.map((prescription) => ( +
    • + Doctor: {prescription.doctor.name}
      + Medicines: {prescription.medicines.map((med) => `${med.name} (${med.dosage}, ${med.duration})`).join(', ')}
      + Notes: {prescription.notes}
      + Filled: {prescription.filled ? 'Yes' : 'No'} +
      + +
    • + ))} + +
    +
    + ); +}; + +export default PatientPrescriptions;