155 lines
5.1 KiB
TypeScript
155 lines
5.1 KiB
TypeScript
// R-UX3 personnel task hub. The loader resolves every count, section and action
|
|
// server-side; the presentational screen never infers permissions from roles.
|
|
|
|
import { type LoaderFunctionArgs, useLoaderData } from "react-router";
|
|
import { buildHrHubVM, type HrHubHrefs, type HrHubVM } from "@hivegrid/view-models";
|
|
import { getTranslations } from "~/locale/i18next.server";
|
|
import {
|
|
hrHasPermission,
|
|
loadHrContext,
|
|
notFoundResponse,
|
|
requireHrPermission,
|
|
} from "~/utils/integrations/hrAccess.server";
|
|
import { listForApprover as listLeaveForApprover, listOwnVacationRequests } from "~/utils/integrations/hrLeaveService.server";
|
|
import { listForApprover as listRequestsForApprover, listOwnRequests } from "~/utils/integrations/hrRequestService.server";
|
|
import { listOwnSickLeave } from "~/utils/integrations/hrSickLeaveService.server";
|
|
import { listDirectoryForViewer } from "~/utils/integrations/hrService.server";
|
|
import {
|
|
hrAdminAnnouncementsPath,
|
|
hrAdminPayslipsPath,
|
|
hrAdminRequestTemplatesPath,
|
|
hrAdminUploadRemindersPath,
|
|
hrAnnouncementsPath,
|
|
hrDirectoryPath,
|
|
hrLeavePath,
|
|
hrLocationsPath,
|
|
hrMyDocumentsPath,
|
|
hrPayslipsPath,
|
|
hrRequestsPath,
|
|
hrSickPath,
|
|
hrSurveysPath,
|
|
HR_MODULE_KEY,
|
|
} from "~/utils/integrations/hrRoutePaths";
|
|
import ServerError from "~/components/ui/errors/ServerError";
|
|
import { HrHubPage } from "~/components/screens/hr/HrHubPage";
|
|
|
|
type LoaderData = { title: string; vm: HrHubVM };
|
|
|
|
function hubHrefs(tenantSlug: string): HrHubHrefs {
|
|
const leave = hrLeavePath(tenantSlug);
|
|
const requests = hrRequestsPath(tenantSlug);
|
|
return {
|
|
leave,
|
|
sick: hrSickPath(tenantSlug),
|
|
documents: hrMyDocumentsPath(tenantSlug),
|
|
payslips: hrPayslipsPath(tenantSlug),
|
|
announcements: hrAnnouncementsPath(tenantSlug),
|
|
requests,
|
|
surveys: hrSurveysPath(tenantSlug),
|
|
teamDirectory: `${hrDirectoryPath(tenantSlug)}?scope=team`,
|
|
teamLeave: `${leave}?tab=team`,
|
|
leaveApprovals: `${leave}?tab=approvals`,
|
|
requestApprovals: `${requests}?tab=approvals`,
|
|
directory: hrDirectoryPath(tenantSlug),
|
|
locations: hrLocationsPath(tenantSlug),
|
|
announcementAdmin: hrAdminAnnouncementsPath(tenantSlug),
|
|
payslipAdmin: hrAdminPayslipsPath(tenantSlug),
|
|
requestTemplates: hrAdminRequestTemplatesPath(tenantSlug),
|
|
uploadReminders: hrAdminUploadRemindersPath(tenantSlug),
|
|
surveyAdmin: hrSurveysPath(tenantSlug),
|
|
};
|
|
}
|
|
|
|
const emptyCounts = {
|
|
ownLeave: 0,
|
|
ownSick: 0,
|
|
ownRequests: 0,
|
|
pendingLeaveApprovals: 0,
|
|
pendingRequestApprovals: 0,
|
|
visibleTeamMembers: 0,
|
|
};
|
|
|
|
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
|
const { t } = await getTranslations(request);
|
|
const ctx = await loadHrContext({ request, params });
|
|
const tenantSlug = params.tenant ?? "";
|
|
const hrefs = hubHrefs(tenantSlug);
|
|
const title = `${t("hr.hub.title")} | ${process.env.APP_NAME}`;
|
|
|
|
if (!ctx.hasModule) {
|
|
return Response.json({
|
|
title,
|
|
vm: buildHrHubVM({
|
|
moduleActive: false,
|
|
hrefs,
|
|
counts: emptyCounts,
|
|
capabilities: {
|
|
canReadAll: false,
|
|
canWrite: false,
|
|
canApproveLeave: false,
|
|
canViewSurveyResults: false,
|
|
},
|
|
}),
|
|
} satisfies LoaderData);
|
|
}
|
|
|
|
try {
|
|
requireHrPermission(ctx, "hr.read_own");
|
|
} catch {
|
|
throw notFoundResponse();
|
|
}
|
|
|
|
const canReadAll = hrHasPermission(ctx, "hr.read_all");
|
|
const canWrite = hrHasPermission(ctx, "hr.write");
|
|
const canApproveLeave = hrHasPermission(ctx, "hr.leave.approve");
|
|
const canViewSurveyResults = hrHasPermission(ctx, "hr.surveys.results");
|
|
const [ownLeave, ownSick, ownRequests, leaveApprovals, requestApprovals, directory] =
|
|
await Promise.all([
|
|
listOwnVacationRequests(ctx.tenantId, ctx.userId),
|
|
listOwnSickLeave(ctx.tenantId, ctx.userId),
|
|
listOwnRequests(ctx.tenantId, ctx.userId),
|
|
listLeaveForApprover(ctx.tenantId, ctx.userId),
|
|
listRequestsForApprover(ctx.tenantId, ctx.userId),
|
|
listDirectoryForViewer({
|
|
tenantId: ctx.tenantId,
|
|
viewerUserId: ctx.userId,
|
|
canReadAll,
|
|
}),
|
|
]);
|
|
|
|
const vm = buildHrHubVM({
|
|
moduleActive: true,
|
|
hrefs,
|
|
counts: {
|
|
ownLeave: ownLeave.filter((entry) =>
|
|
entry.status === "submitted" || entry.status === "clarification"
|
|
).length,
|
|
ownSick: ownSick.filter((entry) => entry.status === "reported").length,
|
|
ownRequests: ownRequests.filter((entry) =>
|
|
entry.status === "submitted" || entry.status === "clarification"
|
|
).length,
|
|
pendingLeaveApprovals: leaveApprovals.length,
|
|
pendingRequestApprovals: requestApprovals.length,
|
|
visibleTeamMembers: directory.filter((entry) => entry.userId !== ctx.userId).length,
|
|
},
|
|
capabilities: {
|
|
canReadAll,
|
|
canWrite,
|
|
canApproveLeave,
|
|
canViewSurveyResults,
|
|
},
|
|
});
|
|
return Response.json({ title, vm } satisfies LoaderData);
|
|
};
|
|
|
|
export default function HrHubRoute() {
|
|
const { vm } = useLoaderData<LoaderData>();
|
|
return <HrHubPage vm={vm} />;
|
|
}
|
|
|
|
export function ErrorBoundary() {
|
|
return <ServerError />;
|
|
}
|
|
|
|
export const handle = { moduleKey: HR_MODULE_KEY };
|