healthdatalayer 1.6.6__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- healthdatalayer/__init__.py +0 -0
- healthdatalayer/config/__init__.py +0 -0
- healthdatalayer/config/config.py +24 -0
- healthdatalayer/config/db.py +12 -0
- healthdatalayer/config/vault.py +20 -0
- healthdatalayer/dtos/__init__.py +2 -0
- healthdatalayer/dtos/collaborator/__init__.py +0 -0
- healthdatalayer/dtos/collaborator/schedule_collaborator.py +13 -0
- healthdatalayer/dtos/medical_visit/__init__.py +0 -0
- healthdatalayer/dtos/medical_visit/medical_certificate.py +62 -0
- healthdatalayer/models/__init__.py +75 -0
- healthdatalayer/models/bridge_area_floor_branch/__init__.py +0 -0
- healthdatalayer/models/bridge_area_floor_branch/area.py +8 -0
- healthdatalayer/models/bridge_area_floor_branch/branch.py +16 -0
- healthdatalayer/models/bridge_area_floor_branch/bridge_area_floor_branch.py +27 -0
- healthdatalayer/models/bridge_area_floor_branch/floor.py +8 -0
- healthdatalayer/models/bridge_area_floor_branch/room.py +8 -0
- healthdatalayer/models/bridge_area_floor_branch/system.py +8 -0
- healthdatalayer/models/client/__init__.py +0 -0
- healthdatalayer/models/client/address.py +13 -0
- healthdatalayer/models/client/city.py +11 -0
- healthdatalayer/models/client/client.py +25 -0
- healthdatalayer/models/client/client_type.py +9 -0
- healthdatalayer/models/client/education.py +9 -0
- healthdatalayer/models/client/emergency_contact.py +17 -0
- healthdatalayer/models/client/gender.py +9 -0
- healthdatalayer/models/client/marriage_status.py +9 -0
- healthdatalayer/models/client/nationality.py +10 -0
- healthdatalayer/models/client/pathological_history.py +29 -0
- healthdatalayer/models/client/pet.py +16 -0
- healthdatalayer/models/client/profession.py +9 -0
- healthdatalayer/models/client/px.py +35 -0
- healthdatalayer/models/client/state.py +9 -0
- healthdatalayer/models/collaborator/__init__.py +0 -0
- healthdatalayer/models/collaborator/collaborator.py +33 -0
- healthdatalayer/models/collaborator/collaborator_speciality.py +8 -0
- healthdatalayer/models/collaborator/collaborator_type.py +9 -0
- healthdatalayer/models/collaborator/speciality.py +23 -0
- healthdatalayer/models/lab/__init__.py +0 -0
- healthdatalayer/models/lab/client_lab.py +13 -0
- healthdatalayer/models/lab/measure_lab.py +11 -0
- healthdatalayer/models/lab/medical_lab.py +17 -0
- healthdatalayer/models/medical_visit/__init__.py +0 -0
- healthdatalayer/models/medical_visit/medical_diagnosis.py +12 -0
- healthdatalayer/models/medical_visit/medical_diagnosis_visit.py +25 -0
- healthdatalayer/models/medical_visit/medical_drug.py +27 -0
- healthdatalayer/models/medical_visit/medical_drug_recipe.py +19 -0
- healthdatalayer/models/medical_visit/medical_recipe_visit.py +28 -0
- healthdatalayer/models/medical_visit/medical_visit.py +53 -0
- healthdatalayer/models/medical_visit/organ_system_review.py +28 -0
- healthdatalayer/models/medical_visit/physical_exam.py +45 -0
- healthdatalayer/models/medical_visit/status_visit_enum.py +8 -0
- healthdatalayer/models/medical_visit/visit_triage.py +28 -0
- healthdatalayer/models/user/__init__.py +0 -0
- healthdatalayer/models/user/permission.py +27 -0
- healthdatalayer/models/user/permission_user.py +8 -0
- healthdatalayer/models/user/role.py +27 -0
- healthdatalayer/models/user/role_permission.py +8 -0
- healthdatalayer/models/user/role_user.py +8 -0
- healthdatalayer/models/user/user.py +30 -0
- healthdatalayer/repositories/__init__.py +40 -0
- healthdatalayer/repositories/client_repositories/__init__.py +0 -0
- healthdatalayer/repositories/client_repositories/address_repository.py +94 -0
- healthdatalayer/repositories/client_repositories/client_type_repository.py +69 -0
- healthdatalayer/repositories/client_repositories/education_repository.py +70 -0
- healthdatalayer/repositories/client_repositories/emergency_contact_repository.py +78 -0
- healthdatalayer/repositories/client_repositories/gender_repository.py +70 -0
- healthdatalayer/repositories/client_repositories/marriage_status_repository.py +70 -0
- healthdatalayer/repositories/client_repositories/nationality_repository.py +69 -0
- healthdatalayer/repositories/client_repositories/pathological_history_repository.py +73 -0
- healthdatalayer/repositories/client_repositories/pet_repository.py +126 -0
- healthdatalayer/repositories/client_repositories/profession_repository.py +70 -0
- healthdatalayer/repositories/client_repositories/px_repository.py +239 -0
- healthdatalayer/repositories/collaborator_repositories/__init__.py +0 -0
- healthdatalayer/repositories/collaborator_repositories/collaborator_repository.py +197 -0
- healthdatalayer/repositories/collaborator_repositories/collaborator_type_repository.py +69 -0
- healthdatalayer/repositories/collaborator_repositories/speciality_repository.py +75 -0
- healthdatalayer/repositories/infraestructure_repositories/__init__.py +0 -0
- healthdatalayer/repositories/infraestructure_repositories/area_repository.py +69 -0
- healthdatalayer/repositories/infraestructure_repositories/branch_repository.py +69 -0
- healthdatalayer/repositories/infraestructure_repositories/bridge_repository.py +82 -0
- healthdatalayer/repositories/infraestructure_repositories/floor_repository.py +69 -0
- healthdatalayer/repositories/infraestructure_repositories/room_repository.py +69 -0
- healthdatalayer/repositories/infraestructure_repositories/system_repository.py +69 -0
- healthdatalayer/repositories/lab_repositories/__init__.py +0 -0
- healthdatalayer/repositories/lab_repositories/measure_lab_repository.py +80 -0
- healthdatalayer/repositories/lab_repositories/medical_lab_repository.py +254 -0
- healthdatalayer/repositories/medical_visit_repositories/__init__.py +0 -0
- healthdatalayer/repositories/medical_visit_repositories/medical_diagnosis_repository.py +98 -0
- healthdatalayer/repositories/medical_visit_repositories/medical_diagnosis_visit_repository.py +109 -0
- healthdatalayer/repositories/medical_visit_repositories/medical_drug_recipe_repository.py +71 -0
- healthdatalayer/repositories/medical_visit_repositories/medical_drug_repository.py +78 -0
- healthdatalayer/repositories/medical_visit_repositories/medical_recipe_visit_repository.py +93 -0
- healthdatalayer/repositories/medical_visit_repositories/medical_visit_repository.py +509 -0
- healthdatalayer/repositories/medical_visit_repositories/organ_system_review_repository.py +89 -0
- healthdatalayer/repositories/medical_visit_repositories/physical_exam_repository.py +89 -0
- healthdatalayer/repositories/medical_visit_repositories/visit_triage_repository.py +95 -0
- healthdatalayer/repositories/user_repositories/__init__.py +0 -0
- healthdatalayer/repositories/user_repositories/permission_repository.py +238 -0
- healthdatalayer/repositories/user_repositories/role_repository.py +174 -0
- healthdatalayer/repositories/user_repositories/user_repository.py +251 -0
- healthdatalayer-1.6.6.dist-info/METADATA +30 -0
- healthdatalayer-1.6.6.dist-info/RECORD +105 -0
- healthdatalayer-1.6.6.dist-info/WHEEL +5 -0
- healthdatalayer-1.6.6.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
from typing import Optional, List
|
|
2
|
+
from uuid import UUID
|
|
3
|
+
from sqlmodel import select, or_
|
|
4
|
+
from sqlalchemy.orm import selectinload,joinedload
|
|
5
|
+
from healthdatalayer.models import MedicalRecipeVisit
|
|
6
|
+
from healthdatalayer.config.db import engines, get_session
|
|
7
|
+
|
|
8
|
+
class MedicalRecipeVisitRepository:
|
|
9
|
+
def __init__(self, tenant: str):
|
|
10
|
+
self.tenant = tenant
|
|
11
|
+
if tenant not in engines:
|
|
12
|
+
raise ValueError(f"Tenant {tenant} is not configured")
|
|
13
|
+
|
|
14
|
+
def create_command(self, medical_recipe_visit: MedicalRecipeVisit) -> MedicalRecipeVisit:
|
|
15
|
+
with get_session(self.tenant) as session:
|
|
16
|
+
session.add(medical_recipe_visit)
|
|
17
|
+
session.commit()
|
|
18
|
+
session.refresh(medical_recipe_visit)
|
|
19
|
+
return medical_recipe_visit
|
|
20
|
+
|
|
21
|
+
def get_by_id_command(self, medical_recipe_visit_id: UUID, load_relations: bool = False) -> Optional[MedicalRecipeVisit]:
|
|
22
|
+
with get_session(self.tenant) as session:
|
|
23
|
+
|
|
24
|
+
if load_relations:
|
|
25
|
+
statement = select(MedicalRecipeVisit).where(MedicalRecipeVisit.medical_recipe_visit_id == medical_recipe_visit_id).options(
|
|
26
|
+
joinedload(MedicalRecipeVisit.medical_visit),
|
|
27
|
+
joinedload(MedicalRecipeVisit.medical_drugs)
|
|
28
|
+
)
|
|
29
|
+
medical_recipe_visit = session.exec(statement).first()
|
|
30
|
+
|
|
31
|
+
return medical_recipe_visit
|
|
32
|
+
else:
|
|
33
|
+
return session.get(MedicalRecipeVisit, medical_recipe_visit_id)
|
|
34
|
+
|
|
35
|
+
def get_by_medical_visit_id_command(self, medical_visit_id: UUID, load_relations: bool = False) -> Optional[MedicalRecipeVisit]:
|
|
36
|
+
with get_session(self.tenant) as session:
|
|
37
|
+
statement = select(MedicalRecipeVisit).where(MedicalRecipeVisit.medical_visit_id == medical_visit_id)
|
|
38
|
+
if load_relations:
|
|
39
|
+
statement = statement.options(
|
|
40
|
+
selectinload(MedicalRecipeVisit.medical_visit),
|
|
41
|
+
selectinload(MedicalRecipeVisit.medical_drugs)
|
|
42
|
+
)
|
|
43
|
+
medical_recipe_visit = session.exec(statement).first()
|
|
44
|
+
|
|
45
|
+
return medical_recipe_visit
|
|
46
|
+
|
|
47
|
+
def list_all_command(self, active_only: bool = True, load_relations: bool = False)->List[MedicalRecipeVisit]:
|
|
48
|
+
with get_session(self.tenant) as session:
|
|
49
|
+
statement = select(MedicalRecipeVisit)
|
|
50
|
+
|
|
51
|
+
if load_relations:
|
|
52
|
+
|
|
53
|
+
statement = select(MedicalRecipeVisit).options(
|
|
54
|
+
selectinload(MedicalRecipeVisit.medical_visit),
|
|
55
|
+
selectinload(MedicalRecipeVisit.medical_drugs)
|
|
56
|
+
)
|
|
57
|
+
if active_only:
|
|
58
|
+
statement = statement.where(MedicalRecipeVisit.is_active == True)
|
|
59
|
+
medical_recipe_visit = session.exec(statement).all()
|
|
60
|
+
|
|
61
|
+
return medical_recipe_visit
|
|
62
|
+
|
|
63
|
+
statement = select(MedicalRecipeVisit)
|
|
64
|
+
return session.exec(statement).all()
|
|
65
|
+
|
|
66
|
+
def update_command(self, medical_recipe_visit: MedicalRecipeVisit) -> MedicalRecipeVisit:
|
|
67
|
+
with get_session(self.tenant) as session:
|
|
68
|
+
existing_medical_recipe_visit = session.get(MedicalRecipeVisit, medical_recipe_visit.medical_recipe_visit_id)
|
|
69
|
+
if not existing_medical_recipe_visit:
|
|
70
|
+
raise ValueError(f"medical_recipe_visit with id {medical_recipe_visit.medical_recipe_visit_id} does not exist")
|
|
71
|
+
|
|
72
|
+
for key, value in medical_recipe_visit.dict(exclude_unset=True).items():
|
|
73
|
+
setattr(existing_medical_recipe_visit, key, value)
|
|
74
|
+
|
|
75
|
+
bd_medical_recipe_visit = session.merge(existing_medical_recipe_visit)
|
|
76
|
+
session.commit()
|
|
77
|
+
session.refresh(bd_medical_recipe_visit)
|
|
78
|
+
return bd_medical_recipe_visit
|
|
79
|
+
|
|
80
|
+
def delete_command(self, medical_recipe_visit_id: UUID, soft_delete: bool = False)->None:
|
|
81
|
+
with get_session(self.tenant) as session:
|
|
82
|
+
existing_medical_recipe_visit = session.get(MedicalRecipeVisit, medical_recipe_visit_id)
|
|
83
|
+
if not existing_medical_recipe_visit:
|
|
84
|
+
raise ValueError(f"MedicalRecipeVisit with id {medical_recipe_visit_id} does not exist")
|
|
85
|
+
|
|
86
|
+
if soft_delete:
|
|
87
|
+
existing_medical_recipe_visit.is_active = False
|
|
88
|
+
session.add(existing_medical_recipe_visit)
|
|
89
|
+
else:
|
|
90
|
+
session.delete(existing_medical_recipe_visit)
|
|
91
|
+
|
|
92
|
+
session.commit()
|
|
93
|
+
|
|
@@ -0,0 +1,509 @@
|
|
|
1
|
+
from typing import Optional, List
|
|
2
|
+
from datetime import datetime, date, time
|
|
3
|
+
from uuid import UUID
|
|
4
|
+
from sqlmodel import select, text
|
|
5
|
+
from sqlalchemy.orm import selectinload,joinedload
|
|
6
|
+
from healthdatalayer.models import MedicalVisit,MedicalDiagnosisVisit
|
|
7
|
+
from healthdatalayer.dtos import MedicalCertificateDTO, DiagnosisDTO, MedicalDiagnosesDTO
|
|
8
|
+
from healthdatalayer.config.db import engines, get_session
|
|
9
|
+
|
|
10
|
+
class MedicalVisitRepository:
|
|
11
|
+
def __init__(self, tenant: str):
|
|
12
|
+
self.tenant = tenant
|
|
13
|
+
if tenant not in engines:
|
|
14
|
+
raise ValueError(f"Tenant {tenant} is not configured")
|
|
15
|
+
|
|
16
|
+
def create_command(self, medical_visit: MedicalVisit) -> MedicalVisit:
|
|
17
|
+
with get_session(self.tenant) as session:
|
|
18
|
+
session.add(medical_visit)
|
|
19
|
+
session.commit()
|
|
20
|
+
session.refresh(medical_visit)
|
|
21
|
+
return medical_visit
|
|
22
|
+
|
|
23
|
+
def get_by_id_command(self, medical_visit_id: UUID, load_relations: bool = False) -> Optional[MedicalVisit]:
|
|
24
|
+
with get_session(self.tenant) as session:
|
|
25
|
+
|
|
26
|
+
if load_relations:
|
|
27
|
+
statement = select(MedicalVisit).where(MedicalVisit.medical_visit_id == medical_visit_id).options(
|
|
28
|
+
joinedload(MedicalVisit.client),
|
|
29
|
+
joinedload(MedicalVisit.collaborator),
|
|
30
|
+
joinedload(MedicalVisit.speciality),
|
|
31
|
+
selectinload(MedicalVisit.medical_diagnosis_visits).selectinload(MedicalDiagnosisVisit.medical_diagnosis),
|
|
32
|
+
selectinload(MedicalVisit.medical_recipe_visits),
|
|
33
|
+
selectinload(MedicalVisit.organ_system_reviews),
|
|
34
|
+
selectinload(MedicalVisit.physical_exams)
|
|
35
|
+
)
|
|
36
|
+
medical_visit = session.exec(statement).first()
|
|
37
|
+
|
|
38
|
+
return medical_visit
|
|
39
|
+
else:
|
|
40
|
+
return session.get(MedicalVisit, medical_visit_id)
|
|
41
|
+
|
|
42
|
+
def list_all_command(self, active_only: bool = True, load_relations: bool = False)->List[MedicalVisit]:
|
|
43
|
+
with get_session(self.tenant) as session:
|
|
44
|
+
statement = select(MedicalVisit)
|
|
45
|
+
|
|
46
|
+
if load_relations:
|
|
47
|
+
|
|
48
|
+
statement = select(MedicalVisit).options(
|
|
49
|
+
joinedload(MedicalVisit.client),
|
|
50
|
+
joinedload(MedicalVisit.collaborator),
|
|
51
|
+
joinedload(MedicalVisit.speciality),
|
|
52
|
+
selectinload(MedicalVisit.medical_diagnosis_visits),
|
|
53
|
+
selectinload(MedicalVisit.medical_recipe_visits),
|
|
54
|
+
selectinload(MedicalVisit.organ_system_reviews),
|
|
55
|
+
selectinload(MedicalVisit.physical_exams)
|
|
56
|
+
)
|
|
57
|
+
if active_only:
|
|
58
|
+
statement = statement.where(MedicalVisit.is_active == True)
|
|
59
|
+
medical_visits = session.exec(statement).all()
|
|
60
|
+
|
|
61
|
+
return medical_visits
|
|
62
|
+
|
|
63
|
+
statement = select(MedicalVisit)
|
|
64
|
+
return session.exec(statement).all()
|
|
65
|
+
|
|
66
|
+
def get_by_client_id_command(self, client_id: UUID, active_only: bool = True, load_relations: bool = False) -> List[MedicalVisit]:
|
|
67
|
+
with get_session(self.tenant) as session:
|
|
68
|
+
statement = select(MedicalVisit).where(MedicalVisit.client_id == client_id)
|
|
69
|
+
if active_only:
|
|
70
|
+
statement = statement.where(MedicalVisit.is_active == True)
|
|
71
|
+
if load_relations:
|
|
72
|
+
statement = statement.options(
|
|
73
|
+
joinedload(MedicalVisit.client),
|
|
74
|
+
joinedload(MedicalVisit.collaborator),
|
|
75
|
+
joinedload(MedicalVisit.speciality),
|
|
76
|
+
selectinload(MedicalVisit.medical_diagnosis_visits).selectinload(MedicalDiagnosisVisit.medical_diagnosis),
|
|
77
|
+
selectinload(MedicalVisit.medical_recipe_visits),
|
|
78
|
+
selectinload(MedicalVisit.organ_system_reviews),
|
|
79
|
+
selectinload(MedicalVisit.physical_exams)
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
medical_visits = session.exec(statement).all()
|
|
83
|
+
return medical_visits
|
|
84
|
+
|
|
85
|
+
def get_by_collaborator_id_command(self, collaborator_id: UUID, active_only: bool = True, load_relations: bool = False) -> List[MedicalVisit]:
|
|
86
|
+
with get_session(self.tenant) as session:
|
|
87
|
+
statement = select(MedicalVisit).where(MedicalVisit.collaborator_id == collaborator_id)
|
|
88
|
+
|
|
89
|
+
if active_only:
|
|
90
|
+
statement = statement.where(MedicalVisit.is_active == True)
|
|
91
|
+
if load_relations:
|
|
92
|
+
statement = statement.options(
|
|
93
|
+
joinedload(MedicalVisit.client),
|
|
94
|
+
joinedload(MedicalVisit.collaborator),
|
|
95
|
+
joinedload(MedicalVisit.speciality),
|
|
96
|
+
selectinload(MedicalVisit.medical_diagnosis_visits).selectinload(MedicalDiagnosisVisit.medical_diagnosis),
|
|
97
|
+
selectinload(MedicalVisit.medical_recipe_visits),
|
|
98
|
+
selectinload(MedicalVisit.organ_system_reviews),
|
|
99
|
+
selectinload(MedicalVisit.physical_exams)
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
medical_visits = session.exec(statement).all()
|
|
103
|
+
return medical_visits
|
|
104
|
+
|
|
105
|
+
def get_by_next_followup_id_command(self, next_followup_id: UUID, active_only: bool = True, load_relations: bool = False) -> List[MedicalVisit]:
|
|
106
|
+
with get_session(self.tenant) as session:
|
|
107
|
+
statement = select(MedicalVisit).where(MedicalVisit.next_followup_visit_id == next_followup_id)
|
|
108
|
+
|
|
109
|
+
if active_only:
|
|
110
|
+
statement = statement.where(MedicalVisit.is_active == True)
|
|
111
|
+
if load_relations:
|
|
112
|
+
statement = statement.options(
|
|
113
|
+
joinedload(MedicalVisit.client),
|
|
114
|
+
joinedload(MedicalVisit.collaborator),
|
|
115
|
+
joinedload(MedicalVisit.speciality),
|
|
116
|
+
selectinload(MedicalVisit.medical_diagnosis_visits),
|
|
117
|
+
selectinload(MedicalVisit.medical_recipe_visits),
|
|
118
|
+
selectinload(MedicalVisit.organ_system_reviews),
|
|
119
|
+
selectinload(MedicalVisit.physical_exams)
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
medical_visits = session.exec(statement).all()
|
|
123
|
+
return medical_visits
|
|
124
|
+
|
|
125
|
+
def get_by_daterange_command(self, start_date: datetime, end_date: datetime, active_only: bool = True, load_relations: bool = False) -> List[MedicalVisit]:
|
|
126
|
+
with get_session(self.tenant) as session:
|
|
127
|
+
statement = select(MedicalVisit).where(
|
|
128
|
+
MedicalVisit.visit_date >= start_date
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
if end_date is not None:
|
|
132
|
+
statement = statement.where(MedicalVisit.visit_date <= end_date)
|
|
133
|
+
if active_only:
|
|
134
|
+
statement = statement.where(MedicalVisit.is_active == True)
|
|
135
|
+
if load_relations:
|
|
136
|
+
statement = statement.options(
|
|
137
|
+
joinedload(MedicalVisit.client),
|
|
138
|
+
joinedload(MedicalVisit.collaborator),
|
|
139
|
+
joinedload(MedicalVisit.speciality),
|
|
140
|
+
selectinload(MedicalVisit.medical_diagnosis_visits).selectinload(MedicalDiagnosisVisit.medical_diagnosis),
|
|
141
|
+
selectinload(MedicalVisit.medical_recipe_visits),
|
|
142
|
+
selectinload(MedicalVisit.organ_system_reviews),
|
|
143
|
+
selectinload(MedicalVisit.physical_exams)
|
|
144
|
+
)
|
|
145
|
+
|
|
146
|
+
medical_visits = session.exec(statement).all()
|
|
147
|
+
return medical_visits
|
|
148
|
+
|
|
149
|
+
def get_by_targetdate_command(self, target_date: datetime, active_only: bool = True, load_relations: bool = False) -> List[MedicalVisit]:
|
|
150
|
+
with get_session(self.tenant) as session:
|
|
151
|
+
|
|
152
|
+
if isinstance(target_date, str):
|
|
153
|
+
try:
|
|
154
|
+
target_date = datetime.strptime(target_date, "%Y-%m-%d")
|
|
155
|
+
except ValueError:
|
|
156
|
+
raise ValueError("Invalid date format")
|
|
157
|
+
|
|
158
|
+
elif isinstance(target_date, date) and not isinstance(target_date, datetime):
|
|
159
|
+
target_date = datetime.combine(target_date, time.min)
|
|
160
|
+
|
|
161
|
+
start_of_day = datetime.combine(target_date.date(), datetime.min.time())
|
|
162
|
+
end_of_day = datetime.combine(target_date.date(), datetime.max.time())
|
|
163
|
+
|
|
164
|
+
statement = select(MedicalVisit).where(
|
|
165
|
+
MedicalVisit.visit_date >= start_of_day,
|
|
166
|
+
MedicalVisit.visit_date <= end_of_day
|
|
167
|
+
)
|
|
168
|
+
|
|
169
|
+
if active_only:
|
|
170
|
+
statement = statement.where(MedicalVisit.is_active == True)
|
|
171
|
+
if load_relations:
|
|
172
|
+
statement = statement.options(
|
|
173
|
+
joinedload(MedicalVisit.client),
|
|
174
|
+
joinedload(MedicalVisit.collaborator),
|
|
175
|
+
joinedload(MedicalVisit.speciality),
|
|
176
|
+
selectinload(MedicalVisit.medical_diagnosis_visits).selectinload(MedicalDiagnosisVisit.medical_diagnosis),
|
|
177
|
+
selectinload(MedicalVisit.medical_recipe_visits),
|
|
178
|
+
selectinload(MedicalVisit.organ_system_reviews),
|
|
179
|
+
selectinload(MedicalVisit.physical_exams)
|
|
180
|
+
)
|
|
181
|
+
|
|
182
|
+
medical_visits = session.exec(statement).all()
|
|
183
|
+
return medical_visits
|
|
184
|
+
|
|
185
|
+
def get_by_collaboratorid_and_daterange_command(self, collaborator_id: UUID, start_date: datetime, end_date: datetime, active_only: bool = True, load_relations: bool = False) -> List[MedicalVisit]:
|
|
186
|
+
with get_session(self.tenant) as session:
|
|
187
|
+
statement = select(MedicalVisit).where(
|
|
188
|
+
MedicalVisit.collaborator_id == collaborator_id,
|
|
189
|
+
MedicalVisit.visit_date >= start_date
|
|
190
|
+
)
|
|
191
|
+
|
|
192
|
+
if end_date is not None:
|
|
193
|
+
statement = statement.where(MedicalVisit.visit_date <= end_date)
|
|
194
|
+
if active_only:
|
|
195
|
+
statement = statement.where(MedicalVisit.is_active == True)
|
|
196
|
+
if load_relations:
|
|
197
|
+
statement = statement.options(
|
|
198
|
+
joinedload(MedicalVisit.client),
|
|
199
|
+
joinedload(MedicalVisit.collaborator),
|
|
200
|
+
joinedload(MedicalVisit.speciality),
|
|
201
|
+
selectinload(MedicalVisit.medical_diagnosis_visits).selectinload(MedicalDiagnosisVisit.medical_diagnosis),
|
|
202
|
+
selectinload(MedicalVisit.medical_recipe_visits),
|
|
203
|
+
selectinload(MedicalVisit.organ_system_reviews),
|
|
204
|
+
selectinload(MedicalVisit.physical_exams)
|
|
205
|
+
)
|
|
206
|
+
|
|
207
|
+
medical_visits = session.exec(statement).all()
|
|
208
|
+
return medical_visits
|
|
209
|
+
|
|
210
|
+
def get_by_collaboratorid_and_targetdate_command(self, collaborator_id: UUID, target_date: datetime, active_only: bool = True, load_relations: bool = False) -> List[MedicalVisit]:
|
|
211
|
+
with get_session(self.tenant) as session:
|
|
212
|
+
|
|
213
|
+
if isinstance(target_date, str):
|
|
214
|
+
try:
|
|
215
|
+
target_date = datetime.strptime(target_date, "%Y-%m-%d")
|
|
216
|
+
except ValueError:
|
|
217
|
+
raise ValueError("Invalid date format")
|
|
218
|
+
|
|
219
|
+
elif isinstance(target_date, date) and not isinstance(target_date, datetime):
|
|
220
|
+
target_date = datetime.combine(target_date, time.min)
|
|
221
|
+
|
|
222
|
+
start_of_day = datetime.combine(target_date.date(), datetime.min.time())
|
|
223
|
+
end_of_day = datetime.combine(target_date.date(), datetime.max.time())
|
|
224
|
+
|
|
225
|
+
statement = select(MedicalVisit).where(
|
|
226
|
+
MedicalVisit.collaborator_id == collaborator_id,
|
|
227
|
+
MedicalVisit.visit_date >= start_of_day,
|
|
228
|
+
MedicalVisit.visit_date <= end_of_day
|
|
229
|
+
)
|
|
230
|
+
|
|
231
|
+
if active_only:
|
|
232
|
+
statement = statement.where(MedicalVisit.is_active == True)
|
|
233
|
+
if load_relations:
|
|
234
|
+
statement = statement.options(
|
|
235
|
+
joinedload(MedicalVisit.client),
|
|
236
|
+
joinedload(MedicalVisit.collaborator),
|
|
237
|
+
joinedload(MedicalVisit.speciality),
|
|
238
|
+
selectinload(MedicalVisit.medical_diagnosis_visits).selectinload(MedicalDiagnosisVisit.medical_diagnosis),
|
|
239
|
+
selectinload(MedicalVisit.medical_recipe_visits),
|
|
240
|
+
selectinload(MedicalVisit.organ_system_reviews),
|
|
241
|
+
selectinload(MedicalVisit.physical_exams)
|
|
242
|
+
)
|
|
243
|
+
|
|
244
|
+
medical_visits = session.exec(statement).all()
|
|
245
|
+
return medical_visits
|
|
246
|
+
|
|
247
|
+
def get_by_collaboratorid_and_specificdatetime_command(self, collaborator_id: UUID, specific_datetime: datetime, active_only: bool = True, load_relations: bool = False) -> Optional[MedicalVisit]:
|
|
248
|
+
with get_session(self.tenant) as session:
|
|
249
|
+
|
|
250
|
+
statement = select(MedicalVisit).where(
|
|
251
|
+
MedicalVisit.collaborator_id == collaborator_id,
|
|
252
|
+
MedicalVisit.visit_date == specific_datetime
|
|
253
|
+
)
|
|
254
|
+
|
|
255
|
+
if active_only:
|
|
256
|
+
statement = statement.where(MedicalVisit.is_active == True)
|
|
257
|
+
if load_relations:
|
|
258
|
+
statement = statement.options(
|
|
259
|
+
joinedload(MedicalVisit.client),
|
|
260
|
+
joinedload(MedicalVisit.collaborator),
|
|
261
|
+
joinedload(MedicalVisit.speciality),
|
|
262
|
+
selectinload(MedicalVisit.medical_diagnosis_visits).selectinload(MedicalDiagnosisVisit.medical_diagnosis),
|
|
263
|
+
selectinload(MedicalVisit.medical_recipe_visits),
|
|
264
|
+
selectinload(MedicalVisit.organ_system_reviews),
|
|
265
|
+
selectinload(MedicalVisit.physical_exams)
|
|
266
|
+
)
|
|
267
|
+
|
|
268
|
+
medical_visit = session.exec(statement).first()
|
|
269
|
+
return medical_visit
|
|
270
|
+
|
|
271
|
+
def get_first_by_clientid_command(self, client_id: UUID, active_only: bool = True, load_relations: bool = False) -> Optional[MedicalVisit]:
|
|
272
|
+
with get_session(self.tenant) as session:
|
|
273
|
+
|
|
274
|
+
statement = select(MedicalVisit).where(MedicalVisit.client_id == client_id).order_by(MedicalVisit.visit_date.asc())
|
|
275
|
+
|
|
276
|
+
if active_only:
|
|
277
|
+
statement = statement.where(MedicalVisit.is_active == True)
|
|
278
|
+
if load_relations:
|
|
279
|
+
statement = statement.options(
|
|
280
|
+
joinedload(MedicalVisit.client),
|
|
281
|
+
joinedload(MedicalVisit.collaborator),
|
|
282
|
+
joinedload(MedicalVisit.speciality),
|
|
283
|
+
selectinload(MedicalVisit.medical_diagnosis_visits).selectinload(MedicalDiagnosisVisit.medical_diagnosis),
|
|
284
|
+
selectinload(MedicalVisit.medical_recipe_visits),
|
|
285
|
+
selectinload(MedicalVisit.organ_system_reviews),
|
|
286
|
+
selectinload(MedicalVisit.physical_exams)
|
|
287
|
+
)
|
|
288
|
+
|
|
289
|
+
medical_visit = session.exec(statement).first()
|
|
290
|
+
return medical_visit
|
|
291
|
+
|
|
292
|
+
def get_last_by_clientid_command(self, client_id: UUID, active_only: bool = True, load_relations: bool = False) -> Optional[MedicalVisit]:
|
|
293
|
+
with get_session(self.tenant) as session:
|
|
294
|
+
|
|
295
|
+
statement = select(MedicalVisit).where(MedicalVisit.client_id == client_id).order_by(MedicalVisit.visit_date.desc())
|
|
296
|
+
|
|
297
|
+
if active_only:
|
|
298
|
+
statement = statement.where(MedicalVisit.is_active == True)
|
|
299
|
+
if load_relations:
|
|
300
|
+
statement = statement.options(
|
|
301
|
+
joinedload(MedicalVisit.client),
|
|
302
|
+
joinedload(MedicalVisit.collaborator),
|
|
303
|
+
joinedload(MedicalVisit.speciality),
|
|
304
|
+
selectinload(MedicalVisit.medical_diagnosis_visits).selectinload(MedicalDiagnosisVisit.medical_diagnosis),
|
|
305
|
+
selectinload(MedicalVisit.medical_recipe_visits),
|
|
306
|
+
selectinload(MedicalVisit.organ_system_reviews),
|
|
307
|
+
selectinload(MedicalVisit.physical_exams)
|
|
308
|
+
)
|
|
309
|
+
|
|
310
|
+
medical_visit = session.exec(statement).first()
|
|
311
|
+
return medical_visit
|
|
312
|
+
|
|
313
|
+
def update_command(self, medical_visit: MedicalVisit) -> MedicalVisit:
|
|
314
|
+
with get_session(self.tenant) as session:
|
|
315
|
+
existing_medical_visit = session.get(MedicalVisit, medical_visit.medical_visit_id)
|
|
316
|
+
if not existing_medical_visit:
|
|
317
|
+
raise ValueError(f"medical_visit with id {medical_visit.medical_visit_id} does not exist")
|
|
318
|
+
|
|
319
|
+
for key, value in medical_visit.dict(exclude_unset=True).items():
|
|
320
|
+
setattr(existing_medical_visit, key, value)
|
|
321
|
+
|
|
322
|
+
bd_medical_visit = session.merge(existing_medical_visit)
|
|
323
|
+
session.commit()
|
|
324
|
+
session.refresh(bd_medical_visit)
|
|
325
|
+
return bd_medical_visit
|
|
326
|
+
|
|
327
|
+
def delete_command(self, medical_visit_id: UUID, soft_delete: bool = False)->None:
|
|
328
|
+
with get_session(self.tenant) as session:
|
|
329
|
+
existing_bridge = session.get(MedicalVisit, medical_visit_id)
|
|
330
|
+
if not existing_bridge:
|
|
331
|
+
raise ValueError(f"MedicalVisit with id {medical_visit_id} does not exist")
|
|
332
|
+
|
|
333
|
+
if soft_delete:
|
|
334
|
+
existing_bridge.is_active = False
|
|
335
|
+
session.add(existing_bridge)
|
|
336
|
+
else:
|
|
337
|
+
session.delete(existing_bridge)
|
|
338
|
+
|
|
339
|
+
session.commit()
|
|
340
|
+
|
|
341
|
+
def exists_by_collaboratoid_and_targetdate_command(self, collaborator_id: UUID, target_date: datetime) -> bool:
|
|
342
|
+
with get_session(self.tenant) as session:
|
|
343
|
+
statement = select(MedicalVisit).where(MedicalVisit.collaborator_id == collaborator_id, MedicalVisit.visit_date == target_date)
|
|
344
|
+
result = session.exec(statement).first()
|
|
345
|
+
return result is not None
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
def get_data_medical_certificate_command(self, medical_visit_id: str) -> MedicalCertificateDTO:
|
|
349
|
+
|
|
350
|
+
with get_session(self.tenant) as session:
|
|
351
|
+
|
|
352
|
+
query = text("""
|
|
353
|
+
select
|
|
354
|
+
s."name" as "sys",
|
|
355
|
+
b2."name" as stablishment,
|
|
356
|
+
px.medical_record_number,
|
|
357
|
+
px.identification as number_his,
|
|
358
|
+
px.last_name ,
|
|
359
|
+
px.first_name,
|
|
360
|
+
case
|
|
361
|
+
when g.name ='Male' then 'M'
|
|
362
|
+
else 'F'
|
|
363
|
+
end as sex,
|
|
364
|
+
EXTRACT(YEAR FROM AGE(px.birth_date)) AS age,
|
|
365
|
+
sp.name as service,
|
|
366
|
+
sp.subspeciality as speciality,
|
|
367
|
+
EXTRACT(YEAR FROM mv.visit_date) AS year_visit,
|
|
368
|
+
EXTRACT(MONTH FROM mv.visit_date) AS month_visit,
|
|
369
|
+
EXTRACT(DAY FROM mv.visit_date) AS day_visit,
|
|
370
|
+
mv.visit_date::time AS hour_start,
|
|
371
|
+
(mv.visit_date + INTERVAL '30 minutes')::time AS hour_end,
|
|
372
|
+
TO_CHAR(mv.visit_date, 'DD') || ' de ' ||
|
|
373
|
+
CASE EXTRACT(MONTH FROM mv.visit_date)
|
|
374
|
+
WHEN 1 THEN 'enero'
|
|
375
|
+
WHEN 2 THEN 'febrero'
|
|
376
|
+
WHEN 3 THEN 'marzo'
|
|
377
|
+
WHEN 4 THEN 'abril'
|
|
378
|
+
WHEN 5 THEN 'mayo'
|
|
379
|
+
WHEN 6 THEN 'junio'
|
|
380
|
+
WHEN 7 THEN 'julio'
|
|
381
|
+
WHEN 8 THEN 'agosto'
|
|
382
|
+
WHEN 9 THEN 'septiembre'
|
|
383
|
+
WHEN 10 THEN 'octubre'
|
|
384
|
+
WHEN 11 THEN 'noviembre'
|
|
385
|
+
WHEN 12 THEN 'diciembre'
|
|
386
|
+
END || ' del ' || EXTRACT(YEAR FROM mv.visit_date) AS visit_date_spanish,
|
|
387
|
+
mv.rest,
|
|
388
|
+
mv.rest_hours,
|
|
389
|
+
mv.rest_date_start ,
|
|
390
|
+
EXTRACT(YEAR FROM mv.rest_date_start) AS year_rest_start,
|
|
391
|
+
EXTRACT(MONTH FROM mv.rest_date_start) AS month_rest_start,
|
|
392
|
+
EXTRACT(DAY FROM mv.rest_date_start) AS day_rest_start,
|
|
393
|
+
TO_CHAR(mv.rest_date_start, 'DD') || ' de ' ||
|
|
394
|
+
CASE EXTRACT(MONTH FROM mv.rest_date_start)
|
|
395
|
+
WHEN 1 THEN 'enero'
|
|
396
|
+
WHEN 2 THEN 'febrero'
|
|
397
|
+
WHEN 3 THEN 'marzo'
|
|
398
|
+
WHEN 4 THEN 'abril'
|
|
399
|
+
WHEN 5 THEN 'mayo'
|
|
400
|
+
WHEN 6 THEN 'junio'
|
|
401
|
+
WHEN 7 THEN 'julio'
|
|
402
|
+
WHEN 8 THEN 'agosto'
|
|
403
|
+
WHEN 9 THEN 'septiembre'
|
|
404
|
+
WHEN 10 THEN 'octubre'
|
|
405
|
+
WHEN 11 THEN 'noviembre'
|
|
406
|
+
WHEN 12 THEN 'diciembre'
|
|
407
|
+
END || ' del ' || EXTRACT(YEAR FROM mv.rest_date_start) AS rest_date_start_spanish,
|
|
408
|
+
mv.rest_date_end,
|
|
409
|
+
EXTRACT(YEAR FROM mv.rest_date_end) AS year_rest_end,
|
|
410
|
+
EXTRACT(MONTH FROM mv.rest_date_end) AS month_rest_end,
|
|
411
|
+
EXTRACT(DAY FROM mv.rest_date_end) AS day_rest_end,
|
|
412
|
+
TO_CHAR( mv.rest_date_end, 'DD') || ' de ' ||
|
|
413
|
+
CASE EXTRACT(MONTH FROM mv.rest_date_end)
|
|
414
|
+
WHEN 1 THEN 'enero'
|
|
415
|
+
WHEN 2 THEN 'febrero'
|
|
416
|
+
WHEN 3 THEN 'marzo'
|
|
417
|
+
WHEN 4 THEN 'abril'
|
|
418
|
+
WHEN 5 THEN 'mayo'
|
|
419
|
+
WHEN 6 THEN 'junio'
|
|
420
|
+
WHEN 7 THEN 'julio'
|
|
421
|
+
WHEN 8 THEN 'agosto'
|
|
422
|
+
WHEN 9 THEN 'septiembre'
|
|
423
|
+
WHEN 10 THEN 'octubre'
|
|
424
|
+
WHEN 11 THEN 'noviembre'
|
|
425
|
+
WHEN 12 THEN 'diciembre'
|
|
426
|
+
END || ' del ' || EXTRACT(YEAR FROM mv.rest_date_end) AS rest_date_end_spanish,
|
|
427
|
+
c.name as doctor_name,
|
|
428
|
+
c.ruc as doctor_ruc
|
|
429
|
+
from medical_visit mv
|
|
430
|
+
left join px on px.client_id = mv.client_id
|
|
431
|
+
left join gender g on g.gender_id = px.gender_id
|
|
432
|
+
left join collaborator c on c.collaborator_id = mv.collaborator_id
|
|
433
|
+
left join bridge_area_floor_branch b on b.bridge_area_floor_branch_id = mv.bridge_area_floor_branch_id
|
|
434
|
+
left join branch b2 on b.branch_id = b2.branch_id
|
|
435
|
+
left join "system" s on s.system_id = b2.system_id
|
|
436
|
+
left join speciality sp on sp.speciality_id = mv.speciality_id
|
|
437
|
+
where mv.medical_visit_id = :medical_visit_id
|
|
438
|
+
and mv.status_visit != 'CANCELADO'
|
|
439
|
+
""")
|
|
440
|
+
|
|
441
|
+
result = session.exec(query, params={"medical_visit_id": medical_visit_id})
|
|
442
|
+
|
|
443
|
+
row = result.fetchone()
|
|
444
|
+
|
|
445
|
+
|
|
446
|
+
if not row:
|
|
447
|
+
raise ValueError(f"No se encontró la visita médica con ID: {medical_visit_id}")
|
|
448
|
+
|
|
449
|
+
|
|
450
|
+
return MedicalCertificateDTO(
|
|
451
|
+
sys=row[0],
|
|
452
|
+
stablishment=row[1],
|
|
453
|
+
medical_record_number=row[2],
|
|
454
|
+
number_his=row[3],
|
|
455
|
+
last_name=row[4],
|
|
456
|
+
first_name=row[5],
|
|
457
|
+
sex=row[6],
|
|
458
|
+
age=row[7],
|
|
459
|
+
service=row[8],
|
|
460
|
+
speciality=row[9],
|
|
461
|
+
year_visit=row[10],
|
|
462
|
+
month_visit=row[11],
|
|
463
|
+
day_visit=row[12],
|
|
464
|
+
hour_start=row[13],
|
|
465
|
+
hour_end=row[14],
|
|
466
|
+
visit_date_spanish=row[15],
|
|
467
|
+
rest=row[16],
|
|
468
|
+
rest_hours=row[17],
|
|
469
|
+
rest_date_start=row[18],
|
|
470
|
+
year_rest_start=row[19],
|
|
471
|
+
month_rest_start=row[20],
|
|
472
|
+
day_rest_start=row[21],
|
|
473
|
+
rest_date_start_spanish=row[22],
|
|
474
|
+
rest_date_end=row[23],
|
|
475
|
+
year_rest_end=row[24],
|
|
476
|
+
month_rest_end=row[25],
|
|
477
|
+
day_rest_end=row[26],
|
|
478
|
+
rest_date_end_spanish=row[27],
|
|
479
|
+
doctor_name=row[28],
|
|
480
|
+
doctor_ruc=row[29]
|
|
481
|
+
)
|
|
482
|
+
|
|
483
|
+
def get_medical_diagnoses_command(self, medical_visit_id: str) -> MedicalDiagnosesDTO:
|
|
484
|
+
|
|
485
|
+
with get_session(self.tenant) as session:
|
|
486
|
+
|
|
487
|
+
query = text("""
|
|
488
|
+
select md."name" as name_diagnosis,
|
|
489
|
+
md.cie_10_code
|
|
490
|
+
from medical_diagnosis md
|
|
491
|
+
join medical_diagnosis_visit mdv on mdv.medical_diagnosis_id = md.medical_diagnosis_id
|
|
492
|
+
where md.is_active = true
|
|
493
|
+
and mdv.is_active = true
|
|
494
|
+
and mdv.medical_visit_id = :medical_visit_id
|
|
495
|
+
""")
|
|
496
|
+
|
|
497
|
+
result = session.exec(query, params={"medical_visit_id": medical_visit_id})
|
|
498
|
+
rows = result.fetchall()
|
|
499
|
+
|
|
500
|
+
# Convertir cada fila a DiagnosisDTO
|
|
501
|
+
diagnoses_list = []
|
|
502
|
+
for row in rows:
|
|
503
|
+
diagnosis = DiagnosisDTO(
|
|
504
|
+
name_diagnosis=row[0],
|
|
505
|
+
cie_10_code=row[1]
|
|
506
|
+
)
|
|
507
|
+
diagnoses_list.append(diagnosis)
|
|
508
|
+
|
|
509
|
+
return MedicalDiagnosesDTO(diagnoses=diagnoses_list)
|