Model/backend/categorisation/processor.py

35 lines
1.3 KiB
Python

from typing import List
from backend.app.db.functions.recommendations_functions import (
get_plans_by_portfolio_id,
get_property_ids,
set_plan_default,
)
from backend.app.db.models.recommendations import Plan
from backend.categorisation.categorisation_logic import CategorisationLogic
def process_portfolio(portfolio_id: int) -> None:
# Get all plans (including scenarios) for all properties in the portfolio
plans: List[Plan] = get_plans_by_portfolio_id(portfolio_id)
# For each property, get all compliant plans
property_ids: List[int] = get_property_ids(portfolio_id)
# For each property, find the cheapest compliant plan
for id in property_ids:
plans_for_property: List[Plan] = [
plan for plan in plans if plan.property_id == id
]
compliant_plans_for_property: List[Plan] = (
CategorisationLogic.get_compliant_plans(plans_for_property)
)
# Choose cheapest compliant plan, or fallback to cheapest overall plan
plans_to_consider = compliant_plans_for_property or plans_for_property
cheapest_plan = CategorisationLogic.get_cheapest_plan(plans_to_consider)
# Update DB: set is_default = True for cheapest plan, False for others
for plan in plans_for_property:
set_plan_default(plan.id, plan.id == cheapest_plan.id)