diff --git a/services/backend/src/routes/admin.rs b/services/backend/src/routes/admin.rs index 7f73475..fad96c5 100644 --- a/services/backend/src/routes/admin.rs +++ b/services/backend/src/routes/admin.rs @@ -73,6 +73,15 @@ pub async fn list_users( .bind(&pattern) .fetch_one(&state.db) .await?; + // For the whole site, not the search: everyone, and who has paid at least + // once (a live payment; test-mode payments counted apart). + let (all_users, buyers, test_buyers): (i64, i64, i64) = sqlx::query_as( + "SELECT (SELECT count(*) FROM users), count(*) FILTER (WHERE live), count(*) FILTER (WHERE NOT live) \ + FROM (SELECT c.user_id, bool_or(c.live) AS live FROM stripe_checkouts c \ + WHERE c.completed_at IS NOT NULL AND c.user_id IS NOT NULL GROUP BY c.user_id) b", + ) + .fetch_one(&state.db) + .await?; let users: Vec = rows .into_iter() .map(|(id, email, name, roles, verified, created, last_login, charts, natal, synastry, forecast, question, tarot)| { @@ -86,7 +95,10 @@ pub async fn list_users( }) }) .collect(); - Ok(Json(json!({ "users": users, "total": total, "limit": limit, "offset": offset }))) + Ok(Json(json!({ + "users": users, "total": total, "limit": limit, "offset": offset, + "stats": { "users": all_users, "buyers": buyers, "test_buyers": test_buyers }, + }))) } #[derive(Deserialize)] diff --git a/services/backend/src/tests.rs b/services/backend/src/tests.rs index 63f3121..d143fc2 100644 --- a/services/backend/src/tests.rs +++ b/services/backend/src/tests.rs @@ -3208,4 +3208,32 @@ async fn the_user_list_pages_with_a_total(pool: PgPool) { assert_eq!((r.body["total"].as_i64(), r.body["users"].as_array().unwrap().len()), (Some(5), 2)); let r = app.get("/v1/admin/users?q=lap&limit=3&offset=3", Some(&admin)).await; assert_eq!((r.body["total"].as_i64(), r.body["users"].as_array().unwrap().len()), (Some(4), 1)); + + // The counts are for the whole site, not the search. A buyer paid at least + // once (two payments still one buyer); a test-mode payment counts apart, + // an unfinished checkout not at all. + let ids: Vec = sqlx::query_scalar("SELECT id FROM users WHERE email LIKE 'lap%' ORDER BY email") + .fetch_all(&pool) + .await + .unwrap(); + for (n, (user, live, done)) in + [(ids[0], true, true), (ids[0], true, true), (ids[1], false, true), (ids[2], true, false)] + .into_iter() + .enumerate() + { + sqlx::query( + "INSERT INTO stripe_checkouts (id, user_id, package, price_huf, live, completed_at) \ + VALUES ($1, $2, 'alap', 1990, $3, CASE WHEN $4 THEN now() END)", + ) + .bind(format!("cs_test_{n}")) + .bind(user) + .bind(live) + .bind(done) + .execute(&pool) + .await + .unwrap(); + } + let r = app.get("/v1/admin/users?q=lap0", Some(&admin)).await; + assert_eq!(r.body["total"].as_i64(), Some(1)); + assert_eq!(r.body["stats"], json!({ "users": 5, "buyers": 1, "test_buyers": 1 })); } diff --git a/services/frontend/src/app/admin/AdminUsers.tsx b/services/frontend/src/app/admin/AdminUsers.tsx index 12b4d55..877d292 100644 --- a/services/frontend/src/app/admin/AdminUsers.tsx +++ b/services/frontend/src/app/admin/AdminUsers.tsx @@ -12,27 +12,31 @@ const PAGE_SIZE = 25; const EDITABLE_ROLES: Role[] = ["user", "creator", "admin"]; type UnitKind = "natal" | "synastry" | "forecast" | "question" | "tarot"; -const UNITS: { value: UnitKind; label: string }[] = [ - { value: "natal", label: "képlet" }, - { value: "synastry", label: "szinasztria" }, - { value: "forecast", label: "előrejelzés" }, - { value: "question", label: "kérdés" }, - { value: "tarot", label: "kártyajóslás" }, +/** `short`: the mark in the list, where the names do not fit (explained under it). */ +const UNITS: { value: UnitKind; label: string; short: string }[] = [ + { value: "natal", label: "képlet", short: "K" }, + { value: "synastry", label: "szinasztria", short: "Sz" }, + { value: "forecast", label: "előrejelzés", short: "E" }, + { value: "question", label: "kérdés", short: "?" }, + { value: "tarot", label: "kártyajóslás", short: "T" }, ]; +type Stats = { users: number; buyers: number; test_buyers: number }; +type UserPage = { users: AdminUser[]; total: number; stats: Stats }; + type Status = { ok: boolean; text: string } | null; export function AdminUsers({ selfId }: { selfId: string }) { const [query, setQuery] = useState(""); const [page, setPage] = useState(0); - const [data, setData] = useState<{ users: AdminUser[]; total: number } | null>(null); + const [data, setData] = useState(null); const [error, setError] = useState(null); const [editing, setEditing] = useState(null); const packages = usePackages() ?? []; const load = useCallback(async (q: string, p: number) => { try { - const res = await api<{ users: AdminUser[]; total: number }>( + const res = await api( `/admin/users?q=${encodeURIComponent(q)}&limit=${PAGE_SIZE}&offset=${p * PAGE_SIZE}`, ); setData(res); @@ -56,6 +60,7 @@ export function AdminUsers({ selfId }: { selfId: string }) { return (
+ {data && }
@@ -71,9 +76,9 @@ export function AdminUsers({ selfId }: { selfId: string }) { placeholder="pl. anna@" />
- {data && ( + {data && query.trim() && ( - {total} felhasználó + {total} találat )}
@@ -93,7 +98,7 @@ export function AdminUsers({ selfId }: { selfId: string }) { Felhasználó Szerepkör - Hátralévő elemzések + Hátralévő Képletek Regisztrált Utoljára belépett @@ -130,6 +135,15 @@ export function AdminUsers({ selfId }: { selfId: string }) { ))} +

+ Hátralévő:{" "} + {UNITS.map((u, i) => ( + + {i > 0 && " · "} + {u.short} {u.label} + + ))} +

)} {data && total > PAGE_SIZE && ( @@ -177,16 +191,41 @@ function RoleBadges({ roles }: { roles: Role[] }) { ); } -/** The remaining units in one line, only the ones the user has. */ +/** Everyone on the site and who has paid (not affected by the search). */ +function SiteStats({ stats }: { stats: Stats }) { + const share = stats.users > 0 ? (stats.buyers / stats.users) * 100 : 0; + return ( +
+
+
Felhasználó
+
{stats.users.toLocaleString("hu-HU")}
+
+
+
Vásárló
+
{stats.buyers.toLocaleString("hu-HU")}
+
+ {stats.users > 0 && `a felhasználók ${share.toLocaleString("hu-HU", { maximumFractionDigits: 1 })}%-a`} + {stats.test_buyers > 0 && ( + <> + {stats.users > 0 &&
}+{stats.test_buyers} csak tesztmódban + + )} +
+
+
+ ); +} + +/** The remaining units as short marks (K 2 · ? 3), only the ones the user has. */ function Balances({ user }: { user: AdminUser }) { const left = UNITS.map((u) => ({ ...u, n: user.analyses_left[u.value] ?? 0 })).filter((u) => u.n !== 0); if (left.length === 0) return –; return ( - - {left.map((u, i) => ( - - {i > 0 && " · "} - {u.label} {u.n} + + {left.map((u) => ( + + + {u.label} {u.n} ))} diff --git a/services/frontend/src/app/globals.css b/services/frontend/src/app/globals.css index ec6904c..7edff36 100644 --- a/services/frontend/src/app/globals.css +++ b/services/frontend/src/app/globals.css @@ -487,6 +487,22 @@ td.num { font-variant-numeric: tabular-nums; white-space: nowrap; } /* Admin: the user list's pager and the user dialog. */ .pager { display: flex; align-items: center; justify-content: center; gap: 14px; flex-wrap: wrap; font-size: 14px; color: var(--ink-2); } .admin-users td { vertical-align: middle; } +.admin-stats { display: flex; flex-wrap: wrap; gap: 12px; margin: 0 0 18px; } +.admin-stats > div { + flex: 1 1 140px; max-width: 220px; background: var(--panel-2); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 16px; +} +.admin-stats dt { font-size: 13px; color: var(--muted); } +.admin-stats dd { margin: 0; } +.admin-stats dd.num { font-size: 26px; font-weight: 600; line-height: 1.25; color: var(--ink); font-variant-numeric: tabular-nums; } +.unit-marks { display: inline-flex; gap: 4px; } +.unit-mark { + display: inline-flex; gap: 4px; align-items: baseline; padding: 1px 7px; border: 1px solid var(--line-2); border-radius: 6px; + font-size: 12px; color: var(--ink-2); white-space: nowrap; +} +.unit-mark > span:first-child { color: var(--gold); font-weight: 600; } +.unit-mark .num { color: var(--ink); font-variant-numeric: tabular-nums; } +.unit-legend { margin: 10px 0 0; } +.unit-legend b { color: var(--gold); font-weight: 600; } dialog.modal { width: min(560px, calc(100vw - 32px)); max-height: calc(100dvh - 32px); padding: 0; margin: auto; background: var(--panel); color: var(--ink); border: 1px solid var(--line-2); border-radius: 14px;