|
28 | 28 | from classes.selection_builder_exception import SelectionBuilderException |
29 | 29 | from classes.appointments_slot_type import AppointmentSlotType |
30 | 30 | from classes.appointment_status_type import AppointmentStatusType |
| 31 | +from classes.diagnosis_date_reason_type import DiagnosisDateReasonType |
31 | 32 | from classes.which_diagnostic_test import WhichDiagnosticTest |
32 | 33 | from classes.diagnostic_test_type import DiagnosticTestType |
33 | 34 | from classes.diagnostic_test_is_void import DiagnosticTestIsVoid |
@@ -739,30 +740,18 @@ def _add_criteria_latest_episode_type(self) -> None: |
739 | 740 | Translates a human-readable episode type string into an internal numeric ID. |
740 | 741 | """ |
741 | 742 | try: |
742 | | - value = self.criteria_value.lower() |
743 | | - comparator = self.criteria_comparator |
744 | | - |
745 | | - # Simulate EpisodeType enum mapping |
746 | | - episode_type_map = { |
747 | | - "referral": 1, |
748 | | - "invitation": 2, |
749 | | - "test_kit_sent": 3, |
750 | | - "reminder": 4, |
751 | | - "episode_end": 5, |
752 | | - # Add more mappings as needed |
753 | | - } |
754 | | - |
755 | | - if value not in episode_type_map: |
756 | | - raise ValueError(f"Unknown episode type: {value}") |
757 | | - |
758 | | - episode_type_id = episode_type_map[value] |
| 743 | + episode_type = EpisodeType.by_description_case_insensitive( |
| 744 | + self.criteria_value |
| 745 | + ) |
| 746 | + if episode_type is None: |
| 747 | + raise SelectionBuilderException( |
| 748 | + self.criteria_key_name, self.criteria_value |
| 749 | + ) |
759 | 750 |
|
760 | | - # Simulate the required join (docs only—no real SQL execution here) |
761 | | - # In real builder this would ensure join to 'latest_episode' alias (ep) |
| 751 | + self._add_join_to_latest_episode() |
762 | 752 | self.sql_where.append( |
763 | | - f"AND ep.episode_type_id {comparator} {episode_type_id}" |
| 753 | + f" AND ep.episode_type_id {self.criteria_comparator}{episode_type.valid_value_id}" |
764 | 754 | ) |
765 | | - |
766 | 755 | except Exception: |
767 | 756 | raise SelectionBuilderException(self.criteria_key_name, self.criteria_value) |
768 | 757 |
|
@@ -816,6 +805,9 @@ def _add_criteria_latest_episode_status(self) -> None: |
816 | 805 | "pending": 102, |
817 | 806 | "cancelled": 103, |
818 | 807 | "invalid": 104, |
| 808 | + "open": 11352, |
| 809 | + "closed": 11353, |
| 810 | + "paused": 11354, |
819 | 811 | # Add actual mappings as needed |
820 | 812 | } |
821 | 813 |
|
@@ -1206,34 +1198,19 @@ def _add_criteria_has_diagnostic_test(self, latest_episode_only: bool) -> None: |
1206 | 1198 |
|
1207 | 1199 | def _add_criteria_diagnosis_date_reason(self) -> None: |
1208 | 1200 | """ |
1209 | | - Adds a filter on ep.diagnosis_date_reason_id. |
1210 | | - Supports symbolic matches (via ID) and special values: NULL, NOT_NULL. |
| 1201 | + Adds a SQL WHERE clause for the latest episode's diagnosis_date_reason_id based on self.criteria_value. |
| 1202 | + Assumes self.sql_where, self.criteria_comparator, and self.criteria_value are set. |
1211 | 1203 | """ |
1212 | 1204 | try: |
1213 | | - value = self.criteria_value.strip().lower() |
1214 | | - comparator = self.criteria_comparator |
1215 | | - |
1216 | | - # Simulated DiagnosisDateReasonType |
1217 | | - reason_map = { |
1218 | | - "patient informed": 900, |
1219 | | - "clinician notified": 901, |
1220 | | - "screening outcome": 902, |
1221 | | - "null": "NULL", |
1222 | | - "not_null": "NOT NULL", |
1223 | | - # Extend as needed |
1224 | | - } |
1225 | | - |
1226 | | - if value not in reason_map: |
1227 | | - raise ValueError(f"Unknown diagnosis date reason: {value}") |
1228 | | - |
1229 | | - resolved = reason_map[value] |
1230 | | - if resolved in ("NULL", "NOT NULL"): |
1231 | | - self.sql_where.append(f"AND ep.diagnosis_date_reason_id IS {resolved}") |
| 1205 | + diagnosis_date_reason = DiagnosisDateReasonType.by_description_case_insensitive(self.criteria_value) |
| 1206 | + if diagnosis_date_reason is None: |
| 1207 | + raise ValueError(f"Unknown diagnosis date reason: {self.criteria_value}") |
| 1208 | + self._add_join_to_latest_episode() |
| 1209 | + self.sql_where.append(" AND ep.diagnosis_date_reason_id ") |
| 1210 | + if diagnosis_date_reason in (DiagnosisDateReasonType.NULL, DiagnosisDateReasonType.NOT_NULL): |
| 1211 | + self.sql_where.append(f" IS {diagnosis_date_reason.get_description()}") |
1232 | 1212 | else: |
1233 | | - self.sql_where.append( |
1234 | | - f"AND ep.diagnosis_date_reason_id {comparator} {resolved}" |
1235 | | - ) |
1236 | | - |
| 1213 | + self.sql_where.append(f"{self.criteria_comparator}{diagnosis_date_reason.get_valid_value_id()}") |
1237 | 1214 | except Exception: |
1238 | 1215 | raise SelectionBuilderException(self.criteria_key_name, self.criteria_value) |
1239 | 1216 |
|
|
0 commit comments