diff --git a/etl/hubspot/hubspot_deal_differ.py b/etl/hubspot/hubspot_deal_differ.py index 6d49cf590..68b075182 100644 --- a/etl/hubspot/hubspot_deal_differ.py +++ b/etl/hubspot/hubspot_deal_differ.py @@ -1,6 +1,7 @@ from typing import Dict, List, Optional from backend.app.db.models.hubspot_deal_data import HubspotDealData +from etl.hubspot.project_data import ProjectData from etl.hubspot.utils import parse_hs_bool, parse_hs_date @@ -12,6 +13,8 @@ class HubspotDealDiffer: ] RETROFIT_DESIGN_COMPLETE = "uploaded" LODGEMENT_COMPLETE: List[str] = ["lodgement complete", "measures lodged"] + ABRI_CONDITION_ASSOCIATED_PROJECT_ID = "123456" + ABRI_CONDITION_PROJECT_CODE = "Abri Condition" @staticmethod def check_for_db_update_trigger( @@ -170,6 +173,33 @@ class HubspotDealDiffer: return False + @staticmethod + def check_for_abri_survey_creation( + new_deal: Dict[str, str], + new_project: Optional[ProjectData], + old_deal: HubspotDealData, + ) -> bool: + if not HubspotDealDiffer._is_abri_condition_deal(new_deal, new_project): + return False + + if old_deal.confirmed_survey_date is not None: + return False + + return parse_hs_date(new_deal.get("confirmed_survey_date")) is not None + + @staticmethod + def _is_abri_condition_deal( + new_deal: Dict[str, str], new_project: Optional[ProjectData] + ) -> bool: + if new_project is not None: + return ( + new_project["project_id"] + == HubspotDealDiffer.ABRI_CONDITION_ASSOCIATED_PROJECT_ID + ) + + new_project_code = (new_deal.get("project_code") or "").lower() + return new_project_code == HubspotDealDiffer.ABRI_CONDITION_PROJECT_CODE.lower() + @staticmethod def check_for_magicplan_trigger( new_deal: Dict[str, str], old_deal: HubspotDealData diff --git a/etl/hubspot/tests/test_hubspot_deal_differ.py b/etl/hubspot/tests/test_hubspot_deal_differ.py index 949524243..a072b175f 100644 --- a/etl/hubspot/tests/test_hubspot_deal_differ.py +++ b/etl/hubspot/tests/test_hubspot_deal_differ.py @@ -6,6 +6,7 @@ import pytest from backend.app.db.models.hubspot_deal_data import HubspotDealData from etl.hubspot.hubspot_deal_differ import HubspotDealDiffer +from etl.hubspot.project_data import ProjectData BASE_TIME = datetime(2025, 12, 1, 12, 0, 0) @@ -343,6 +344,210 @@ def test_magicplan_trigger__outcome_surveyed_uppercase__returns_true() -> None: assert result is True +# ================================== +# ABRI SURVEY CREATION TRIGGER TESTS +# ================================== + + +def test_abri_survey_creation__date_first_set_on_abri_deal__returns_true() -> None: + deal_id = uuid.uuid4() + + # Arrange + old_deal = make_old_deal( + id=deal_id, + project_code="Abri Condition", + confirmed_survey_date=None, + ) + new_deal = make_new_deal( + deal_id, + project_code="Abri Condition", + confirmed_survey_date="2026-07-15", + ) + + # Act + result = HubspotDealDiffer.check_for_abri_survey_creation( + new_deal=new_deal, + new_project=None, + old_deal=old_deal, + ) + + # Assert + assert result is True + + +def test_abri_survey_creation__date_already_set__returns_false() -> None: + deal_id = uuid.uuid4() + + # Arrange + old_deal = make_old_deal( + id=deal_id, + project_code="Abri Condition", + confirmed_survey_date=datetime(2026, 7, 1, tzinfo=timezone.utc), + ) + new_deal = make_new_deal( + deal_id, + project_code="Abri Condition", + confirmed_survey_date="2026-07-15", + ) + + # Act + result = HubspotDealDiffer.check_for_abri_survey_creation( + new_deal=new_deal, + new_project=None, + old_deal=old_deal, + ) + + # Assert + assert result is False + + +def test_abri_survey_creation__non_abri_project_code__returns_false() -> None: + deal_id = uuid.uuid4() + + # Arrange + old_deal = make_old_deal( + id=deal_id, + project_code="Southern Retrofit", + confirmed_survey_date=None, + ) + new_deal = make_new_deal( + deal_id, + project_code="Southern Retrofit", + confirmed_survey_date="2026-07-15", + ) + + # Act + result = HubspotDealDiffer.check_for_abri_survey_creation( + new_deal=new_deal, + new_project=None, + old_deal=old_deal, + ) + + # Assert + assert result is False + + +def test_abri_survey_creation__project_code_cased_differently__returns_true() -> None: + deal_id = uuid.uuid4() + + # Arrange + old_deal = make_old_deal( + id=deal_id, + project_code="ABRI CONDITION", + confirmed_survey_date=None, + ) + new_deal = make_new_deal( + deal_id, + project_code="ABRI CONDITION", + confirmed_survey_date="2026-07-15", + ) + + # Act + result = HubspotDealDiffer.check_for_abri_survey_creation( + new_deal=new_deal, + new_project=None, + old_deal=old_deal, + ) + + # Assert + assert result is True + + +@pytest.mark.parametrize( + "new_overrides", + [ + {}, + {"confirmed_survey_date": ""}, + {"confirmed_survey_date": "not-a-date"}, + ], +) +def test_abri_survey_creation__no_parseable_new_date__returns_false( + new_overrides: Dict[str, str], +) -> None: + deal_id = uuid.uuid4() + + # Arrange + old_deal = make_old_deal( + id=deal_id, + project_code="Abri Condition", + confirmed_survey_date=None, + ) + new_deal = make_new_deal( + deal_id, + project_code="Abri Condition", + **new_overrides, + ) + + # Act + result = HubspotDealDiffer.check_for_abri_survey_creation( + new_deal=new_deal, + new_project=None, + old_deal=old_deal, + ) + + # Assert + assert result is False + + +def test_abri_survey_creation__associated_abri_project_id__returns_true() -> None: + deal_id = uuid.uuid4() + + # Arrange + old_deal = make_old_deal( + id=deal_id, + project_code="Southern Retrofit", + confirmed_survey_date=None, + ) + new_deal = make_new_deal( + deal_id, + project_code="Southern Retrofit", + confirmed_survey_date="2026-07-15", + ) + new_project = ProjectData( + project_id=HubspotDealDiffer.ABRI_CONDITION_ASSOCIATED_PROJECT_ID, + name="Abri Condition", + ) + + # Act + result = HubspotDealDiffer.check_for_abri_survey_creation( + new_deal=new_deal, + new_project=new_project, + old_deal=old_deal, + ) + + # Assert + assert result is True + + +def test_abri_survey_creation__non_abri_project_id_with_abri_code__returns_false() -> ( + None +): + deal_id = uuid.uuid4() + + # Arrange + old_deal = make_old_deal( + id=deal_id, + project_code="Abri Condition", + confirmed_survey_date=None, + ) + new_deal = make_new_deal( + deal_id, + project_code="Abri Condition", + confirmed_survey_date="2026-07-15", + ) + new_project = ProjectData(project_id="999999", name="Southern Retrofit") + + # Act + result = HubspotDealDiffer.check_for_abri_survey_creation( + new_deal=new_deal, + new_project=new_project, + old_deal=old_deal, + ) + + # Assert + assert result is False + + # ======================= # DB UPDATE TRIGGER TESTS # =======================