panel: gifting action (provision-gift) + gift pill and datalist for member emails
The Send-gift control sits in the members-view toolbar: recipient email + name + inviting member (datalist of member emails), proxied to the portal's /admin/provision-gift which enforces the abuse rules and returns clear refusals. Gift rows carry the 'gift' pill in the Source column (inviter + expiry in the tooltip); the pre-built Guests section shows inviter, expiry, credits and ledger status as columns once the portal payload carries account_type.
This commit is contained in:
1 parent
2a6ec9301e
commit
4744a4fd31
2 files changed
+269
-4
No files matched your search
+136
-4
@@ -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 = """<!DOCTYPE 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 = """<!DOCTYPE html>
|
||||
|
||||
<div class="totals">
|
||||
<div class="tot"><div class="num" id="tot-members">—</div><div class="lbl">members</div></div>
|
||||
<div class="tot"><div class="num" id="tot-guests">—</div><div class="lbl">guests — gift accounts</div><div class="sub">excluded from member counts</div></div>
|
||||
<div class="tot"><div class="num" id="tot-spend-cycle">—</div><div class="lbl">current balance used — members + co-op's own</div><div class="sub">co-op's own share: <span id="tot-spend-cycle-own">—</span></div></div>
|
||||
<div class="tot"><div class="num" id="tot-spend-all">—</div><div class="lbl">all-time spend — members + co-op's own</div><div class="sub">co-op's own share: <span id="tot-spend-all-own">—</span></div></div>
|
||||
</div>
|
||||
@@ -371,6 +400,14 @@ PAGE_HTML = """<!DOCTYPE html>
|
||||
<button class="range-btn active" id="mmode-balance" onclick="setMemberMode('balance')">Balance</button>
|
||||
<button class="range-btn" id="mmode-all" onclick="setMemberMode('all')">All time</button>
|
||||
</span>
|
||||
<span class="range-group" id="gift-group">
|
||||
<span class="range-label">Send gift</span>
|
||||
<input id="gift-email" type="email" placeholder="friend@example.com" list="member-email-list" autocomplete="off" />
|
||||
<input id="gift-name" type="text" placeholder="Their name" autocomplete="off" />
|
||||
<input id="gift-inviter" type="email" placeholder="Inviting member" list="member-email-list" autocomplete="off" />
|
||||
<datalist id="member-email-list"></datalist>
|
||||
<button class="range-btn" id="gift-send" onclick="sendGift()">Gift $5 access</button>
|
||||
</span>
|
||||
<span class="range-group" id="range-group" style="display:none;">
|
||||
<span class="range-label">Period</span>
|
||||
<button class="range-btn" id="range-30" onclick="setRange(30)">Last 30 days</button>
|
||||
@@ -398,6 +435,25 @@ PAGE_HTML = """<!DOCTYPE html>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<div id="guests-section" style="display:none;">
|
||||
<h3 style="margin:20px 0 8px;">Guests</h3>
|
||||
<p style="color:var(--muted);font-size:12px;margin:0 0 8px;">Gift accounts — access an inviting member gave to someone outside the co-op. Not members — excluded from member counts; a gift lapses on its time box or when its credits are spent.</p>
|
||||
<div class="table-scroll">
|
||||
<table id="table-guests">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Recipient</th>
|
||||
<th>Invited by</th>
|
||||
<th>Credits remaining</th>
|
||||
<th>Expires</th>
|
||||
<th>Status</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody id="guest-rows"></tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div id="trends-section" style="display:none;">
|
||||
<div class="trend-controls">
|
||||
<span class="range-group">
|
||||
@@ -536,7 +592,21 @@ async function load() {
|
||||
// rather than left for the reader to guess.
|
||||
$('tot-spend-cycle-own').textContent = fmt(d.totals.operator_spend ?? 0);
|
||||
$('tot-spend-all-own').textContent = fmt(d.totals.operator_all_time_spend ?? 0);
|
||||
membersData = d.members || [];
|
||||
// Guests: gift accounts live in their own list and never count as members.
|
||||
// The portal splits them out (guests[]); an older payload that still tags
|
||||
// rows account_type='gift' inside members[] is derived here, so the member
|
||||
// count and the spend totals exclude guests in either payload shape.
|
||||
const allRows = d.members || [];
|
||||
const newShape = Array.isArray(d.guests);
|
||||
guestsData = newShape ? d.guests : allRows.filter(m => (m.account_type || 'member') === 'gift');
|
||||
membersData = newShape ? allRows : allRows.filter(m => (m.account_type || 'member') !== 'gift');
|
||||
const gSpend = guestsData.reduce((s, m) => s + (Number(m.spend) || 0), 0);
|
||||
const gAll = guestsData.reduce((s, m) => s + (Number(m.all_time_spend) || 0), 0);
|
||||
const t = d.totals || {};
|
||||
$('tot-members').textContent = newShape ? (t.members ?? membersData.length) : ((t.members ?? allRows.length) - guestsData.length);
|
||||
$('tot-guests').textContent = guestsData.length;
|
||||
$('tot-spend-cycle').textContent = fmt((t.total_spend ?? 0) - (newShape ? 0 : gSpend));
|
||||
$('tot-spend-all').textContent = fmt((t.total_all_time_spend ?? t.total_spend ?? 0) - (newShape ? 0 : gAll));
|
||||
adminsData = d.admins || [];
|
||||
agentsData = d.agents || [];
|
||||
if (currentView === 'members') renderMembers();
|
||||
@@ -997,6 +1067,7 @@ function renderMoneyDaily(host, rows) {
|
||||
|
||||
// ---- shared state ----
|
||||
let membersData = [];
|
||||
let guestsData = [];
|
||||
let adminsData = [];
|
||||
let agentsData = [];
|
||||
let modelsData = [];
|
||||
@@ -1028,6 +1099,7 @@ function switchView(view) {
|
||||
$('table-models').style.display = view === 'models' ? '' : 'none';
|
||||
// The Admin section is a members-view category; hide it in the Models view
|
||||
// (model usage already integrates admin spend into the totals).
|
||||
$('guests-section').style.display = view === 'members' ? '' : 'none';
|
||||
$('admins-section').style.display = view === 'members' ? '' : 'none';
|
||||
$('agents-section').style.display = view === 'members' ? '' : 'none';
|
||||
$('trends-section').style.display = view === 'trends' ? '' : 'none';
|
||||
@@ -1037,9 +1109,11 @@ function switchView(view) {
|
||||
$('tab-trends').className = 'tab-btn' + (view === 'trends' ? ' active' : '');
|
||||
$('tab-money').className = 'tab-btn' + (view === 'money' ? ' active' : '');
|
||||
$('view-title').textContent = view === 'members' ? 'Members' : (view === 'models' ? 'Model usage' : (view === 'trends' ? 'Trends' : 'Money'));
|
||||
// Mode groups: Balance/All-time for members; Period for models.
|
||||
// Mode groups: Balance/All-time for members; Period for models; the gift
|
||||
// action belongs to the members view too.
|
||||
$('range-group').style.display = view === 'models' ? '' : 'none';
|
||||
$('member-mode-group').style.display = view === 'members' ? '' : 'none';
|
||||
$('gift-group').style.display = view === 'members' ? '' : 'none';
|
||||
// Trends renders on first visit only, so the panel's initial load stays fast.
|
||||
if (view === 'trends') loadMetrics();
|
||||
// Money renders on first visit only, same rule: one fetch, then the
|
||||
@@ -1094,6 +1168,10 @@ function render() {
|
||||
}
|
||||
|
||||
function renderMembers() {
|
||||
// Datalist for the gift form's recipient/inviter fields: member emails the
|
||||
// operator can pick from (rebuilt per render; tiny).
|
||||
const dl = document.getElementById('member-email-list');
|
||||
if (dl) dl.innerHTML = membersData.map(m => '<option value="' + esc(m.email) + '"></option>').join('');
|
||||
const rows = [...membersData].sort((a, b) => {
|
||||
let va = a[memberSort.key], vb = b[memberSort.key];
|
||||
if (memberSort.key === 'spend') { va = memberSpend(a); vb = memberSpend(b); }
|
||||
@@ -1129,9 +1207,13 @@ function renderMembers() {
|
||||
const creditsCell = creditsShown > 0
|
||||
? fmt(creditsShown)
|
||||
: '<span style="color:var(--muted)">—</span>';
|
||||
const src = m.manual
|
||||
const src = m.account_type === 'gift'
|
||||
? '<span class="pill gift" title="' + esc('Gift account — from ' + (m.gift_inviter || '?') +
|
||||
' · expires ' + (m.gift_expires_at ? String(m.gift_expires_at).slice(0, 10) : '?') +
|
||||
' · $5 credit, no OC membership, never swept for dues') + '">gift</span>'
|
||||
: (m.manual
|
||||
? '<span class="pill manual" title="Manual member (no OC subscription; exempt from sweep)">manual</span>'
|
||||
: '<span class="pill active" title="Open Collective Membership subscription">OC</span>';
|
||||
: '<span class="pill active" title="Open Collective Membership subscription">OC</span>');
|
||||
// Effective Matrix handle: dashboard-set external MXID, else the local
|
||||
// @username:inference.coop identity (pending users have no username yet).
|
||||
// Account cell: username on top (prominent); email + Matrix handle beneath
|
||||
@@ -1164,6 +1246,29 @@ function renderMembers() {
|
||||
}).join('');
|
||||
clearArrows('members', memberSort);
|
||||
|
||||
// Guests — gift accounts in their own list, never in the members table or
|
||||
// the member count. Inviter and expiry come from the portal gifts ledger.
|
||||
const guestSection = document.getElementById('guests-section');
|
||||
if (guestsData.length) {
|
||||
guestSection.style.display = '';
|
||||
$('guest-rows').innerHTML = guestsData.map(g => {
|
||||
const top = g.username || g.email;
|
||||
const emailLine = g.username ? '<div style="color:var(--muted);font-size:11px">' + esc(g.email) + '</div>' : '';
|
||||
const credits = Number(g.credits_remaining ?? g.credit_balance ?? 0);
|
||||
const exp = g.gift_expires_at ? String(g.gift_expires_at).slice(0, 10) : '—';
|
||||
const st = String(g.gift_status || (g.active ? 'active' : 'inactive'));
|
||||
const stClass = st === 'active' ? 'active' : (st === 'converted' ? 'org' : 'inactive');
|
||||
const stTitle = { active: 'Gift live', converted: 'Gift became a member', expired: 'Time box elapsed', depleted: 'Credits spent' }[st] || '';
|
||||
return '<tr>' +
|
||||
'<td><strong>' + esc(top) + '</strong>' + emailLine + '</td>' +
|
||||
'<td>' + esc(g.gift_inviter || '—') + '</td>' +
|
||||
'<td class="num remaining-col">' + fmt(credits) + '</td>' +
|
||||
'<td>' + esc(exp) + '</td>' +
|
||||
'<td><span class="pill ' + stClass + '"' + (stTitle ? ' title="' + esc(stTitle) + '"' : '') + '>' + esc(st) + '</span></td>' +
|
||||
'</tr>';
|
||||
}).join('');
|
||||
} else { guestSection.style.display = 'none'; $('guest-rows').innerHTML = ''; }
|
||||
|
||||
// Agents/bots — real spend, not members. Rendered before the admin block, which
|
||||
// returns early when there are no operator accounts.
|
||||
const agentSection = document.getElementById('agents-section');
|
||||
@@ -1256,6 +1361,33 @@ async function addBalance(email) {
|
||||
} catch (e) { flash(e.message, false); }
|
||||
}
|
||||
|
||||
// Gift action: provision a $5 guest account for a friend of a member. The
|
||||
// portal enforces the abuse rules (one gift per inviter per 30 days, one per
|
||||
// recipient ever, inviter must be an active member) and reports a clear
|
||||
// refusal; the panel only collects the three fields.
|
||||
async function sendGift() {
|
||||
const email = ($('gift-email').value || '').trim();
|
||||
const name = ($('gift-name').value || '').trim();
|
||||
const inviter = ($('gift-inviter').value || '').trim();
|
||||
const btn = $('gift-send');
|
||||
if (!email || email.indexOf('@') < 1) { flash("Enter the recipient's email", false); return; }
|
||||
if (!inviter || inviter.indexOf('@') < 1) { flash("Enter the inviting member's email", false); return; }
|
||||
btn.disabled = true; btn.textContent = "Sending…";
|
||||
try {
|
||||
const r = await fetch('/api/provision-gift', {
|
||||
method: 'POST', headers: {'Content-Type': 'application/json'},
|
||||
body: JSON.stringify({ email, name, inviter })
|
||||
});
|
||||
const d = await r.json().catch(() => ({}));
|
||||
if (!r.ok || d.error) { flash(d.error || ('Gift failed (' + r.status + ')'), false); return; }
|
||||
flash('Gift sent — ' + email + ' has $5 of credits (expires in ' + (d.expires_in_days || 60) +
|
||||
' days). The welcome email is on its way.', true);
|
||||
$('gift-email').value = ''; $('gift-name').value = '';
|
||||
load();
|
||||
} catch (e) { flash('Gift failed: ' + e.message, false); }
|
||||
finally { btn.disabled = false; btn.textContent = 'Gift $5 access'; }
|
||||
}
|
||||
|
||||
load();
|
||||
loadModels();
|
||||
</script>
|
||||
|
||||
Reference in new issue
Block a user