132 lines
6.0 KiB
TypeScript
132 lines
6.0 KiB
TypeScript
// P17 HR-Hub workflows (AK-P17b) — upload-reminder editor (admin). Define
|
|
// reminders (document kind, cadence, audience, mandatory, escalate); pause/resume
|
|
// and archive. Dispatch itself runs in the worker cron. Needs hr.write.
|
|
|
|
import { type ActionFunctionArgs, type LoaderFunctionArgs, useFetcher, useLoaderData } from "react-router";
|
|
import { buildHrReminderListVM, type HrReminderListVM, type HrReminderInputRow, type HrReminderCadenceKind } from "@hivegrid/view-models";
|
|
import { getTranslations } from "~/locale/i18next.server";
|
|
import { badRequestResponse, hrActionErrorResponse, loadHrContext, notFoundResponse, requireHrPermission } from "~/utils/integrations/hrAccess.server";
|
|
import { archiveReminder, createReminder, listDispatches, listReminders, setReminderStatus, type HrAudienceKind, type HrDocumentKind, type HrReminderStatus } from "~/utils/integrations/hrUploadReminderService.server";
|
|
import { hrAdminUploadRemindersPath } from "~/utils/integrations/hrRoutePaths";
|
|
import ServerError from "~/components/ui/errors/ServerError";
|
|
import { HrAdminUploadRemindersPage } from "~/components/screens/hr/HrAdminUploadRemindersPage";
|
|
|
|
type LoaderData = { title: string; vm: HrReminderListVM };
|
|
const CADENCES: HrReminderCadenceKind[] = ["once", "yearly", "every_n_months", "on_expiry"];
|
|
const DOC_KINDS: HrDocumentKind[] = ["contract", "certificate", "identification", "qualification", "other"];
|
|
const AUDIENCES: HrAudienceKind[] = ["all", "team", "location"];
|
|
|
|
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
|
const { t } = await getTranslations(request);
|
|
const ctx = await loadHrContext({ request, params });
|
|
const tenantSlug = params.tenant ?? "";
|
|
const createHref = hrAdminUploadRemindersPath(tenantSlug);
|
|
const title = `${t("hr.reminders.title")} | ${process.env.APP_NAME}`;
|
|
if (!ctx.hasModule) return Response.json({ title, vm: buildHrReminderListVM({ rows: [], createHref }) } satisfies LoaderData);
|
|
try {
|
|
requireHrPermission(ctx, "hr.write");
|
|
} catch {
|
|
throw notFoundResponse();
|
|
}
|
|
const reminders = await listReminders(ctx.tenantId);
|
|
const rows: HrReminderInputRow[] = [];
|
|
for (const reminder of reminders) {
|
|
const dispatches = await listDispatches(ctx.tenantId, reminder.id);
|
|
rows.push({
|
|
id: reminder.id,
|
|
title: reminder.title,
|
|
cadenceKind: reminder.cadenceKind,
|
|
intervalMonths: reminder.intervalMonths,
|
|
documentKind: reminder.documentKind,
|
|
audienceKind: reminder.audienceKind,
|
|
mandatory: reminder.mandatory,
|
|
escalateToManager: reminder.escalateToManager,
|
|
status: reminder.status,
|
|
dueIso: reminder.dueAt ? reminder.dueAt.toISOString() : null,
|
|
pendingCount: dispatches.filter((d) => d.status !== "acknowledged").length,
|
|
detailHref: createHref,
|
|
});
|
|
}
|
|
return Response.json({ title, vm: buildHrReminderListVM({ rows, createHref }) } 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();
|
|
try {
|
|
if (intent === "create") {
|
|
const cadence = (form.get("cadenceKind")?.toString() ?? "once") as HrReminderCadenceKind;
|
|
const docKind = (form.get("documentKind")?.toString() ?? "other") as HrDocumentKind;
|
|
const audience = (form.get("audienceKind")?.toString() ?? "all") as HrAudienceKind;
|
|
const interval = form.get("intervalMonths")?.toString();
|
|
const due = form.get("dueAt")?.toString();
|
|
await createReminder({
|
|
tenantId: ctx.tenantId,
|
|
actorUserId: ctx.userId,
|
|
title: form.get("title")?.toString() ?? "",
|
|
documentKind: DOC_KINDS.includes(docKind) ? docKind : "other",
|
|
cadenceKind: CADENCES.includes(cadence) ? cadence : "once",
|
|
intervalMonths: interval ? Number(interval) : null,
|
|
audienceKind: AUDIENCES.includes(audience) ? audience : "all",
|
|
mandatory: form.get("mandatory") === "1",
|
|
escalateToManager: form.get("escalate") === "1",
|
|
dueAt: due ? new Date(due) : null,
|
|
});
|
|
return Response.json({ ok: true });
|
|
}
|
|
if (intent === "status") {
|
|
await setReminderStatus({ tenantId: ctx.tenantId, actorUserId: ctx.userId, reminderId: form.get("reminderId")?.toString() ?? "", status: (form.get("status")?.toString() ?? "active") as HrReminderStatus });
|
|
return Response.json({ ok: true });
|
|
}
|
|
if (intent === "archive") {
|
|
await archiveReminder({ tenantId: ctx.tenantId, actorUserId: ctx.userId, reminderId: form.get("reminderId")?.toString() ?? "" });
|
|
return Response.json({ ok: true });
|
|
}
|
|
throw badRequestResponse();
|
|
} catch (error) {
|
|
if (error instanceof Response) throw error;
|
|
return hrActionErrorResponse(error);
|
|
}
|
|
};
|
|
|
|
export default function HrAdminUploadRemindersRoute() {
|
|
const { vm } = useLoaderData<LoaderData>();
|
|
const fetcher = useFetcher();
|
|
return (
|
|
<HrAdminUploadRemindersPage
|
|
vm={vm}
|
|
cadences={CADENCES}
|
|
documentKinds={DOC_KINDS}
|
|
audiences={AUDIENCES}
|
|
submitting={fetcher.state !== "idle"}
|
|
actions={{
|
|
onCreate: (input) =>
|
|
fetcher.submit(
|
|
{
|
|
intent: "create",
|
|
title: input.title,
|
|
documentKind: input.documentKind,
|
|
cadenceKind: input.cadenceKind,
|
|
intervalMonths: input.intervalMonths,
|
|
audienceKind: input.audienceKind,
|
|
dueAt: input.dueAt,
|
|
mandatory: input.mandatory ? "1" : "0",
|
|
escalate: input.escalate ? "1" : "0",
|
|
},
|
|
{ method: "post" },
|
|
),
|
|
onSetStatus: (reminderId, status) => fetcher.submit({ intent: "status", reminderId, status }, { method: "post" }),
|
|
onArchive: (reminderId) => fetcher.submit({ intent: "archive", reminderId }, { method: "post" }),
|
|
}}
|
|
/>
|
|
);
|
|
}
|
|
|
|
export function ErrorBoundary() {
|
|
return <ServerError />;
|
|
}
|
|
|
|
export const handle = { moduleKey: "hr" };
|