diff --git a/etl/hubspot/hubspotClient.py b/etl/hubspot/hubspotClient.py index ed456478..c87ea872 100644 --- a/etl/hubspot/hubspotClient.py +++ b/etl/hubspot/hubspotClient.py @@ -25,6 +25,7 @@ from hubspot.crm.associations.v4.models import ( # type: ignore[reportMissingTy ForwardPaging as AssociationsPaging, NextPage as AssociationsPagingNext, ) +from etl.hubspot.hubspotDataTodB import CompanyData from backend.app.config import get_settings @@ -223,7 +224,7 @@ class HubspotClient: return deal, company, listing - def get_company_information(self, company_id: str) -> dict[str, str]: + def get_company_information(self, company_id: str) -> CompanyData: companies_api: CompaniesBasicApi = self.client.crm.companies.basic_api # type: ignore[reportUnknownMemberType] company: HubspotObject = companies_api.get_by_id( # type: ignore[reportUnknownMemberType] @@ -233,7 +234,7 @@ class HubspotClient: ], ) - company_info: dict[str, str] = cast(dict[str, str], company.properties) # type: ignore[reportUnknownMemberType] + company_info: CompanyData = company.properties # type: ignore[reportUnknownMemberType] return company_info def get_all_pipelines(self) -> list[dict[str, str]]: diff --git a/etl/hubspot/hubspotDataTodB.py b/etl/hubspot/hubspotDataTodB.py index 24df240e..8fe61a3e 100644 --- a/etl/hubspot/hubspotDataTodB.py +++ b/etl/hubspot/hubspotDataTodB.py @@ -1,4 +1,4 @@ -from backend.app.db.connection import db_session +from backend.app.db.connection import db_read_session from backend.app.db.models.organisation import Organisation from sqlmodel import select from datetime import datetime, timezone @@ -15,13 +15,18 @@ class HubspotDataToDb: pass def read_org_table(self, limit: int = 10): - with db_session() as session: + with db_read_session() as session: records = session.exec(select(Organisation).limit(limit)).all() return records + def get_org_names(self, limit: int = 10) -> list[str]: + """Returns a list of organisation names.""" + records = self.read_org_table(limit) + return [org.name for org in records if org.name] + 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: + with db_read_session() as session: hubspot_id = company_data.get("hs_object_id") company_name = company_data.get("name") diff --git a/etl/hubspot/scripts/onboarding/new_organisation.py b/etl/hubspot/scripts/onboarding/new_organisation.py index f5faead3..5a11266f 100644 --- a/etl/hubspot/scripts/onboarding/new_organisation.py +++ b/etl/hubspot/scripts/onboarding/new_organisation.py @@ -1,19 +1,19 @@ from etl.hubspot.hubspotClient import HubspotClient, Companies -from etl.hubspot.hubspotDataTodB import HubspotDataToDb +from etl.hubspot.hubspotDataTodB import HubspotDataToDb, CompanyData hubspot = HubspotClient() - +dbRead = HubspotDataToDb() 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 + company_info: CompanyData = hubspot.get_company_information(company.value) + dbRead.upsert_company(company_info) + dbRead = HubspotDataToDb() - -dbRead.read_org_table() +names = dbRead.get_org_names() +print(f"Organisations in database: {names}")