portal: persist each member's current OC dues level

reconcile_allowances already matches every member's Open Collective orders;
it now records what those orders say the member currently pays:
dues_amount / dues_frequency / dues_updated_at (added via the lazy ALTER
pattern). Only a LIVE recurring order (MONTHLY/YEARLY, ACTIVE/PROCESSING)
counts — one-time orders are credit purchases, cancelled/errored ones are not
a current level, and anything unreadable is NULL rather than a guessed tier.

/admin/overview carries the three fields so the admin panel can show a real
Dues column instead of a credit figure. The write happens in report mode too:
it records OC's own truth and makes no membership decision.

tests/test_dues_level.py pins the extraction rules.
This commit is contained in:
inference-bot committed 2026-11-17 09:02:22 +00:00
1 parent 1216d9a873
commit a135560899
2 files changed
+171 -6

No files matched your search

+82 -6
View File
@@ -1110,6 +1110,19 @@ def get_db() -> sqlite3.Connection:
# MEMBER_SOURCES). NULL means "not stated": fall back to MANUAL_MEMBERS.
if "source" not in cols:
conn.execute("ALTER TABLE members ADD COLUMN source TEXT")
# Migration: dues_amount / dues_frequency / dues_updated_at — the member's
# CURRENT Open Collective contribution level (e.g. 10.0 + 'MONTHLY' for a
# $10/month subscription). Written by reconcile_allowances at the point it
# matches the member's OC orders, so the admin panel can show what the
# member actually pays instead of a credit figure. NULL means "no readable
# live OC contribution" — consumers render '—'; never default to the
# uniform allowance (15), which is a budget, not a dues level.
if "dues_amount" not in cols:
conn.execute("ALTER TABLE members ADD COLUMN dues_amount REAL")
if "dues_frequency" not in cols:
conn.execute("ALTER TABLE members ADD COLUMN dues_frequency TEXT")
if "dues_updated_at" not in cols:
conn.execute("ALTER TABLE members ADD COLUMN dues_updated_at TEXT")
# Migration: oc_email — the Open Collective email a member's contribution
# arrives under when it differs from their account email (set via
# POST /admin/guest-convert when a invited friend joins under another
@@ -5158,7 +5171,8 @@ async def admin_overview(request: Request):
conn = get_db()
rows = conn.execute(
"SELECT email, active, balance, cloudron_user_id, org_slug, matrix_mxid, "
"account_type, oc_email, guest_expires_at FROM members ORDER BY email"
"account_type, oc_email, guest_expires_at, dues_amount, dues_frequency, "
"dues_updated_at FROM members ORDER BY email"
).fetchall()
# Guest inviter/expiry from the guests ledger (latest row per recipient) —
# the abuse-protection context the panel shows on the guest pill, plus the
@@ -5242,7 +5256,7 @@ async def admin_overview(request: Request):
operator_all_time = 0.0
guest_spend = 0.0
guest_all_time = 0.0
for email, active, balance, user_id, org_slug, matrix_mxid, acct_type, oc_email, guest_exp in rows:
for email, active, balance, user_id, org_slug, matrix_mxid, acct_type, oc_email, guest_exp, dues_amount, dues_freq, dues_updated in rows:
team = teams.get(f"member:{email}", {})
spend = float(team.get("spend") or 0.0)
member_all_time = float(all_time_spend.get(f"member:{email}", 0.0))
@@ -5298,6 +5312,13 @@ async def admin_overview(request: Request):
"cloudron_user_id": user_id,
"account_type": acct_type or "member",
"oc_email": oc_email or None,
# Current Open Collective dues level, written by the reconciliation
# sweep when it matches this member's orders. None = no readable
# live recurring contribution; the panel renders '—' and must not
# substitute the allowance.
"dues_amount": float(dues_amount) if dues_amount is not None else None,
"dues_frequency": dues_freq or None,
"dues_updated_at": dues_updated or None,
"guest_expires_at": guest_exp or None,
}
if (acct_type or "member") == "guest":
@@ -7370,6 +7391,44 @@ async def fetch_member_orders() -> dict[str, list[dict]]:
return by_slug
def _current_dues(orders: list[dict]) -> tuple[float | None, str | None]:
"""The member's CURRENT recurring Open Collective level: (amount, frequency).
This is the dues figure the admin panel shows — what the member pays on OC
(e.g. (10.0, 'MONTHLY') for $10/month), NOT their allowance.
Rules:
* only a RECURRING order (MONTHLY or YEARLY) can be a dues level; a
ONE-TIME order is a credit purchase and never sets dues;
* only a LIVE one (ACTIVE or PROCESSING) counts — a CANCELLED subscription
is not what the member currently pays;
* the amount is the order's per-charge total (`totalAmount.value`), the
same figure OC bills each cycle;
* no live recurrence, an unparseable amount, or a non-positive amount →
(None, None). NEVER guess and NEVER fall back to the $15 allowance.
"""
live: list[tuple[str, float, str]] = []
for o in orders or []:
freq = (o.get("frequency") or "").upper()
status = (o.get("status") or "").upper()
if freq not in ("MONTHLY", "YEARLY") or status not in ("ACTIVE", "PROCESSING"):
continue
try:
amt = float((o.get("totalAmount") or {}).get("value"))
except (TypeError, ValueError):
continue
if amt <= 0:
continue
when = str(o.get("lastChargedAt") or o.get("createdAt") or "")
live.append((when, amt, freq))
if not live:
return None, None
# The most recently charged/created live recurrence is the one in force.
live.sort(key=lambda t: t[0])
_, amt, freq = live[-1]
return round(amt, 2), freq
def _payment_verdict(orders: list[dict], cycle_start: datetime,
now: datetime | None = None) -> tuple[bool, str]:
"""Did this member's recurring contribution pay for the current cycle?
@@ -7684,10 +7743,12 @@ async def reconcile_allowances(send: bool = False) -> dict:
"""Decide every active member's allowance renewal from OC payment evidence.
`send=False` is a REPORT: it computes and records every decision (would
renew / would remind / would deactivate) and changes NOTHING — no resets,
no emails, no group changes. `send=True` performs renewals and sends the
single reminder. Deactivation still additionally requires
ALLOWANCE_DEACTIVATE=on.
renew / would remind / would deactivate) and changes NOTHING about
membership — no resets, no emails, no group changes. The one write a report
run does make is the dues snapshot (`dues_amount/dues_frequency/
dues_updated_at`), which merely records what OC already says.
`send=True` performs renewals and sends the single reminder. Deactivation
still additionally requires ALLOWANCE_DEACTIVATE=on.
"""
try:
by_slug = await fetch_member_orders()
@@ -7790,6 +7851,21 @@ async def reconcile_allowances(send: bool = False) -> dict:
orders = by_slug.get(oc_slug, [])
paid, evidence = _payment_verdict(orders, cycle_start)
# Record the CURRENT OC dues level for this member at the moment their
# orders are matched — in report mode too: this is a snapshot of OC's
# own truth (a read-model cache the panel renders), not a membership
# decision, and it changes nothing about renew/remind/deactivate.
# `_current_dues` returns None for members with no live recurrence, so
# a lapsed or one-time-only payer's dues clears to NULL rather than
# keeping a stale level.
dues_amount, dues_frequency = _current_dues(orders)
if "dues_amount" in cols:
conn.execute(
"UPDATE members SET dues_amount = ?, dues_frequency = ?, "
"dues_updated_at = datetime('now') WHERE email = ?",
(dues_amount, dues_frequency, email),
)
conn.commit()
if paid:
next_reset = next_reset_for_anchor_day(anchor_day, now)