mirror of
https://github.com/Hestia-Homes/Model.git
synced 2026-06-08 11:17:27 +00:00
added company information
This commit is contained in:
parent
e01b7225bb
commit
a362e1dd99
3 changed files with 82 additions and 0 deletions
13
backend/app/db/models/organisation.py
Normal file
13
backend/app/db/models/organisation.py
Normal 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
|
||||
50
etl/hubspot/hubspotDataTodB.py
Normal file
50
etl/hubspot/hubspotDataTodB.py
Normal 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
|
||||
19
etl/hubspot/scripts/onboarding/new_organisation.py
Normal file
19
etl/hubspot/scripts/onboarding/new_organisation.py
Normal 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()
|
||||
Loading…
Add table
Reference in a new issue