72 lines
3.4 KiB
TypeScript
72 lines
3.4 KiB
TypeScript
// P17 HR-Hub workflows (AK-P17b) — employee payslips. Own list; opening a
|
|
// payslip marks it read and audits the read, then returns the File id to open.
|
|
// Read needs hr.read_own. Transitional UI.
|
|
|
|
import { type ActionFunctionArgs, type LoaderFunctionArgs, useFetcher, useLoaderData } from "react-router";
|
|
import { buildHrPayslipOwnVM, type HrPayslipOwnVM } from "@hivegrid/view-models";
|
|
import { getTranslations } from "~/locale/i18next.server";
|
|
import { badRequestResponse, hrActionErrorResponse, hrHasPermission, loadHrContext, notFoundResponse, requireHrPermission } from "~/utils/integrations/hrAccess.server";
|
|
import { listOwnPayslips, openPayslip } from "~/utils/integrations/hrPayslipService.server";
|
|
import { hrPayslipOpenPath, hrPayslipsPath } from "~/utils/integrations/hrRoutePaths";
|
|
import ServerError from "~/components/ui/errors/ServerError";
|
|
import { HrPayslipsPage } from "~/components/screens/hr/HrPayslipsPage";
|
|
|
|
type LoaderData = { title: string; selfHref: string; filesBase: string; vm: HrPayslipOwnVM };
|
|
|
|
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.payslips.title")} | ${process.env.APP_NAME}`;
|
|
const selfHref = hrPayslipsPath(tenantSlug);
|
|
const filesBase = `/${tenantSlug}/files`;
|
|
if (!ctx.hasModule) return Response.json({ title, selfHref, filesBase, vm: buildHrPayslipOwnVM({ rows: [] }) } satisfies LoaderData);
|
|
try {
|
|
requireHrPermission(ctx, "hr.read_own");
|
|
} catch {
|
|
throw notFoundResponse();
|
|
}
|
|
const payslips = await listOwnPayslips(ctx.tenantId, ctx.userId);
|
|
const vm = buildHrPayslipOwnVM({
|
|
rows: payslips.map((p) => ({ id: p.id, periodYear: p.periodYear, periodMonth: p.periodMonth, isRead: p.isRead, openHref: hrPayslipOpenPath(tenantSlug, p.id) })),
|
|
});
|
|
return Response.json({ title, selfHref, filesBase, vm } satisfies LoaderData);
|
|
};
|
|
|
|
export const action = async ({ request, params }: ActionFunctionArgs) => {
|
|
const ctx = await loadHrContext({ request, params, requireModule: true });
|
|
requireHrPermission(ctx, "hr.read_own");
|
|
const form = await request.formData();
|
|
if (form.get("intent")?.toString() !== "open") throw badRequestResponse();
|
|
try {
|
|
const fileId = await openPayslip({ tenantId: ctx.tenantId, payslipId: form.get("payslipId")?.toString() ?? "", userId: ctx.userId, canReadAll: hrHasPermission(ctx, "hr.read_all") });
|
|
return Response.json({ ok: true, fileId });
|
|
} catch (error) {
|
|
if (error instanceof Response) throw error;
|
|
return hrActionErrorResponse(error);
|
|
}
|
|
};
|
|
|
|
export default function HrPayslipsRoute() {
|
|
const { selfHref, filesBase, vm } = useLoaderData<LoaderData>();
|
|
const fetcher = useFetcher();
|
|
|
|
async function open(payslipId: string) {
|
|
const body = new FormData();
|
|
body.set("intent", "open");
|
|
body.set("payslipId", payslipId);
|
|
const response = await fetch(selfHref, { method: "POST", body });
|
|
if (!response.ok) return;
|
|
const data = (await response.json()) as { fileId?: string | null };
|
|
if (data.fileId) window.open(`${filesBase}/${data.fileId}`, "_blank", "noreferrer");
|
|
}
|
|
|
|
return <HrPayslipsPage vm={vm} submitting={fetcher.state !== "idle"} actions={{ onOpen: open }} />;
|
|
}
|
|
|
|
export function ErrorBoundary() {
|
|
return <ServerError />;
|
|
}
|
|
|
|
export const handle = { moduleKey: "hr" };
|