// P17 HR-Hub core (AK-P17a) — locations route. Loader builds HrLocationListVM with // per-location employee counts. Read needs hr.read_own; managing (create/edit/ // default/reassign) needs hr.write — gated in the action. Module off -> locked // empty; missing permission -> 404. import { type ActionFunctionArgs, type LoaderFunctionArgs, useFetcher, useLoaderData } from "react-router"; import { buildHrLocationListVM, type HrLocationRow as HrLocationVMRow, type HrLocationListVM } from "@hivegrid/view-models"; import { getTranslations } from "~/locale/i18next.server"; import { badRequestResponse, hrActionErrorResponse, hrHasPermission, loadHrContext, notFoundResponse, requireHrPermission, } from "~/utils/integrations/hrAccess.server"; import { archiveLocation, createLocation, listDirectory, listLocations, reassignLocationEmployees, updateLocation } from "~/utils/integrations/hrService.server"; import { hrLocationDetailPath, hrLocationsPath } from "~/utils/integrations/hrRoutePaths"; import ServerError from "~/components/ui/errors/ServerError"; import { HrLocationsPage } from "~/components/screens/hr/HrLocationsPage"; type LoaderData = { title: string; vm: HrLocationListVM }; export const loader = async ({ request, params }: LoaderFunctionArgs) => { const { t } = await getTranslations(request); const ctx = await loadHrContext({ request, params }); const tenantSlug = params.tenant ?? ""; if (!ctx.hasModule) { return Response.json({ title: `${t("hr.locations.title")} | ${process.env.APP_NAME}`, vm: buildHrLocationListVM({ rows: [], canManage: false, createHref: hrLocationsPath(tenantSlug) }) } satisfies LoaderData); } try { requireHrPermission(ctx, "hr.read_own"); } catch { throw notFoundResponse(); } const [locations, directory] = await Promise.all([listLocations(ctx.tenantId), listDirectory(ctx.tenantId)]); const countByLocation = new Map(); for (const entry of directory) { if (entry.locationId) { countByLocation.set(entry.locationId, (countByLocation.get(entry.locationId) ?? 0) + 1); } } const rows: HrLocationVMRow[] = locations.map((location) => ({ id: location.id, name: location.name, addressLine1: location.addressLine1, postalCode: location.postalCode, city: location.city, country: location.country, phone: location.phone, latitude: location.latitude, longitude: location.longitude, description: location.description, isDefault: location.isDefault, employeeCount: countByLocation.get(location.id) ?? 0, detailHref: hrLocationDetailPath(tenantSlug, location.id), })); const vm = buildHrLocationListVM({ rows, canManage: hrHasPermission(ctx, "hr.write"), createHref: hrLocationsPath(tenantSlug) }); return Response.json({ title: `${t("hr.locations.title")} | ${process.env.APP_NAME}`, vm } 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") { await createLocation({ tenantId: ctx.tenantId, name: form.get("name")?.toString() ?? "", city: form.get("city")?.toString() ?? null, isDefault: form.get("isDefault") === "1" }); return Response.json({ ok: true }); } if (intent === "update") { await updateLocation({ tenantId: ctx.tenantId, locationId: form.get("locationId")?.toString() ?? "", name: form.get("name")?.toString(), city: form.get("city")?.toString() }); return Response.json({ ok: true }); } if (intent === "set-default") { await updateLocation({ tenantId: ctx.tenantId, locationId: form.get("locationId")?.toString() ?? "", isDefault: true }); return Response.json({ ok: true }); } if (intent === "archive") { await archiveLocation({ tenantId: ctx.tenantId, locationId: form.get("locationId")?.toString() ?? "" }); return Response.json({ ok: true }); } if (intent === "reassign") { const count = await reassignLocationEmployees({ tenantId: ctx.tenantId, fromLocationId: form.get("fromLocationId")?.toString() ?? "", toLocationId: form.get("toLocationId")?.toString() ?? "" }); return Response.json({ ok: true, count }); } throw badRequestResponse(); } catch (error) { if (error instanceof Response) { throw error; } return hrActionErrorResponse(error); } }; export default function HrLocationsRoute() { const { vm } = useLoaderData(); const fetcher = useFetcher(); return ( fetcher.submit({ intent: "create", name, city, isDefault: isDefault ? "1" : "0" }, { method: "post" }), onSetDefault: (locationId) => fetcher.submit({ intent: "set-default", locationId }, { method: "post" }), onArchive: (locationId) => fetcher.submit({ intent: "archive", locationId }, { method: "post" }), }} /> ); } export function ErrorBoundary() { return ; }