Update database.py
Browse files- database.py +51 -1
database.py
CHANGED
|
@@ -439,6 +439,51 @@ class DatabaseManager:
|
|
| 439 |
finally:
|
| 440 |
conn.close()
|
| 441 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 442 |
def get_patient_consultations(self, patient_id: int) -> List[Dict]:
|
| 443 |
conn = self.get_connection()
|
| 444 |
try:
|
|
@@ -484,7 +529,12 @@ class DatabaseManager:
|
|
| 484 |
offset = (page - 1) * per_page
|
| 485 |
|
| 486 |
rows = conn.execute(
|
| 487 |
-
f"""SELECT c.*,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 488 |
{base} {where}
|
| 489 |
ORDER BY c.consultationDate DESC
|
| 490 |
LIMIT ? OFFSET ?""",
|
|
|
|
| 439 |
finally:
|
| 440 |
conn.close()
|
| 441 |
|
| 442 |
+
def get_consultation_by_id(self, consultation_id: int, user_id: int, role: str) -> Optional[Dict]:
|
| 443 |
+
"""Devuelve una consulta completa con datos del paciente y factores de riesgo.
|
| 444 |
+
Los doctores solo pueden ver sus propias consultas."""
|
| 445 |
+
conn = self.get_connection()
|
| 446 |
+
try:
|
| 447 |
+
query = """
|
| 448 |
+
SELECT
|
| 449 |
+
c.consultationID, c.patientID, c.createdByUserID,
|
| 450 |
+
c.diabeticRetinopathy, c.notes, c.consultationDate,
|
| 451 |
+
c.imagePath, c.confidence, c.rawOutput,
|
| 452 |
+
p.name AS patientName,
|
| 453 |
+
p.birthDate AS birthDate,
|
| 454 |
+
p.gender AS gender,
|
| 455 |
+
p.diabetesType AS diabetesType,
|
| 456 |
+
u.username AS doctorName
|
| 457 |
+
FROM Consultations c
|
| 458 |
+
JOIN Patients p ON c.patientID = p.patientID
|
| 459 |
+
JOIN Users u ON c.createdByUserID = u.userID
|
| 460 |
+
WHERE c.consultationID = ?
|
| 461 |
+
"""
|
| 462 |
+
params = [consultation_id]
|
| 463 |
+
if role != "Admin":
|
| 464 |
+
query += " AND c.createdByUserID = ?"
|
| 465 |
+
params.append(user_id)
|
| 466 |
+
|
| 467 |
+
row = conn.execute(query, params).fetchone()
|
| 468 |
+
if not row:
|
| 469 |
+
return None
|
| 470 |
+
|
| 471 |
+
result = self._serialize(row)
|
| 472 |
+
|
| 473 |
+
# Cargar factores de riesgo del paciente
|
| 474 |
+
rf_rows = conn.execute(
|
| 475 |
+
"""SELECT rf.riskFactorID, rf.name, rf.description
|
| 476 |
+
FROM RiskFactors rf
|
| 477 |
+
JOIN PatientsRiskFactors prf ON rf.riskFactorID = prf.riskFactorID
|
| 478 |
+
WHERE prf.patientID = ?""",
|
| 479 |
+
(result["patientID"],)
|
| 480 |
+
).fetchall()
|
| 481 |
+
result["riskFactors"] = [self._serialize(r) for r in rf_rows]
|
| 482 |
+
|
| 483 |
+
return result
|
| 484 |
+
finally:
|
| 485 |
+
conn.close()
|
| 486 |
+
|
| 487 |
def get_patient_consultations(self, patient_id: int) -> List[Dict]:
|
| 488 |
conn = self.get_connection()
|
| 489 |
try:
|
|
|
|
| 529 |
offset = (page - 1) * per_page
|
| 530 |
|
| 531 |
rows = conn.execute(
|
| 532 |
+
f"""SELECT c.*,
|
| 533 |
+
p.name AS patientName,
|
| 534 |
+
p.birthDate AS birthDate,
|
| 535 |
+
p.gender AS gender,
|
| 536 |
+
p.diabetesType AS diabetesType,
|
| 537 |
+
u.username AS doctorName
|
| 538 |
{base} {where}
|
| 539 |
ORDER BY c.consultationDate DESC
|
| 540 |
LIMIT ? OFFSET ?""",
|