- env.ts: added 10 missing Zod-validated env vars (imager, paypal currency, argon2/bcrypt params) - Migrated 6 modules from process.env to validated env.* (auth, proxy-auth, paypal, password, redis, imager, moderation, alert, logger) - Replaced console.warn/error with pino logger in 9 server-side modules - Removed 50+ dead exports (SWF wrappers, coalesceHotelName, signIn, isStaff re-export, formatTimestamp, Skeleton/SkeletonCard, 4 unused housekeeping sections) - Consolidated date formatting: 28 files migrated to shared formatDate() from @/lib/format-date - Wired 4 radio/settings API routes through cached siteSettings service instead of raw Prisma queries - Added getMany()/getAll() helpers to SiteSettings service - Removed 88 dead Prisma model definitions (schema 2763→1846 lines) - Created admin action-helper.ts with wrapAction() for standardized error handling - Fixed useEffect dependency arrays in 4 data-heavy components - Replaced raw btn CSS classes with shadcn Button component across admin pages - Stripped dead i18n namespaces (common, pages.client) from all 22 translation files - Removed 2 dead scripts (create-release.sh, check-local-imports.ts) - Fixed knip.json configuration - Added 7 new test suites: format-date, paypal, moderation, alert, webhook, action-helper, and fixed password.test.ts for env mocking - All 358 tests passing across 72 test files - TypeScript: 0 errors
128 lines
3.4 KiB
TypeScript
128 lines
3.4 KiB
TypeScript
import { getTranslations } from "next-intl/server";
|
|
import { redirect } from "next/navigation";
|
|
import { StatusCard } from "@/components/admin/dashboard";
|
|
import { buildStaffActivityWhere } from "@/lib/admin/log-filters";
|
|
import { calcPagination, parseListParams } from "@/lib/admin-helpers";
|
|
import { canAccess, getAdminContext, PERMS } from "@/lib/permissions";
|
|
import { formatDate } from "@/lib/format-date";
|
|
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 (
|
|
<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>
|
|
);
|
|
}
|