diff options
| author | stuppie | 2026-08-17 16:14:21 -0600 |
|---|---|---|
| committer | stuppie | 2026-08-17 16:14:21 -0600 |
| commit | 254312c1cf91dcbf228944afd3932db8b5bde3a3 (patch) | |
| tree | b99ff9ce78296cb4cf08e79fefbed70228358fc9 | |
| parent | 7ff0d6fddb333dd973a827805c0c0cdcded220c7 (diff) | |
| download | generalresearch-254312c1cf91dcbf228944afd3932db8b5bde3a3.tar.gz generalresearch-254312c1cf91dcbf228944afd3932db8b5bde3a3.zip | |
model gr business: prefetch_bp_accounts: During ledger account check, make any that dont exist instead of failingv3.3.9
| -rw-r--r-- | generalresearch/models/gr/business.py | 55 | ||||
| -rw-r--r-- | pyproject.toml | 2 | ||||
| -rw-r--r-- | tests/models/gr/test_business.py | 10 |
3 files changed, 45 insertions, 22 deletions
diff --git a/generalresearch/models/gr/business.py b/generalresearch/models/gr/business.py index 2df7d61..3e6573d 100644 --- a/generalresearch/models/gr/business.py +++ b/generalresearch/models/gr/business.py @@ -194,7 +194,7 @@ class Business(BaseModel): ) tax_number: str | None = Field(default=None, max_length=20) - contact: "BusinessContact" | None = Field(default=None) + contact: "BusinessContact | None" = Field(default=None) # Initialization is deferred until it is actually needed # (see .prefetch_***()) @@ -205,7 +205,7 @@ class Business(BaseModel): # Initialization is deferred until unless it's called # (see .prebuild_***()) - balance: "BusinessBalances" | None = Field(default=None, name="Business Balance") + balance: "BusinessBalances | None" = Field(default=None, name="Business Balance") payouts_total_str: str | None = Field(default=None) payouts_total: USDCent | None = Field(default=None) @@ -299,18 +299,41 @@ class Business(BaseModel): bam = BusinessBankAccountManager(pg_config=pg_config) self.bank_accounts = bam.get_by_business_id(business_id=self.id) - def prefetch_bp_accounts(self, lm: LedgerManager, thl_pg_config: PostgresConfig): + def prefetch_bp_accounts( + self, thl_lm: ThlLedgerManager, thl_pg_config: PostgresConfig + ): # We need to prefetch the Products everytime because there is no way # of knowing if a new Product has been added since the last time it # ran. self.prefetch_products(thl_pg_config=thl_pg_config) + product_lookup = {p.uuid: p for p in self.products} - accounts = lm.get_accounts_if_exists( + accounts = thl_lm.get_accounts_if_exists( qualified_names=[ - f"{lm.currency.value}:bp_wallet:{bpid}" for bpid in self.product_uuids + f"{thl_lm.currency.value}:bp_wallet:{bpid}" + for bpid in self.product_uuids ] ) + # They should exist, but create a wallet account if it doesn't... + bp_account = {a.reference_uuid for a in accounts} + refresh = False + for product_uuid in self.product_uuids: + if product_uuid not in bp_account: + refresh = True + logging.exception( + f"Business {self.uuid} does not have a BP Wallet Account for Product {product_uuid}. Creating..." + ) + product = product_lookup[product_uuid] + thl_lm.get_account_or_create_bp_wallet(product=product) + if refresh: + accounts = thl_lm.get_accounts_if_exists( + qualified_names=[ + f"{thl_lm.currency.value}:bp_wallet:{bpid}" + for bpid in self.product_uuids + ] + ) + assert len(accounts) == len(self.product_uuids) self.bp_accounts = accounts @@ -323,7 +346,7 @@ class Business(BaseModel): lm: "LedgerManager", ds: "GRLDatasets", client: Client, - pop_ledger: "PopLedgerMerge" | None = None, + pop_ledger: "PopLedgerMerge | None" = None, at_timestamp: AwareDatetime | None = None, ) -> None: """ @@ -440,10 +463,10 @@ class Business(BaseModel): def prebuild_pop_financial( self, thl_pg_config: PostgresConfig, - lm: "LedgerManager", + thl_lm: "ThlLedgerManager", ds: "GRLDatasets", client: Client, - pop_ledger: "PopLedgerMerge" | None = None, + pop_ledger: "PopLedgerMerge | None" = None, ) -> None: """This is very similar to the Product POP Financial endpoint; however, it returns more than one item for a single time interval. This is @@ -451,7 +474,7 @@ class Business(BaseModel): financial activity within that time window. """ if self.bp_accounts is None: - self.prefetch_bp_accounts(lm=lm, thl_pg_config=thl_pg_config) + self.prefetch_bp_accounts(thl_lm=thl_lm, thl_pg_config=thl_pg_config) from generalresearch.models.admin.request import ( ReportRequest, @@ -498,7 +521,7 @@ class Business(BaseModel): ds: "GRLDatasets", client: Client, mnt_gr_api: Path, - enriched_session: "EnrichedSessionMerge" | None = None, + enriched_session: "EnrichedSessionMerge | None" = None, ) -> None: self.prefetch_products(thl_pg_config=thl_pg_config) @@ -543,7 +566,7 @@ class Business(BaseModel): ds: "GRLDatasets", client: Client, mnt_gr_api: Path, - enriched_wall: "EnrichedWallMerge" | None = None, + enriched_wall: "EnrichedWallMerge | None" = None, ) -> None: self.prefetch_products(thl_pg_config=thl_pg_config) @@ -621,9 +644,9 @@ class Business(BaseModel): thl_lm: "ThlLedgerManager", bpem: "BusinessPayoutEventManager", mnt_gr_api: Path | str, - pop_ledger: "PopLedgerMerge" | None = None, - enriched_session: "EnrichedSessionMerge" | None = None, - enriched_wall: "EnrichedWallMerge" | None = None, + pop_ledger: "PopLedgerMerge | None" = None, + enriched_session: "EnrichedSessionMerge | None" = None, + enriched_wall: "EnrichedWallMerge | None" = None, ) -> None: LOG.debug(f"Business.set_cache({self.uuid=})") @@ -633,7 +656,7 @@ class Business(BaseModel): self.prefetch_teams(pg_config=pg_config) self.prefetch_products(thl_pg_config=thl_web_rr) self.prefetch_bank_accounts(pg_config=pg_config) - self.prefetch_bp_accounts(lm=lm, thl_pg_config=thl_web_rr) + self.prefetch_bp_accounts(thl_lm=thl_lm, thl_pg_config=thl_web_rr) self.prebuild_balance( thl_pg_config=thl_web_rr, @@ -645,7 +668,7 @@ class Business(BaseModel): self.prebuild_payouts(thl_pg_config=thl_web_rr, thl_lm=thl_lm, bpem=bpem) self.prebuild_pop_financial( thl_pg_config=thl_web_rr, - lm=lm, + thl_lm=thl_lm, ds=ds, client=client, pop_ledger=pop_ledger, diff --git a/pyproject.toml b/pyproject.toml index 9929a76..faa1e15 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "generalresearch" -version = "3.3.8" +version = "3.3.9" description = "Python Utilities for General Research" readme = "README.md" requires-python = ">=3.8" diff --git a/tests/models/gr/test_business.py b/tests/models/gr/test_business.py index 9a7718d..0b78374 100644 --- a/tests/models/gr/test_business.py +++ b/tests/models/gr/test_business.py @@ -134,7 +134,7 @@ class TestBusiness: assert "Not Loaded" in res2 business.prefetch_products(thl_pg_config=thl_web_rr) - business.prefetch_bp_accounts(lm=lm, thl_pg_config=thl_web_rr) + business.prefetch_bp_accounts(thl_lm=thl_lm, thl_pg_config=thl_web_rr) res3 = str(business) assert "Products: 2" in res3 assert "Ledger Accounts: 2" in res3 @@ -376,7 +376,7 @@ class TestBusiness: self, business, thl_web_rr, - lm, + thl_lm, mnt_filepath, client_no_amm, pop_ledger_merge, @@ -384,7 +384,7 @@ class TestBusiness: assert business.pop_financial is None business.prebuild_pop_financial( thl_pg_config=thl_web_rr, - lm=lm, + thl_lm=thl_lm, ds=mnt_filepath, client=client_no_amm, pop_ledger=pop_ledger_merge, @@ -393,7 +393,7 @@ class TestBusiness: def test_bp_accounts(self, business, lm, thl_web_rr, product_factory, thl_lm): assert business.bp_accounts is None - business.prefetch_bp_accounts(lm=lm, thl_pg_config=thl_web_rr) + business.prefetch_bp_accounts(thl_lm=thl_lm, thl_pg_config=thl_web_rr) assert business.bp_accounts == [] from generalresearch.models.thl.product import Product @@ -401,7 +401,7 @@ class TestBusiness: p1: Product = product_factory(business=business) thl_lm.get_account_or_create_bp_wallet(product=p1) - business.prefetch_bp_accounts(lm=lm, thl_pg_config=thl_web_rr) + business.prefetch_bp_accounts(thl_lm=thl_lm, thl_pg_config=thl_web_rr) assert len(business.bp_accounts) == 1 |
