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.
Files changed (105) hide show
  1. healthdatalayer/__init__.py +0 -0
  2. healthdatalayer/config/__init__.py +0 -0
  3. healthdatalayer/config/config.py +24 -0
  4. healthdatalayer/config/db.py +12 -0
  5. healthdatalayer/config/vault.py +20 -0
  6. healthdatalayer/dtos/__init__.py +2 -0
  7. healthdatalayer/dtos/collaborator/__init__.py +0 -0
  8. healthdatalayer/dtos/collaborator/schedule_collaborator.py +13 -0
  9. healthdatalayer/dtos/medical_visit/__init__.py +0 -0
  10. healthdatalayer/dtos/medical_visit/medical_certificate.py +62 -0
  11. healthdatalayer/models/__init__.py +75 -0
  12. healthdatalayer/models/bridge_area_floor_branch/__init__.py +0 -0
  13. healthdatalayer/models/bridge_area_floor_branch/area.py +8 -0
  14. healthdatalayer/models/bridge_area_floor_branch/branch.py +16 -0
  15. healthdatalayer/models/bridge_area_floor_branch/bridge_area_floor_branch.py +27 -0
  16. healthdatalayer/models/bridge_area_floor_branch/floor.py +8 -0
  17. healthdatalayer/models/bridge_area_floor_branch/room.py +8 -0
  18. healthdatalayer/models/bridge_area_floor_branch/system.py +8 -0
  19. healthdatalayer/models/client/__init__.py +0 -0
  20. healthdatalayer/models/client/address.py +13 -0
  21. healthdatalayer/models/client/city.py +11 -0
  22. healthdatalayer/models/client/client.py +25 -0
  23. healthdatalayer/models/client/client_type.py +9 -0
  24. healthdatalayer/models/client/education.py +9 -0
  25. healthdatalayer/models/client/emergency_contact.py +17 -0
  26. healthdatalayer/models/client/gender.py +9 -0
  27. healthdatalayer/models/client/marriage_status.py +9 -0
  28. healthdatalayer/models/client/nationality.py +10 -0
  29. healthdatalayer/models/client/pathological_history.py +29 -0
  30. healthdatalayer/models/client/pet.py +16 -0
  31. healthdatalayer/models/client/profession.py +9 -0
  32. healthdatalayer/models/client/px.py +35 -0
  33. healthdatalayer/models/client/state.py +9 -0
  34. healthdatalayer/models/collaborator/__init__.py +0 -0
  35. healthdatalayer/models/collaborator/collaborator.py +33 -0
  36. healthdatalayer/models/collaborator/collaborator_speciality.py +8 -0
  37. healthdatalayer/models/collaborator/collaborator_type.py +9 -0
  38. healthdatalayer/models/collaborator/speciality.py +23 -0
  39. healthdatalayer/models/lab/__init__.py +0 -0
  40. healthdatalayer/models/lab/client_lab.py +13 -0
  41. healthdatalayer/models/lab/measure_lab.py +11 -0
  42. healthdatalayer/models/lab/medical_lab.py +17 -0
  43. healthdatalayer/models/medical_visit/__init__.py +0 -0
  44. healthdatalayer/models/medical_visit/medical_diagnosis.py +12 -0
  45. healthdatalayer/models/medical_visit/medical_diagnosis_visit.py +25 -0
  46. healthdatalayer/models/medical_visit/medical_drug.py +27 -0
  47. healthdatalayer/models/medical_visit/medical_drug_recipe.py +19 -0
  48. healthdatalayer/models/medical_visit/medical_recipe_visit.py +28 -0
  49. healthdatalayer/models/medical_visit/medical_visit.py +53 -0
  50. healthdatalayer/models/medical_visit/organ_system_review.py +28 -0
  51. healthdatalayer/models/medical_visit/physical_exam.py +45 -0
  52. healthdatalayer/models/medical_visit/status_visit_enum.py +8 -0
  53. healthdatalayer/models/medical_visit/visit_triage.py +28 -0
  54. healthdatalayer/models/user/__init__.py +0 -0
  55. healthdatalayer/models/user/permission.py +27 -0
  56. healthdatalayer/models/user/permission_user.py +8 -0
  57. healthdatalayer/models/user/role.py +27 -0
  58. healthdatalayer/models/user/role_permission.py +8 -0
  59. healthdatalayer/models/user/role_user.py +8 -0
  60. healthdatalayer/models/user/user.py +30 -0
  61. healthdatalayer/repositories/__init__.py +40 -0
  62. healthdatalayer/repositories/client_repositories/__init__.py +0 -0
  63. healthdatalayer/repositories/client_repositories/address_repository.py +94 -0
  64. healthdatalayer/repositories/client_repositories/client_type_repository.py +69 -0
  65. healthdatalayer/repositories/client_repositories/education_repository.py +70 -0
  66. healthdatalayer/repositories/client_repositories/emergency_contact_repository.py +78 -0
  67. healthdatalayer/repositories/client_repositories/gender_repository.py +70 -0
  68. healthdatalayer/repositories/client_repositories/marriage_status_repository.py +70 -0
  69. healthdatalayer/repositories/client_repositories/nationality_repository.py +69 -0
  70. healthdatalayer/repositories/client_repositories/pathological_history_repository.py +73 -0
  71. healthdatalayer/repositories/client_repositories/pet_repository.py +126 -0
  72. healthdatalayer/repositories/client_repositories/profession_repository.py +70 -0
  73. healthdatalayer/repositories/client_repositories/px_repository.py +239 -0
  74. healthdatalayer/repositories/collaborator_repositories/__init__.py +0 -0
  75. healthdatalayer/repositories/collaborator_repositories/collaborator_repository.py +197 -0
  76. healthdatalayer/repositories/collaborator_repositories/collaborator_type_repository.py +69 -0
  77. healthdatalayer/repositories/collaborator_repositories/speciality_repository.py +75 -0
  78. healthdatalayer/repositories/infraestructure_repositories/__init__.py +0 -0
  79. healthdatalayer/repositories/infraestructure_repositories/area_repository.py +69 -0
  80. healthdatalayer/repositories/infraestructure_repositories/branch_repository.py +69 -0
  81. healthdatalayer/repositories/infraestructure_repositories/bridge_repository.py +82 -0
  82. healthdatalayer/repositories/infraestructure_repositories/floor_repository.py +69 -0
  83. healthdatalayer/repositories/infraestructure_repositories/room_repository.py +69 -0
  84. healthdatalayer/repositories/infraestructure_repositories/system_repository.py +69 -0
  85. healthdatalayer/repositories/lab_repositories/__init__.py +0 -0
  86. healthdatalayer/repositories/lab_repositories/measure_lab_repository.py +80 -0
  87. healthdatalayer/repositories/lab_repositories/medical_lab_repository.py +254 -0
  88. healthdatalayer/repositories/medical_visit_repositories/__init__.py +0 -0
  89. healthdatalayer/repositories/medical_visit_repositories/medical_diagnosis_repository.py +98 -0
  90. healthdatalayer/repositories/medical_visit_repositories/medical_diagnosis_visit_repository.py +109 -0
  91. healthdatalayer/repositories/medical_visit_repositories/medical_drug_recipe_repository.py +71 -0
  92. healthdatalayer/repositories/medical_visit_repositories/medical_drug_repository.py +78 -0
  93. healthdatalayer/repositories/medical_visit_repositories/medical_recipe_visit_repository.py +93 -0
  94. healthdatalayer/repositories/medical_visit_repositories/medical_visit_repository.py +509 -0
  95. healthdatalayer/repositories/medical_visit_repositories/organ_system_review_repository.py +89 -0
  96. healthdatalayer/repositories/medical_visit_repositories/physical_exam_repository.py +89 -0
  97. healthdatalayer/repositories/medical_visit_repositories/visit_triage_repository.py +95 -0
  98. healthdatalayer/repositories/user_repositories/__init__.py +0 -0
  99. healthdatalayer/repositories/user_repositories/permission_repository.py +238 -0
  100. healthdatalayer/repositories/user_repositories/role_repository.py +174 -0
  101. healthdatalayer/repositories/user_repositories/user_repository.py +251 -0
  102. healthdatalayer-1.6.6.dist-info/METADATA +30 -0
  103. healthdatalayer-1.6.6.dist-info/RECORD +105 -0
  104. healthdatalayer-1.6.6.dist-info/WHEEL +5 -0
  105. healthdatalayer-1.6.6.dist-info/top_level.txt +1 -0
@@ -0,0 +1,70 @@
1
+ from typing import Optional, List
2
+ from uuid import UUID
3
+ from sqlmodel import select
4
+
5
+ from healthdatalayer.models import Education
6
+ from healthdatalayer.config.db import engines, get_session
7
+
8
+ class EducationRepository:
9
+
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, education: Education) -> Education:
16
+ with get_session(self.tenant) as session:
17
+ session.add(education)
18
+ session.commit()
19
+ session.refresh(education)
20
+ return education
21
+
22
+ def get_by_id_command(self, education_id: UUID) -> Optional[Education]:
23
+ with get_session(self.tenant) as session:
24
+ return session.get(Education, education_id)
25
+
26
+ def get_by_name_command(self, name: str) -> Optional[Education]:
27
+ with get_session(self.tenant) as session:
28
+ statement = select(Education).where(Education.name == name)
29
+ result = session.exec(statement).first()
30
+ return result
31
+
32
+ def list_all_command(self, active_only: bool = True) -> List[Education]:
33
+ with get_session(self.tenant) as session:
34
+ statement = select(Education)
35
+
36
+ if active_only:
37
+ statement = statement.where(Education.is_active == True)
38
+
39
+ results = session.exec(statement)
40
+ return results.all()
41
+
42
+ def update_command(self, education_id: UUID, **kwargs) -> Optional[Education]:
43
+ with get_session(self.tenant) as session:
44
+ db_education = session.get(Education, education_id)
45
+ if not db_education:
46
+ return None
47
+
48
+ for key, value in kwargs.items():
49
+ if hasattr(db_education, key):
50
+ setattr(db_education, key, value)
51
+
52
+ session.add(db_education)
53
+ session.commit()
54
+ session.refresh(db_education)
55
+ return db_education
56
+
57
+ def delete_command(self, education_id: UUID, soft_delete: bool = True) -> bool:
58
+ with get_session(self.tenant) as session:
59
+ db_education = session.get(Education, education_id)
60
+ if not db_education:
61
+ return False
62
+
63
+ if soft_delete:
64
+ db_education.is_active = False
65
+ session.add(db_education)
66
+ else:
67
+ session.delete(db_education)
68
+
69
+ session.commit()
70
+ return True
@@ -0,0 +1,78 @@
1
+ from typing import Optional, List
2
+ from uuid import UUID
3
+ from sqlmodel import select
4
+
5
+ from healthdatalayer.models import EmergencyContact
6
+ from healthdatalayer.config.db import engines, get_session
7
+
8
+ class EmergencyContactRepository:
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, emergency_contact: EmergencyContact) -> EmergencyContact:
15
+ with get_session(self.tenant) as session:
16
+ session.add(emergency_contact)
17
+ session.commit()
18
+ session.refresh(emergency_contact)
19
+ return emergency_contact
20
+
21
+ def get_by_id_command(self, emergency_contact_id: UUID) -> Optional[EmergencyContact]:
22
+ with get_session(self.tenant) as session:
23
+ return session.get(EmergencyContact, emergency_contact_id)
24
+
25
+ def get_by_client_id_command(self, client_id: UUID) -> List[EmergencyContact]:
26
+ with get_session(self.tenant) as session:
27
+ statement = select(EmergencyContact).where(
28
+ EmergencyContact.client_id == client_id,
29
+ EmergencyContact.is_active == True
30
+ )
31
+ results = session.exec(statement)
32
+ return results.all()
33
+
34
+ def get_by_name_command(self, name: str) -> Optional[EmergencyContact]:
35
+ with get_session(self.tenant) as session:
36
+ statement = select(EmergencyContact).where(EmergencyContact.name == name)
37
+ result = session.exec(statement).first()
38
+ return result
39
+
40
+ def list_all_command(self, active_only: bool = True) -> List[EmergencyContact]:
41
+ with get_session(self.tenant) as session:
42
+ statement = select(EmergencyContact)
43
+
44
+ if active_only:
45
+ statement = statement.where(EmergencyContact.is_active == True)
46
+
47
+ results = session.exec(statement)
48
+ return results.all()
49
+
50
+ def update_command(self, emergency_contact_id: UUID, **kwargs) -> Optional[EmergencyContact]:
51
+ with get_session(self.tenant) as session:
52
+ db_emergency_contact = session.get(EmergencyContact, emergency_contact_id)
53
+ if not db_emergency_contact:
54
+ return None
55
+
56
+ for key, value in kwargs.items():
57
+ if hasattr(db_emergency_contact, key):
58
+ setattr(db_emergency_contact, key, value)
59
+
60
+ session.add(db_emergency_contact)
61
+ session.commit()
62
+ session.refresh(db_emergency_contact)
63
+ return db_emergency_contact
64
+
65
+ def delete_command(self, emergency_contact_id: UUID, soft_delete: bool = True) -> bool:
66
+ with get_session(self.tenant) as session:
67
+ db_emergency_contact = session.get(EmergencyContact, emergency_contact_id)
68
+ if not db_emergency_contact:
69
+ return False
70
+
71
+ if soft_delete:
72
+ db_emergency_contact.is_active = False
73
+ session.add(db_emergency_contact)
74
+ else:
75
+ session.delete(db_emergency_contact)
76
+
77
+ session.commit()
78
+ return True
@@ -0,0 +1,70 @@
1
+ from typing import Optional, List
2
+ from uuid import UUID
3
+ from sqlmodel import select
4
+
5
+ from healthdatalayer.models import Gender
6
+ from healthdatalayer.config.db import engines, get_session
7
+
8
+ class GenderRepository:
9
+
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, gender: Gender) -> Gender:
16
+ with get_session(self.tenant) as session:
17
+ session.add(gender)
18
+ session.commit()
19
+ session.refresh(gender)
20
+ return gender
21
+
22
+ def get_by_id_command(self, gender_id: UUID) -> Optional[Gender]:
23
+ with get_session(self.tenant) as session:
24
+ return session.get(Gender, gender_id)
25
+
26
+ def get_by_name_command(self, name: str) -> Optional[Gender]:
27
+ with get_session(self.tenant) as session:
28
+ statement = select(Gender).where(Gender.name == name)
29
+ result = session.exec(statement).first()
30
+ return result
31
+
32
+ def list_all_command(self, active_only: bool = True) -> List[Gender]:
33
+ with get_session(self.tenant) as session:
34
+ statement = select(Gender)
35
+
36
+ if active_only:
37
+ statement = statement.where(Gender.is_active == True)
38
+
39
+ results = session.exec(statement)
40
+ return results.all()
41
+
42
+ def update_command(self, gender_id: UUID, **kwargs) -> Optional[Gender]:
43
+ with get_session(self.tenant) as session:
44
+ db_gender = session.get(Gender, gender_id)
45
+ if not db_gender:
46
+ return None
47
+
48
+ for key, value in kwargs.items():
49
+ if hasattr(db_gender, key):
50
+ setattr(db_gender, key, value)
51
+
52
+ session.add(db_gender)
53
+ session.commit()
54
+ session.refresh(db_gender)
55
+ return db_gender
56
+
57
+ def delete_command(self, gender_id: UUID, soft_delete: bool = True) -> bool:
58
+ with get_session(self.tenant) as session:
59
+ db_gender = session.get(Gender, gender_id)
60
+ if not db_gender:
61
+ return False
62
+
63
+ if soft_delete:
64
+ db_gender.is_active = False
65
+ session.add(db_gender)
66
+ else:
67
+ session.delete(db_gender)
68
+
69
+ session.commit()
70
+ return True
@@ -0,0 +1,70 @@
1
+ from typing import Optional, List
2
+ from uuid import UUID
3
+ from sqlmodel import select
4
+
5
+ from healthdatalayer.models import MarriageStatus
6
+ from healthdatalayer.config.db import engines, get_session
7
+
8
+ class MarriageStatusRepository:
9
+
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, marriage_status: MarriageStatus) -> MarriageStatus:
16
+ with get_session(self.tenant) as session:
17
+ session.add(marriage_status)
18
+ session.commit()
19
+ session.refresh(marriage_status)
20
+ return marriage_status
21
+
22
+ def get_by_id_command(self, marriage_status_id: UUID) -> Optional[MarriageStatus]:
23
+ with get_session(self.tenant) as session:
24
+ return session.get(MarriageStatus, marriage_status_id)
25
+
26
+ def get_by_name_command(self, name: str) -> Optional[MarriageStatus]:
27
+ with get_session(self.tenant) as session:
28
+ statement = select(MarriageStatus).where(MarriageStatus.name == name)
29
+ result = session.exec(statement).first()
30
+ return result
31
+
32
+ def list_all_command(self, active_only: bool = True) -> List[MarriageStatus]:
33
+ with get_session(self.tenant) as session:
34
+ statement = select(MarriageStatus)
35
+
36
+ if active_only:
37
+ statement = statement.where(MarriageStatus.is_active == True)
38
+
39
+ results = session.exec(statement)
40
+ return results.all()
41
+
42
+ def update_command(self, marriage_status_id: UUID, **kwargs) -> Optional[MarriageStatus]:
43
+ with get_session(self.tenant) as session:
44
+ db_marriage_status = session.get(MarriageStatus, marriage_status_id)
45
+ if not db_marriage_status:
46
+ return None
47
+
48
+ for key, value in kwargs.items():
49
+ if hasattr(db_marriage_status, key):
50
+ setattr(db_marriage_status, key, value)
51
+
52
+ session.add(db_marriage_status)
53
+ session.commit()
54
+ session.refresh(db_marriage_status)
55
+ return db_marriage_status
56
+
57
+ def delete_command(self, marriage_status_id: UUID, soft_delete: bool = True) -> bool:
58
+ with get_session(self.tenant) as session:
59
+ db_marriage_status = session.get(MarriageStatus, marriage_status_id)
60
+ if not db_marriage_status:
61
+ return False
62
+
63
+ if soft_delete:
64
+ db_marriage_status.is_active = False
65
+ session.add(db_marriage_status)
66
+ else:
67
+ session.delete(db_marriage_status)
68
+
69
+ session.commit()
70
+ 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 healthdatalayer.models import Nationality
6
+ from healthdatalayer.config.db import engines, get_session
7
+
8
+ class NationalityRepository:
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, nationality: Nationality) -> Nationality:
15
+ with get_session(self.tenant) as session:
16
+ session.add(nationality)
17
+ session.commit()
18
+ session.refresh(nationality)
19
+ return nationality
20
+
21
+ def get_by_id_command(self, nationality_id: UUID) -> Optional[Nationality]:
22
+ with get_session(self.tenant) as session:
23
+ return session.get(Nationality, nationality_id)
24
+
25
+ def get_by_name_command(self, name: str) -> Optional[Nationality]:
26
+ with get_session(self.tenant) as session:
27
+ statement = select(Nationality).where(Nationality.name.ilike(f"%{name}%"), Nationality.is_active == True)
28
+ result = session.exec(statement).first()
29
+ return result
30
+
31
+ def list_all_command(self, active_only: bool = True) -> List[Nationality]:
32
+ with get_session(self.tenant) as session:
33
+ statement = select(Nationality)
34
+
35
+ if active_only:
36
+ statement = statement.where(Nationality.is_active == True)
37
+
38
+ results = session.exec(statement)
39
+ return results.all()
40
+
41
+ def update_command(self, nationality_id: UUID, **kwargs) -> Optional[Nationality]:
42
+ with get_session(self.tenant) as session:
43
+ db_nationality = session.get(Nationality, nationality_id)
44
+ if not db_nationality:
45
+ return None
46
+
47
+ for key, value in kwargs.items():
48
+ if hasattr(db_nationality, key):
49
+ setattr(db_nationality, key, value)
50
+
51
+ session.add(db_nationality)
52
+ session.commit()
53
+ session.refresh(db_nationality)
54
+ return db_nationality
55
+
56
+ def delete_command(self, nationality_id: UUID, soft_delete: bool = True) -> bool:
57
+ with get_session(self.tenant) as session:
58
+ db_nationality = session.get(Nationality, nationality_id)
59
+ if not db_nationality:
60
+ return False
61
+
62
+ if soft_delete:
63
+ db_nationality.is_active = False
64
+ session.add(db_nationality)
65
+ else:
66
+ session.delete(db_nationality)
67
+
68
+ session.commit()
69
+ return True
@@ -0,0 +1,73 @@
1
+ from typing import Optional, List
2
+ from uuid import UUID
3
+ from sqlmodel import select
4
+
5
+ from healthdatalayer.models import PathologicalHistory
6
+ from healthdatalayer.config.db import engines, get_session
7
+
8
+ class PathologicalHistoryRepository:
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, pathological_history: PathologicalHistory) -> PathologicalHistory:
15
+ with get_session(self.tenant) as session:
16
+ session.add(pathological_history)
17
+ session.commit()
18
+ session.refresh(pathological_history)
19
+ return pathological_history
20
+
21
+ def get_by_id_command(self, pathological_history_id: UUID) -> Optional[PathologicalHistory]:
22
+ with get_session(self.tenant) as session:
23
+ return session.get(PathologicalHistory, pathological_history_id)
24
+
25
+ def get_by_client_id_command(self, client_id: UUID) -> List[PathologicalHistory]:
26
+ with get_session(self.tenant) as session:
27
+ statement = select(PathologicalHistory).where(
28
+ PathologicalHistory.client_id == client_id,
29
+ PathologicalHistory.is_active == True
30
+ )
31
+ results = session.exec(statement)
32
+ return results.all()
33
+
34
+
35
+ def list_all_command(self, active_only: bool = True) -> List[PathologicalHistory]:
36
+ with get_session(self.tenant) as session:
37
+ statement = select(PathologicalHistory)
38
+
39
+ if active_only:
40
+ statement = statement.where(PathologicalHistory.is_active == True)
41
+
42
+ results = session.exec(statement)
43
+ return results.all()
44
+
45
+ def update_command(self, pathological_history_id: UUID, **kwargs) -> Optional[PathologicalHistory]:
46
+ with get_session(self.tenant) as session:
47
+ db_pathological_history = session.get(PathologicalHistory, pathological_history_id)
48
+ if not db_pathological_history:
49
+ return None
50
+
51
+ for key, value in kwargs.items():
52
+ if hasattr(db_pathological_history, key):
53
+ setattr(db_pathological_history, key, value)
54
+
55
+ session.add(db_pathological_history)
56
+ session.commit()
57
+ session.refresh(db_pathological_history)
58
+ return db_pathological_history
59
+
60
+ def delete_command(self, pathological_history_id: UUID, soft_delete: bool = True) -> bool:
61
+ with get_session(self.tenant) as session:
62
+ db_pathological_history = session.get(PathologicalHistory, pathological_history_id)
63
+ if not db_pathological_history:
64
+ return False
65
+
66
+ if soft_delete:
67
+ db_pathological_history.is_active = False
68
+ session.add(db_pathological_history)
69
+ else:
70
+ session.delete(db_pathological_history)
71
+
72
+ session.commit()
73
+ return True
@@ -0,0 +1,126 @@
1
+ from typing import Optional, List
2
+ from uuid import UUID
3
+ from sqlmodel import select
4
+ from sqlalchemy.orm import selectinload
5
+
6
+ from healthdatalayer.models import Pet
7
+ from healthdatalayer.config.db import engines, get_session
8
+
9
+ class PetRepository:
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, pet: Pet) -> Pet:
16
+ with get_session(self.tenant) as session:
17
+ session.add(pet)
18
+ session.commit()
19
+ session.refresh(pet)
20
+ return pet
21
+
22
+ def get_by_id_command(self, pet_id: UUID, load_relations: bool = False) -> Optional[Pet]:
23
+ with get_session(self.tenant) as session:
24
+ if load_relations:
25
+ statement = (
26
+ select(Pet)
27
+ .where(Pet.client_id == pet_id)
28
+ .options(
29
+ selectinload(Pet.gender),
30
+ selectinload(Pet.address),
31
+ selectinload(Pet.marriage_status),
32
+ selectinload(Pet.profession),
33
+ selectinload(Pet.education),
34
+ selectinload(Pet.user),
35
+ )
36
+ )
37
+ result = session.exec(statement).first()
38
+ return result
39
+ else:
40
+ return session.get(Pet, pet_id)
41
+
42
+ def get_by_identification_command(self, identification: str, load_relations: bool = False) -> Optional[Pet]:
43
+ with get_session(self.tenant) as session:
44
+ statement = select(Pet).where(Pet.identification == identification)
45
+
46
+ if load_relations:
47
+ statement = statement.options(
48
+ selectinload(Pet.gender),
49
+ selectinload(Pet.address),
50
+ selectinload(Pet.marriage_status),
51
+ selectinload(Pet.profession),
52
+ selectinload(Pet.education),
53
+ selectinload(Pet.user),
54
+ )
55
+
56
+ result = session.exec(statement).first()
57
+ return result
58
+
59
+ def search_by_name_command(self, name: str, load_relations: bool = False) -> List[Pet]:
60
+ with get_session(self.tenant) as session:
61
+ statement = select(Pet).where(
62
+ (Pet.first_name.ilike(f"%{name}%")) |
63
+ (Pet.last_name.ilike(f"%{name}%"))
64
+ )
65
+
66
+ if load_relations:
67
+ statement = statement.options(
68
+ selectinload(Pet.gender),
69
+ selectinload(Pet.address),
70
+ selectinload(Pet.marriage_status),
71
+ selectinload(Pet.profession),
72
+ selectinload(Pet.education),
73
+ selectinload(Pet.user),
74
+ )
75
+
76
+ results = session.exec(statement)
77
+ return results.all()
78
+
79
+ def list_all_command(self, active_only: bool = True, load_relations: bool = False) -> List[Pet]:
80
+ with get_session(self.tenant) as session:
81
+ statement = select(Pet)
82
+
83
+ if active_only:
84
+ statement = statement.where(Pet.is_active == True)
85
+
86
+ if load_relations:
87
+ statement = statement.options(
88
+ selectinload(Pet.gender),
89
+ selectinload(Pet.address),
90
+ selectinload(Pet.marriage_status),
91
+ selectinload(Pet.profession),
92
+ selectinload(Pet.education),
93
+ )
94
+
95
+ results = session.exec(statement)
96
+ return results.all()
97
+
98
+ def update_command(self, pet: Pet) -> Pet:
99
+ with get_session(self.tenant) as session:
100
+ db_pet = session.merge(pet)
101
+ session.commit()
102
+ session.refresh(db_pet)
103
+ return db_pet
104
+
105
+ def delete_command(self, pet_id: UUID, soft_delete: bool = True) -> bool:
106
+ with get_session(self.tenant) as session:
107
+ db_pet = session.get(Pet, pet_id)
108
+ if not db_pet:
109
+ return False
110
+
111
+ if soft_delete:
112
+ db_pet.is_active = False
113
+ session.add(db_pet)
114
+ else:
115
+ session.delete(db_pet)
116
+
117
+ session.commit()
118
+ return True
119
+
120
+ def count_command(self, active_only: bool = True) -> int:
121
+ with get_session(self.tenant) as session:
122
+ statement = select(Pet)
123
+ if active_only:
124
+ statement = statement.where(Pet.is_active == True)
125
+ results = session.exec(statement)
126
+ return len(results.all())
@@ -0,0 +1,70 @@
1
+ from typing import Optional, List
2
+ from uuid import UUID
3
+ from sqlmodel import select
4
+
5
+ from healthdatalayer.models import Profession
6
+ from healthdatalayer.config.db import engines, get_session
7
+
8
+ class ProfessionRepository:
9
+
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, profession: Profession) -> Profession:
16
+ with get_session(self.tenant) as session:
17
+ session.add(profession)
18
+ session.commit()
19
+ session.refresh(profession)
20
+ return profession
21
+
22
+ def get_by_id_command(self, profession_id: UUID) -> Optional[Profession]:
23
+ with get_session(self.tenant) as session:
24
+ return session.get(Profession, profession_id)
25
+
26
+ def get_by_name_command(self, name: str) -> Optional[Profession]:
27
+ with get_session(self.tenant) as session:
28
+ statement = select(Profession).where(Profession.name == name)
29
+ result = session.exec(statement).first()
30
+ return result
31
+
32
+ def list_all_command(self, active_only: bool = True) -> List[Profession]:
33
+ with get_session(self.tenant) as session:
34
+ statement = select(Profession)
35
+
36
+ if active_only:
37
+ statement = statement.where(Profession.is_active == True)
38
+
39
+ results = session.exec(statement)
40
+ return results.all()
41
+
42
+ def update_command(self, profession_id: UUID, **kwargs) -> Optional[Profession]:
43
+ with get_session(self.tenant) as session:
44
+ db_profession = session.get(Profession, profession_id)
45
+ if not db_profession:
46
+ return None
47
+
48
+ for key, value in kwargs.items():
49
+ if hasattr(db_profession, key):
50
+ setattr(db_profession, key, value)
51
+
52
+ session.add(db_profession)
53
+ session.commit()
54
+ session.refresh(db_profession)
55
+ return db_profession
56
+
57
+ def delete_command(self, profession_id: UUID, soft_delete: bool = True) -> bool:
58
+ with get_session(self.tenant) as session:
59
+ db_profession = session.get(Profession, profession_id)
60
+ if not db_profession:
61
+ return False
62
+
63
+ if soft_delete:
64
+ db_profession.is_active = False
65
+ session.add(db_profession)
66
+ else:
67
+ session.delete(db_profession)
68
+
69
+ session.commit()
70
+ return True