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) 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. """Evidence that this order is ALREADY in our books, else None.
The reference is NOT uniform across our own rows, and a bare 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 rows, what in ((ledger_rows, "ledger"), (held_rows, "held")):
for r in rows: for r in rows:
ref = (r.get("reference") or r.get("oc_reference") or "") ref = (r.get("reference") or r.get("oc_reference") or "")
email = (r.get("email") or "").lower() if not _row_belongs_to(r, slug, member_email):
if slug and slug.lower() not in ref.lower() and slug.lower() not in email:
continue continue
try: try:
amt = float(r.get("delta") if what == "ledger" else r.get("amount") or 0) 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 kind, rows in (("ledger", ledger_rows), ("held", held_rows)):
for r in rows: for r in rows:
ref = (r.get("reference") or r.get("oc_reference") or "") ref = (r.get("reference") or r.get("oc_reference") or "")
em = (r.get("email") or "").lower() if not _row_belongs_to(r, slug, email):
if slug and slug.lower() not in ref.lower() and slug.lower() not in em:
continue
if email and email.lower() not in em:
continue continue
rt = _parse_ts(r.get("created_at")) rt = _parse_ts(r.get("created_at"))
if rt and abs((rt - when).total_seconds()) <= days * 86400: 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: if when and when < cutoff:
continue continue
packs += 1 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) unaccounted.append(o)
acting = bool(apply and CREDIT_RECONCILE == "on") acting = bool(apply and CREDIT_RECONCILE == "on")
+13
View File
@@ -213,6 +213,19 @@ async def main():
m.credit_pack_accounted(next(x for x in ORDERS if x["id"] == "or_CCC"), m.credit_pack_accounted(next(x for x in ORDERS if x["id"] == "or_CCC"),
ledger_rows(), []) is None) 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' --------- # ---------- a read that fails is UNKNOWN, never 'nothing missing' ---------
FAIL_ORDERS = True FAIL_ORDERS = True
res = await m.reconcile_credit_packs(apply=False) res = await m.reconcile_credit_packs(apply=False)