125 lines
4.2 KiB
TypeScript
125 lines
4.2 KiB
TypeScript
import type { AdminAuditLogFilters, AdminAuditLogRow } from "@hivegrid/db/logs.server";
|
|
import { getAdminAuditLogList, serializeAdminAuditLogsCsv } from "@hivegrid/db/logs.server";
|
|
import { useTranslation } from "react-i18next";
|
|
import { LoaderFunctionArgs, MetaFunction, useLoaderData } from "react-router";
|
|
import AdminAuditLogToolbar from "~/components/audit/AdminAuditLogToolbar";
|
|
import LogsTable from "~/components/app/events/LogsTable";
|
|
import IndexPageLayout from "~/components/ui/layouts/IndexPageLayout";
|
|
import { getTranslations } from "~/locale/i18next.server";
|
|
import { verifyUserHasPermission } from "~/utils/helpers/.server/PermissionsService";
|
|
import { getFiltersFromCurrentUrl, getPaginationFromCurrentUrl } from "~/utils/helpers/RowPaginationHelper";
|
|
import { PaginationDto } from "~/application/dtos/data/PaginationDto";
|
|
import { FilterablePropertyDto } from "~/application/dtos/data/FilterablePropertyDto";
|
|
|
|
const CSV_EXPORT_TAKE = 500;
|
|
|
|
type LoaderData = {
|
|
title: string;
|
|
items: AdminAuditLogRow[];
|
|
pagination: PaginationDto;
|
|
filterableProperties: FilterablePropertyDto[];
|
|
};
|
|
|
|
export const loader = async ({ request }: LoaderFunctionArgs) => {
|
|
await verifyUserHasPermission(request, "admin.auditTrails.view");
|
|
const { t } = await getTranslations(request);
|
|
|
|
const filterableProperties = getAuditLogFilterableProperties();
|
|
const filters = getFiltersFromCurrentUrl(request, filterableProperties);
|
|
const urlSearchParams = new URL(request.url).searchParams;
|
|
const auditLogFilters = getAuditLogFiltersFromUrl(urlSearchParams);
|
|
|
|
if (urlSearchParams.get("export") === "csv") {
|
|
const { rows } = await getAdminAuditLogList({ ...auditLogFilters, take: CSV_EXPORT_TAKE, skip: 0 });
|
|
return new Response(serializeAdminAuditLogsCsv(rows), {
|
|
headers: {
|
|
"Content-Type": "text/csv; charset=utf-8",
|
|
"Content-Disposition": 'attachment; filename="admin-audit-logs.csv"',
|
|
},
|
|
});
|
|
}
|
|
|
|
const currentPagination = getPaginationFromCurrentUrl(urlSearchParams);
|
|
const pageSize = normalizePositiveInteger(currentPagination.pageSize, 10);
|
|
const page = normalizePositiveInteger(currentPagination.page, 1);
|
|
const { rows, total, take } = await getAdminAuditLogList({
|
|
...auditLogFilters,
|
|
take: pageSize,
|
|
skip: pageSize * (page - 1),
|
|
});
|
|
|
|
const data: LoaderData = {
|
|
title: `${t("models.log.plural")} | ${process.env.APP_NAME}`,
|
|
items: rows,
|
|
pagination: {
|
|
page,
|
|
pageSize: take,
|
|
totalItems: total,
|
|
totalPages: Math.ceil(total / take),
|
|
sortedBy: currentPagination.sortedBy,
|
|
query: currentPagination.query,
|
|
},
|
|
filterableProperties: filters.properties,
|
|
};
|
|
return data;
|
|
};
|
|
|
|
function getAuditLogFilterableProperties(): FilterablePropertyDto[] {
|
|
return [
|
|
{
|
|
name: "action",
|
|
title: "models.log.action",
|
|
},
|
|
{
|
|
name: "url",
|
|
title: "models.log.url",
|
|
},
|
|
{
|
|
name: "tenantId",
|
|
title: "models.tenant.object",
|
|
},
|
|
{
|
|
name: "userId",
|
|
title: "shared.createdBy",
|
|
},
|
|
{
|
|
name: "createdAtFrom",
|
|
title: "shared.createdAt",
|
|
},
|
|
{
|
|
name: "createdAtTo",
|
|
title: "shared.createdAt",
|
|
},
|
|
];
|
|
}
|
|
|
|
function getAuditLogFiltersFromUrl(urlSearchParams: URLSearchParams): AdminAuditLogFilters {
|
|
return {
|
|
q: urlSearchParams.get("q"),
|
|
action: urlSearchParams.get("action"),
|
|
url: urlSearchParams.get("url"),
|
|
tenantId: urlSearchParams.get("tenantId"),
|
|
userId: urlSearchParams.get("userId"),
|
|
createdAtFrom: urlSearchParams.get("createdAtFrom"),
|
|
createdAtTo: urlSearchParams.get("createdAtTo"),
|
|
};
|
|
}
|
|
|
|
function normalizePositiveInteger(value: number | null | undefined, fallback: number): number {
|
|
if (!Number.isFinite(value) || !value || value < 1) return fallback;
|
|
return Math.trunc(value);
|
|
}
|
|
|
|
export const meta: MetaFunction<typeof loader> = ({ data }) => [{ title: data?.title }];
|
|
|
|
export default function Events() {
|
|
const { t } = useTranslation();
|
|
const data = useLoaderData<LoaderData>();
|
|
|
|
return (
|
|
<IndexPageLayout title={t("models.log.plural")} buttons={<AdminAuditLogToolbar filters={data.filterableProperties} />}>
|
|
<LogsTable withTenant={true} items={data.items} pagination={data.pagination} />
|
|
</IndexPageLayout>
|
|
);
|
|
}
|