mirror of
https://github.com/Hestia-Homes/Model.git
synced 2026-06-08 11:17:27 +00:00
61 lines
1.9 KiB
Python
61 lines
1.9 KiB
Python
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
|