109 lines
5.0 KiB
TypeScript
109 lines
5.0 KiB
TypeScript
// P17 HR-Hub workflows (AK-P17b) — surveys list. Participate in open surveys,
|
|
// view gated results; managers (hr.write) create surveys and open/close them.
|
|
// Result visibility is enforced by hr.surveys.results. Transitional UI.
|
|
|
|
import { type ActionFunctionArgs, type LoaderFunctionArgs, useFetcher, useLoaderData } from "react-router";
|
|
import { buildHrSurveyListVM, type HrSurveyListVM } from "@hivegrid/view-models";
|
|
import { getTranslations } from "~/locale/i18next.server";
|
|
import { badRequestResponse, hrActionErrorResponse, hrHasPermission, loadHrContext, notFoundResponse, requireHrPermission } from "~/utils/integrations/hrAccess.server";
|
|
import { archiveSurvey, createSurvey, hasSubmitted, listSurveys, setSurveyStatus, type HrSurveyResultVisibility, type HrSurveyStatus } from "~/utils/integrations/hrSurveyService.server";
|
|
import { hrSurveyParticipatePath, hrSurveyResultsPath, hrSurveysPath } from "~/utils/integrations/hrRoutePaths";
|
|
import ServerError from "~/components/ui/errors/ServerError";
|
|
import { HrSurveysListPage } from "~/components/screens/hr/HrSurveysListPage";
|
|
|
|
type LoaderData = { title: string; vm: HrSurveyListVM; canManage: boolean; statuses: HrSurveyStatus[] };
|
|
const VISIBILITIES: HrSurveyResultVisibility[] = ["admins", "participants", "everyone"];
|
|
|
|
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
|
const { t } = await getTranslations(request);
|
|
const ctx = await loadHrContext({ request, params });
|
|
const tenantSlug = params.tenant ?? "";
|
|
const title = `${t("hr.surveys.title")} | ${process.env.APP_NAME}`;
|
|
const createHref = hrSurveysPath(tenantSlug);
|
|
if (!ctx.hasModule) return Response.json({ title, vm: buildHrSurveyListVM({ rows: [], canManage: false, createHref }), canManage: false, statuses: [] } satisfies LoaderData);
|
|
try {
|
|
requireHrPermission(ctx, "hr.read_own");
|
|
} catch {
|
|
throw notFoundResponse();
|
|
}
|
|
const canManage = hrHasPermission(ctx, "hr.write");
|
|
const canResults = hrHasPermission(ctx, "hr.surveys.results");
|
|
|
|
const surveys = await listSurveys(ctx.tenantId);
|
|
const rows = await Promise.all(
|
|
surveys.map(async (s) => {
|
|
const submitted = await hasSubmitted(ctx.tenantId, s.id, ctx.userId, s.isAnonymous);
|
|
const canViewResults = canResults || s.resultVisibility === "everyone" || (s.resultVisibility === "participants" && submitted);
|
|
return {
|
|
id: s.id,
|
|
title: s.title,
|
|
status: s.status,
|
|
isAnonymous: s.isAnonymous,
|
|
hasSubmitted: submitted,
|
|
canViewResults,
|
|
participateHref: hrSurveyParticipatePath(tenantSlug, s.id),
|
|
resultsHref: hrSurveyResultsPath(tenantSlug, s.id),
|
|
};
|
|
}),
|
|
);
|
|
return Response.json({ title, vm: buildHrSurveyListVM({ rows, canManage, createHref }), canManage, statuses: ["open", "closed"] } satisfies LoaderData);
|
|
};
|
|
|
|
export const action = async ({ request, params }: ActionFunctionArgs) => {
|
|
const ctx = await loadHrContext({ request, params, requireModule: true });
|
|
requireHrPermission(ctx, "hr.write");
|
|
const form = await request.formData();
|
|
const intent = form.get("intent")?.toString();
|
|
const surveyId = form.get("surveyId")?.toString() ?? "";
|
|
try {
|
|
if (intent === "create") {
|
|
const visibility = (form.get("resultVisibility")?.toString() ?? "admins") as HrSurveyResultVisibility;
|
|
await createSurvey({
|
|
tenantId: ctx.tenantId,
|
|
authorUserId: ctx.userId,
|
|
title: form.get("title")?.toString() ?? "",
|
|
description: form.get("description")?.toString() || null,
|
|
isAnonymous: form.get("isAnonymous") === "1",
|
|
resultVisibility: VISIBILITIES.includes(visibility) ? visibility : "admins",
|
|
});
|
|
return Response.json({ ok: true });
|
|
}
|
|
if (intent === "open" || intent === "close") {
|
|
await setSurveyStatus({ tenantId: ctx.tenantId, actorUserId: ctx.userId, surveyId, status: intent === "open" ? "open" : "closed" });
|
|
return Response.json({ ok: true });
|
|
}
|
|
if (intent === "archive") {
|
|
await archiveSurvey({ tenantId: ctx.tenantId, actorUserId: ctx.userId, surveyId });
|
|
return Response.json({ ok: true });
|
|
}
|
|
throw badRequestResponse();
|
|
} catch (error) {
|
|
if (error instanceof Response) throw error;
|
|
return hrActionErrorResponse(error);
|
|
}
|
|
};
|
|
|
|
export default function HrSurveysRoute() {
|
|
const { vm, canManage } = useLoaderData<LoaderData>();
|
|
const fetcher = useFetcher();
|
|
return (
|
|
<HrSurveysListPage
|
|
vm={vm}
|
|
canManage={canManage}
|
|
submitting={fetcher.state !== "idle"}
|
|
actions={{
|
|
onCreate: ({ title, description, isAnonymous, resultVisibility }) =>
|
|
fetcher.submit({ intent: "create", title, description, isAnonymous: isAnonymous ? "1" : "0", resultVisibility }, { method: "post" }),
|
|
onSetStatus: (surveyId, status) => fetcher.submit({ intent: status, surveyId }, { method: "post" }),
|
|
onArchive: (surveyId) => fetcher.submit({ intent: "archive", surveyId }, { method: "post" }),
|
|
}}
|
|
/>
|
|
);
|
|
}
|
|
|
|
export function ErrorBoundary() {
|
|
return <ServerError />;
|
|
}
|
|
|
|
export const handle = { moduleKey: "hr" };
|