Model/etl/hubspot/scripts/scraper/main.py
2026-04-09 10:32:59 +00:00

84 lines
2.7 KiB
Python

from backend.app.db.models.organisation import HubspotDealData
from etl.hubspot.hubspotClient import HubspotClient
from etl.hubspot.hubspotDataTodB import CompanyData, HubspotDataToDb
from backend.utils.subtasks import task_handler
from typing import Any, Dict, Optional
from etl.hubspot.hubspot_deal_differ import HubspotDealDiffer
from etl.hubspot.hubspot_trigger_orchestrator_trigger_request import (
HubspotTriggerOrchestratorTriggerRequest,
)
@task_handler()
def handler(body: dict[str, Any], context: Any) -> None:
db_client = HubspotDataToDb()
hubspot_client = HubspotClient()
payload = HubspotTriggerOrchestratorTriggerRequest.model_validate(body)
hubspot_deal_id: str = payload.hubspot_deal_id
db_deal: Optional[HubspotDealData] = db_client.find_deal_with_deal_id(
hubspot_deal_id
)
hubspot_deal: Dict[str, str]
company: Optional[str]
listing: Optional[dict[str, str]]
hubspot_deal, company, listing = hubspot_client.get_deal_and_company_and_listing(
hubspot_deal_id
)
if not db_deal:
# New hubspot deal, no diffing to do
if company:
company_data: CompanyData = hubspot_client.get_company_information(company)
db_client: HubspotDataToDb = HubspotDataToDb()
db_client.upsert_company(company_data)
db_client.upsert_deal(hubspot_deal, company, listing, hubspot_client)
return
if HubspotDealDiffer.check_for_db_update_trigger(
new_deal=hubspot_deal,
new_company=company,
new_listing=listing,
old_deal=db_deal,
):
db_client.update_deal_with_checks(
deal_in_db=db_deal,
hubspot_client=hubspot_client,
hs_deal=hubspot_deal,
hs_company_id=company,
hs_listing=listing,
)
return
# ==============================
# Orchestration of other lambdas
# ==============================
if HubspotDealDiffer.check_for_pashub_trigger(
new_deal=hubspot_deal, old_deal=db_deal
):
# TODO: trigger pashub file fetcher
return
# if db_deal:
# db_client.update_deal_with_checks(db_deal, hubspot_client)
# else:
# hubspot_deal: Dict[str, str]
# company: Optional[str]
# listing: Optional[dict[str, str]]
# hubspot_deal, company, listing = (
# hubspot_client.get_deal_and_company_and_listing(hubspot_deal_id)
# )
# if company:
# company_data: CompanyData = hubspot_client.get_company_information(company)
# db_client: HubspotDataToDb = HubspotDataToDb()
# db_client.upsert_company(company_data)
# db_client.upsert_deal(hubspot_deal, company, listing, hubspot_client)