1111from eligibility_signposting_api .audit .audit_context import AuditContext
1212from eligibility_signposting_api .model import campaign_config , eligibility_status
1313from eligibility_signposting_api .model .eligibility_status import (
14- BestIterationResult ,
1514 CohortGroupResult ,
1615 Condition ,
1716 ConditionName ,
1817 EligibilityStatus ,
1918 IterationResult ,
19+ IterationResultSummary ,
2020 Reason ,
2121 Status ,
2222 StatusText ,
3232 from eligibility_signposting_api .model .campaign_config import (
3333 CampaignConfig ,
3434 CohortLabel ,
35- IterationName ,
3635 )
3736 from eligibility_signposting_api .model .person import Person
3837
@@ -81,31 +80,32 @@ def get_the_best_cohort_memberships(
8180
8281 return best_status , best_cohorts
8382
84- def get_eligibility_status (self , include_actions : str , conditions : list [str ], category : str ) -> EligibilityStatus :
83+ def get_eligibility_status (
84+ self , include_actions : str , conditions : list [str ], requested_category : str
85+ ) -> EligibilityStatus :
8586 include_actions_flag = include_actions .upper () == "Y"
8687 condition_results : dict [ConditionName , IterationResult ] = {}
8788 final_result = []
8889
89- requested_grouped_campaigns = self .campaign_evaluator .get_requested_grouped_campaigns (
90- self .campaign_configs , conditions , category
90+ requested_cc_with_active_iteration = (
91+ self .campaign_evaluator .get_campaign_with_latest_active_iteration_per_target (
92+ self .campaign_configs , conditions , requested_category
93+ )
9194 )
92- for condition_name , campaign_group in requested_grouped_campaigns :
93- best_iteration_result = self .get_best_iteration_result (campaign_group )
94-
95- if best_iteration_result is None :
96- continue
95+ for condition_name , campaign in requested_cc_with_active_iteration :
96+ iteration_result_summary = self .evaluate_iteration_result_summary (campaign )
9797
9898 matched_action_detail = self .action_rule_handler .get_actions (
9999 self .person ,
100- best_iteration_result .active_iteration ,
101- best_iteration_result .iteration_result ,
100+ iteration_result_summary .active_iteration ,
101+ iteration_result_summary .iteration_result ,
102102 include_actions_flag = include_actions_flag ,
103103 )
104104
105- best_iteration_result = TokenProcessor .find_and_replace_tokens (self .person , best_iteration_result )
105+ iteration_result_summary = TokenProcessor .find_and_replace_tokens (self .person , iteration_result_summary )
106106 matched_action_detail = TokenProcessor .find_and_replace_tokens (self .person , matched_action_detail )
107107
108- condition_results [condition_name ] = best_iteration_result .iteration_result
108+ condition_results [condition_name ] = iteration_result_summary .iteration_result
109109 condition_results [condition_name ].actions = matched_action_detail .actions
110110
111111 condition : Condition = self .build_condition (
@@ -116,54 +116,34 @@ def get_eligibility_status(self, include_actions: str, conditions: list[str], ca
116116
117117 AuditContext .append_audit_condition (
118118 condition_name ,
119- best_iteration_result ,
119+ iteration_result_summary ,
120120 matched_action_detail ,
121121 )
122122
123123 # Consolidate all the results and return
124124 return eligibility_status .EligibilityStatus (conditions = final_result )
125125
126- def get_best_iteration_result (self , campaign_group : list [CampaignConfig ]) -> BestIterationResult | None :
127- iteration_results = self .get_iteration_results (campaign_group )
128-
129- if not iteration_results :
130- return None
131-
132- (_best_iteration_name , best_iteration_result ) = max (
133- iteration_results .items (),
134- key = lambda item : next (iter (item [1 ].cohort_results .values ())).status .value
135- # Below handles the case where there are no cohort results
136- if item [1 ].cohort_results
137- else - 1 ,
126+ def evaluate_iteration_result_summary (
127+ self , campaign_with_active_iteration : CampaignConfig
128+ ) -> IterationResultSummary :
129+ active_iteration = campaign_with_active_iteration .current_iteration
130+ cohort_results : dict [CohortLabel , CohortGroupResult ] = self .rule_processor .get_cohort_group_results (
131+ self .person , active_iteration
138132 )
139133
140- return best_iteration_result
141-
142- def get_iteration_results (self , campaign_group : list [CampaignConfig ]) -> dict [IterationName , BestIterationResult ]:
143- iteration_results : dict [IterationName , BestIterationResult ] = {}
144-
145- for cc in campaign_group :
146- try :
147- active_iteration = cc .current_iteration
148- except StopIteration :
149- logger .info ("Skipping campaign ID %s as no active iteration was found." , cc .id )
150- continue
151- cohort_results : dict [CohortLabel , CohortGroupResult ] = self .rule_processor .get_cohort_group_results (
152- self .person , active_iteration
153- )
154-
155- # Determine Result between cohorts - get the best
156- status , best_cohorts = self .get_the_best_cohort_memberships (cohort_results )
157- status_text = self .get_status_text (active_iteration .status_text , ConditionName (cc .target ), status )
134+ # Determine Result between cohorts - get the best
135+ status , best_cohorts = self .get_the_best_cohort_memberships (cohort_results )
136+ status_text = self .get_status_text (
137+ active_iteration .status_text , ConditionName (campaign_with_active_iteration .target ), status
138+ )
158139
159- iteration_results [active_iteration .name ] = BestIterationResult (
160- IterationResult (status , status_text , best_cohorts , []),
161- active_iteration ,
162- cc .id ,
163- cc .version ,
164- cohort_results ,
165- )
166- return iteration_results
140+ return IterationResultSummary (
141+ IterationResult (status , status_text , best_cohorts , []),
142+ active_iteration ,
143+ campaign_with_active_iteration .id ,
144+ campaign_with_active_iteration .version ,
145+ cohort_results ,
146+ )
167147
168148 @staticmethod
169149 def get_status_text (
0 commit comments