109 lines
3.6 KiB
TypeScript
109 lines
3.6 KiB
TypeScript
import type { AdminAuditLogRow } from "@hivegrid/db/logs.server";
|
|
import { getAdminAuditLogList } 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 { AdminPageHeader } from "~/components/screens/admin/AdminPageHeader";
|
|
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";
|
|
import { getAdminAuditLogFiltersFromUrl } from "./audit-trails.helpers.server";
|
|
|
|
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 = getAdminAuditLogFiltersFromUrl(urlSearchParams);
|
|
|
|
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 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 (
|
|
<div className="flex w-full flex-col">
|
|
<AdminPageHeader
|
|
title={t("models.log.plural")}
|
|
description={t("admin.auditTrails.pageDescription")}
|
|
actions={<AdminAuditLogToolbar filters={data.filterableProperties} />}
|
|
/>
|
|
<div className="mobile:px-4 w-full space-y-2 px-6 py-6">
|
|
<LogsTable withTenant={true} items={data.items} pagination={data.pagination} />
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|