diff --git a/backend/app/domain/classes/plan.py b/backend/app/domain/classes/plan.py index 7970abcd..351ea512 100644 --- a/backend/app/domain/classes/plan.py +++ b/backend/app/domain/classes/plan.py @@ -60,6 +60,14 @@ class Plan: case _: raise NotImplementedError + @property + def cost(self) -> float: + return ( + self.record.cost_of_works + if self.record.cost_of_works is not None + else float("inf") + ) + def to_sqlalchemy(self) -> PlanPersistence: scenario_record = self.scenario.record diff --git a/backend/categorisation/processor.py b/backend/categorisation/processor.py index e5d69dcf..e90c3b08 100644 --- a/backend/categorisation/processor.py +++ b/backend/categorisation/processor.py @@ -50,10 +50,13 @@ def process_portfolio( if not property_plans: raise ValueError(f"No plans for property {property_id}") - cheapest_plan = choose_cheapest_relevant_plan( - property_plans, scenario_priority_order - ) - logger.info(f"Successfully found cheapest plan for Property {property_id}") + try: + cheapest_plan = choose_cheapest_relevant_plan( + property_plans, scenario_priority_order + ) + except Exception: + logger.error(f"Failed to find cheapest plan for property {property_id}") + raise updated_property_plan_models, updated_property_scenario_models = ( _update_plan_and_scenario_objects(property_plans, cheapest_plan) @@ -85,23 +88,11 @@ def choose_cheapest_relevant_plan( f"All plans must have an ID, but found a plan with no ID: {plan}" ) - min_cost: float = min( - ( - plan.record.cost_of_works - if plan.record.cost_of_works is not None - else float("inf") - ) - for plan in eligible_plans - ) + min_cost: float = min(plan.cost for plan in eligible_plans) - if all(p.record.cost_of_works == 0 for p in eligible_plans): - cheapest_plans = eligible_plans - else: - cheapest_plans: List[Plan] = [ - plan - for plan in eligible_plans - if (plan.record.cost_of_works or float("inf")) == min_cost - ] + cheapest_plans: List[Plan] = [ + plan for plan in eligible_plans if plan.cost == min_cost + ] for priority_scenario_id in scenario_priority_order: for plan in cheapest_plans: diff --git a/backend/categorisation/tests/test_prioritised_plan_selected.py b/backend/categorisation/tests/test_prioritised_plan_selected.py index 5ddc7b8f..a9529a53 100644 --- a/backend/categorisation/tests/test_prioritised_plan_selected.py +++ b/backend/categorisation/tests/test_prioritised_plan_selected.py @@ -127,7 +127,7 @@ def test_some_plans_zero_cost__cheapest_returned( created_at_datetime, False, cost_of_works=50.0, name="EPC C - Minor Works" ) scenario_priority_order: List[int] = [4, 3] - expected_default_plan_id = 2 + expected_default_plan_id = 1 # act actual_default_plan = choose_cheapest_relevant_plan(