Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/web/app/utils/data/.server/adminDashboardRouteData.ts
T

112 lines
4.1 KiB
TypeScript

import type { LoaderFunctionArgs } from "react-router";
import type { AdminDashboardVM } from "@hivegrid/view-models";
import {
isAdminRoleKey,
isAdminPermissionName,
type AdminPermissionName,
type AdminRoleKey,
} from "@hivegrid/db/admin-permissions";
import { listAdminPermissionNames } from "@hivegrid/db/admin-rbac.server";
import { getTranslations } from "~/locale/i18next.server";
import { createMetrics } from "~/modules/metrics/services/.server/MetricTracker";
import { adminGetAllTenantsWithUsage, type TenantWithUsage } from "~/utils/db/tenants.db.server";
import { getPaginationFromCurrentUrl } from "~/utils/helpers/RowPaginationHelper";
import PeriodHelper from "~/utils/helpers/PeriodHelper";
import { promiseHash } from "~/utils/promises/promiseHash";
import { getAdminDashboardOverview, getAdminDashboardStats, type AdminDashboardOverview } from "~/utils/services/adminDashboardService";
import type { Stat } from "~/application/dtos/stats/Stat";
import type { PaginationDto } from "~/application/dtos/data/PaginationDto";
import { runAsAdminIdentity } from "~/services/admin/admin-identity.server";
import { ADMIN_AREA_ACCESS } from "~/services/admin/admin-access-contract";
import { loadPersonalizedAdminDashboard } from "~/services/admin/admin-dashboard-personalization.server";
import { db } from "~/utils/db.server";
export type AdminDashboardLoaderData = {
title: string;
dashboard: AdminDashboardVM;
superOverview: {
stats: Stat[];
overview: AdminDashboardOverview;
tenants: {
items: TenantWithUsage[];
pagination: PaginationDto;
};
} | null;
};
type AdminDashboardDataArgs = Pick<LoaderFunctionArgs, "request" | "params">;
export async function loadAdminDashboardRouteData({ request, params }: AdminDashboardDataArgs) {
const { time, getServerTimingHeader } = await createMetrics({ request, params }, "admin.dashboard");
const { t, locale } = await time(getTranslations(request), "getTranslations");
const { dashboard, superOverview } = await time(
runAsAdminIdentity(request, ADMIN_AREA_ACCESS.dashboard, async (session) => {
const [permissionNames, roleRows] = await Promise.all([
listAdminPermissionNames(session.adminUserId),
db.adminUserRole.findMany({
where: {
adminUserId: session.adminUserId,
role: { active: true },
},
select: { role: { select: { systemKey: true } } },
orderBy: { createdAt: "asc" },
}),
]);
const permissions = permissionNames.filter(isAdminPermissionName);
const roleKeys = roleRows
.map((row) => row.role.systemKey)
.filter((roleKey): roleKey is AdminRoleKey =>
Boolean(roleKey && isAdminRoleKey(roleKey))
);
const dashboard = await loadPersonalizedAdminDashboard({
adminUserId: session.adminUserId,
roleKeys,
permissions: permissions as AdminPermissionName[],
locale,
});
const superOverview = roleKeys.includes("super_admin")
? await loadSuperAdminOverview(request, t, time)
: null;
return { dashboard, superOverview };
}),
"admin.dashboard.personalized"
);
const data: AdminDashboardLoaderData = {
title: `${t("app.sidebar.dashboard")} | ${process.env.APP_NAME}`,
dashboard,
superOverview,
};
return { data, headers: getServerTimingHeader() };
}
async function loadSuperAdminOverview(
request: Request,
t: (key: string) => string,
time: <T>(promise: Promise<T>, name: string) => Promise<T>
) {
const currentPagination = getPaginationFromCurrentUrl(
new URL(request.url).searchParams
);
return time(
promiseHash({
stats: time(
getAdminDashboardStats({
t,
gte: PeriodHelper.getGreaterThanOrEqualsFromRequest({ request }),
}),
"admin.dashboard.details.getAdminDashboardStats"
),
overview: time(
getAdminDashboardOverview(),
"admin.dashboard.details.getAdminDashboardOverview"
),
tenants: time(
adminGetAllTenantsWithUsage(undefined, currentPagination),
"admin.dashboard.details.adminGetAllTenantsWithUsage"
),
}),
"admin.dashboard.super"
);
}