diff --git a/backend/hubspot_trigger_orchestrator/handler.py b/backend/hubspot_trigger_orchestrator/handler.py new file mode 100644 index 00000000..1f83ed80 --- /dev/null +++ b/backend/hubspot_trigger_orchestrator/handler.py @@ -0,0 +1,61 @@ +import json +from typing import Any, Dict, Mapping, Optional + +from backend.app.db.models.organisation import HubspotDealData +from backend.hubspot_trigger_orchestrator.hubspot_deal_differ import HubspotDealDiffer +from backend.hubspot_trigger_orchestrator.hubspot_trigger_orchestrator_trigger_request import ( + HubspotTriggerOrchestratorTriggerRequest, +) +from backend.utils.subtasks import task_handler +from etl.hubspot.hubspotClient import HubspotClient +from etl.hubspot.hubspotDataTodB import HubspotDataToDb +from utils.logger import setup_logger + +logger = setup_logger() + + +@task_handler() +def handler(event: Mapping[str, Any], context: Any) -> None: + + db_client = HubspotDataToDb() + hubspot_client = HubspotClient() + + for record in event.get("Records", []): + body_dict = json.loads(record["body"]) + + logger.debug("Validating request body") + payload = HubspotTriggerOrchestratorTriggerRequest.model_validate(body_dict) + logger.debug("Successfully validated request body") + + hubspot_deal_id: str = payload.hubspot_deal_id + + db_deal: Optional[HubspotDealData] = db_client.find_deal_with_deal_id( + hubspot_deal_id + ) + if not db_deal: + # new hubspot deal, no diffing to do + # TODO: trigger hubspot to db ETL + return + + hubspot_deal: Dict[str, str] + company: Optional[str] + listing: Optional[dict[str, str]] + + hubspot_deal, company, listing = hubspot_client.get_deal_info_for_db( + hubspot_deal_id + ) + + if HubspotDealDiffer.check_for_pashub_trigger( + new_deal=hubspot_deal, old_deal=db_deal + ): + # TODO: trigger pashub file fetcher + return + + if HubspotDealDiffer.check_for_db_update_trigger( + new_deal=hubspot_deal, + new_company=company, + new_listing=listing, + old_deal=db_deal, + ): + # TODO: trigger db upsert + return diff --git a/backend/hubspot_trigger_orchestrator/hubspot_deal_differ.py b/backend/hubspot_trigger_orchestrator/hubspot_deal_differ.py new file mode 100644 index 00000000..9d66c637 --- /dev/null +++ b/backend/hubspot_trigger_orchestrator/hubspot_deal_differ.py @@ -0,0 +1,21 @@ +from typing import Dict, Optional + +from backend.app.db.models.organisation import HubspotDealData + + +class HubspotDealDiffer: + + @staticmethod + def check_for_pashub_trigger( + new_deal: Dict[str, str], old_deal: HubspotDealData + ) -> bool: + raise NotImplementedError + + @staticmethod + def check_for_db_update_trigger( + new_deal: Dict[str, str], + new_company: Optional[str], + new_listing: Optional[Dict[str, str]], + old_deal: HubspotDealData, + ) -> bool: + raise NotImplementedError diff --git a/backend/hubspot_trigger_orchestrator/hubspot_trigger_orchestrator_trigger_request.py b/backend/hubspot_trigger_orchestrator/hubspot_trigger_orchestrator_trigger_request.py new file mode 100644 index 00000000..1adfa07c --- /dev/null +++ b/backend/hubspot_trigger_orchestrator/hubspot_trigger_orchestrator_trigger_request.py @@ -0,0 +1,5 @@ +from pydantic import BaseModel + + +class HubspotTriggerOrchestratorTriggerRequest(BaseModel): + hubspot_deal_id: str diff --git a/etl/hubspot/hubspotDataTodB.py b/etl/hubspot/hubspotDataTodB.py index 6325efc2..36167bf0 100644 --- a/etl/hubspot/hubspotDataTodB.py +++ b/etl/hubspot/hubspotDataTodB.py @@ -74,7 +74,7 @@ class HubspotDataToDb: .all() ) - def find_deal_with_deal_id(self, deal_id): + def find_deal_with_deal_id(self, deal_id: str) -> Optional[HubspotDealData]: with db_read_session() as session: return ( session.query(HubspotDealData) @@ -477,7 +477,9 @@ class HubspotDataToDb: dealname=deal_data.get("dealname"), dealstage=deal_data.get("dealstage"), listing_id=listing.get("listing_id", None) if listing else None, - landlord_property_id=listing.get("owner_property_id") if listing else None, + landlord_property_id=( + listing.get("owner_property_id") if listing else None + ), uprn=listing.get("national_uprn") if listing else None, outcome=deal_data.get("outcome"), outcome_notes=deal_data.get("outcome_notes"), diff --git a/etl/hubspot/scripts/scraper/main.py b/etl/hubspot/scripts/scraper/main.py index 4f71c6d0..a003ad28 100644 --- a/etl/hubspot/scripts/scraper/main.py +++ b/etl/hubspot/scripts/scraper/main.py @@ -16,9 +16,9 @@ def handler(body: dict[str, Any], context: Any) -> None: hubspot: HubspotClient = HubspotClient() dbloader: HubspotDataToDb = HubspotDataToDb() - deal = dbloader.find_deal_with_deal_id(hubspot_deal_id) - if deal: - dbloader.update_deal_with_checks(deal, hubspot) + db_deal = dbloader.find_deal_with_deal_id(hubspot_deal_id) + if db_deal: + dbloader.update_deal_with_checks(db_deal, hubspot) else: - deal, company, listing = hubspot.get_deal_info_for_db(hubspot_deal_id) - dbloader.upsert_deal(deal, company, listing, hubspot) + hubspot_deal, company, listing = hubspot.get_deal_info_for_db(hubspot_deal_id) + dbloader.upsert_deal(hubspot_deal, company, listing, hubspot)