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. # MEMBER_SOURCES). NULL means "not stated": fall back to MANUAL_MEMBERS.
if "source" not in cols: if "source" not in cols:
conn.execute("ALTER TABLE members ADD COLUMN source TEXT") 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 # Migration: oc_email — the Open Collective email a member's contribution
# arrives under when it differs from their account email (set via # arrives under when it differs from their account email (set via
# POST /admin/guest-convert when a invited friend joins under another # POST /admin/guest-convert when a invited friend joins under another
@@ -5158,7 +5171,8 @@ async def admin_overview(request: Request):
conn = get_db() conn = get_db()
rows = conn.execute( rows = conn.execute(
"SELECT email, active, balance, cloudron_user_id, org_slug, matrix_mxid, " "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() ).fetchall()
# Guest inviter/expiry from the guests ledger (latest row per recipient) — # Guest inviter/expiry from the guests ledger (latest row per recipient) —
# the abuse-protection context the panel shows on the guest pill, plus the # 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 operator_all_time = 0.0
guest_spend = 0.0 guest_spend = 0.0
guest_all_time = 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}", {}) team = teams.get(f"member:{email}", {})
spend = float(team.get("spend") or 0.0) spend = float(team.get("spend") or 0.0)
member_all_time = float(all_time_spend.get(f"member:{email}", 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, "cloudron_user_id": user_id,
"account_type": acct_type or "member", "account_type": acct_type or "member",
"oc_email": oc_email or None, "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, "guest_expires_at": guest_exp or None,
} }
if (acct_type or "member") == "guest": if (acct_type or "member") == "guest":
@@ -7370,6 +7391,44 @@ async def fetch_member_orders() -> dict[str, list[dict]]:
return by_slug 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, def _payment_verdict(orders: list[dict], cycle_start: datetime,
now: datetime | None = None) -> tuple[bool, str]: now: datetime | None = None) -> tuple[bool, str]:
"""Did this member's recurring contribution pay for the current cycle? """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. """Decide every active member's allowance renewal from OC payment evidence.
`send=False` is a REPORT: it computes and records every decision (would `send=False` is a REPORT: it computes and records every decision (would
renew / would remind / would deactivate) and changes NOTHING — no resets, renew / would remind / would deactivate) and changes NOTHING about
no emails, no group changes. `send=True` performs renewals and sends the membership — no resets, no emails, no group changes. The one write a report
single reminder. Deactivation still additionally requires run does make is the dues snapshot (`dues_amount/dues_frequency/
ALLOWANCE_DEACTIVATE=on. 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: try:
by_slug = await fetch_member_orders() by_slug = await fetch_member_orders()
@@ -7790,6 +7851,21 @@ async def reconcile_allowances(send: bool = False) -> dict:
orders = by_slug.get(oc_slug, []) orders = by_slug.get(oc_slug, [])
paid, evidence = _payment_verdict(orders, cycle_start) 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: if paid:
next_reset = next_reset_for_anchor_day(anchor_day, now) next_reset = next_reset_for_anchor_day(anchor_day, now)
+89
View File
@@ -0,0 +1,89 @@
#!/usr/bin/env python3
"""Tests for app.main._current_dues (the member's CURRENT OC dues level).
The admin panel's "Dues" column shows what a member pays on Open Collective
(e.g. $10/mo). That figure is persisted by reconcile_allowances from the OC
orders it already fetches; this file pins the pure extraction rules:
* a live MONTHLY order -> its per-charge amount + 'MONTHLY';
* a live YEARLY order -> its amount + 'YEARLY' (no monthly normalization);
* ACTIVE and PROCESSING both count (a payment in flight is still the level);
* ONE-TIME orders never become dues — credit packs are purchases, not dues;
* CANCELLED / ERROR / PAST_DUE recurrences are not what the member pays now;
* no orders, an unparseable amount or a zero amount -> (None, None).
NULL is the ONLY honest answer; the code must never fall back to the $15
allowance or to a guessed tier.
python3 tests/test_dues_level.py
"""
import os
import sys
for _cand in (os.environ.get("PORTAL_CODE_DIR"), "/app/code",
os.path.dirname(os.path.dirname(os.path.abspath(__file__)))):
if _cand and os.path.isfile(os.path.join(_cand, "app", "main.py")):
sys.path.insert(0, _cand)
break
from app.main import _current_dues # noqa: E402
FAILS = []
def check(cond, msg):
print((" ok " if cond else " FAIL ") + msg)
if not cond:
FAILS.append(msg)
def order(freq, status, amount, created="2026-11-17T01:52:56.450Z"):
return {"frequency": freq, "status": status,
"totalAmount": {"value": amount, "currency": "USD"},
"createdAt": created, "lastChargedAt": created}
print("1. a live monthly subscription is the dues level")
check(_current_dues([order("MONTHLY", "ACTIVE", 10)]) == (10.0, "MONTHLY"),
"$10/month ACTIVE monthly -> (10.0, 'MONTHLY')")
check(_current_dues([order("MONTHLY", "PROCESSING", 12.5)]) == (12.5, "MONTHLY"),
"a PROCESSING monthly is still the current level (12.5, 'MONTHLY')")
print("2. yearly dues are reported as charged — not divided into months")
check(_current_dues([order("YEARLY", "ACTIVE", 120)]) == (120.0, "YEARLY"),
"$120/year ACTIVE yearly -> (120.0, 'YEARLY') (the panel appends '/yr')")
print("3. one-time orders are purchases, never a dues level")
check(_current_dues([order("ONETIME", "PAID", 10)]) == (None, None),
"a PAID $10 one-time order does not become dues")
check(_current_dues([order("ONETIME", "PAID", 10),
order("MONTHLY", "ACTIVE", 10)]) == (10.0, "MONTHLY"),
"a member with BOTH a one-time pack and a monthly subscription reads the monthly")
print("4. ended subscriptions do not report a stale level")
check(_current_dues([order("MONTHLY", "CANCELLED", 10)]) == (None, None),
"a CANCELLED monthly -> (None, None)")
check(_current_dues([order("MONTHLY", "ERROR", 10)]) == (None, None),
"an ERROR (failed charge) monthly -> (None, None)")
print("5. nothing readable -> NULL, never a guess")
check(_current_dues([]) == (None, None), "no orders -> (None, None)")
check(_current_dues(None) == (None, None), "a missing order list -> (None, None)")
check(_current_dues([{"frequency": "MONTHLY", "status": "ACTIVE"}]) == (None, None),
"a monthly with no totalAmount -> (None, None)")
check(_current_dues([order("MONTHLY", "ACTIVE", 0)]) == (None, None),
"a $0 monthly -> (None, None), not dues of zero")
check(_current_dues([{"frequency": "MONTHLY", "status": "ACTIVE",
"totalAmount": {"value": "not-a-number"}}]) == (None, None),
"an unparseable amount -> (None, None)")
print("6. the most recent live recurrence wins when a member has history")
old = order("MONTHLY", "ACTIVE", 5, "2024-01-01T00:00:00.000Z")
new = order("MONTHLY", "ACTIVE", 10, "2026-11-17T01:52:56.450Z")
check(_current_dues([new, old]) == (10.0, "MONTHLY"),
"the newest live monthly ($10) wins over an older $5 monthly")
print()
if FAILS:
print(f"dues level: {len(FAILS)} FAILURE(S)")
sys.exit(1)
print("dues level: all checks passed")