diff --git a/admin-panel-guests-harness.js b/admin-panel-guests-harness.js new file mode 100644 index 0000000..6d4b1e8 --- /dev/null +++ b/admin-panel-guests-harness.js @@ -0,0 +1,133 @@ +// Harness for the admin panel's GUESTS list (gift accounts), run through the +// skill's checker alongside the chart harness: +// ~/.hermes/skills/research/inference-cooperative/scripts/check-page-js.sh \ +// app/main.py admin-panel-guests-harness.js +// Exercises load() against both payload shapes — the portal's split payload +// (guests[]) and an older payload that still tags rows account_type='gift' +// inside members[] — and asserts the invariants that matter: +// * guests NEVER count as members, in either shape; +// * guests never render in the members table; +// * the guests list shows recipient, inviter, credits remaining, expiry +// and status, and hides entirely when there are no guests; +// * the Members-view switch rule keeps the section's visibility handled +// in switchView (the trap that stranded the Admin section on Models). +// The checker evals in CommonJS (no top-level await), so the body is wrapped. +const assert = (cond, msg) => { if (!cond) { console.error("FAIL: " + msg); process.exit(1); } }; +const count = (s, re) => (String(s).match(re) || []).length; +const txt = (id) => String(document.getElementById(id).textContent); // stub keeps raw types; the DOM stringifies + +const memberRow = (email, spend) => ({ + email, username: email.split("@")[0], active: true, activated: true, + account_type: "member", spend, all_time_spend: spend * 10, + balance: 15, credit_balance: 0, credits_remaining: 0, remaining: 15 - spend, +}); + +async function runLoad(overview) { + globalThis.fetch = async (url) => ({ + status: 200, + json: async () => (String(url).indexOf("/api/overview") !== -1 ? overview : { models: [] }), + }); + await load(); +} + +(async () => { +// ---- Shape 1: the portal splits guests into their own list ---- +await runLoad({ + members: [memberRow("alice@example.com", 2), memberRow("bob@example.com", 1)], + guests: [{ + email: "friend@example.com", username: "friend", active: true, + account_type: "gift", gift_inviter: "alice@example.com", + credit_balance: 5, credits_remaining: 3.5, + gift_expires_at: "2027-01-07T02:00:00", gift_status: "active", + spend: 1.5, all_time_spend: 1.5, + }], + totals: { members: 2, total_spend: 4.5, total_all_time_spend: 45, + operator_spend: 1, operator_all_time_spend: 10 }, +}); +assert(txt("tot-members") === "2", + "split payload: member count is 2 (got " + txt("tot-members") + ")"); +assert(txt("tot-guests") === "1", + "split payload: guests count is 1"); +assert(txt("tot-spend-cycle") === "$4.50", + "split payload: spend total unchanged by the guests split"); +{ + const rowsHtml = document.getElementById("rows").innerHTML; + assert(count(rowsHtml, //g) === 2, "members table holds only the 2 members"); + assert(rowsHtml.indexOf("friend@example.com") === -1, "guest never renders in the members table"); + const g = document.getElementById("guest-rows").innerHTML; + assert(count(g, //g) === 1, "guests list holds exactly the 1 guest"); + assert(g.indexOf("friend@example.com") !== -1, "recipient email shown"); + assert(g.indexOf("alice@example.com") !== -1, "inviting member shown from the ledger"); + assert(g.indexOf("$3.50") !== -1, "credits remaining shown"); + assert(g.indexOf("2027-01-07") !== -1, "time-box expiry date shown"); + assert(/pill active[^>]*>active]*>expired]*>converted { console.error("FAIL: " + (e && e.stack || e)); process.exit(1); }); diff --git a/app/main.py b/app/main.py index 92cab8a..373a33b 100644 --- a/app/main.py +++ b/app/main.py @@ -197,6 +197,31 @@ async def api_set_credits(request: Request): return JSONResponse({"error": e.detail}, status_code=e.status_code) +@app.post("/api/provision-gift") +async def api_provision_gift(request: Request): + """Provision a gift account via the portal's /admin/provision-gift. + + The portal enforces the abuse rules (one gift per inviter per 30 days, + one per recipient ever, active-member inviter) and returns a clear + refusal — surfaced to the operator as-is. + """ + if not is_admin(request): + return JSONResponse({"error": "forbidden"}, status_code=403) + body = await request.json() + email = (body.get("email") or "").strip().lower() + name = (body.get("name") or "").strip() + inviter = (body.get("inviter") or body.get("inviter_email") or "").strip().lower() + if not email or "@" not in email: + return JSONResponse({"error": "A valid recipient email is required"}, status_code=400) + if not inviter or "@" not in inviter: + return JSONResponse({"error": "The inviting member's email is required"}, status_code=400) + try: + return await portal_post("/admin/provision-gift", + {"email": email, "name": name, "inviter": inviter}) + except HTTPException as e: + return JSONResponse({"error": e.detail}, status_code=e.status_code) + + # --------------------------------------------------------------------------- # OIDC routes — authorization-code flow against Cloudron's OIDC provider. # Active only when the `oidc` addon is configured (CLOUDRON_OIDC_* present). @@ -290,6 +315,9 @@ PAGE_HTML = """ .pill.manual { background: #f1e8da; color: #a97a4a; } .pill.org { background: #e7edf5; color: #4a6a8a; } .pill.grace { background: #fdf3e3; color: #a97a2a; } + .pill.gift { background: #efe7f2; color: #7a5a92; } + #gift-group input { padding: 5px 8px; border: 1px solid var(--border); border-radius: 6px; font-size: 12px; width: 170px; } + #gift-group input#gift-name { width: 120px; } /* Trends tab (charts). Colours come from the same vars as the rest of the panel; the SVG is inline so there is no chart library and no CDN. */ .trend-controls { display: flex; gap: 22px; flex-wrap: wrap; margin: 14px 0 0; } @@ -355,6 +383,7 @@ PAGE_HTML = """
—
members
+
—
guests — gift accounts
excluded from member counts
—
current balance used — members + co-op's own
co-op's own share: —
—
all-time spend — members + co-op's own
co-op's own share: —
@@ -371,6 +400,14 @@ PAGE_HTML = """ + + Send gift + + + + + +