reconcile: link a row to its member by slug OR email
ci/woodpecker/push/woodpecker Pipeline was successful

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.
This commit is contained in:
inference-admin committed 2026-11-13 20:20:40 +00:00
1 parent 711371f9d7
commit c174fa2ffe
2 files changed
+35 -8

No files matched your search

+22 -8
View File
@@ -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")