healthdatalayer 1.0.1__py3-none-any.whl → 1.2.0__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.

Potentially problematic release.


This version of healthdatalayer might be problematic. Click here for more details.

Files changed (96) hide show
  1. healthdatalayer/config/__init__.py +0 -0
  2. healthdatalayer/models/bridge_area_floor_branch/__init__.py +0 -0
  3. healthdatalayer/models/bridge_area_floor_branch/area.py +8 -0
  4. healthdatalayer/models/bridge_area_floor_branch/branch.py +16 -0
  5. healthdatalayer/models/bridge_area_floor_branch/bridge_area_floor_branch.py +27 -0
  6. healthdatalayer/models/bridge_area_floor_branch/floor.py +8 -0
  7. healthdatalayer/models/bridge_area_floor_branch/room.py +8 -0
  8. healthdatalayer/models/bridge_area_floor_branch/system.py +8 -0
  9. healthdatalayer/models/client/__init__.py +0 -0
  10. healthdatalayer/models/client/address.py +13 -0
  11. healthdatalayer/models/client/client.py +25 -0
  12. healthdatalayer/models/client/client_type.py +9 -0
  13. healthdatalayer/models/client/education.py +9 -0
  14. healthdatalayer/models/client/emergency_contact.py +17 -0
  15. healthdatalayer/models/client/gender.py +9 -0
  16. healthdatalayer/models/client/marriage_status.py +9 -0
  17. healthdatalayer/models/client/nationality.py +10 -0
  18. healthdatalayer/models/client/pathological_history.py +29 -0
  19. healthdatalayer/models/client/pet.py +16 -0
  20. healthdatalayer/models/client/profession.py +9 -0
  21. healthdatalayer/models/client/px.py +28 -0
  22. healthdatalayer/models/collaborator/__init__.py +0 -0
  23. healthdatalayer/models/collaborator/collaborator.py +33 -0
  24. healthdatalayer/models/collaborator/collaborator_speciality.py +8 -0
  25. healthdatalayer/models/collaborator/collaborator_type.py +9 -0
  26. healthdatalayer/models/collaborator/speciality.py +23 -0
  27. healthdatalayer/models/lab/__init__.py +0 -0
  28. healthdatalayer/models/lab/client_lab.py +13 -0
  29. healthdatalayer/models/lab/measure_lab.py +11 -0
  30. healthdatalayer/models/lab/medical_lab.py +17 -0
  31. healthdatalayer/models/medical_visit/__init__.py +0 -0
  32. healthdatalayer/models/medical_visit/medical_diagnosis.py +12 -0
  33. healthdatalayer/models/medical_visit/medical_diagnosis_visit.py +25 -0
  34. healthdatalayer/models/medical_visit/medical_drug.py +27 -0
  35. healthdatalayer/models/medical_visit/medical_drug_recipe.py +18 -0
  36. healthdatalayer/models/medical_visit/medical_recipe_visit.py +28 -0
  37. healthdatalayer/models/medical_visit/medical_visit.py +51 -0
  38. healthdatalayer/models/medical_visit/organ_system_review.py +28 -0
  39. healthdatalayer/models/medical_visit/physical_exam.py +45 -0
  40. healthdatalayer/models/medical_visit/visit_triage.py +28 -0
  41. healthdatalayer/models/user/__init__.py +0 -0
  42. healthdatalayer/models/user/permission.py +27 -0
  43. healthdatalayer/models/user/permission_user.py +8 -0
  44. healthdatalayer/models/user/role.py +27 -0
  45. healthdatalayer/models/user/role_permission.py +8 -0
  46. healthdatalayer/models/user/role_user.py +8 -0
  47. healthdatalayer/models/user/user.py +30 -0
  48. healthdatalayer/repositories/client_repositories/__init__.py +0 -0
  49. healthdatalayer/repositories/client_repositories/address_repository.py +64 -0
  50. healthdatalayer/repositories/client_repositories/client_type_repository.py +69 -0
  51. healthdatalayer/repositories/client_repositories/education_repository.py +70 -0
  52. healthdatalayer/repositories/client_repositories/gender_repository.py +70 -0
  53. healthdatalayer/repositories/client_repositories/marriage_status_repository.py +70 -0
  54. healthdatalayer/repositories/client_repositories/pet_repository.py +126 -0
  55. healthdatalayer/repositories/client_repositories/profession_repository.py +70 -0
  56. healthdatalayer/repositories/client_repositories/px_repository.py +210 -0
  57. healthdatalayer/repositories/collaborator_repositories/__init__.py +0 -0
  58. healthdatalayer/repositories/collaborator_repositories/collaborator_repository.py +150 -0
  59. healthdatalayer/repositories/collaborator_repositories/collaborator_type_repository.py +69 -0
  60. healthdatalayer/repositories/collaborator_repositories/speciality_repository.py +75 -0
  61. healthdatalayer/repositories/infraestructure_repositories/__init__.py +0 -0
  62. healthdatalayer/repositories/infraestructure_repositories/area_repository.py +69 -0
  63. healthdatalayer/repositories/infraestructure_repositories/branch_repository.py +69 -0
  64. healthdatalayer/repositories/infraestructure_repositories/bridge_repository.py +80 -0
  65. healthdatalayer/repositories/infraestructure_repositories/floor_repository.py +69 -0
  66. healthdatalayer/repositories/infraestructure_repositories/room_repository.py +69 -0
  67. healthdatalayer/repositories/infraestructure_repositories/system_repository.py +69 -0
  68. healthdatalayer/repositories/lab_repositories/__init__.py +0 -0
  69. healthdatalayer/repositories/lab_repositories/measure_lab_repository.py +80 -0
  70. healthdatalayer/repositories/lab_repositories/medical_lab_repository.py +254 -0
  71. healthdatalayer/repositories/medical_visit_repositories/__init__.py +0 -0
  72. healthdatalayer/repositories/medical_visit_repositories/medical_diagnosis_repository.py +63 -0
  73. healthdatalayer/repositories/medical_visit_repositories/medical_diagnosis_visit_repository.py +92 -0
  74. healthdatalayer/repositories/medical_visit_repositories/medical_drug_recipe_repository.py +70 -0
  75. healthdatalayer/repositories/medical_visit_repositories/medical_drug_repository.py +63 -0
  76. healthdatalayer/repositories/medical_visit_repositories/medical_recipe_visit_repository.py +93 -0
  77. healthdatalayer/repositories/medical_visit_repositories/medical_visit_repository.py +110 -0
  78. healthdatalayer/repositories/medical_visit_repositories/organ_system_review_repository.py +89 -0
  79. healthdatalayer/repositories/medical_visit_repositories/physical_exam_repository.py +89 -0
  80. healthdatalayer/repositories/medical_visit_repositories/visit_triage_repository.py +89 -0
  81. healthdatalayer/repositories/user_repositories/__init__.py +0 -0
  82. healthdatalayer/repositories/user_repositories/permission_repository.py +238 -0
  83. healthdatalayer/repositories/user_repositories/role_repository.py +174 -0
  84. healthdatalayer/repositories/user_repositories/user_repository.py +251 -0
  85. {healthdatalayer-1.0.1.dist-info → healthdatalayer-1.2.0.dist-info}/METADATA +2 -2
  86. healthdatalayer-1.2.0.dist-info/RECORD +94 -0
  87. healthdatalayer-1.2.0.dist-info/top_level.txt +1 -0
  88. healthdatalayer-1.0.1.dist-info/RECORD +0 -10
  89. healthdatalayer-1.0.1.dist-info/top_level.txt +0 -3
  90. {config → healthdatalayer}/__init__.py +0 -0
  91. {config → healthdatalayer/config}/config.py +0 -0
  92. {config → healthdatalayer/config}/db.py +0 -0
  93. {config → healthdatalayer/config}/vault.py +0 -0
  94. {models → healthdatalayer/models}/__init__.py +0 -0
  95. {repositories → healthdatalayer/repositories}/__init__.py +0 -0
  96. {healthdatalayer-1.0.1.dist-info → healthdatalayer-1.2.0.dist-info}/WHEEL +0 -0
@@ -0,0 +1,69 @@
1
+ from typing import Optional, List
2
+ from uuid import UUID
3
+ from sqlmodel import select
4
+
5
+ from models import Branch
6
+ from config.db import engines, get_session
7
+
8
+ class BranchRepository:
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, branch: Branch) -> Branch:
15
+ with get_session(self.tenant) as session:
16
+ session.add(branch)
17
+ session.commit()
18
+ session.refresh(branch)
19
+ return branch
20
+
21
+ def get_by_id_command(self, branch_id: UUID) -> Optional[Branch]:
22
+ with get_session(self.tenant) as session:
23
+ return session.get(Branch, branch_id)
24
+
25
+ def get_by_name_command(self, name: str) -> Optional[Branch]:
26
+ with get_session(self.tenant) as session:
27
+ statement = select(Branch).where(Branch.name == name)
28
+ result = session.exec(statement).first()
29
+ return result
30
+
31
+ def list_all_command(self, active_only: bool = True) -> List[Branch]:
32
+ with get_session(self.tenant) as session:
33
+ statement = select(Branch)
34
+
35
+ if active_only:
36
+ statement = statement.where(Branch.is_active == True)
37
+
38
+ results = session.exec(statement)
39
+ return results.all()
40
+
41
+ def update_command(self, branch_id: UUID, **kwargs) -> Optional[Branch]:
42
+ with get_session(self.tenant) as session:
43
+ db_branch = session.get(Branch, branch_id)
44
+ if not db_branch:
45
+ return None
46
+
47
+ for key, value in kwargs.items():
48
+ if hasattr(db_branch, key):
49
+ setattr(db_branch, key, value)
50
+
51
+ session.add(db_branch)
52
+ session.commit()
53
+ session.refresh(db_branch)
54
+ return db_branch
55
+
56
+ def delete_command(self, branch_id: UUID, soft_delete: bool = True) -> bool:
57
+ with get_session(self.tenant) as session:
58
+ db_branch = session.get(Branch, branch_id)
59
+ if not db_branch:
60
+ return False
61
+
62
+ if soft_delete:
63
+ db_branch.is_active = False
64
+ session.add(db_branch)
65
+ else:
66
+ session.delete(db_branch)
67
+
68
+ session.commit()
69
+ return True
@@ -0,0 +1,80 @@
1
+ from typing import Optional, List
2
+ from uuid import UUID
3
+ from sqlmodel import select
4
+ from sqlalchemy.orm import selectinload,joinedload
5
+
6
+ from models import BridgeAreaFloorBranch
7
+ from models import Branch
8
+ from config.db import engines, get_session
9
+
10
+ class BridgeAreaFloorBranchRepository:
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, bridge: BridgeAreaFloorBranch) -> BridgeAreaFloorBranch:
17
+ with get_session(self.tenant) as session:
18
+ session.add(bridge)
19
+ session.commit()
20
+ session.refresh(bridge)
21
+ return bridge
22
+
23
+ def get_by_id_command(self, bridge_id: UUID, load_related: bool = False) -> Optional[BridgeAreaFloorBranch]:
24
+ with get_session(self.tenant) as session:
25
+ statement = select(BridgeAreaFloorBranch).where(BridgeAreaFloorBranch.bridge_area_floor_branch_id == bridge_id)
26
+ if load_related:
27
+ statement = statement.options(
28
+ selectinload(BridgeAreaFloorBranch.branch).selectinload(Branch.system),
29
+ selectinload(BridgeAreaFloorBranch.area),
30
+ selectinload(BridgeAreaFloorBranch.floor)
31
+ )
32
+ bridge = session.exec(statement).first()
33
+
34
+
35
+ return bridge
36
+
37
+ def get_all_command(self, active_only: bool = True,load_related: bool = False) -> List[BridgeAreaFloorBranch]:
38
+ with get_session(self.tenant) as session:
39
+ statement = select(BridgeAreaFloorBranch)
40
+ if active_only:
41
+ statement = statement.where(BridgeAreaFloorBranch.is_active == True)
42
+ #results = session.exec(statement).all()
43
+
44
+ if load_related:
45
+ statement = statement.options(
46
+ selectinload(BridgeAreaFloorBranch.branch).selectinload(Branch.system),
47
+ selectinload(BridgeAreaFloorBranch.area),
48
+ selectinload(BridgeAreaFloorBranch.floor)
49
+ )
50
+
51
+ results = session.exec(statement).all()
52
+ return results
53
+
54
+ def update_command(self, bridge: BridgeAreaFloorBranch) -> BridgeAreaFloorBranch:
55
+ with get_session(self.tenant) as session:
56
+ existing_bridge = session.get(BridgeAreaFloorBranch, bridge.bridge_area_floor_branch_id)
57
+ if not existing_bridge:
58
+ raise ValueError(f"BridgeAreaFloorBranch with id {bridge.bridge_area_floor_branch_id} does not exist")
59
+
60
+ for key, value in bridge.dict(exclude_unset=True).items():
61
+ setattr(existing_bridge, key, value)
62
+
63
+ bd_bridge = session.merge(existing_bridge)
64
+ session.commit()
65
+ session.refresh(bd_bridge)
66
+ return bd_bridge
67
+
68
+ def delete_command(self, bridge_id: UUID, soft_delete: bool = False) -> None:
69
+ with get_session(self.tenant) as session:
70
+ existing_bridge = session.get(BridgeAreaFloorBranch, bridge_id)
71
+ if not existing_bridge:
72
+ raise ValueError(f"BridgeAreaFloorBranch with id {bridge_id} does not exist")
73
+
74
+ if soft_delete:
75
+ existing_bridge.is_active = False
76
+ session.add(existing_bridge)
77
+ else:
78
+ session.delete(existing_bridge)
79
+
80
+ session.commit()
@@ -0,0 +1,69 @@
1
+ from typing import Optional, List
2
+ from uuid import UUID
3
+ from sqlmodel import select
4
+
5
+ from models import Floor
6
+ from config.db import engines, get_session
7
+
8
+ class FloorRepository:
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, floor: Floor) -> Floor:
15
+ with get_session(self.tenant) as session:
16
+ session.add(floor)
17
+ session.commit()
18
+ session.refresh(floor)
19
+ return floor
20
+
21
+ def get_by_id_command(self, floor_id: UUID) -> Optional[Floor]:
22
+ with get_session(self.tenant) as session:
23
+ return session.get(Floor, floor_id)
24
+
25
+ def get_by_name_command(self, name: str) -> Optional[Floor]:
26
+ with get_session(self.tenant) as session:
27
+ statement = select(Floor).where(Floor.name == name)
28
+ result = session.exec(statement).first()
29
+ return result
30
+
31
+ def list_all_command(self, active_only: bool = True) -> List[Floor]:
32
+ with get_session(self.tenant) as session:
33
+ statement = select(Floor)
34
+
35
+ if active_only:
36
+ statement = statement.where(Floor.is_active == True)
37
+
38
+ results = session.exec(statement)
39
+ return results.all()
40
+
41
+ def update_command(self, floor_id: UUID, **kwargs) -> Optional[Floor]:
42
+ with get_session(self.tenant) as session:
43
+ db_floor = session.get(Floor, floor_id)
44
+ if not db_floor:
45
+ return None
46
+
47
+ for key, value in kwargs.items():
48
+ if hasattr(db_floor, key):
49
+ setattr(db_floor, key, value)
50
+
51
+ session.add(db_floor)
52
+ session.commit()
53
+ session.refresh(db_floor)
54
+ return db_floor
55
+
56
+ def delete_command(self, floor_id: UUID, soft_delete: bool = True) -> bool:
57
+ with get_session(self.tenant) as session:
58
+ db_floor = session.get(Floor, floor_id)
59
+ if not db_floor:
60
+ return False
61
+
62
+ if soft_delete:
63
+ db_floor.is_active = False
64
+ session.add(db_floor)
65
+ else:
66
+ session.delete(db_floor)
67
+
68
+ session.commit()
69
+ return True
@@ -0,0 +1,69 @@
1
+ from typing import Optional, List
2
+ from uuid import UUID
3
+ from sqlmodel import select
4
+
5
+ from models import Room
6
+ from config.db import engines, get_session
7
+
8
+ class RoomRepository:
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, room: Room) -> Room:
15
+ with get_session(self.tenant) as session:
16
+ session.add(room)
17
+ session.commit()
18
+ session.refresh(room)
19
+ return room
20
+
21
+ def get_by_id_command(self, room_id: UUID) -> Optional[Room]:
22
+ with get_session(self.tenant) as session:
23
+ return session.get(Room, room_id)
24
+
25
+ def get_by_name_command(self, name: str) -> Optional[Room]:
26
+ with get_session(self.tenant) as session:
27
+ statement = select(Room).where(Room.name == name)
28
+ result = session.exec(statement).first()
29
+ return result
30
+
31
+ def list_all_command(self, active_only: bool = True) -> List[Room]:
32
+ with get_session(self.tenant) as session:
33
+ statement = select(Room)
34
+
35
+ if active_only:
36
+ statement = statement.where(Room.is_active == True)
37
+
38
+ results = session.exec(statement)
39
+ return results.all()
40
+
41
+ def update_command(self, room_id: UUID, **kwargs) -> Optional[Room]:
42
+ with get_session(self.tenant) as session:
43
+ db_room = session.get(Room, room_id)
44
+ if not db_room:
45
+ return None
46
+
47
+ for key, value in kwargs.items():
48
+ if hasattr(db_room, key):
49
+ setattr(db_room, key, value)
50
+
51
+ session.add(db_room)
52
+ session.commit()
53
+ session.refresh(db_room)
54
+ return db_room
55
+
56
+ def delete_command(self, room_id: UUID, soft_delete: bool = True) -> bool:
57
+ with get_session(self.tenant) as session:
58
+ db_room = session.get(Room, room_id)
59
+ if not db_room:
60
+ return False
61
+
62
+ if soft_delete:
63
+ db_room.is_active = False
64
+ session.add(db_room)
65
+ else:
66
+ session.delete(db_room)
67
+
68
+ session.commit()
69
+ return True
@@ -0,0 +1,69 @@
1
+ from typing import Optional, List
2
+ from uuid import UUID
3
+ from sqlmodel import select
4
+
5
+ from models import System
6
+ from config.db import engines, get_session
7
+
8
+ class SystemRepository:
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, system: System) -> System:
15
+ with get_session(self.tenant) as session:
16
+ session.add(system)
17
+ session.commit()
18
+ session.refresh(system)
19
+ return system
20
+
21
+ def get_by_id_command(self, system_id: UUID) -> Optional[System]:
22
+ with get_session(self.tenant) as session:
23
+ return session.get(System, system_id)
24
+
25
+ def get_by_name_command(self, name: str) -> Optional[System]:
26
+ with get_session(self.tenant) as session:
27
+ statement = select(System).where(System.name == name)
28
+ result = session.exec(statement).first()
29
+ return result
30
+
31
+ def list_all_command(self, active_only: bool = True) -> List[System]:
32
+ with get_session(self.tenant) as session:
33
+ statement = select(System)
34
+
35
+ if active_only:
36
+ statement = statement.where(System.is_active == True)
37
+
38
+ results = session.exec(statement)
39
+ return results.all()
40
+
41
+ def update_command(self, system_id: UUID, **kwargs) -> Optional[System]:
42
+ with get_session(self.tenant) as session:
43
+ db_system = session.get(System, system_id)
44
+ if not db_system:
45
+ return None
46
+
47
+ for key, value in kwargs.items():
48
+ if hasattr(db_system, key):
49
+ setattr(db_system, key, value)
50
+
51
+ session.add(db_system)
52
+ session.commit()
53
+ session.refresh(db_system)
54
+ return db_system
55
+
56
+ def delete_command(self, system_id: UUID, soft_delete: bool = True) -> bool:
57
+ with get_session(self.tenant) as session:
58
+ db_system = session.get(System, system_id)
59
+ if not db_system:
60
+ return False
61
+
62
+ if soft_delete:
63
+ db_system.is_active = False
64
+ session.add(db_system)
65
+ else:
66
+ session.delete(db_system)
67
+
68
+ session.commit()
69
+ return True
@@ -0,0 +1,80 @@
1
+ from typing import Optional, List
2
+ from uuid import UUID
3
+ from sqlmodel import select
4
+
5
+ from models import MeasureLab
6
+ from config.db import engines, get_session
7
+
8
+ class MeasureLabRepository:
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, measure_lab: MeasureLab) -> MeasureLab:
15
+ with get_session(self.tenant) as session:
16
+ session.add(measure_lab)
17
+ session.commit()
18
+ session.refresh(measure_lab)
19
+ return measure_lab
20
+
21
+ def get_by_id_command(self, measure_lab_id: UUID) -> Optional[MeasureLab]:
22
+ with get_session(self.tenant) as session:
23
+ return session.get(MeasureLab, measure_lab_id)
24
+
25
+ def get_by_name_command(self, name: str) -> Optional[MeasureLab]:
26
+ with get_session(self.tenant) as session:
27
+ statement = select(MeasureLab).where(MeasureLab.name == name)
28
+ return session.exec(statement).first()
29
+
30
+ def search_by_name_command(self, name: str) -> List[MeasureLab]:
31
+ with get_session(self.tenant) as session:
32
+ statement = select(MeasureLab).where(MeasureLab.name.ilike(f"%{name}%"))
33
+ results = session.exec(statement)
34
+ return results.all()
35
+
36
+ def list_all_command(self, active_only: bool = True) -> List[MeasureLab]:
37
+ with get_session(self.tenant) as session:
38
+ statement = select(MeasureLab)
39
+
40
+ if active_only:
41
+ statement = statement.where(MeasureLab.is_active == True)
42
+
43
+ results = session.exec(statement)
44
+ return results.all()
45
+
46
+ def update_command(self, measure_lab: MeasureLab) -> MeasureLab:
47
+ with get_session(self.tenant) as session:
48
+ db_measure_lab = session.merge(measure_lab)
49
+ session.commit()
50
+ session.refresh(db_measure_lab)
51
+ return db_measure_lab
52
+
53
+ def delete_command(self, measure_lab_id: UUID, soft_delete: bool = True) -> bool:
54
+ with get_session(self.tenant) as session:
55
+ db_measure_lab = session.get(MeasureLab, measure_lab_id)
56
+ if not db_measure_lab:
57
+ return False
58
+
59
+ if soft_delete:
60
+ db_measure_lab.is_active = False
61
+ session.add(db_measure_lab)
62
+ else:
63
+ session.delete(db_measure_lab)
64
+
65
+ session.commit()
66
+ return True
67
+
68
+ def count_command(self, active_only: bool = True) -> int:
69
+ with get_session(self.tenant) as session:
70
+ statement = select(MeasureLab)
71
+ if active_only:
72
+ statement = statement.where(MeasureLab.is_active == True)
73
+ results = session.exec(statement)
74
+ return len(results.all())
75
+
76
+ def exists_by_name_command(self, name: str) -> bool:
77
+ with get_session(self.tenant) as session:
78
+ statement = select(MeasureLab).where(MeasureLab.name == name)
79
+ result = session.exec(statement).first()
80
+ return result is not None
@@ -0,0 +1,254 @@
1
+ from typing import Optional, List
2
+ from uuid import UUID
3
+ from sqlmodel import select
4
+
5
+ from models import MedicalLab
6
+ from models import Px
7
+ from config.db import engines, get_session
8
+
9
+ class MedicalLabRepository:
10
+ def __init__(self, tenant: str):
11
+ self.tenant = tenant
12
+ if tenant not in engines:
13
+ raise ValueError(f"Tenant {tenant} is not configured")
14
+
15
+ def create_command(self, medical_lab: MedicalLab) -> MedicalLab:
16
+ with get_session(self.tenant) as session:
17
+ session.add(medical_lab)
18
+ session.commit()
19
+ session.refresh(medical_lab)
20
+ return medical_lab
21
+
22
+ def get_by_id_command(self, medical_lab_id: UUID, load_relations: bool = False) -> Optional[MedicalLab]:
23
+ with get_session(self.tenant) as session:
24
+ medical_lab = session.get(MedicalLab, medical_lab_id)
25
+
26
+ if medical_lab and load_relations:
27
+ if medical_lab.measure_lab_id:
28
+ from models import MeasureLab
29
+ measure_lab_obj = session.get(MeasureLab, medical_lab.measure_lab_id)
30
+ object.__setattr__(medical_lab, 'measure_lab', measure_lab_obj)
31
+
32
+ from models.lab.client_lab import ClientLab
33
+ statement = select(ClientLab).where(ClientLab.medical_lab_id == medical_lab_id)
34
+ client_labs = session.exec(statement).all()
35
+
36
+ pxs_list = []
37
+ for cl in client_labs:
38
+ px = session.get(Px, cl.client_id)
39
+ if px:
40
+ pxs_list.append(px)
41
+
42
+ object.__setattr__(medical_lab, 'pxs', pxs_list)
43
+
44
+ return medical_lab
45
+
46
+ def get_by_parameter_command(self, parameter: str, load_relations: bool = False) -> Optional[MedicalLab]:
47
+ with get_session(self.tenant) as session:
48
+ statement = select(MedicalLab).where(MedicalLab.parameter == parameter)
49
+ medical_lab = session.exec(statement).first()
50
+
51
+ if medical_lab and load_relations:
52
+ if medical_lab.measure_lab_id:
53
+ from models import MeasureLab
54
+ measure_lab_obj = session.get(MeasureLab, medical_lab.measure_lab_id)
55
+ object.__setattr__(medical_lab, 'measure_lab', measure_lab_obj)
56
+
57
+ from models.lab.client_lab import ClientLab
58
+ statement_cl = select(ClientLab).where(ClientLab.medical_lab_id == medical_lab.medical_lab_id)
59
+ client_labs = session.exec(statement_cl).all()
60
+
61
+ pxs_list = []
62
+ for cl in client_labs:
63
+ px = session.get(Px, cl.client_id)
64
+ if px:
65
+ pxs_list.append(px)
66
+
67
+ object.__setattr__(medical_lab, 'pxs', pxs_list)
68
+
69
+ return medical_lab
70
+
71
+ def search_by_parameter_command(self, parameter: str, load_relations: bool = False) -> List[MedicalLab]:
72
+ with get_session(self.tenant) as session:
73
+ statement = select(MedicalLab).where(MedicalLab.parameter.ilike(f"%{parameter}%"))
74
+ results = session.exec(statement).all()
75
+
76
+ if load_relations:
77
+ from models import MeasureLab
78
+ from models.lab.client_lab import ClientLab
79
+
80
+ for medical_lab in results:
81
+ if medical_lab.measure_lab_id:
82
+ measure_lab_obj = session.get(MeasureLab, medical_lab.measure_lab_id)
83
+ object.__setattr__(medical_lab, 'measure_lab', measure_lab_obj)
84
+
85
+ statement_cl = select(ClientLab).where(ClientLab.medical_lab_id == medical_lab.medical_lab_id)
86
+ client_labs = session.exec(statement_cl).all()
87
+
88
+ pxs_list = []
89
+ for cl in client_labs:
90
+ px = session.get(Px, cl.client_id)
91
+ if px:
92
+ pxs_list.append(px)
93
+
94
+ object.__setattr__(medical_lab, 'pxs', pxs_list)
95
+
96
+ return results
97
+
98
+ def list_all_command(self, active_only: bool = True, load_relations: bool = False) -> List[MedicalLab]:
99
+ with get_session(self.tenant) as session:
100
+ statement = select(MedicalLab)
101
+
102
+ if active_only:
103
+ statement = statement.where(MedicalLab.is_active == True)
104
+
105
+ results = session.exec(statement).all()
106
+
107
+ if load_relations:
108
+ from models import MeasureLab
109
+ from models.lab.client_lab import ClientLab
110
+
111
+ for medical_lab in results:
112
+ if medical_lab.measure_lab_id:
113
+ measure_lab_obj = session.get(MeasureLab, medical_lab.measure_lab_id)
114
+ object.__setattr__(medical_lab, 'measure_lab', measure_lab_obj)
115
+
116
+ statement_cl = select(ClientLab).where(ClientLab.medical_lab_id == medical_lab.medical_lab_id)
117
+ client_labs = session.exec(statement_cl).all()
118
+
119
+ pxs_list = []
120
+ for cl in client_labs:
121
+ px = session.get(Px, cl.client_id)
122
+ if px:
123
+ pxs_list.append(px)
124
+
125
+ object.__setattr__(medical_lab, 'pxs', pxs_list)
126
+
127
+ return results
128
+
129
+ def list_by_measure_lab_command(self, measure_lab_id: UUID, active_only: bool = True) -> List[MedicalLab]:
130
+ with get_session(self.tenant) as session:
131
+ statement = select(MedicalLab).where(MedicalLab.measure_lab_id == measure_lab_id)
132
+
133
+ if active_only:
134
+ statement = statement.where(MedicalLab.is_active == True)
135
+
136
+ results = session.exec(statement)
137
+ return results.all()
138
+
139
+ def update_command(self, medical_lab: MedicalLab) -> MedicalLab:
140
+ with get_session(self.tenant) as session:
141
+ db_medical_lab = session.merge(medical_lab)
142
+ session.commit()
143
+ session.refresh(db_medical_lab)
144
+ return db_medical_lab
145
+
146
+ def delete_command(self, medical_lab_id: UUID, soft_delete: bool = True) -> bool:
147
+ with get_session(self.tenant) as session:
148
+ db_medical_lab = session.get(MedicalLab, medical_lab_id)
149
+ if not db_medical_lab:
150
+ return False
151
+
152
+ if soft_delete:
153
+ db_medical_lab.is_active = False
154
+ session.add(db_medical_lab)
155
+ else:
156
+ session.delete(db_medical_lab)
157
+
158
+ session.commit()
159
+ return True
160
+
161
+ def count_command(self, active_only: bool = True) -> int:
162
+ with get_session(self.tenant) as session:
163
+ statement = select(MedicalLab)
164
+ if active_only:
165
+ statement = statement.where(MedicalLab.is_active == True)
166
+ results = session.exec(statement)
167
+ return len(results.all())
168
+
169
+ def exists_by_parameter_command(self, parameter: str) -> bool:
170
+ with get_session(self.tenant) as session:
171
+ statement = select(MedicalLab).where(MedicalLab.parameter == parameter)
172
+ result = session.exec(statement).first()
173
+ return result is not None
174
+
175
+ def get_medical_lab_patients_command(self, medical_lab_id: UUID) -> List[Px]:
176
+ """Get all patients associated with a medical lab"""
177
+ with get_session(self.tenant) as session:
178
+ from models.lab.client_lab import ClientLab
179
+
180
+ statement = select(ClientLab).where(ClientLab.medical_lab_id == medical_lab_id)
181
+ client_labs = session.exec(statement).all()
182
+
183
+ pxs_list = []
184
+ for cl in client_labs:
185
+ px = session.get(Px, cl.client_id)
186
+ if px:
187
+ pxs_list.append(px)
188
+
189
+ return pxs_list
190
+
191
+ def assign_patient_command(self, medical_lab_id: UUID, px_id: UUID) -> Optional[MedicalLab]:
192
+ """Assign a patient to a medical lab"""
193
+ with get_session(self.tenant) as session:
194
+ from models.lab.client_lab import ClientLab
195
+
196
+ medical_lab = session.get(MedicalLab, medical_lab_id)
197
+ if not medical_lab:
198
+ return None
199
+
200
+ px = session.get(Px, px_id)
201
+ if not px:
202
+ return None
203
+
204
+ existing = session.exec(
205
+ select(ClientLab).where(
206
+ ClientLab.medical_lab_id == medical_lab_id,
207
+ ClientLab.client_id == px_id
208
+ )
209
+ ).first()
210
+
211
+ if not existing:
212
+ client_lab = ClientLab(medical_lab_id=medical_lab_id, client_id=px_id)
213
+ session.add(client_lab)
214
+ session.commit()
215
+
216
+ session.refresh(medical_lab)
217
+ return medical_lab
218
+
219
+ def remove_patient_command(self, medical_lab_id: UUID, px_id: UUID) -> Optional[MedicalLab]:
220
+ """Remove a patient from a medical lab"""
221
+ with get_session(self.tenant) as session:
222
+ from models.lab.client_lab import ClientLab
223
+
224
+ medical_lab = session.get(MedicalLab, medical_lab_id)
225
+ if not medical_lab:
226
+ return None
227
+
228
+ client_lab = session.exec(
229
+ select(ClientLab).where(
230
+ ClientLab.medical_lab_id == medical_lab_id,
231
+ ClientLab.client_id == px_id
232
+ )
233
+ ).first()
234
+
235
+ if client_lab:
236
+ session.delete(client_lab)
237
+ session.commit()
238
+
239
+ session.refresh(medical_lab)
240
+ return medical_lab
241
+
242
+ def is_patient_assigned_command(self, medical_lab_id: UUID, px_id: UUID) -> bool:
243
+ """Check if a patient is assigned to a medical lab"""
244
+ with get_session(self.tenant) as session:
245
+ from models.lab.client_lab import ClientLab
246
+
247
+ result = session.exec(
248
+ select(ClientLab).where(
249
+ ClientLab.medical_lab_id == medical_lab_id,
250
+ ClientLab.client_id == px_id
251
+ )
252
+ ).first()
253
+
254
+ return result is not None