Pre-launch — Gluecron is in final validation. Public signups and git hosting for non-owner users open after launch review.
CodeIssuesPull RequestsActionsSecurityInsightsSettings
✨ AI
More
Blame · Line-by-line history

admin-deletions.tsx

Each line is annotated with the commit that last touched it. Click any SHA to jump to that commit and see the surrounding change.

admin-deletions.tsxBlame301 lines · 2 contributors
7293c67Claude1/**
2 * GET /admin/deletions — list users pending purge + completed purges
3 * POST /admin/deletions/:id/purge-now — force-run deletion cascade immediately
4 *
5 * Gated by isSiteAdmin (same pattern as all other admin sub-pages).
6 */
7
8import { Hono } from "hono";
9import { eq, isNotNull, lt } from "drizzle-orm";
10import { db } from "../db";
11import { users } from "../db/schema";
12import { Layout } from "../views/layout";
f4a1547ccantynz-alt13import { AdminShell } from "../views/admin-shell";
7293c67Claude14import { softAuth } from "../middleware/auth";
15import type { AuthEnv } from "../middleware/auth";
16import { isSiteAdmin } from "../lib/admin";
17import { purgeScheduledAccounts } from "../lib/account-deletion";
18import { audit } from "../lib/notify";
19
20const adminDeletions = new Hono<AuthEnv>();
21adminDeletions.use("*", softAuth);
22
23async function gate(c: any): Promise<{ user: any } | Response> {
24 const user = c.get("user");
25 if (!user) return c.redirect("/login?next=/admin/deletions");
26 if (!(await isSiteAdmin(user.id))) {
27 return c.html(
28 <Layout title="Forbidden" user={user}>
29 <div style="max-width:540px;margin:80px auto;padding:32px;text-align:center;background:var(--bg-elevated);border:1px solid var(--border);border-radius:16px;">
30 <h2 style="font-family:var(--font-display);font-size:22px;margin:0 0 8px;color:var(--text-strong);">403 — Not a site admin</h2>
31 <p style="color:var(--text-muted);margin:0;font-size:14px;">You don't have permission to view this page.</p>
32 </div>
33 </Layout>,
34 403
35 );
36 }
37 return { user };
38}
39
40const styles = `
41 .adm-del-wrap { max-width: 1400px; margin: 0 auto; padding: var(--space-6) var(--space-4); }
42
43 .adm-del-hero {
44 position: relative;
45 margin-bottom: var(--space-5);
46 padding: var(--space-5) var(--space-6);
47 background: var(--bg-elevated);
48 border: 1px solid var(--border);
49 border-radius: 16px;
50 overflow: hidden;
51 }
52 .adm-del-hero::before {
53 content: '';
54 position: absolute;
55 top: 0; left: 0; right: 0;
56 height: 2px;
57 background: linear-gradient(90deg, transparent 0%, #f87171 30%, #fb923c 70%, transparent 100%);
58 opacity: 0.7;
59 pointer-events: none;
60 }
61 .adm-del-hero-inner { position: relative; z-index: 1; display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; flex-wrap: wrap; }
62 .adm-del-eyebrow { font-size: 11px; font-weight: 600; letter-spacing: 0.08em; text-transform: uppercase; color: #fb923c; margin-bottom: 6px; }
63 .adm-del-title { font-family: var(--font-display); font-size: clamp(24px,3.5vw,36px); font-weight: 800; letter-spacing: -0.025em; margin: 0 0 4px; color: var(--text-strong); }
64 .adm-del-sub { font-size: 14px; color: var(--text-muted); margin: 0; line-height: 1.5; }
65 .adm-del-back { display: inline-flex; align-items: center; gap: 6px; padding: 7px 12px; font-size: 12.5px; color: var(--text-muted); background: rgba(255,255,255,0.02); border: 1px solid var(--border); border-radius: 8px; text-decoration: none; font-weight: 500; transition: border-color 120ms,color 120ms,background 120ms; }
66 .adm-del-back:hover { border-color: var(--border-strong); color: var(--text-strong); background: rgba(255,255,255,0.04); }
67
68 .adm-del-section { margin-bottom: var(--space-6); }
69 .adm-del-section-title { font-family: var(--font-display); font-size: 16px; font-weight: 700; letter-spacing: -0.012em; color: var(--text-strong); margin: 0 0 var(--space-3); display: flex; align-items: center; gap: 10px; }
70 .adm-del-badge { display: inline-flex; align-items: center; padding: 2px 8px; border-radius: 9999px; font-size: 11px; font-weight: 700; }
71 .adm-del-badge-warn { background: rgba(251,146,60,0.15); color: #fdba74; box-shadow: inset 0 0 0 1px rgba(251,146,60,0.35); }
e589f77ccantynz-alt72 .adm-del-badge-ok { background: rgba(52,211,153,0.12); color: var(--green); box-shadow: inset 0 0 0 1px rgba(52,211,153,0.30); }
7293c67Claude73
74 .adm-del-table { width: 100%; border-collapse: collapse; background: var(--bg-elevated); border: 1px solid var(--border); border-radius: 14px; overflow: hidden; }
75 .adm-del-table thead th { text-align: left; font-size: 11px; font-weight: 600; letter-spacing: 0.08em; text-transform: uppercase; color: var(--text-muted); padding: 10px 14px; background: rgba(255,255,255,0.015); border-bottom: 1px solid var(--border); }
76 .adm-del-table tbody td { padding: 10px 14px; border-bottom: 1px solid var(--border-subtle); font-size: 13px; color: var(--text); vertical-align: middle; }
77 .adm-del-table tbody tr:last-child td { border-bottom: none; }
78 .adm-del-table tbody tr:hover td { background: rgba(255,255,255,0.018); }
79 .adm-del-table code { font-family: var(--font-mono); font-size: 11.5px; color: var(--text-strong); }
80
e589f77ccantynz-alt81 .adm-del-btn { display: inline-flex; align-items: center; gap: 6px; padding: 6px 12px; border-radius: 7px; font-size: 12.5px; font-weight: 600; cursor: pointer; font-family: inherit; line-height: 1; transition: background 120ms, border-color 120ms, color 120ms; border: 1px solid rgba(248,113,113,0.45); background: rgba(248,113,113,0.08); color: var(--red); }
7293c67Claude82 .adm-del-btn:hover { background: rgba(248,113,113,0.18); border-color: rgba(248,113,113,0.70); color: #fecaca; }
83
84 .adm-del-empty { padding: var(--space-6); text-align: center; color: var(--text-muted); font-size: 13.5px; background: var(--bg-elevated); border: 1px dashed var(--border); border-radius: 14px; }
85
86 .adm-del-banner { margin-bottom: var(--space-4); padding: 10px 14px; border-radius: 10px; font-size: 13.5px; border: 1px solid var(--border); background: rgba(255,255,255,0.025); color: var(--text); }
87 .adm-del-banner.is-ok { border-color: rgba(52,211,153,0.40); background: rgba(52,211,153,0.08); color: #bbf7d0; }
88 .adm-del-banner.is-err { border-color: rgba(248,113,113,0.40); background: rgba(248,113,113,0.08); color: #fecaca; }
89
90 @media (max-width: 720px) {
91 .adm-del-wrap { padding: var(--space-4) var(--space-3); }
92 .adm-del-hero { padding: var(--space-4); }
93 .adm-del-table { display: block; overflow-x: auto; }
94 }
95`;
96
97adminDeletions.get("/admin/deletions", async (c) => {
98 const g = await gate(c);
99 if (g instanceof Response) return g;
100 const { user } = g;
101
102 const now = new Date();
103
104 // Users pending purge: deletion_scheduled_for is set and in the past (overdue)
105 const pendingRows = await db
106 .select({
107 id: users.id,
108 username: users.username,
109 email: users.email,
110 deletedAt: users.deletedAt,
111 deletionScheduledFor: users.deletionScheduledFor,
112 })
113 .from(users)
114 .where(lt(users.deletionScheduledFor, now))
115 .orderBy(users.deletionScheduledFor)
116 .limit(200);
117
118 // Users in grace period (deletion_scheduled_for is set but not yet overdue).
119 // We fetch all with a non-null scheduled_for and filter in JS.
120 const scheduledRows = await db
121 .select({
122 id: users.id,
123 username: users.username,
124 email: users.email,
125 deletedAt: users.deletedAt,
126 deletionScheduledFor: users.deletionScheduledFor,
127 })
128 .from(users)
129 .where(isNotNull(users.deletionScheduledFor))
130 .orderBy(users.deletionScheduledFor)
131 .limit(400);
132
133 // Filter: grace period = scheduled but not yet overdue
134 const graceRows = scheduledRows.filter(
135 (r) => r.deletionScheduledFor && r.deletionScheduledFor > now
136 );
137
138 const msg = c.req.query("msg") || "";
139 const errMsg = c.req.query("err") || "";
140
141 const fmt = (d: Date | null | undefined) =>
142 d ? new Date(d).toLocaleString() : "—";
143
144 return c.html(
f4a1547ccantynz-alt145 <AdminShell active="deletions" title="Account Deletions" user={user}>
7293c67Claude146 <style dangerouslySetInnerHTML={{ __html: styles }} />
147 <div class="adm-del-wrap">
148 <div class="adm-del-hero">
149 <div class="adm-del-hero-inner">
150 <div>
151 <div class="adm-del-eyebrow">Admin / GDPR</div>
152 <h1 class="adm-del-title">Account Deletions</h1>
153 <p class="adm-del-sub">
154 Users scheduled for deletion, their grace-period status, and completed purges.
155 </p>
156 </div>
157 <a href="/admin" class="adm-del-back">← Admin</a>
158 </div>
159 </div>
160
161 {msg && <div class="adm-del-banner is-ok">{msg}</div>}
162 {errMsg && <div class="adm-del-banner is-err">{errMsg}</div>}
163
164 {/* Overdue — pending execution */}
165 <div class="adm-del-section">
166 <div class="adm-del-section-title">
167 Overdue (pending execution)
168 <span class="adm-del-badge adm-del-badge-warn">{pendingRows.length}</span>
169 </div>
170 {pendingRows.length === 0 ? (
171 <div class="adm-del-empty">No overdue deletions. Autopilot is keeping up.</div>
172 ) : (
173 <table class="adm-del-table">
174 <thead>
175 <tr>
176 <th>Username</th>
177 <th>Email</th>
178 <th>Deleted at</th>
179 <th>Scheduled for</th>
180 <th>Action</th>
181 </tr>
182 </thead>
183 <tbody>
184 {pendingRows.map((r) => (
185 <tr>
186 <td><code>{r.username}</code></td>
187 <td>{r.email}</td>
188 <td>{fmt(r.deletedAt)}</td>
189 <td style="color:#fb923c">{fmt(r.deletionScheduledFor)}</td>
190 <td>
191 <form method="post" action={`/admin/deletions/${r.id}/purge-now`}>
192 <button type="submit" class="adm-del-btn"
193 onclick="return confirm('Hard-delete this user immediately? This cannot be undone.')">
194 Purge now
195 </button>
196 </form>
197 </td>
198 </tr>
199 ))}
200 </tbody>
201 </table>
202 )}
203 </div>
204
205 {/* Grace period */}
206 <div class="adm-del-section">
207 <div class="adm-del-section-title">
208 In grace period
209 <span class="adm-del-badge adm-del-badge-ok">{graceRows.length}</span>
210 </div>
211 {graceRows.length === 0 ? (
212 <div class="adm-del-empty">No accounts in the grace period.</div>
213 ) : (
214 <table class="adm-del-table">
215 <thead>
216 <tr>
217 <th>Username</th>
218 <th>Email</th>
219 <th>Deletion initiated</th>
220 <th>Purge scheduled for</th>
221 <th>Action</th>
222 </tr>
223 </thead>
224 <tbody>
225 {graceRows.map((r) => (
226 <tr>
227 <td><code>{r.username}</code></td>
228 <td>{r.email}</td>
229 <td>{fmt(r.deletedAt)}</td>
230 <td>{fmt(r.deletionScheduledFor)}</td>
231 <td>
232 <form method="post" action={`/admin/deletions/${r.id}/purge-now`}>
233 <button type="submit" class="adm-del-btn"
234 onclick="return confirm('Hard-delete this user now, skipping the grace period? This cannot be undone.')">
235 Purge now
236 </button>
237 </form>
238 </td>
239 </tr>
240 ))}
241 </tbody>
242 </table>
243 )}
244 </div>
245 </div>
f4a1547ccantynz-alt246 </AdminShell>
7293c67Claude247 );
248});
249
250// Force-run deletion cascade immediately for a specific user.
251adminDeletions.post("/admin/deletions/:id/purge-now", async (c) => {
252 const g = await gate(c);
253 if (g instanceof Response) return g;
254 const { user: adminUser } = g;
255
256 const targetId = c.req.param("id");
257
258 // Fetch the target user to confirm they're scheduled for deletion.
259 const targetRows = await db
260 .select({ id: users.id, username: users.username, deletionScheduledFor: users.deletionScheduledFor, deletedAt: users.deletedAt })
261 .from(users)
262 .where(eq(users.id, targetId))
263 .limit(1);
264
265 const target = targetRows[0] ?? null;
266 if (target && !target.deletedAt) {
267 return c.redirect("/admin/deletions?err=" + encodeURIComponent("User is not scheduled for deletion."));
268 }
269 if (!target) {
270 return c.redirect("/admin/deletions?err=" + encodeURIComponent("User not found or not scheduled for deletion."));
271 }
272
273 // Force-run purge by setting deletion_scheduled_for to epoch so lt(now) picks it up.
274 try {
275 await db
276 .update(users)
277 .set({ deletionScheduledFor: new Date(0) })
278 .where(eq(users.id, targetId));
279 } catch (err) {
280 console.error("[admin-deletions] force-schedule update failed:", err);
281 }
282
283 // Use the import from account-deletion which already handles all cascade steps.
284 const result = await purgeScheduledAccounts({ cap: 1 });
285
286 await audit({
287 userId: adminUser.id,
288 action: "account.admin_purge",
289 targetType: "user",
290 targetId,
291 metadata: { username: target.username, triggeredBy: adminUser.id },
292 });
293
294 if (result.purged > 0) {
295 return c.redirect("/admin/deletions?msg=" + encodeURIComponent(`User "${target.username}" has been permanently purged.`));
296 } else {
297 return c.redirect("/admin/deletions?err=" + encodeURIComponent(`Purge encountered errors. Check server logs.`));
298 }
299});
300
301export default adminDeletions;