Track HiveGrid Cloud planning reference
This commit is contained in:
1 parent
b3d5ea6764
commit
e341c5938a
5784 files changed
+1104929
-3
No files matched your search
@@ -0,0 +1,150 @@
|
||||
import type { AdminAuditLogFilters, AdminAuditLogRow } from "@toolctrl/db/logs.server";
|
||||
import { getTenantAuditLogList, serializeAdminAuditLogsCsv } from "@toolctrl/db/logs.server";
|
||||
import { LoaderFunctionArgs, MetaFunction, useLoaderData } from "react-router";
|
||||
import { useParams } from "react-router";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { FilterablePropertyDto } from "~/application/dtos/data/FilterablePropertyDto";
|
||||
import { PaginationDto } from "~/application/dtos/data/PaginationDto";
|
||||
import TenantActivityLogToolbar from "~/components/activity/TenantActivityLogToolbar";
|
||||
import LogsTable from "~/components/app/events/LogsTable";
|
||||
import EditPageLayout from "~/components/ui/layouts/EditPageLayout";
|
||||
import UrlUtils from "~/utils/app/UrlUtils";
|
||||
import { getTranslations } from "~/locale/i18next.server";
|
||||
import { verifyUserHasPermission } from "~/utils/helpers/.server/PermissionsService";
|
||||
import { getFiltersFromCurrentUrl, getPaginationFromCurrentUrl } from "~/utils/helpers/RowPaginationHelper";
|
||||
import { getTenantIdFromUrl } from "~/utils/services/.server/urlService";
|
||||
import Tabs from "~/components/ui/tabs/Tabs";
|
||||
|
||||
const CSV_EXPORT_TAKE = 500;
|
||||
|
||||
type LoaderData = {
|
||||
title: string;
|
||||
items: AdminAuditLogRow[];
|
||||
pagination: PaginationDto;
|
||||
filterableProperties: FilterablePropertyDto[];
|
||||
};
|
||||
|
||||
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
||||
const tenantId = await getTenantIdFromUrl(params);
|
||||
await verifyUserHasPermission(request, "app.settings.auditTrails.view", tenantId);
|
||||
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 getTenantAuditLogList(tenantId, { ...auditLogFilters, take: CSV_EXPORT_TAKE, skip: 0 });
|
||||
return new Response(serializeAdminAuditLogsCsv(rows), {
|
||||
headers: {
|
||||
"Content-Type": "text/csv; charset=utf-8",
|
||||
"Content-Disposition": 'attachment; filename="tenant-audit-logs.csv"',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
const currentPagination = getPaginationFromCurrentUrl(urlSearchParams);
|
||||
const pageSize = normalizePositiveInteger(currentPagination.pageSize, 10);
|
||||
const page = normalizePositiveInteger(currentPagination.page, 1);
|
||||
const { rows, total, take } = await getTenantAuditLogList(tenantId, {
|
||||
...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: "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"),
|
||||
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>();
|
||||
const params = useParams();
|
||||
return (
|
||||
<EditPageLayout
|
||||
// title={t("models.log.plural")}
|
||||
>
|
||||
<div data-ui-id="ui.settings.logs.page" className="space-y-2">
|
||||
<div className="flex justify-between gap-2">
|
||||
<div data-ui-id="ui.settings.logs.tabs" className="grow">
|
||||
<Tabs
|
||||
tabs={[
|
||||
{
|
||||
name: t("models.log.plural"),
|
||||
routePath: UrlUtils.getModulePath(params, "logs"),
|
||||
},
|
||||
{
|
||||
name: t("models.event.plural"),
|
||||
routePath: UrlUtils.getModulePath(params, "logs/events"),
|
||||
},
|
||||
]}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div data-ui-id="ui.settings.logs.filters">
|
||||
<TenantActivityLogToolbar filters={data.filterableProperties} />
|
||||
</div>
|
||||
</div>
|
||||
<div data-ui-id="ui.settings.logs.table">
|
||||
<LogsTable withTenant={false} items={data.items} pagination={data.pagination} />
|
||||
</div>
|
||||
</div>
|
||||
</EditPageLayout>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user