From 41a77c1ca932b4f49f5d178bc3d61c41330c9ef2 Mon Sep 17 00:00:00 2001 From: beshO-Oy Date: Mon, 13 Nov 2023 19:40:47 +0200 Subject: [PATCH 1/4] Add a new health record. --- backend/Controllers/doctorController.js | 14 ++++++++++++++ backend/Routes/doctorRoutes.js | 2 ++ 2 files changed, 16 insertions(+) diff --git a/backend/Controllers/doctorController.js b/backend/Controllers/doctorController.js index 6a79682..27239d9 100644 --- a/backend/Controllers/doctorController.js +++ b/backend/Controllers/doctorController.js @@ -196,3 +196,17 @@ exports.acceptContract = async (req, res) => { res.status(500).json({ error: 'Internal server error' }); } }; + +exports.addHealthRecord = async (req, res) => { + try { + const newHealthRecord = await new Prescription({ + patient: req.body.patient, + doctor: req.user, + date: req.body.date, + description: req.body.description, + prescription: req.body.prescription, + }).save(); + } catch (error) { + res.status(500).json({ message: error.message }); + } +}; diff --git a/backend/Routes/doctorRoutes.js b/backend/Routes/doctorRoutes.js index aa60158..9665753 100644 --- a/backend/Routes/doctorRoutes.js +++ b/backend/Routes/doctorRoutes.js @@ -24,4 +24,6 @@ router.route('/filterDoctors').get(doctorController.filterDoctors); router.route('/acceptContract').put(doctorController.acceptContract); +router.route('/addHealthRecord').post(protect, restrictTo('doctor'), doctorController.addHealthRecord); + module.exports = router; From 1baec502c842edef7cfdc4b9b3bba15054ee8586 Mon Sep 17 00:00:00 2001 From: beshO-Oy Date: Tue, 14 Nov 2023 05:13:48 +0200 Subject: [PATCH 2/4] add health record --- backend/Controllers/doctorController.js | 19 +++++++++++++++---- backend/Models/Admin.js | 5 +++-- backend/Models/Doctor.js | 4 +++- backend/Models/Patient.js | 5 +++-- 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/backend/Controllers/doctorController.js b/backend/Controllers/doctorController.js index 27239d9..00f325f 100644 --- a/backend/Controllers/doctorController.js +++ b/backend/Controllers/doctorController.js @@ -199,13 +199,24 @@ exports.acceptContract = async (req, res) => { exports.addHealthRecord = async (req, res) => { try { + const patientId = req.body.patientId; + const patient = await Patient.findById(patientId); + + const medicines = req.body.medicines.map(medicine => ({ + name: medicine.name, + dosage: medicine.dosage, + duration: medicine.duration, + })); + const newHealthRecord = await new Prescription({ - patient: req.body.patient, + patient: patient, doctor: req.user, - date: req.body.date, - description: req.body.description, - prescription: req.body.prescription, + medicines: medicines, + notes: req.body.notes, + filled: req.body.filled, }).save(); + + res.status(201).json(newHealthRecord); // Assuming you want to return the created health record } catch (error) { res.status(500).json({ message: error.message }); } diff --git a/backend/Models/Admin.js b/backend/Models/Admin.js index f3e145f..2f1f0fe 100644 --- a/backend/Models/Admin.js +++ b/backend/Models/Admin.js @@ -14,8 +14,9 @@ const adminSchema = new mongoose.Schema({ }); adminSchema.pre('save', async function (next) { - this.password = await bcrypt.hash(this.password, 12); - + if (this.isModified('password')) { + this.password = await bcrypt.hash(this.password, 12); + } next(); }); diff --git a/backend/Models/Doctor.js b/backend/Models/Doctor.js index 838e4c9..fc86660 100644 --- a/backend/Models/Doctor.js +++ b/backend/Models/Doctor.js @@ -54,7 +54,9 @@ const doctorSchema = new mongoose.Schema({ }); doctorSchema.pre('save', async function (next) { - this.password = await bcrypt.hash(this.password, 12); + if (this.isModified('password')) { + this.password = await bcrypt.hash(this.password, 12); + } next(); }); diff --git a/backend/Models/Patient.js b/backend/Models/Patient.js index 2e5d0d9..b266c29 100644 --- a/backend/Models/Patient.js +++ b/backend/Models/Patient.js @@ -57,8 +57,9 @@ const patientSchema = new mongoose.Schema({ }); patientSchema.pre('save', async function (next) { - this.password = await bcrypt.hash(this.password, 12); - + if (this.isModified('password')) { + this.password = await bcrypt.hash(this.password, 12); + } next(); }); From 2fe7d040e32e2e21bc13c9eeb975e107b675bf33 Mon Sep 17 00:00:00 2001 From: beshO-Oy Date: Tue, 14 Nov 2023 09:34:35 +0200 Subject: [PATCH 3/4] Add health record --- backend/Controllers/doctorController.js | 6 +++--- backend/Controllers/patientController.js | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/backend/Controllers/doctorController.js b/backend/Controllers/doctorController.js index 00f325f..2d5174e 100644 --- a/backend/Controllers/doctorController.js +++ b/backend/Controllers/doctorController.js @@ -199,8 +199,8 @@ exports.acceptContract = async (req, res) => { exports.addHealthRecord = async (req, res) => { try { - const patientId = req.body.patientId; - const patient = await Patient.findById(patientId); + const patientUsername = req.body.patientUsername; + const patient = await Patient.findOne({ username: patientUsername }); const medicines = req.body.medicines.map(medicine => ({ name: medicine.name, @@ -216,7 +216,7 @@ exports.addHealthRecord = async (req, res) => { filled: req.body.filled, }).save(); - res.status(201).json(newHealthRecord); // Assuming you want to return the created health record + res.status(201).json(newHealthRecord); } catch (error) { res.status(500).json({ message: error.message }); } diff --git a/backend/Controllers/patientController.js b/backend/Controllers/patientController.js index 5eab289..e1be411 100644 --- a/backend/Controllers/patientController.js +++ b/backend/Controllers/patientController.js @@ -251,7 +251,7 @@ exports.subscribeToHealthPackage = async (req, res) => { exports.viewHealthPackageOfPatient = async (req, res) => { try { - const patientId = req.user._id; // Assuming patientId is in the URL parameters + const patientId = req.user._id; const patient = await Patient.findById(patientId).populate('healthPackage'); const patientPackage = patient.healthPackage; res.status(200).send(patientPackage); From 5f67b7eb42ee3d21a9c000e3ea2fe2332410acd3 Mon Sep 17 00:00:00 2001 From: beshO-Oy Date: Tue, 14 Nov 2023 09:35:46 +0200 Subject: [PATCH 4/4] Add health records - final --- frontend/src/App.jsx | 2 + frontend/src/components/AddHealthRecord.jsx | 83 +++++++++++++++++++++ frontend/src/components/DoctorHome.jsx | 3 + 3 files changed, 88 insertions(+) create mode 100644 frontend/src/components/AddHealthRecord.jsx diff --git a/frontend/src/App.jsx b/frontend/src/App.jsx index 9ae5346..c435104 100644 --- a/frontend/src/App.jsx +++ b/frontend/src/App.jsx @@ -26,6 +26,7 @@ import { BrowserRouter, Routes, Route } from "react-router-dom"; import HealthPackages from "./components/HealthPackages"; import Contract from "./components/Contract"; import SubscribeHealthPackages from "./components/SubscribeHealthPackages"; +import AddHealthRecord from "./components/AddHealthRecord"; import "./App.css"; function App() { @@ -56,6 +57,7 @@ function App() { } /> } /> } /> + } /> {/* diff --git a/frontend/src/components/AddHealthRecord.jsx b/frontend/src/components/AddHealthRecord.jsx new file mode 100644 index 0000000..6beb952 --- /dev/null +++ b/frontend/src/components/AddHealthRecord.jsx @@ -0,0 +1,83 @@ +import { useState } from "react"; +import axios from "axios"; + +const AddHealthRecordForm = () => { + const [patientUsername, setPatientUsername] = useState(''); + const [medicineName, setMedicineName] = useState(''); + const [dosage, setDosage] = useState(''); + const [duration, setDuration] = useState(''); + const [notes, setNotes] = useState(''); + const [filled, setFilled] = useState(true); + const [msg, setMsg] = useState(''); + + const handleAddHealthRecord = async (e) => { + e.preventDefault(); + try { + const response = await axios.post('http://localhost:8000/api/v1/doctors/addHealthRecord', + { + patientUsername: patientUsername, + medicines: [ + { + name: medicineName, + dosage: dosage, + duration: duration, + }, + ], + notes: notes, + filled: filled}, + { withCredentials: true }); + setPatientUsername(patientUsername); + setMedicineName(medicineName); + setDosage(dosage); + setDuration(duration); + setNotes(notes); + setFilled(filled); + const newHealthRecord = response.data; + console.log('Health Record added successfully:', newHealthRecord); + setMsg('Health Record added successfully!'); + } catch (error) { + console.error('Error:', error.message); + } + }; + + return ( +
+

Add Health Record

+ +
+ +
+ +
+ +
+