From 5281c1d79b10593db8fa0479b564dcb1f62dcad6 Mon Sep 17 00:00:00 2001 From: gti Date: Sat, 26 Sep 2026 10:12:16 -0300 Subject: [PATCH] fix: OSCBR backfill sends GTIN-8 in its canonical 8-digit form --- central/app/enrichment/backfill.py | 11 ++++++++++- central/tests/test_oscbr_backfill.py | 6 ++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/central/app/enrichment/backfill.py b/central/app/enrichment/backfill.py index 0e6b69d..3405670 100644 --- a/central/app/enrichment/backfill.py +++ b/central/app/enrichment/backfill.py @@ -78,6 +78,15 @@ def apply_outcome(session, gtin_key: str, provider: str, outcome, is_official) - return rec +def provider_query(key: str) -> str: + """Canonical form sent to providers: GTIN-8 stays 8 digits, UPC-12/EAN-13 as 13 digits (same rule as the resolver), GTIN-14 with an indicator digit stays 14.""" + if len(key) == 14 and key.startswith("000000"): + return key[-8:] + if len(key) == 14 and key.startswith("0"): + return key[1:] + return key + + def run(session, gateway, provider, *, limit: int, reserve_percent: int, is_official, dry_run: bool = False, wait_rate_window: bool = True) -> dict: """One backfill pass. dry_run only lists the sample (no request).""" sample = select_sample(session, is_official, limit, provider.name) @@ -86,7 +95,7 @@ def run(session, gateway, provider, *, limit: int, reserve_percent: int, is_offi report["records"] = [{k: v for k, v in s.items() if k != "product_id"} for s in sample] return report for item in sample: - query = item["gtin_key"][1:] if item["gtin_key"].startswith("0") and len(item["gtin_key"]) == 14 else item["gtin_key"] + query = provider_query(item["gtin_key"]) outcome = gateway.lookup(provider, query, "BACKFILL", reserve_percent) while outcome.state == "SKIPPED" and outcome.reason == "RATE_WINDOW" and wait_rate_window: # per-minute governor: wait for the window, never exceed it time.sleep(max(1, min(65, int((outcome.retry_at - _now()).total_seconds()) + 1)) if outcome.retry_at else 12) diff --git a/central/tests/test_oscbr_backfill.py b/central/tests/test_oscbr_backfill.py index bb0e27a..7248e53 100644 --- a/central/tests/test_oscbr_backfill.py +++ b/central/tests/test_oscbr_backfill.py @@ -102,3 +102,9 @@ def test_cosmos_out_of_chain_and_token_preferred(): assert "cosmos" not in names and names[0] == "oscbr" and build_providers(S())[0].configured S.cosmos_enabled = True assert build_providers(S())[0].name == "cosmos" + + +def test_provider_query_canonical_forms(): + assert backfill.provider_query("00000078936683") == "78936683" # GTIN-8 + assert backfill.provider_query("07891000100103") == "7891000100103" # EAN-13 + assert backfill.provider_query("17891000100100") == "17891000100100" # GTIN-14 with indicator