Files
podcastdistributiona/lib/admin/audit.ts
T

42 lines
1.2 KiB
TypeScript

import type { Prisma } from "@prisma/client";
import { prisma } from "@/lib/db";
import { rangeWindow, type Range } from "./range";
export const AUDIT_PAGE_SIZE = 30;
export async function listAudit(params: {
action?: string;
actor?: string;
range?: Range;
page: number;
pageSize?: number;
}) {
const pageSize = params.pageSize ?? AUDIT_PAGE_SIZE;
const where: Prisma.AuditLogWhereInput = {};
if (params.action) where.action = params.action;
if (params.actor) where.actor = { email: { contains: params.actor, mode: "insensitive" } };
if (params.range) where.createdAt = { gte: rangeWindow(params.range).since };
const [rows, total] = await Promise.all([
prisma.auditLog.findMany({
where,
orderBy: { createdAt: "desc" },
skip: (params.page - 1) * pageSize,
take: pageSize,
include: { actor: { select: { email: true } } },
}),
prisma.auditLog.count({ where }),
]);
return { rows, total };
}
/** Distinct action names for the filter dropdown. */
export async function getAuditActions(): Promise<string[]> {
const rows = await prisma.auditLog.findMany({
distinct: ["action"],
select: { action: true },
orderBy: { action: "asc" },
});
return rows.map((r) => r.action);
}