added company information

This commit is contained in:
Jun-te Kim 2026-03-24 15:34:33 +00:00
parent e01b7225bb
commit a362e1dd99
3 changed files with 82 additions and 0 deletions

View file

@ -0,0 +1,13 @@
from sqlmodel import SQLModel, Field
from datetime import datetime, timezone
from typing import Optional
import uuid
class Organisation(SQLModel, table=True):
__tablename__ = "organisation"
id: uuid.UUID = Field(default_factory=uuid.uuid4, primary_key=True)
created_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
updated_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
hubspot_company_id: Optional[str] = None
name: Optional[str] = None

View file

@ -0,0 +1,50 @@
from backend.app.db.connection import db_session
from backend.app.db.models.organisation import Organisation
from sqlmodel import select
from datetime import datetime, timezone
from typing import TypedDict
class CompanyData(TypedDict):
hs_object_id: str
name: str
class HubspotDataToDb:
def __init__(self):
pass
def read_org_table(self, limit: int = 10):
with db_session() as session:
records = session.exec(select(Organisation).limit(limit)).all()
return records
def upsert_company(self, company_data: CompanyData) -> Organisation:
"""Upserts a company record. Updates if hubspot_company_id exists, otherwise creates new."""
with db_session() as session:
hubspot_id = company_data.get("hs_object_id")
company_name = company_data.get("name")
# Check if company already exists
existing = session.exec(
select(Organisation).where(
Organisation.hubspot_company_id == hubspot_id
)
).first()
if existing:
# Update existing record
existing.name = company_name
existing.updated_at = datetime.now(timezone.utc)
session.add(existing)
record = existing
else:
# Create new record
record = Organisation(
hubspot_company_id=hubspot_id,
name=company_name,
)
session.add(record)
session.commit()
return record

View file

@ -0,0 +1,19 @@
from etl.hubspot.hubspotClient import HubspotClient, Companies
from etl.hubspot.hubspotDataTodB import HubspotDataToDb
hubspot = HubspotClient()
companies_to_add_or_ensure_it_exists = [
Companies.THE_GUINESS_PARTNERSHIP,
Companies.SOUTHERN_HOUSING_GROUP,
]
for company in companies_to_add_or_ensure_it_exists:
company_info = hubspot.get_company_information(company.value)
company_info
break
dbRead = HubspotDataToDb()
dbRead.read_org_table()