Skip to content

Commit

Permalink
delete Health Record by doc and view Medical History uploaded by patient
Browse files Browse the repository at this point in the history
  • Loading branch information
Dareenfadel authored and Dareenfadel committed Nov 13, 2023
1 parent fdb602e commit dcb884f
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 1 deletion.
24 changes: 23 additions & 1 deletion backend/src/app/controllers/patient.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
getHealthRecordsFiles,
getPatientHealthRecords,
deleteMedicalHistory,
deleteHealthRecord,
getMedicalHistoryFiles,
} from '../services/patient.service'
import {
GetAPatientResponse,
Expand Down Expand Up @@ -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) => {
Expand All @@ -78,7 +101,6 @@ patientRouter.post(
res.send(response)
})
)

patientRouter.post(
'/uploadHealthRecords/:id',
upload.single('HealthRecord'),
Expand Down
25 changes: 25 additions & 0 deletions backend/src/app/services/patient.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,38 @@ export async function deleteMedicalHistory(
}
}

export async function deleteHealthRecord(
id: string,
url: string
): Promise<void> {
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<string[]> {
const patient = await PatientModel.findOne({ user: id }).exec()
if (patient == null) throw new NotFoundError()

return patient.documents
}

export async function getMedicalHistoryFiles(id: string): Promise<string[]> {
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<HydratedDocument<AppointmentDocument>>
Expand Down
24 changes: 24 additions & 0 deletions frontend/src/features/doctor-dashboard/routes/AddHealthRecord.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
Expand Down Expand Up @@ -72,6 +89,13 @@ function AddHealthRecord() {
<h2>{'File ' + (index + 1)}</h2>
<iframe title={'File' + index} src={url} width="80%" height="400px" />
<br />
<Button
color="error"
variant="outlined"
onClick={() => handleDelete(url)}
>
Delete
</Button>
</div>
))}
</div>
Expand Down
5 changes: 5 additions & 0 deletions frontend/src/features/doctor-dashboard/routes/Patient.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ export function Patient() {
Health Records Files
</Button>
</Link>
<Link to={'../medicalHistory/' + id}>
<Button variant="contained" color="primary">
Medical History Files
</Button>
</Link>
</Stack>
</CardContent>
</Card>
Expand Down
5 changes: 5 additions & 0 deletions frontend/src/features/doctor-dashboard/routes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ViewMyAvailableTimeSlots } from './ViewMyAvailableTimeSlots'
import { Wallet } from './Wallet'
import { EmploymentContract } from './EmploymentContract'
import AddHealthRecord from './AddHealthRecord'
import ViewMedicalHistory from './viewPatientMedicalHistory'

export const doctorDashboardRoutes: RouteObject[] = [
{
Expand Down Expand Up @@ -51,6 +52,10 @@ export const doctorDashboardRoutes: RouteObject[] = [
path: 'healthRecords/:id',
element: <AddHealthRecord />,
},
{
path: 'medicalHistory/:id',
element: <ViewMedicalHistory />,
},
],
},
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { api } from '@/api'
import { Typography, Container } from '@mui/material'
import { useEffect, useState } from 'react'
import { useParams } from 'react-router-dom'

function ViewMedicalHistory() {
const { id } = useParams()
const [MedicalHistory, setMedicalHistoryFiles] = useState([])

useEffect(() => {
const fetchMedicalHistory = async () => {
try {
const response = await api.get(
`http://localhost:3000/patients/getMedicalHistory/${id}`
)
setMedicalHistoryFiles(response.data)
} catch (error) {
console.error('Error fetching health records:', error)
}
}

fetchMedicalHistory()
}, [id])

return (
<Container maxWidth="md" className="App">
<Typography
variant="h3"
style={{ marginTop: '50px' }}
color="primary"
component="h1"
gutterBottom
>
Health Records Files
</Typography>

{MedicalHistory.map((url, index) => (
<div key={index}>
<h2>{'File ' + (index + 1)}</h2>
<iframe title={'File' + index} src={url} width="80%" height="400px" />
<br />
</div>
))}
</Container>
)
}

export default ViewMedicalHistory

0 comments on commit dcb884f

Please sign in to comment.