From c174fa2ffe3e02338560af422b6285bbf4c43ff7 Mon Sep 17 00:00:00 2001 From: Inferencebot Date: Fri, 13 Nov 2026 20:20:40 +0000 Subject: [PATCH] reconcile: link a row to its member by slug OR email MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Both routes are needed. A manual grant often carries no reference at all, so the row's EMAIL is the only link to its member — and the slug and email local part routinely differ ('henry-smith' vs 'h.smith@x.org'). The membership test was slug-first and, when the slug matched neither the reference nor the email, dropped the row outright: a hand-credited purchase therefore reads as a gap (ntnsndr's 2026-09-28 packs, where the ledger carries +20.00, +5.75 and a -10.75 correction with no references). _row_belongs_to() accepts either route, in both the accounted-for test and the nearby-rows context. 38 checks; all 18 suites green. --- app/main.py | 30 +++++++++++++++++++++-------- tests/test_credit_pack_reconcile.py | 13 +++++++++++++ 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/app/main.py b/app/main.py index db45ca7..d605a8d 100644 --- a/app/main.py +++ b/app/main.py @@ -7741,7 +7741,23 @@ def _parse_ts(s) -> datetime | None: return d if d.tzinfo else d.replace(tzinfo=timezone.utc) -def credit_pack_accounted(order: dict, ledger_rows: list, held_rows: list) -> str | None: +def _row_belongs_to(r: dict, slug: str, email: str) -> bool: + """Does a ledger/held row identify this member — by slug in the reference, or by email? + + Both routes matter: a manual grant often carries NO reference at all, and the + only link is the row's email. Requiring the slug to appear would drop those + rows (the slug and the email local part frequently differ), which is how a + hand-credited purchase comes to look like a gap. + """ + ref = (r.get("reference") or r.get("oc_reference") or "").lower() + em = (r.get("email") or "").lower() + slug_hit = bool(slug) and (slug.lower() in ref or slug.lower() in em) + email_hit = bool(email) and email.lower() in em + return slug_hit or email_hit + + +def credit_pack_accounted(order: dict, ledger_rows: list, held_rows: list, + member_email: str = "") -> str | None: """Evidence that this order is ALREADY in our books, else None. The reference is NOT uniform across our own rows, and a bare @@ -7778,8 +7794,7 @@ def credit_pack_accounted(order: dict, ledger_rows: list, held_rows: list) -> st for rows, what in ((ledger_rows, "ledger"), (held_rows, "held")): for r in rows: ref = (r.get("reference") or r.get("oc_reference") or "") - email = (r.get("email") or "").lower() - if slug and slug.lower() not in ref.lower() and slug.lower() not in email: + if not _row_belongs_to(r, slug, member_email): continue try: amt = float(r.get("delta") if what == "ledger" else r.get("amount") or 0) @@ -7812,10 +7827,7 @@ def _nearby_book_rows(slug: str, email: str, when: datetime | None, ledger_rows: for kind, rows in (("ledger", ledger_rows), ("held", held_rows)): for r in rows: ref = (r.get("reference") or r.get("oc_reference") or "") - em = (r.get("email") or "").lower() - if slug and slug.lower() not in ref.lower() and slug.lower() not in em: - continue - if email and email.lower() not in em: + if not _row_belongs_to(r, slug, email): continue rt = _parse_ts(r.get("created_at")) if rt and abs((rt - when).total_seconds()) <= days * 86400: @@ -7892,7 +7904,9 @@ async def reconcile_credit_packs(apply: bool = False) -> dict: if when and when < cutoff: continue packs += 1 - if not credit_pack_accounted(o, ledger_rows, held_rows): + slug = ((o.get("fromAccount") or {}).get("slug") or "").strip() + if not credit_pack_accounted(o, ledger_rows, held_rows, + member_email=slug2email.get(slug, "")): unaccounted.append(o) acting = bool(apply and CREDIT_RECONCILE == "on") diff --git a/tests/test_credit_pack_reconcile.py b/tests/test_credit_pack_reconcile.py index b176cd6..5a23a5c 100644 --- a/tests/test_credit_pack_reconcile.py +++ b/tests/test_credit_pack_reconcile.py @@ -213,6 +213,19 @@ async def main(): m.credit_pack_accounted(next(x for x in ORDERS if x["id"] == "or_CCC"), ledger_rows(), []) is None) + # A manual grant with NO reference links to its member by EMAIL, and the slug + # and email local part routinely differ ("henry-smith" vs "h.smith@x.org"). + # Matching only on the slug in the reference drops that row — which is how a + # hand-credited purchase comes to look like a gap. + henry = order("or_HH2", "henry-smith", 12, 1.0) + henry_rows = [{"email": "h.smith@x.org", "delta": 12.0, "reference": None, + "created_at": stamp(1.0), "reason": "manual application"}] + check("a reference-less manual grant is matched via the MEMBER EMAIL", + m.credit_pack_accounted(henry, henry_rows, [], + member_email="h.smith@x.org") is not None) + check("...and is NOT matched without it (the slug alone finds nothing)", + m.credit_pack_accounted(henry, henry_rows, []) is None) + # ---------- a read that fails is UNKNOWN, never 'nothing missing' --------- FAIL_ORDERS = True res = await m.reconcile_credit_packs(apply=False)