78 lines
2.9 KiB
TypeScript
78 lines
2.9 KiB
TypeScript
import type { TFunction } from "i18next";
|
|
import { redirect } from "react-router";
|
|
|
|
import { adminGetAllActiveTenants } from "@hivegrid/db/auth-boundary.server";
|
|
import { listAdminPermissionNames } from "@hivegrid/db/admin-rbac.server";
|
|
import { runWithRlsContext } from "@hivegrid/db/rls-context.server";
|
|
|
|
import type { DefaultPermission } from "~/application/dtos/shared/DefaultPermissions";
|
|
import type { TimeFunction } from "~/modules/metrics/services/.server/MetricTracker";
|
|
import UrlUtils from "~/utils/app/UrlUtils";
|
|
import { db } from "~/utils/db.server";
|
|
import { NOT_FOUND_ENVELOPE } from "~/utils/errors.server";
|
|
import { getUserInfo, getValidatedServerSession } from "~/utils/session.server";
|
|
import type { AdminLoaderData } from "../useAdminData";
|
|
|
|
export async function loadAdminData({ request, t: _t }: { request: Request; t: TFunction }, time: TimeFunction) {
|
|
const userInfo = await time(getUserInfo(request), "getUserInfo");
|
|
const url = new URL(request.url);
|
|
if (UrlUtils.stripTrailingSlash(url.pathname) === "/admin") {
|
|
throw Response.json(NOT_FOUND_ENVELOPE, { status: 404 });
|
|
}
|
|
if (!userInfo.userId) {
|
|
const searchParams = new URLSearchParams([["redirect", url.pathname + url.search]]);
|
|
throw redirect(`/login?${searchParams}`);
|
|
}
|
|
|
|
const validatedSession = await time(getValidatedServerSession(request, userInfo), "getValidatedAdminSession");
|
|
if (!validatedSession || !("adminUser" in validatedSession)) {
|
|
throw Response.json(NOT_FOUND_ENVELOPE, { status: 404 });
|
|
}
|
|
const admin = validatedSession.adminUser;
|
|
|
|
const { permissions, roles, myTenants } = await time(
|
|
runWithRlsContext({ userId: admin.id }, async () => ({
|
|
permissions: await listAdminPermissionNames(admin.id),
|
|
roles: await db.adminRole.findMany({
|
|
where: { active: true },
|
|
select: { id: true, name: true, description: true },
|
|
orderBy: { name: "asc" },
|
|
}),
|
|
myTenants: await adminGetAllActiveTenants(),
|
|
})),
|
|
"loadAdminData.getDetails"
|
|
);
|
|
const isSuperAdmin = await runWithRlsContext({ userId: admin.id }, () =>
|
|
db.adminUserRole.findFirst({
|
|
where: { adminUserId: admin.id, role: { systemKey: "super_admin", active: true } },
|
|
select: { adminUserId: true },
|
|
})
|
|
);
|
|
|
|
const data: AdminLoaderData = {
|
|
user: {
|
|
id: admin.id,
|
|
email: admin.email,
|
|
firstName: admin.firstName,
|
|
lastName: admin.lastName,
|
|
avatar: admin.avatar,
|
|
phone: null,
|
|
admin: { userId: admin.id },
|
|
defaultTenantId: null,
|
|
createdAt: validatedSession.createdAt,
|
|
locale: admin.locale,
|
|
lightOrDarkMode: null,
|
|
},
|
|
myTenants,
|
|
currentTenant: null,
|
|
allRoles: roles,
|
|
permissions: permissions as DefaultPermission[],
|
|
isSuperUser: isSuperAdmin !== null,
|
|
isSuperAdmin: isSuperAdmin !== null,
|
|
myGroups: [],
|
|
lng: admin.locale ?? userInfo.lng,
|
|
onboardingSession: null,
|
|
};
|
|
return data;
|
|
}
|