diff --git a/backend/condition/domain/mapping/lbwf_mapper.py b/backend/condition/domain/mapping/lbwf_mapper.py index 0bbdc0d6..63434240 100644 --- a/backend/condition/domain/mapping/lbwf_mapper.py +++ b/backend/condition/domain/mapping/lbwf_mapper.py @@ -1,15 +1,59 @@ from typing import Any, List, Optional +from datetime import datetime, date + from backend.condition.domain.asset_condition import AssetCondition from backend.condition.domain.element import Element from backend.condition.domain.mapping.mapper import Mapper from backend.condition.parsing.records.lbwf.lbwf_asset_condition import LbwfAssetCondition +from backend.condition.parsing.records.lbwf.lbwf_house import LbwfHouse +from utils.logger import setup_logger +logger = setup_logger() class LbwfMapper(Mapper): - def map_asset_conditions(self, client_data: List[Any]) -> List[AssetCondition]: - raise NotImplementedError + def map_asset_conditions_for_property(self, client_data: Any) -> List[AssetCondition]: + assert isinstance(client_data, LbwfHouse) # TODO: think of a better way to do this + + mapped_assets: List[AssetCondition] = [] + + uprn: int = client_data.uprn + for raw_asset in client_data.assets: + try: + element: Element = LbwfMapper._map_element(raw_asset.element_code) + except: + logger.warning(f"Unrecognised LBWF Asset Element Code: {raw_asset.element_code}. Skipping record") + continue + + + mapped_assets.append( + AssetCondition( + uprn=uprn, + element=element, + condition_description=raw_asset.attribute_code_description, + quantity=raw_asset.quantity, + renewal_year=LbwfMapper._calculate_renewal_year(raw_asset), + source=raw_asset.element_comments, + ) + ) + + return mapped_assets + + @staticmethod - def _map_element(lbwf_asset: LbwfAssetCondition) -> Optional[Element]: - raise NotImplementedError \ No newline at end of file + def _map_element(lbwf_element_code: LbwfAssetCondition) -> Element: + return Element[lbwf_element_code] + + @staticmethod + def _calculate_renewal_year(lbwf_asset: LbwfAssetCondition) -> Optional[int]: + remaining_life_years: Optional[int] = lbwf_asset.remaining_life + if not remaining_life_years: + return None + + try: + survey_year: int = datetime.now().year # TODO: get survey year from filename or elsewhere + return survey_year + remaining_life_years + except: + logger.debug(f"Unable to map LBWF Asset remaining life {remaining_life_years} to renewal year, returning None") + return None \ No newline at end of file diff --git a/backend/condition/domain/mapping/mapper.py b/backend/condition/domain/mapping/mapper.py index b314e01c..f08fa4e1 100644 --- a/backend/condition/domain/mapping/mapper.py +++ b/backend/condition/domain/mapping/mapper.py @@ -6,6 +6,6 @@ from backend.condition.domain.asset_condition import AssetCondition class Mapper(ABC): @abstractmethod - def map_asset_conditions(self, client_data: List[Any]) -> List[AssetCondition]: + def map_asset_conditions_for_property(self, client_data: Any) -> List[AssetCondition]: #TODO: client_data should be properly typed pass \ No newline at end of file diff --git a/backend/condition/tests/mapping/test_lbwf_mapper.py b/backend/condition/tests/mapping/test_lbwf_mapper.py index e26b9928..3e066d27 100644 --- a/backend/condition/tests/mapping/test_lbwf_mapper.py +++ b/backend/condition/tests/mapping/test_lbwf_mapper.py @@ -233,7 +233,7 @@ def test_lbwf_mapper_maps_house(): ] # act - actual_assets: List[AssetCondition] = mapper.map_asset_conditions(lbwf_house) + actual_assets: List[AssetCondition] = mapper.map_asset_conditions_for_property(lbwf_house) # assert assert actual_assets == expected_assets \ No newline at end of file