Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
+85
-20
@@ -1,46 +1,111 @@
|
||||
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 { getAdminDashboardStats } from "~/utils/services/adminDashboardService";
|
||||
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;
|
||||
stats: Stat[];
|
||||
tenants: {
|
||||
items: TenantWithUsage[];
|
||||
pagination: PaginationDto;
|
||||
};
|
||||
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 urlSearchParams = new URL(request.url).searchParams;
|
||||
const currentPagination = getPaginationFromCurrentUrl(urlSearchParams);
|
||||
|
||||
const { t } = await time(getTranslations(request), "getTranslations");
|
||||
const { stats, tenants } = await time(
|
||||
promiseHash({
|
||||
stats: time(
|
||||
getAdminDashboardStats({ t, gte: PeriodHelper.getGreaterThanOrEqualsFromRequest({ request }) }),
|
||||
"admin.dashboard.details.getAdminDashboardStats"
|
||||
),
|
||||
tenants: time(adminGetAllTenantsWithUsage(undefined, currentPagination), "admin.dashboard.details.adminGetAllTenantsWithUsage"),
|
||||
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.details"
|
||||
"admin.dashboard.personalized"
|
||||
);
|
||||
|
||||
const data: AdminDashboardLoaderData = {
|
||||
title: `${t("app.sidebar.dashboard")} | ${process.env.APP_NAME}`,
|
||||
stats,
|
||||
tenants,
|
||||
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"
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user