-
Notifications
You must be signed in to change notification settings - Fork 0
/
patient_encounter.py
192 lines (149 loc) · 5.98 KB
/
patient_encounter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import uuid
from typing import Optional, List, Dict, Any
import argon2 as standalone_argon2
import logging
from passlib.hash import argon2
from sqlalchemy import Column, DateTime, Integer, String, Boolean
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import Session
from api.main.database import BaseMedical
from api.models.mixins import BasicMetrics
logger = logging.getLogger(__name__)
class PatientEncounter(BaseMedical, BasicMetrics):
__tablename__ = "patient_encounters"
id = Column(Integer, primary_key=True, index=True, autoincrement=True)
patient_encounter_uuid = Column(UUID(as_uuid=True), default=uuid.uuid4, unique=True)
user_uuid = Column(UUID(as_uuid=True), default=uuid.uuid4)
document_num = Column(String)
patient_rfid = Column(String, nullable=True)
location = Column(String)
qr_code = Column(String, nullable=True)
arrival_date = Column(DateTime)
arrival_method = Column(String)
arrival_time = Column(DateTime)
on_shift = Column(Boolean)
triage_acuity = Column(String)
chief_complaints = Column(String)
departure_date = Column(DateTime, nullable=True)
departure_time = Column(DateTime, nullable=True)
departure_dest = Column(String, nullable=True)
handover_from = Column(String, nullable=True)
handover_too = Column(String)
comment = Column(String, nullable=True)
age = Column(Integer, nullable=True)
gender = Column(String, nullable=True)
def get_patient_encounter_by_id(db: Session, id: int) -> Optional[PatientEncounter]:
return (
db.query(PatientEncounter)
.filter(PatientEncounter.id == id, PatientEncounter.deleted == False)
.first()
)
def get_patient_encounter_by_uuid(
db: Session, uuid: uuid.UUID
) -> Optional[PatientEncounter]:
return (
db.query(PatientEncounter)
.filter(
PatientEncounter.patient_encounter_uuid == uuid,
PatientEncounter.deleted == False,
)
.first()
)
def get_patient_encounter_by_document_number(
db: Session, document_number: str
) -> Optional[PatientEncounter]:
return (
db.query(PatientEncounter)
.filter(
PatientEncounter.document_num == document_number,
PatientEncounter.deleted == False,
)
.first()
)
def get_latest_patient_encounter_by_patient_rfid(
db: Session, patient_rfid: str
) -> Optional[PatientEncounter]:
"""
Returns the latest patient encounter for a given patient RFID.
If the patient RFID is found returns the latest patient encounter; otherwise, returns None.
Returns:
A PatientEncounter.
"""
patient_encounters = (
db.query(PatientEncounter)
.filter(PatientEncounter.deleted == False)
.order_by(PatientEncounter.last_updated_timestamp.desc())
.all()
)
for encounter in patient_encounters:
if encounter.patient_rfid.startswith("$argon2"):
try:
if argon2.verify(patient_rfid, encounter.patient_rfid):
return encounter
except standalone_argon2.exceptions.VerifyMismatchError:
continue
except (
standalone_argon2.exceptions.VerificationError,
standalone_argon2.exceptions.InvalidHash,
) as e:
logger.error(e, exc_info=True)
elif encounter.patient_rfid == patient_rfid:
return encounter
return None
def get_all_patient_encounters(
db: Session, arrival_date_min: Optional[str], arrival_date_max: Optional[str]
) -> Optional[List[PatientEncounter]]:
# Base query
query = db.query(PatientEncounter).filter(PatientEncounter.deleted == False)
# If arrival_date_min is provided, filter by arrival_date_min
if arrival_date_min:
query = query.filter(PatientEncounter.arrival_date >= arrival_date_min)
# If arrival_date_max is provided, filter by arrival_date_max
if arrival_date_max:
query = query.filter(PatientEncounter.arrival_date <= arrival_date_max)
return query.all()
def create_patient_encounter(db: Session, data: PatientEncounter) -> PatientEncounter:
"""Create a patient encounter with a unique ID.
If provided, the patient RFID is hashed before being stored in the database; otherwise, it is stored as null.
Returns:
A PatientEncounter.
"""
created_patient_encounter = data
if (
created_patient_encounter.patient_rfid
and created_patient_encounter.patient_rfid != ""
):
hashed_rfid = argon2.hash(created_patient_encounter.patient_rfid)
created_patient_encounter.patient_rfid = hashed_rfid
db.add(created_patient_encounter)
db.commit()
db.refresh(created_patient_encounter)
return created_patient_encounter
def update_patient_encounter(
db: Session, encounter: PatientEncounter, updated_values: Dict[str, Any]
) -> PatientEncounter:
"""
Update an existing patient encounter document using its existing UUID. Returns the updated patient encounter.
If provided, the patient RFID is hashed before being stored in the database; otherwise, it is stored as null.
"""
updated_encounter = encounter
# Check if the patient RFID has been updated
if "patient_rfid" in updated_values:
updated_rfid = updated_values["patient_rfid"]
hashed_rfid = argon2.hash(updated_rfid)
updated_values["patient_rfid"] = hashed_rfid
db.query(PatientEncounter).filter(
PatientEncounter.deleted == False,
PatientEncounter.patient_encounter_uuid == encounter.patient_encounter_uuid,
).update(values=updated_values)
db.commit()
db.refresh(updated_encounter)
return updated_encounter
def soft_delete_patient_encounter(db: Session, uuid: uuid.UUID) -> None:
"""
Update patient encounter property to set deleted to True.
"""
db.query(PatientEncounter).filter(
PatientEncounter.patient_encounter_uuid == uuid
).update(values={"deleted": True})
db.commit()