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