mirror of
https://github.com/Hestia-Homes/Model.git
synced 2026-06-08 11:17:27 +00:00
initial setup
This commit is contained in:
parent
89ad1c9050
commit
942c2923da
5 changed files with 96 additions and 7 deletions
61
backend/hubspot_trigger_orchestrator/handler.py
Normal file
61
backend/hubspot_trigger_orchestrator/handler.py
Normal file
|
|
@ -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
|
||||
21
backend/hubspot_trigger_orchestrator/hubspot_deal_differ.py
Normal file
21
backend/hubspot_trigger_orchestrator/hubspot_deal_differ.py
Normal file
|
|
@ -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
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class HubspotTriggerOrchestratorTriggerRequest(BaseModel):
|
||||
hubspot_deal_id: str
|
||||
|
|
@ -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"),
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue