168 lines
5.9 KiB
TypeScript
168 lines
5.9 KiB
TypeScript
// P17 HR-Hub core (AK-P17a) — employee detail route. Shows master data, lets the
|
|
// employee inline-edit their own self-editable fields, and reveals the encrypted
|
|
// emergency contact ON DEMAND (self or hr.read_all only) — audited. The reveal
|
|
// surfaces the M002 "encryption not provisioned" Klartext until the AD-8 envelope
|
|
// ticket lands (crypto seam deferred).
|
|
|
|
import { type ActionFunctionArgs, type LoaderFunctionArgs, useFetcher, useLoaderData } from "react-router";
|
|
import {
|
|
badRequestResponse,
|
|
hrActionErrorResponse,
|
|
hrHasPermission,
|
|
loadHrContext,
|
|
notFoundResponse,
|
|
requireHrPermission,
|
|
} from "~/utils/integrations/hrAccess.server";
|
|
import { getProfileByUser, resolveUserLabels, revealEmergencyContact, setEmergencyContact, updateOwnProfile } from "~/utils/integrations/hrService.server";
|
|
import { hrDirectoryPath } from "~/utils/integrations/hrRoutePaths";
|
|
import ServerError from "~/components/ui/errors/ServerError";
|
|
import {
|
|
HrDirectoryDetailPage,
|
|
type RevealedEmergencyContact,
|
|
} from "~/components/screens/hr/HrDirectoryDetailPage";
|
|
|
|
type LoaderData = {
|
|
title: string;
|
|
backHref: string;
|
|
userId: string;
|
|
name: string;
|
|
email: string;
|
|
position: string | null;
|
|
team: string | null;
|
|
isSelf: boolean;
|
|
canRevealEmergency: boolean;
|
|
canEditEmergency: boolean;
|
|
hasEmergencyContact: boolean;
|
|
};
|
|
|
|
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
|
const ctx = await loadHrContext({ request, params });
|
|
const tenantSlug = params.tenant ?? "";
|
|
const userId = params.userId ?? "";
|
|
|
|
if (!ctx.hasModule) {
|
|
throw notFoundResponse();
|
|
}
|
|
try {
|
|
requireHrPermission(ctx, "hr.read_own");
|
|
} catch {
|
|
throw notFoundResponse();
|
|
}
|
|
|
|
const isSelf = userId === ctx.userId;
|
|
// Non-self profiles require hr.read_all (HR-admin view); else 404 (A.5.3).
|
|
if (!isSelf && !hrHasPermission(ctx, "hr.read_all")) {
|
|
throw notFoundResponse();
|
|
}
|
|
|
|
const [profile, labels] = await Promise.all([getProfileByUser(ctx.tenantId, userId), resolveUserLabels([userId])]);
|
|
const labeled = labels.get(userId);
|
|
|
|
return Response.json({
|
|
title: `${labeled?.label ?? userId} | ${process.env.APP_NAME}`,
|
|
backHref: hrDirectoryPath(tenantSlug),
|
|
userId,
|
|
name: labeled?.label ?? userId,
|
|
email: labeled?.email ?? "",
|
|
position: profile?.position ?? null,
|
|
team: profile?.team ?? null,
|
|
isSelf,
|
|
canRevealEmergency: isSelf || hrHasPermission(ctx, "hr.read_all"),
|
|
canEditEmergency: isSelf || hrHasPermission(ctx, "hr.write"),
|
|
hasEmergencyContact: Boolean(profile?.emergencyContactCiphertext),
|
|
} satisfies LoaderData);
|
|
};
|
|
|
|
export const action = async ({ request, params }: ActionFunctionArgs) => {
|
|
const ctx = await loadHrContext({ request, params, requireModule: true });
|
|
const userId = params.userId ?? "";
|
|
const form = await request.formData();
|
|
const intent = form.get("intent")?.toString();
|
|
|
|
try {
|
|
if (intent === "update-own-profile") {
|
|
if (userId !== ctx.userId) {
|
|
// Editing someone else's profile via the self endpoint -> 404.
|
|
throw notFoundResponse();
|
|
}
|
|
requireHrPermission(ctx, "hr.read_own");
|
|
await updateOwnProfile({
|
|
tenantId: ctx.tenantId,
|
|
userId: ctx.userId,
|
|
data: { position: form.get("position")?.toString() ?? null, team: form.get("team")?.toString() ?? null },
|
|
});
|
|
return Response.json({ ok: true });
|
|
}
|
|
|
|
if (intent === "reveal-emergency") {
|
|
const isSelf = userId === ctx.userId;
|
|
if (!isSelf) {
|
|
requireHrPermission(ctx, "hr.read_all");
|
|
} else {
|
|
requireHrPermission(ctx, "hr.read_own");
|
|
}
|
|
const contact = await revealEmergencyContact({ tenantId: ctx.tenantId, userId, revealerUserId: ctx.userId });
|
|
return Response.json({ ok: true, contact });
|
|
}
|
|
|
|
if (intent === "set-emergency") {
|
|
// Self may set their own; setting someone else's requires hr.write (manage).
|
|
const isSelf = userId === ctx.userId;
|
|
requireHrPermission(ctx, isSelf ? "hr.read_own" : "hr.write");
|
|
const name = (form.get("name")?.toString() ?? "").trim();
|
|
const phone = (form.get("phone")?.toString() ?? "").trim();
|
|
const relation = (form.get("relation")?.toString() ?? "").trim();
|
|
if (!name || !phone) {
|
|
throw badRequestResponse();
|
|
}
|
|
await setEmergencyContact({ tenantId: ctx.tenantId, userId, contact: { name, phone, relation } });
|
|
return Response.json({ ok: true });
|
|
}
|
|
|
|
throw badRequestResponse();
|
|
} catch (error) {
|
|
if (error instanceof Response) {
|
|
throw error;
|
|
}
|
|
return hrActionErrorResponse(error);
|
|
}
|
|
};
|
|
|
|
type ActionResult = { ok?: boolean; contact?: RevealedEmergencyContact | null };
|
|
|
|
export default function HrDirectoryDetailRoute() {
|
|
const data = useLoaderData<LoaderData>();
|
|
const saveFetcher = useFetcher<ActionResult>();
|
|
const revealFetcher = useFetcher<ActionResult>();
|
|
const emergencyFetcher = useFetcher<ActionResult>();
|
|
|
|
return (
|
|
<HrDirectoryDetailPage
|
|
backHref={data.backHref}
|
|
name={data.name}
|
|
email={data.email}
|
|
position={data.position}
|
|
team={data.team}
|
|
isSelf={data.isSelf}
|
|
canRevealEmergency={data.canRevealEmergency}
|
|
canEditEmergency={data.canEditEmergency}
|
|
hasEmergencyContact={data.hasEmergencyContact}
|
|
revealedContact={revealFetcher.data?.contact ?? null}
|
|
revealing={revealFetcher.state !== "idle"}
|
|
saving={saveFetcher.state !== "idle"}
|
|
savingEmergency={emergencyFetcher.state !== "idle"}
|
|
actions={{
|
|
onSaveProfile: ({ position, team }) =>
|
|
saveFetcher.submit({ intent: "update-own-profile", position, team }, { method: "post" }),
|
|
onRevealEmergency: () => revealFetcher.submit({ intent: "reveal-emergency" }, { method: "post" }),
|
|
onSetEmergency: ({ name, phone, relation }) =>
|
|
emergencyFetcher.submit({ intent: "set-emergency", name, phone, relation }, { method: "post" }),
|
|
}}
|
|
/>
|
|
);
|
|
}
|
|
|
|
export function ErrorBoundary() {
|
|
return <ServerError />;
|
|
}
|