From dccdc270fc8ed54f3ce9a340b46059402b893654 Mon Sep 17 00:00:00 2001 From: Danny <77023480+TheDanDan@users.noreply.github.com> Date: Sun, 31 Mar 2024 19:45:19 -0400 Subject: [PATCH] fixed admin logs to match other pages --- .../auth_service/database_Authentications.py | 13 +++ .../database_Prescriptions.py | 52 ++---------- frontend/src/components/AdminPrescription.jsx | 80 +++++++++++++++++++ frontend/src/pages/AdminLogs.jsx | 73 +++++++---------- frontend/src/pages/PatientSettings.jsx | 2 +- 5 files changed, 131 insertions(+), 89 deletions(-) create mode 100644 frontend/src/components/AdminPrescription.jsx diff --git a/backend/auth_service/database_Authentications.py b/backend/auth_service/database_Authentications.py index 547a6bc..72879b8 100644 --- a/backend/auth_service/database_Authentications.py +++ b/backend/auth_service/database_Authentications.py @@ -212,6 +212,19 @@ def close_client_connection(mongoClient): except Exception as e: return jsonify({"error": str(e)}), 500 +@app.route("/api/getUserID/", methods=["GET"]) +@cross_origin() +def getPatientPrescriptions(username): + + try: + user = collection.find_one({"email": username}) + + return user, 200, {"Content-Type": "application/json"} + + except Exception as e: + return jsonify({"error": str(e)}), 500 + + @app.route("/listUsers", methods=["GET"]) @cross_origin() diff --git a/backend/database_functions/database_Prescriptions.py b/backend/database_functions/database_Prescriptions.py index 8d7aa74..ca6dd2f 100644 --- a/backend/database_functions/database_Prescriptions.py +++ b/backend/database_functions/database_Prescriptions.py @@ -54,7 +54,7 @@ COMPLETE = "Complete" PR_LOGGED = "Pr Logged" PA_LOGGED = "Pa Logged" -BOTH_LOGGED = "Both logged with Discovery Pass" +BOTH_LOGGED = "Both Logged with Discovery Pass" COMPLETE_WITH_DP = "Complete with Discovery Pass" # required_PAT_prescription_fields = [ @@ -236,57 +236,17 @@ def search_prescriptions(): def update_prescription(oid): data = request.json - # Ensure incoming data fields are within the allowed list - if not set(data.keys()).issubset(template_PR): - invalid_fields = set(data.keys()) - set(template_PR) - return jsonify({"error": "Invalid fields in update request", "invalid_fields": list(invalid_fields)}), 400 - elif "status" in data and data.get("status") != COMPLETE_WITH_DP: - return jsonify({"error": "Invalid status input in update request. Check if status input is automatically assigned only."}), 400 - try: - result = collection.find_one({"_id": ObjectId(oid)}) - if not result: - return jsonify({"message": "No prescription found matching the criteria"}), 404 - elif "status" in data and result.get("status") != BOTH_LOGGED: - return jsonify({"message": "Status assigned automatically cannot be changed"}), 400 - - update_data = {k: v for k, v in data.items() if k in result and k in template_PR} - - if result.get("status") == BOTH_LOGGED and "prescriber" in result and "patient" in result: - update_data["prescriber"] = {k: v for k, v in data.items() if k in result["prescriber"]} - update_data["patient"] = {k: v for k, v in data.items() if k in result["patient"]} - if "discoveryPass" in data and data.get("discoveryPass") == "No": - update_data["status"] = COMPLETE - update_data["prescriber"]["status"] = COMPLETE - update_data["patient"]["status"] = COMPLETE - - elif result.get("status") == COMPLETE and "prescriber" in result and "patient" in result: - update_data["prescriber"] = {k: v for k, v in data.items() if k in result["prescriber"]} - update_data["patient"] = {k: v for k, v in data.items() if k in result["patient"]} - if "discoveryPass" in data and data.get("discoveryPass") == "Yes": - update_data["status"] = BOTH_LOGGED - update_data["prescriber"]["status"] = PA_LOGGED - update_data["patient"]["status"] = PR_LOGGED - - elif result.get("status") == PA_NOT_LOGGED and "prescriber" in result: - # Start with a copy of the existing 'prescriber' fields from the result - update_data["prescriber"] = result["prescriber"].copy() - # Update/overwrite with fields from 'data' that exist in the 'prescriber' structure - update_data["prescriber"].update((k, v) for k, v in data.items() if k in result["prescriber"]) - - elif result.get("status") == PR_NOT_LOGGED and "patient" in result: - # Start with a copy of the existing 'patient' fields from the result - update_data["patient"] = result["patient"].copy() - # Update/overwrite with fields from 'data' that exist in the 'patient' structure - update_data["patient"].update((k, v) for k, v in data.items() if k in result["patient"]) - - update_operation = {"$set": update_data} - updated_result = collection.find_one_and_update({"_id": ObjectId(oid)}, update_operation, return_document=True) + if "_id" in data: + del data["_id"] + update_operation = {"$set": data} + updated_result = collection.find_one_and_update({"_id": ObjectId(oid)}, update_operation) if updated_result: return jsonify({"message": "Prescription updated successfully"}), 200 except Exception as e: + print("error", e) return jsonify({"error": str(e)}), 500 return jsonify({"message": "An unexpected error occurred"}), 500 diff --git a/frontend/src/components/AdminPrescription.jsx b/frontend/src/components/AdminPrescription.jsx new file mode 100644 index 0000000..bad76b4 --- /dev/null +++ b/frontend/src/components/AdminPrescription.jsx @@ -0,0 +1,80 @@ +import React, { useEffect, useState } from "react"; +import api from "../axiosConfig"; + +const AdminPrescription = ({ item }) => { + const [data, setData] = useState(item); + + const fields = [ + { field: "Date", key: "date" }, + { field: "Patient Initials", key: "patient_initials" }, + { field: "Prescriber Code", key: "patient_email" }, + { field: "Comments", key: "comments" } + ]; + + const handleChange = (e) => { + const newStatus = e.target.value; + data.status = newStatus; + data.patient.status = newStatus; + data.prescriber.status = newStatus; + setData(data); + console.log(data); + }; + + const updateLogStatus = async () => { + console.log("Trying:", data._id.$oid); + try { + const response = await api.post(`http://localhost:5001/api/update-prescription/${data._id.$oid}`, data); + console.log(response.data); + + // const res = await fetch(`http://localhost:5001/`, { + // method: "POST", + // body: { + // targetUser: , + // title: "Prescription Updated", + // message: "Your prescription was updated by admin" + // }, + // }); + // const resjson = response.json(); + // if (resjson) { + // setTimeout(() => { + // window.location.reload(); + // }, 50); + // } + } catch (error) { + console.error("UpdateError:",error); + } + }; + + return ( + <> +
+
+ {data && ( + + + {fields.map(({ field, key }) => ( + + + setData(prevData => ({ ...prevData, [key]: e.target.value }))}> + + ))} + + + + + +
{field}:
Status: + {item.status && (item.status.includes("Complete") || item.status.includes("Both")) ? () :
{item.status}
}
+ )} +
+
+ + ); +}; + +export default AdminPrescription; diff --git a/frontend/src/pages/AdminLogs.jsx b/frontend/src/pages/AdminLogs.jsx index 7296e50..b3926c5 100644 --- a/frontend/src/pages/AdminLogs.jsx +++ b/frontend/src/pages/AdminLogs.jsx @@ -1,12 +1,14 @@ import React, { useEffect, useState } from "react"; import PageHeader from "../components/PageHeader"; import pic from "../assets/prescribertable.jpg"; -import api from "../axiosConfig"; +import AdminPrescription from "../components/AdminPrescription"; const AdminLogs = () => { const [data, setData] = useState(null); const [shownData, setShownData] = useState(null); const [searchInput, setSearchInput] = useState(""); + const [notification, setNotification] = useState(null); + const [myItem, setItem] = useState(null); // Other states and useEffects... const [updatedItemDisplay, setUpdatedItemDisplay] = useState({}); @@ -46,6 +48,15 @@ const AdminLogs = () => { fetchData(); }, [data, searchInput]); + const itemClick = (item) => { + if (myItem !== item) { + setItem(item); + } + else { + setItem(null); + } + }; + async function filterData(data, filterString) { if (!filterString) return data; @@ -73,33 +84,6 @@ const AdminLogs = () => { })); }; - const updateLogStatus = async (updatedItem) => { - const payload = { - date: updatedItem.date, - patient_initials: updatedItem.patient_initials, - prescriber_code: updatedItem.parx_code, - discoveryPass: updatedItem.discoveryPass, - }; - - // Filter out attributes with empty string values - const filteredPayload = Object.fromEntries( - Object.entries(payload).filter(([key, value]) => value !== '') - ); - - // Assuming this function does something with updatedItem, like sending it to an API - console.log("Updating item with the following details:", updatedItem); - - // Update the state to display the updated item data - setUpdatedItemDisplay(payload); - - try { - const response = await api.post(`http://localhost:5001/api/update-prescription/${updatedItem.oid.$oid}`, filteredPayload); - console.log(response.data); - } catch (error) { - console.error(error); - } - }; - return ( <> @@ -133,9 +117,9 @@ const AdminLogs = () => { <> - - - +
{item.date}
+
{item.patient_initials}
+
{item.prescriber_code}
{ - - {/* Display the updated item data */} + {myItem === item && ( + + + + + )} ))} diff --git a/frontend/src/pages/PatientSettings.jsx b/frontend/src/pages/PatientSettings.jsx index 3142436..9092135 100644 --- a/frontend/src/pages/PatientSettings.jsx +++ b/frontend/src/pages/PatientSettings.jsx @@ -9,7 +9,7 @@ import api from "../axiosConfig"; const PatientSettings = () => { const [userData, setData] = useState(null); - const { user, updateUser } = useContext(UserContext); + const { user, updateUser, getNotifications } = useContext(UserContext); useEffect(() => { if (user) setData(user);