136 lines
3.7 KiB
TypeScript
136 lines
3.7 KiB
TypeScript
import { ClipboardList } from "lucide-react";
|
|
import { redirect } from "next/navigation";
|
|
import { getTranslations } from "next-intl/server";
|
|
import { AdminPageShell } from "@/components/admin/admin-page-shell";
|
|
import { StatusCard } from "@/components/admin/dashboard";
|
|
import { buildStaffActivityWhere } from "@/lib/admin/log-filters";
|
|
import { calcPagination, parseListParams } from "@/lib/admin-helpers";
|
|
import { formatDate } from "@/lib/format-date";
|
|
import { canAccess, getAdminContext, PERMS } from "@/lib/permissions";
|
|
import { prisma } from "@/lib/prisma";
|
|
import { StaffActivitiesTable } from "./staff-activities-table";
|
|
|
|
export const dynamic = "force-dynamic";
|
|
|
|
const SORT_MAP: Record<string, "createdAt" | "userId" | "action"> = {
|
|
when: "createdAt",
|
|
staff: "userId",
|
|
action: "action",
|
|
};
|
|
|
|
export default async function AdminStaffActivities({
|
|
searchParams,
|
|
}: {
|
|
searchParams: Promise<Record<string, string>>;
|
|
}) {
|
|
const { session, permissions } = await getAdminContext();
|
|
if (!canAccess(permissions, PERMS.LOGS_VIEW, session.user.rank)) {
|
|
redirect("/admin");
|
|
}
|
|
|
|
const t = await getTranslations("pages.admin.logs");
|
|
const raw = await searchParams;
|
|
const parsed = parseListParams(new URLSearchParams(raw));
|
|
const authorizationOnly = raw.filter_authorization === "1";
|
|
const whereClause = buildStaffActivityWhere({
|
|
q: parsed.search,
|
|
authorizationOnly,
|
|
});
|
|
|
|
const sortField = SORT_MAP[parsed.sort ?? ""] ?? "createdAt";
|
|
|
|
const total = await prisma.staffActivities
|
|
.count({ where: whereClause })
|
|
.catch(() => 0);
|
|
|
|
const pagination = calcPagination(total, parsed.page, parsed.perPage);
|
|
|
|
const activities = await prisma.staffActivities
|
|
.findMany({
|
|
where: whereClause,
|
|
select: {
|
|
id: true,
|
|
userId: true,
|
|
action: true,
|
|
description: true,
|
|
targetType: true,
|
|
targetId: true,
|
|
ipAddress: true,
|
|
createdAt: true,
|
|
},
|
|
orderBy: { [sortField]: parsed.order },
|
|
skip: pagination.offset,
|
|
take: pagination.perPage,
|
|
})
|
|
.catch(() => []);
|
|
|
|
const staffIds = Array.from(
|
|
new Set(activities.map((a) => Number(a.userId))),
|
|
).filter((n) => n > 0);
|
|
let nameById = new Map<number, string>();
|
|
if (staffIds.length) {
|
|
const users = await prisma.user
|
|
.findMany({
|
|
where: { id: { in: staffIds } },
|
|
select: { id: true, username: true },
|
|
})
|
|
.catch(() => []);
|
|
nameById = new Map(users.map((x) => [x.id, x.username]));
|
|
}
|
|
|
|
const recentCount = activities.filter(
|
|
(a) => a.createdAt && new Date(a.createdAt).getTime() > Date.now() - 60_000,
|
|
).length;
|
|
|
|
const rows = activities.map((a) => ({
|
|
id: Number(a.id),
|
|
when: formatDate(a.createdAt),
|
|
staff: t("staffWithId", {
|
|
id: Number(a.userId),
|
|
name: nameById.get(Number(a.userId)) ?? t("unknownUser"),
|
|
}),
|
|
action: a.action,
|
|
description: a.description,
|
|
target:
|
|
a.targetType && a.targetId
|
|
? `${a.targetType === "user" ? t("userPrefix") : a.targetType} #${String(a.targetId)}`
|
|
: "—",
|
|
ip: a.ipAddress || "—",
|
|
}));
|
|
|
|
return (
|
|
<AdminPageShell
|
|
icon={ClipboardList}
|
|
title={t("title")}
|
|
subtitle={t("subtitle")}
|
|
>
|
|
<div className="space-y-6">
|
|
<div className="grid grid-cols-[repeat(auto-fit,minmax(180px,1fr))] gap-3.5">
|
|
<StatusCard label={t("totalActivities")} value={total} icon="📋" />
|
|
<StatusCard
|
|
label={t("onThisPage")}
|
|
value={activities.length}
|
|
icon="📄"
|
|
/>
|
|
<StatusCard
|
|
label={t("recent")}
|
|
value={recentCount}
|
|
state={recentCount > 0 ? "ok" : "neutral"}
|
|
icon="🟢"
|
|
/>
|
|
</div>
|
|
|
|
<StaffActivitiesTable
|
|
data={{
|
|
rows,
|
|
total,
|
|
page: pagination.page,
|
|
perPage: pagination.perPage,
|
|
lastPage: pagination.lastPage,
|
|
}}
|
|
/>
|
|
</div>
|
|
</AdminPageShell>
|
|
);
|
|
}
|