Files
Epicure/apps/web/app/admin/audit-logs/page.tsx
T
Arnaud 4c3880e07f feat: moderator-scoped admin access + fix push notifications not displaying (v0.66.0)
Moderator role existed in the schema and was already respected by
comment deletion, but every admin page/route treated moderator
identically to a regular user (403/redirect). Wires it up narrowly:
admin/layout.tsx now lets admin+moderator through and filters the
nav by role, while every admin-only page (users, tiers, settings,
webhooks, insights, etc.) explicitly redirects moderators away via a
new requireFullAdminPage() helper -- the nav filter is UX, this is
the actual gate. Moderators land on Reports and Recipes: reports
GET/PATCH now accept requireAdmin({allowModerator: true}), and a new
PATCH /api/v1/admin/recipes/[id] lets admin+moderator unpublish a
public recipe (flip to private) as a takedown action, audit-logged.

Also found and fixed a real bug while auditing the PWA push pipeline
for a "push click-through" gap: public/sw.js had no `push` event
listener at all, so incoming push messages never displayed anything
-- push was silently non-functional end-to-end despite the
subscribe/send plumbing all working. Added the push listener
(showNotification) and a notificationclick listener that focuses an
existing tab or opens one at the payload's url.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-21 23:31:47 +02:00

117 lines
4.0 KiB
TypeScript

import type { Metadata } from "next";
import { db, auditLogs, users, eq, desc } from "@epicure/db";
import { requireFullAdminPage } from "@/lib/require-admin-page";
export const metadata: Metadata = {};
const PAGE_SIZE = 50;
interface PageProps {
searchParams: Promise<{ page?: string }>;
}
export default async function AdminAuditLogsPage({ searchParams }: PageProps) {
await requireFullAdminPage();
const { page: pageParam } = await searchParams;
const page = Math.max(1, parseInt(pageParam ?? "1", 10));
const offset = (page - 1) * PAGE_SIZE;
const rows = await db
.select({
id: auditLogs.id,
action: auditLogs.action,
targetType: auditLogs.targetType,
targetId: auditLogs.targetId,
metadata: auditLogs.metadata,
createdAt: auditLogs.createdAt,
actorName: users.name,
actorEmail: users.email,
})
.from(auditLogs)
.leftJoin(users, eq(auditLogs.userId, users.id))
.orderBy(desc(auditLogs.createdAt))
.limit(PAGE_SIZE)
.offset(offset);
return (
<div className="space-y-6">
<div>
<h1 className="text-2xl font-bold tracking-tight">Audit Logs</h1>
<p className="text-muted-foreground text-sm mt-1">
Admin actions recorded for accountability. Page {page}.
</p>
</div>
<div className="rounded-md border overflow-x-auto">
<table className="w-full text-sm">
<thead className="border-b bg-muted/50">
<tr>
<th className="px-4 py-3 text-left font-medium whitespace-nowrap">Actor</th>
<th className="px-4 py-3 text-left font-medium whitespace-nowrap">Action</th>
<th className="px-4 py-3 text-left font-medium whitespace-nowrap">Target</th>
<th className="px-4 py-3 text-left font-medium whitespace-nowrap">Metadata</th>
<th className="px-4 py-3 text-left font-medium whitespace-nowrap">Timestamp</th>
</tr>
</thead>
<tbody>
{rows.length === 0 ? (
<tr>
<td colSpan={5} className="px-4 py-8 text-center text-muted-foreground">
No audit log entries yet.
</td>
</tr>
) : (
rows.map((row) => (
<tr key={row.id} className="border-b last:border-0 hover:bg-muted/30">
<td className="px-4 py-3">
<div className="font-medium">{row.actorName ?? "System"}</div>
{row.actorEmail && (
<div className="text-xs text-muted-foreground">{row.actorEmail}</div>
)}
</td>
<td className="px-4 py-3 font-mono text-xs">{row.action}</td>
<td className="px-4 py-3 text-muted-foreground text-xs">
{row.targetType ? (
<span>
{row.targetType}
{row.targetId ? `: ${row.targetId.slice(0, 8)}…` : ""}
</span>
) : (
"—"
)}
</td>
<td className="px-4 py-3 font-mono text-xs text-muted-foreground max-w-xs truncate">
{row.metadata ?? "—"}
</td>
<td className="px-4 py-3 text-muted-foreground whitespace-nowrap">
{row.createdAt.toLocaleString()}
</td>
</tr>
))
)}
</tbody>
</table>
</div>
<div className="flex gap-2">
{page > 1 && (
<a
href={`/admin/audit-logs?page=${page - 1}`}
className="rounded-md border px-3 py-1 text-sm hover:bg-accent"
>
Previous
</a>
)}
{rows.length === PAGE_SIZE && (
<a
href={`/admin/audit-logs?page=${page + 1}`}
className="rounded-md border px-3 py-1 text-sm hover:bg-accent"
>
Next
</a>
)}
</div>
</div>
);
}