diff --git a/backend/src/app/controllers/patient.controller.ts b/backend/src/app/controllers/patient.controller.ts index a5adced9..47d15c27 100644 --- a/backend/src/app/controllers/patient.controller.ts +++ b/backend/src/app/controllers/patient.controller.ts @@ -15,6 +15,8 @@ import { getHealthRecordsFiles, getPatientHealthRecords, deleteMedicalHistory, + deleteHealthRecord, + getMedicalHistoryFiles, } from '../services/patient.service' import { GetAPatientResponse, @@ -64,6 +66,27 @@ patientRouter.post( }) ) +patientRouter.post( + '/deleteHealthRecord/:id', + asyncWrapper(async (req, res) => { + const id = req.params.id + const url = req.body.url + console.log(url) + const response = await deleteHealthRecord(id, url) + res.send(response) + }) +) + +patientRouter.get( + //Health Records Uploads for doctor + '/getMedicalHistory/:id', + asyncWrapper(async (req, res) => { + const medicalHistory = await getMedicalHistoryFiles(req.params.id) + + res.send(medicalHistory) + }) +) + patientRouter.post( '/deleteMedicalHistory/mine', asyncWrapper(async (req, res) => { @@ -78,7 +101,6 @@ patientRouter.post( res.send(response) }) ) - patientRouter.post( '/uploadHealthRecords/:id', upload.single('HealthRecord'), diff --git a/backend/src/app/services/patient.service.ts b/backend/src/app/services/patient.service.ts index c85d00b9..a03d5b67 100644 --- a/backend/src/app/services/patient.service.ts +++ b/backend/src/app/services/patient.service.ts @@ -81,6 +81,24 @@ export async function deleteMedicalHistory( } } +export async function deleteHealthRecord( + id: string, + url: string +): Promise { + const updateResult = await PatientModel.updateOne( + { _id: id }, + { $pull: { healthRecords: url } } + ).exec() + + if (updateResult.matchedCount === 0) { + throw new NotFoundError() // If no document was found + } + + if (updateResult.modifiedCount === 0) { + throw new Error('No document was modified') // If the document was found but not modified + } +} + export async function getMyMedicalHistory(id: string): Promise { const patient = await PatientModel.findOne({ user: id }).exec() if (patient == null) throw new NotFoundError() @@ -88,6 +106,13 @@ export async function getMyMedicalHistory(id: string): Promise { return patient.documents } +export async function getMedicalHistoryFiles(id: string): Promise { + const patient = await PatientModel.findOne({ _id: id }).exec() + if (patient == null) throw new NotFoundError() + + return patient.documents +} + export async function getPatientByID(id: string): Promise<{ patient: PatientDocumentWithUser appointments: Array> diff --git a/frontend/src/features/doctor-dashboard/routes/AddHealthRecord.tsx b/frontend/src/features/doctor-dashboard/routes/AddHealthRecord.tsx index e8121e02..c29dba6d 100644 --- a/frontend/src/features/doctor-dashboard/routes/AddHealthRecord.tsx +++ b/frontend/src/features/doctor-dashboard/routes/AddHealthRecord.tsx @@ -29,6 +29,23 @@ function AddHealthRecord() { setImageValue({ file: event.currentTarget.files[0] }) } + const handleDelete = async (urlToDelete: string) => { + try { + // Send a POST request to delete the URL + await api.post( + `http://localhost:3000/patients/deleteHealthRecord/${id}`, + { url: urlToDelete } + ) + + // Remove the deleted URL from the state + setDownloadURLs((prevURLs) => + prevURLs.filter((url) => url !== urlToDelete) + ) + } catch (err) { + console.error('Error deleting URL:', err) + } + } + const handleUpload = async () => { if (!imageValue.file) { alert('Please select a file to upload.') @@ -72,6 +89,13 @@ function AddHealthRecord() {

{'File ' + (index + 1)}