Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
+101
-166
@@ -1,183 +1,118 @@
|
||||
import { useLoaderData, type ActionFunctionArgs, type LoaderFunctionArgs } from "react-router";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import {
|
||||
archivePipeline,
|
||||
createPipeline,
|
||||
duplicatePipeline,
|
||||
ensureDefaultSalesPipeline,
|
||||
listPipelines,
|
||||
updatePipeline,
|
||||
upsertPipelineAccess,
|
||||
upsertPipelineFieldConfig,
|
||||
upsertPipelineFieldVisibility,
|
||||
} from "~/utils/integrations/projects/projectService.server";
|
||||
import { upsertAuftragPipelineStageConfig } from "~/utils/integrations/projects/auftragService.server";
|
||||
import { loadProjectContext, projectActionErrorResponse, requireProjectPermission } from "~/utils/integrations/projects/projectAccess.server";
|
||||
import type { CreatePipelineStageInput, PipelinePrincipalType, PipelineRow, PipelineStageRow, PipelineTypHint } from "~/utils/integrations/projects/projectService.server";
|
||||
// WF-P8 S07 "Boards-Übersicht" route: the board table. Each row links to S04
|
||||
// (admin.pipelines.$pipelineId) for the per-board configuration. Both routes
|
||||
// share handlePipelineAdminAction so the write path stays single-sourced.
|
||||
|
||||
type LoaderData = { pipelines: Array<PipelineRow & { stages: PipelineStageRow[] }> };
|
||||
import { useActionData, useLoaderData, useNavigation, useSubmit, type ActionFunctionArgs, type LoaderFunctionArgs } from "react-router";
|
||||
import { buildPipelineAdminVM, type PipelineAdminVM } from "@hivegrid/view-models";
|
||||
import {
|
||||
ensureDefaultSalesPipeline,
|
||||
listPipelinesForAdmin,
|
||||
loadPipelineAdminCounts,
|
||||
} from "~/utils/integrations/projects/projectService.server";
|
||||
import { handlePipelineAdminAction } from "~/utils/integrations/projects/pipelineAdminAction.server";
|
||||
import { loadProjectContext, requireProjectPermission } from "~/utils/integrations/projects/projectAccess.server";
|
||||
import {
|
||||
PROJECTS_MODULE_KEY,
|
||||
projectBoardSettingsPath,
|
||||
projectLostReasonsAdminPath,
|
||||
projectsBoardsPath,
|
||||
} from "~/utils/integrations/projects/projectRoutePaths";
|
||||
import { PipelineAdminPage } from "~/components/screens/projects/PipelineAdminPage";
|
||||
import { getTranslations } from "~/locale/i18next.server";
|
||||
import { v2MetaFunction } from "~/utils/compat/v2MetaFunction";
|
||||
import { useSaveToast } from "~/hooks/use-save-toast";
|
||||
|
||||
type LoaderData = {
|
||||
vm: PipelineAdminVM;
|
||||
title: string;
|
||||
breadcrumbAncestors: { label: string; href: string }[];
|
||||
lostReasonsHref: string;
|
||||
tenantSlug: string;
|
||||
};
|
||||
|
||||
export const meta: v2MetaFunction<LoaderData> = ({ data }) => [{ title: data?.title }];
|
||||
|
||||
export async function loader({ request, params }: LoaderFunctionArgs) {
|
||||
const ctx = requireProjectPermission(await loadProjectContext({ request, params, requireModule: true }), "projects.admin");
|
||||
const { t } = await getTranslations(request);
|
||||
await ensureDefaultSalesPipeline({ tenantId: ctx.tenantId, userId: ctx.userId });
|
||||
const pipelines = await listPipelines({ tenantId: ctx.tenantId });
|
||||
return Response.json({ pipelines } satisfies LoaderData);
|
||||
}
|
||||
|
||||
function formString(formData: FormData, name: string): string | null {
|
||||
const value = formData.get(name);
|
||||
return typeof value === "string" ? value : null;
|
||||
}
|
||||
|
||||
function formBoolean(formData: FormData, name: string): boolean | undefined {
|
||||
const value = formString(formData, name);
|
||||
if (value === null) return undefined;
|
||||
return value === "true" || value === "1" || value === "on";
|
||||
}
|
||||
|
||||
function formNumber(formData: FormData, name: string): number | undefined {
|
||||
const value = formString(formData, name);
|
||||
if (!value) return undefined;
|
||||
const parsed = Number(value);
|
||||
return Number.isFinite(parsed) ? parsed : undefined;
|
||||
}
|
||||
|
||||
function formStages(formData: FormData): CreatePipelineStageInput[] {
|
||||
const raw = formString(formData, "stages");
|
||||
if (!raw) return [];
|
||||
const parsed = JSON.parse(raw) as unknown;
|
||||
if (!Array.isArray(parsed)) return [];
|
||||
return parsed.filter((stage): stage is CreatePipelineStageInput => typeof stage === "object" && stage !== null && "name" in stage && "stageKind" in stage);
|
||||
const [pipelines, counts] = await Promise.all([
|
||||
listPipelinesForAdmin({ tenantId: ctx.tenantId }),
|
||||
loadPipelineAdminCounts({ tenantId: ctx.tenantId }),
|
||||
]);
|
||||
const vm = buildPipelineAdminVM({
|
||||
canAdmin: true,
|
||||
pipelines: pipelines.map((pipeline) => ({
|
||||
id: pipeline.id,
|
||||
name: pipeline.name,
|
||||
typHint: pipeline.typHint,
|
||||
isDefault: pipeline.isDefault,
|
||||
nachfolgePipelineId: pipeline.nachfolgePipelineId,
|
||||
projectCount: counts.byPipeline.get(pipeline.id) ?? 0,
|
||||
updatedAtIso: pipeline.updatedAt.toISOString(),
|
||||
stages: pipeline.stages.map((stage) => ({
|
||||
id: stage.id,
|
||||
name: stage.name,
|
||||
stageKind: stage.stageKind,
|
||||
winProbability: stage.winProbability,
|
||||
color: stage.color,
|
||||
position: stage.position,
|
||||
projectCount: counts.byStage.get(stage.id) ?? 0,
|
||||
})),
|
||||
accessRules: pipeline.accessRules.map((rule) => ({ id: rule.id, principalType: rule.principalType, principalId: rule.principalId, kannSehen: rule.kannSehen, kannBearbeiten: rule.kannBearbeiten })),
|
||||
fieldConfigs: pipeline.fieldConfigs.map((config) => ({
|
||||
id: config.id,
|
||||
stageId: config.stageId,
|
||||
fieldKey: config.fieldKey,
|
||||
sichtbar: config.sichtbar,
|
||||
pflicht: config.pflicht,
|
||||
reihenfolge: config.reihenfolge,
|
||||
visibilities: config.visibilities.map((visibility) => ({ id: visibility.id, principalType: visibility.principalType, principalId: visibility.principalId, kannSehen: visibility.kannSehen, kannBearbeiten: visibility.kannBearbeiten })),
|
||||
})),
|
||||
})),
|
||||
});
|
||||
const tenantSlug = params.tenant ?? "";
|
||||
return Response.json({
|
||||
vm,
|
||||
title: `${t("projects.admin.title")} | ${process.env.APP_NAME}`,
|
||||
breadcrumbAncestors: [{ label: t("projects.boards.title"), href: projectsBoardsPath(tenantSlug) }],
|
||||
lostReasonsHref: projectLostReasonsAdminPath(tenantSlug),
|
||||
tenantSlug,
|
||||
} satisfies LoaderData);
|
||||
}
|
||||
|
||||
export async function action({ request, params }: ActionFunctionArgs) {
|
||||
const ctx = requireProjectPermission(await loadProjectContext({ request, params, requireModule: true }), "projects.admin");
|
||||
const formData = await request.formData();
|
||||
const intent = formString(formData, "_action");
|
||||
try {
|
||||
if (intent === "createPipeline") {
|
||||
const pipeline = await createPipeline({
|
||||
tenantId: ctx.tenantId,
|
||||
userId: ctx.userId,
|
||||
name: formString(formData, "name") ?? "",
|
||||
typHint: (formString(formData, "typHint") as PipelineTypHint | null) ?? "custom",
|
||||
position: formNumber(formData, "position"),
|
||||
stages: formStages(formData),
|
||||
});
|
||||
return Response.json({ ok: true, pipeline });
|
||||
}
|
||||
if (intent === "updatePipeline") {
|
||||
const pipeline = await updatePipeline({
|
||||
tenantId: ctx.tenantId,
|
||||
pipelineId: formString(formData, "pipelineId") ?? "",
|
||||
name: formString(formData, "name"),
|
||||
position: formNumber(formData, "position"),
|
||||
typHint: formString(formData, "typHint") as PipelineTypHint | null,
|
||||
});
|
||||
return Response.json({ ok: true, pipeline });
|
||||
}
|
||||
if (intent === "archivePipeline") {
|
||||
const pipeline = await archivePipeline({ tenantId: ctx.tenantId, pipelineId: formString(formData, "pipelineId") ?? "" });
|
||||
return Response.json({ ok: true, pipeline });
|
||||
}
|
||||
if (intent === "duplicatePipeline") {
|
||||
const pipeline = await duplicatePipeline({
|
||||
tenantId: ctx.tenantId,
|
||||
userId: ctx.userId,
|
||||
pipelineId: formString(formData, "pipelineId") ?? "",
|
||||
name: formString(formData, "name") ?? "",
|
||||
});
|
||||
return Response.json({ ok: true, pipeline });
|
||||
}
|
||||
if (intent === "upsertAccess") {
|
||||
await upsertPipelineAccess({
|
||||
tenantId: ctx.tenantId,
|
||||
pipelineId: formString(formData, "pipelineId") ?? "",
|
||||
principalType: formString(formData, "principalType") as PipelinePrincipalType,
|
||||
principalId: formString(formData, "principalId") ?? "",
|
||||
kannSehen: formBoolean(formData, "kannSehen"),
|
||||
kannBearbeiten: formBoolean(formData, "kannBearbeiten"),
|
||||
});
|
||||
return Response.json({ ok: true });
|
||||
}
|
||||
if (intent === "upsertFieldConfig") {
|
||||
await upsertPipelineFieldConfig({
|
||||
tenantId: ctx.tenantId,
|
||||
pipelineId: formString(formData, "pipelineId") ?? "",
|
||||
stageId: formString(formData, "stageId"),
|
||||
fieldKey: formString(formData, "fieldKey") ?? "",
|
||||
sichtbar: formBoolean(formData, "sichtbar"),
|
||||
pflicht: formBoolean(formData, "pflicht"),
|
||||
reihenfolge: formNumber(formData, "reihenfolge"),
|
||||
});
|
||||
return Response.json({ ok: true });
|
||||
}
|
||||
if (intent === "upsertFieldVisibility") {
|
||||
await upsertPipelineFieldVisibility({
|
||||
tenantId: ctx.tenantId,
|
||||
fieldConfigId: formString(formData, "fieldConfigId") ?? "",
|
||||
principalType: formString(formData, "principalType") as PipelinePrincipalType,
|
||||
principalId: formString(formData, "principalId") ?? "",
|
||||
kannSehen: formBoolean(formData, "kannSehen"),
|
||||
kannBearbeiten: formBoolean(formData, "kannBearbeiten"),
|
||||
});
|
||||
return Response.json({ ok: true });
|
||||
}
|
||||
if (intent === "upsertAuftragStageConfig") {
|
||||
await upsertAuftragPipelineStageConfig({
|
||||
tenantId: ctx.tenantId,
|
||||
pipelineId: formString(formData, "pipelineId") ?? "",
|
||||
stageId: formString(formData, "stageId") ?? "",
|
||||
requiresAcceptanceProtocol: formBoolean(formData, "requiresAcceptanceProtocol"),
|
||||
acceptanceFormTemplateId: formString(formData, "acceptanceFormTemplateId"),
|
||||
autoArchiveAfterDays: formNumber(formData, "autoArchiveAfterDays") ?? null,
|
||||
});
|
||||
return Response.json({ ok: true });
|
||||
}
|
||||
return Response.json({ error: "projects.error.unsupportedAction" }, { status: 400 });
|
||||
} catch (error) {
|
||||
return projectActionErrorResponse(error);
|
||||
}
|
||||
return handlePipelineAdminAction({ tenantId: ctx.tenantId, userId: ctx.userId, formData: await request.formData() });
|
||||
}
|
||||
|
||||
export default function PipelinesAdminRoute() {
|
||||
const { t } = useTranslation();
|
||||
const data = useLoaderData<LoaderData>();
|
||||
const submit = useSubmit();
|
||||
const navigation = useNavigation();
|
||||
const actionData = useActionData<{ error?: string; ok?: boolean; intent?: string; messageKey?: string }>();
|
||||
useSaveToast(actionData);
|
||||
const vmData = data.vm.status === "ready" ? data.vm.data : null;
|
||||
|
||||
if (!vmData) return null;
|
||||
|
||||
return (
|
||||
<main className="mx-auto flex w-full max-w-5xl flex-col gap-6 p-6">
|
||||
<header>
|
||||
<h1 className="text-2xl font-semibold">{t("projects.pipelines.title")}</h1>
|
||||
</header>
|
||||
<section className="grid gap-4">
|
||||
{data.pipelines.map((pipeline) => (
|
||||
<article key={pipeline.id} className="rounded-md border bg-background p-4">
|
||||
<h2 className="font-semibold">{pipeline.name}</h2>
|
||||
<p className="text-sm text-muted-foreground">{pipeline.typHint}</p>
|
||||
<ol className="mt-3 flex flex-wrap gap-2">
|
||||
{pipeline.stages.map((stage) => (
|
||||
<li key={stage.id} className="rounded-md border px-3 py-1 text-sm">
|
||||
{stage.name} · {stage.stageKind}
|
||||
<form className="mt-2 grid gap-2" method="post">
|
||||
<input type="hidden" name="_action" value="upsertAuftragStageConfig" />
|
||||
<input type="hidden" name="pipelineId" value={pipeline.id} />
|
||||
<input type="hidden" name="stageId" value={stage.id} />
|
||||
<label className="flex items-center gap-2">
|
||||
<input name="requiresAcceptanceProtocol" type="checkbox" />
|
||||
{t("projects.fields.requiresAcceptanceProtocol")}
|
||||
</label>
|
||||
<input className="rounded-md border px-2 py-1" name="acceptanceFormTemplateId" placeholder={t("projects.fields.acceptanceFormTemplateId")} />
|
||||
<input className="rounded-md border px-2 py-1" name="autoArchiveAfterDays" placeholder={t("projects.fields.autoArchiveAfterDays")} type="number" />
|
||||
<button className="rounded-md border px-2 py-1" type="submit">
|
||||
{t("projects.actions.saveAuftragConfig")}
|
||||
</button>
|
||||
</form>
|
||||
</li>
|
||||
))}
|
||||
</ol>
|
||||
</article>
|
||||
))}
|
||||
</section>
|
||||
</main>
|
||||
<PipelineAdminPage
|
||||
data={vmData}
|
||||
submitting={navigation.state !== "idle"}
|
||||
errorKey={actionData?.error ?? null}
|
||||
lostReasonsHref={data.lostReasonsHref}
|
||||
settingsHref={(pipelineId) => projectBoardSettingsPath(data.tenantSlug, pipelineId)}
|
||||
actions={{
|
||||
onSubmit: (intent, fields) => {
|
||||
const fd = new FormData();
|
||||
fd.set("_action", intent);
|
||||
for (const [key, value] of Object.entries(fields)) fd.set(key, value);
|
||||
submit(fd, { method: "post" });
|
||||
},
|
||||
}}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
export const handle = { moduleKey: PROJECTS_MODULE_KEY };
|
||||
Reference in new issue
Block a user