diff --git a/app/main.py b/app/main.py index 31945b3..b16046f 100644 --- a/app/main.py +++ b/app/main.py @@ -3175,12 +3175,15 @@ async def admin_overview(request: Request): credits, spend, ) + mstate, mstate_reason = member_membership_state(email, bool(active)) entry = { "email": email, "all_time_spend": round(member_all_time, 2), "username": usernames.get(user_id), "activated": invite_accepted.get(user_id, False), "active": bool(active), + "state": mstate, + "state_reason": mstate_reason, "manual": email.lower() in MANUAL_MEMBERS, "org": org_slug or None, "matrix_mxid": matrix_mxid or None, @@ -4090,6 +4093,45 @@ def _ensure_allowance_tables(conn) -> None: conn.commit() +def member_membership_state(email: str, active: bool) -> tuple[str, str]: + """Derived membership state for the admin panel — read-only, no side effects. + + Mirrors what the allowance reconciliation already knows, so the panel reflects + the portal rather than inventing policy of its own: + + inactive — membership deactivated (members.active = 0). + in_grace — a payment reminder has been sent for this member's + current cycle and no contribution has arrived yet; + access continues for PAYMENT_GRACE_DAYS. + awaiting_contribution — the reconciliation flagged the member unpaid but no + reminder has been sent yet. + active — current / no open reminder. + """ + if not active: + return "inactive", "deactivated" + try: + conn = get_db() + rem = conn.execute( + "SELECT sent_at FROM payment_reminders WHERE email = ? " + "ORDER BY sent_at DESC LIMIT 1", + (email,), + ).fetchone() + log = conn.execute( + "SELECT decision FROM allowance_log WHERE email = ? ORDER BY id DESC LIMIT 1", + (email,), + ).fetchone() + conn.close() + except Exception: # tables absent / unreadable: fail safe + return "active", "" + if rem: + return "in_grace", (f"awaiting contribution — reminder sent " + f"{str(rem[0])[:10]}, grace {int(PAYMENT_GRACE_DAYS)}d") + if log and log[0] in ("reminded", "would_remind", "remind_failed"): + return "awaiting_contribution", "no current contribution at Open Collective" + return "active", "" + + + def _parse_oc_dt(value): """Parse an Open Collective ISO timestamp ('…Z') into an aware datetime.""" if not value: