orders: read every page, never a fixed 200-order window
The collective crossed 200 orders on 2026-11-23 (203). fetch_member_orders() asked for orders(limit: 200) in one request, so the orders beyond the window — the OLDEST ones, where long-standing members' recurring contributions live — were absent from the sweep's read. Three paying members (ntnsndr, elizabeth-barry1, guest-0f6d98ac) therefore read as "no recurring (monthly) contribution on Open Collective" and were sent a payment reminder at 03:02 UTC for contributions that were live and charged on 2 Nov. fetch_oc_monthly_order_charges() had the same fixed window, hiding the oldest orders' charge ladders from the shortfall credit pass. Both now page by offset until OC's totalCount is covered, and a read that falls short of totalCount RAISES instead of returning a short list: a short list is the exact shape that reads as "nobody is paying".
This commit is contained in:
1 parent
f09bec67d8
commit
c60dc2c482
2 files changed
+244
-39
No files matched your search
+74
-39
@@ -8088,32 +8088,49 @@ async def fetch_member_orders() -> dict[str, list[dict]]:
|
||||
"""
|
||||
if not OC_PERSONAL_TOKEN:
|
||||
raise RuntimeError("OC_PERSONAL_TOKEN not set; cannot fetch orders")
|
||||
query = (
|
||||
'{ collective(slug: "%s") { orders(limit: 200) { totalCount nodes { '
|
||||
per_page = 100
|
||||
max_pages = 20 # 2,000 orders — a runaway loop is worse than a short read
|
||||
query_tpl = (
|
||||
'{ collective(slug: "%s") { orders(limit: %d, offset: %d) { totalCount nodes { '
|
||||
"frequency status createdAt lastChargedAt nextChargeDate "
|
||||
"fromAccount { slug } amount { value currency } totalAmount { value currency } } } } }"
|
||||
% OC_COLLECTIVE_SLUG
|
||||
)
|
||||
async with httpx.AsyncClient(timeout=30.0) as client:
|
||||
r = await client.post(
|
||||
OC_GRAPHQL_URL,
|
||||
headers={"Content-Type": "application/json",
|
||||
"Authorization": f"Bearer {OC_PERSONAL_TOKEN}",
|
||||
"Api-Version": "2022-01-01",
|
||||
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64)"},
|
||||
json={"query": query},
|
||||
)
|
||||
if r.status_code != 200:
|
||||
raise RuntimeError(f"OC order fetch failed: HTTP {r.status_code}")
|
||||
body = r.json()
|
||||
if body.get("errors"):
|
||||
raise RuntimeError(f"OC order fetch errors: {str(body['errors'])[:200]}")
|
||||
orders = (((body.get("data") or {}).get("collective") or {}).get("orders") or {})
|
||||
by_slug: dict[str, list[dict]] = {}
|
||||
for o in orders.get("nodes") or []:
|
||||
slug = ((o.get("fromAccount") or {}).get("slug") or "").strip()
|
||||
if slug:
|
||||
by_slug.setdefault(slug, []).append(o)
|
||||
seen = 0
|
||||
total = None
|
||||
async with httpx.AsyncClient(timeout=30.0) as client:
|
||||
for page in range(max_pages):
|
||||
r = await client.post(
|
||||
OC_GRAPHQL_URL,
|
||||
headers={"Content-Type": "application/json",
|
||||
"Authorization": f"Bearer {OC_PERSONAL_TOKEN}",
|
||||
"Api-Version": "2022-01-01",
|
||||
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64)"},
|
||||
json={"query": query_tpl % (OC_COLLECTIVE_SLUG, per_page, page * per_page)},
|
||||
)
|
||||
if r.status_code != 200:
|
||||
raise RuntimeError(f"OC order fetch failed: HTTP {r.status_code}")
|
||||
body = r.json()
|
||||
if body.get("errors"):
|
||||
raise RuntimeError(f"OC order fetch errors: {str(body['errors'])[:200]}")
|
||||
orders = (((body.get("data") or {}).get("collective") or {}).get("orders") or {})
|
||||
nodes = orders.get("nodes") or []
|
||||
total = orders.get("totalCount") or 0
|
||||
for o in nodes:
|
||||
slug = ((o.get("fromAccount") or {}).get("slug") or "").strip()
|
||||
if slug:
|
||||
by_slug.setdefault(slug, []).append(o)
|
||||
seen += len(nodes)
|
||||
if len(nodes) < per_page or seen >= total:
|
||||
break
|
||||
# A read that fell short of OC's own total is PARTIAL, however it ended:
|
||||
# a partial order list reads as "this member has no recurring
|
||||
# contribution", which reminds (then deactivates) a paying member —
|
||||
# so fail loudly instead of returning a shorter, wrong answer.
|
||||
if total is not None and seen < total:
|
||||
raise RuntimeError(
|
||||
f"OC order fetch incomplete: read {seen} of {total} orders "
|
||||
f"(page cap {max_pages})")
|
||||
return by_slug
|
||||
|
||||
|
||||
@@ -8882,27 +8899,45 @@ async def fetch_oc_monthly_order_charges() -> list[dict]:
|
||||
"""
|
||||
if not OC_PERSONAL_TOKEN:
|
||||
raise RuntimeError("OC_PERSONAL_TOKEN not set; cannot fetch order charges")
|
||||
query = (
|
||||
'{ collective(slug: "%s") { orders(limit: 200) { totalCount nodes { '
|
||||
# Paginate: a fixed limit silently dropped the OLDEST orders once the
|
||||
# collective passed it, and an order missing from this read is a truncated
|
||||
# cycle the shortfall pass can never see (or, worse, an order it misreads).
|
||||
per_page = 100
|
||||
max_pages = 20
|
||||
query_tpl = (
|
||||
'{ collective(slug: "%s") { orders(limit: %d, offset: %d) { totalCount nodes { '
|
||||
"id frequency fromAccount { slug } "
|
||||
"transactions { publicId createdAt type kind isRefund } } } } }"
|
||||
% OC_COLLECTIVE_SLUG
|
||||
)
|
||||
nodes: list[dict] = []
|
||||
seen = 0
|
||||
total = None
|
||||
async with httpx.AsyncClient(timeout=30.0) as client:
|
||||
r = await client.post(
|
||||
OC_GRAPHQL_URL,
|
||||
headers={"Content-Type": "application/json",
|
||||
"Authorization": f"Bearer {OC_PERSONAL_TOKEN}",
|
||||
"Api-Version": "2022-01-01",
|
||||
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64)"},
|
||||
json={"query": query},
|
||||
)
|
||||
if r.status_code != 200:
|
||||
raise RuntimeError(f"OC order-charge fetch failed: HTTP {r.status_code}")
|
||||
body = r.json()
|
||||
if body.get("errors"):
|
||||
raise RuntimeError(f"OC order-charge fetch errors: {str(body['errors'])[:200]}")
|
||||
nodes = (((body.get("data") or {}).get("collective") or {}).get("orders") or {}).get("nodes") or []
|
||||
for page in range(max_pages):
|
||||
r = await client.post(
|
||||
OC_GRAPHQL_URL,
|
||||
headers={"Content-Type": "application/json",
|
||||
"Authorization": f"Bearer {OC_PERSONAL_TOKEN}",
|
||||
"Api-Version": "2022-01-01",
|
||||
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64)"},
|
||||
json={"query": query_tpl % (OC_COLLECTIVE_SLUG, per_page, page * per_page)},
|
||||
)
|
||||
if r.status_code != 200:
|
||||
raise RuntimeError(f"OC order-charge fetch failed: HTTP {r.status_code}")
|
||||
body = r.json()
|
||||
if body.get("errors"):
|
||||
raise RuntimeError(f"OC order-charge fetch errors: {str(body['errors'])[:200]}")
|
||||
orders = (((body.get("data") or {}).get("collective") or {}).get("orders") or {})
|
||||
page_nodes = orders.get("nodes") or []
|
||||
total = orders.get("totalCount") or 0
|
||||
nodes.extend(page_nodes)
|
||||
seen += len(page_nodes)
|
||||
if len(page_nodes) < per_page or seen >= total:
|
||||
break
|
||||
if total is not None and seen < total:
|
||||
raise RuntimeError(
|
||||
f"OC order-charge fetch incomplete: read {seen} of {total} orders "
|
||||
f"(page cap {max_pages})")
|
||||
out: list[dict] = []
|
||||
for o in nodes:
|
||||
if (o.get("frequency") or "").upper() != "MONTHLY":
|
||||
|
||||
Reference in new issue
Block a user