206 lines
8.6 KiB
TypeScript
206 lines
8.6 KiB
TypeScript
// R-P6 (P6 CRM core, Variante A) — custom-field admin (`/contacts/admin/custom-fields`).
|
|
// Lists + creates + archives the tenant's CONTACT custom-field definitions (DoD 3).
|
|
// Gated on crm.admin (missing -> 404); module off -> locked empty.
|
|
|
|
import { type ActionFunctionArgs, type LoaderFunctionArgs, useActionData, useLoaderData } from "react-router";
|
|
import { useTranslation } from "react-i18next";
|
|
import { buildCustomFieldAdminVM, type CustomFieldAdminVM } from "@hivegrid/view-models";
|
|
import { getTranslations } from "~/locale/i18next.server";
|
|
import {
|
|
badRequestResponse,
|
|
crmActionErrorResponse,
|
|
loadCrmContext,
|
|
notFoundResponse,
|
|
requireCrmPermission,
|
|
} from "~/utils/integrations/crm/crmAccess.server";
|
|
import {
|
|
archiveCustomFieldDefinition,
|
|
createCustomFieldDefinition,
|
|
listCustomFieldDefinitions,
|
|
type CustomFieldDatentyp,
|
|
} from "~/utils/integrations/crm/customFieldService.server";
|
|
import { auditCrmEvent } from "~/utils/integrations/crm/crmAudit.server";
|
|
import { CRM_MODULE_KEY } from "~/utils/integrations/crm/crmRoutePaths";
|
|
import ServerError from "~/components/ui/errors/ServerError";
|
|
|
|
const ENTITY_TYPE = "kontakt";
|
|
const DATENTYPEN: CustomFieldDatentyp[] = [
|
|
"text",
|
|
"textarea",
|
|
"zahl",
|
|
"waehrung",
|
|
"datum",
|
|
"datum_range",
|
|
"select",
|
|
"multiselect",
|
|
"boolean",
|
|
"email",
|
|
"telefon",
|
|
"datei",
|
|
];
|
|
|
|
type LoaderData = { title: string; moduleActive: boolean; vm: CustomFieldAdminVM };
|
|
|
|
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
|
const { t } = await getTranslations(request);
|
|
const ctx = await loadCrmContext({ request, params });
|
|
const title = `${t("crm.admin.customFields.title")} | ${process.env.APP_NAME}`;
|
|
if (!ctx.hasModule) {
|
|
return Response.json({ title, moduleActive: false, vm: buildCustomFieldAdminVM({ rows: [], entityType: ENTITY_TYPE, canWrite: false }) } satisfies LoaderData);
|
|
}
|
|
try {
|
|
requireCrmPermission(ctx, "crm.admin");
|
|
} catch {
|
|
throw notFoundResponse();
|
|
}
|
|
const definitions = await listCustomFieldDefinitions({ tenantId: ctx.tenantId, entityType: ENTITY_TYPE, includeArchived: true });
|
|
const vm = buildCustomFieldAdminVM({ rows: definitions, entityType: ENTITY_TYPE, canWrite: true });
|
|
return Response.json({ title, moduleActive: true, vm } satisfies LoaderData);
|
|
};
|
|
|
|
export const action = async ({ request, params }: ActionFunctionArgs) => {
|
|
const ctx = await loadCrmContext({ request, params, requireModule: true });
|
|
const form = await request.formData();
|
|
const intent = form.get("intent")?.toString();
|
|
try {
|
|
requireCrmPermission(ctx, "crm.admin");
|
|
if (intent === "create") {
|
|
const datentyp = (form.get("datentyp")?.toString() ?? "text") as CustomFieldDatentyp;
|
|
const optionenRaw = form.get("optionen")?.toString().trim();
|
|
const optionen = optionenRaw ? optionenRaw.split(",").map((option) => option.trim()).filter(Boolean) : null;
|
|
const created = await createCustomFieldDefinition({
|
|
tenantId: ctx.tenantId,
|
|
entityType: ENTITY_TYPE,
|
|
key: form.get("key")?.toString() ?? "",
|
|
label: form.get("label")?.toString() ?? "",
|
|
datentyp,
|
|
optionen,
|
|
});
|
|
await auditCrmEvent({ ctx, action: "custom_field.created", targetType: "custom_field", targetId: created.id, metadata: { key: created.key } });
|
|
return Response.json({ ok: true });
|
|
}
|
|
if (intent === "archive") {
|
|
const definitionId = form.get("definitionId")?.toString().trim();
|
|
if (!definitionId) {
|
|
throw badRequestResponse();
|
|
}
|
|
await archiveCustomFieldDefinition({ tenantId: ctx.tenantId, definitionId });
|
|
await auditCrmEvent({ ctx, action: "custom_field.archived", targetType: "custom_field", targetId: definitionId });
|
|
return Response.json({ ok: true });
|
|
}
|
|
throw badRequestResponse();
|
|
} catch (error) {
|
|
if (error instanceof Response) {
|
|
throw error;
|
|
}
|
|
return crmActionErrorResponse(error);
|
|
}
|
|
};
|
|
|
|
export default function CustomFieldsAdminRoute() {
|
|
const { t } = useTranslation();
|
|
const { moduleActive, vm } = useLoaderData<LoaderData>();
|
|
const actionData = useActionData<{ error?: string }>();
|
|
|
|
if (!moduleActive) {
|
|
return (
|
|
<main className="mx-auto max-w-4xl px-4 py-10" data-ui-id="ui.crm.admin.customFields">
|
|
<div className="mt-10 text-center">
|
|
<p className="text-foreground font-medium">{t("crm.locked.headline")}</p>
|
|
<p className="text-muted-foreground mt-1 text-sm">{t("crm.locked.body")}</p>
|
|
</div>
|
|
</main>
|
|
);
|
|
}
|
|
const rows = vm.status === "ready" && vm.data ? vm.data.rows : [];
|
|
|
|
return (
|
|
<main className="mx-auto max-w-4xl px-4 py-10" data-ui-id="ui.crm.admin.customFields">
|
|
<h1 className="text-foreground text-xl font-semibold">{t("crm.admin.customFields.title")}</h1>
|
|
<p className="text-muted-foreground mt-1 text-sm">{t("crm.admin.customFields.subtitle")}</p>
|
|
|
|
{actionData?.error ? (
|
|
<p className="border-destructive text-destructive mt-4 rounded border px-3 py-2 text-sm" role="alert">
|
|
{t(actionData.error)}
|
|
</p>
|
|
) : null}
|
|
|
|
{rows.length === 0 ? (
|
|
<p className="text-muted-foreground mt-6 text-sm" data-ui-id="ui.crm.admin.customFields.empty">
|
|
{t("crm.admin.customFields.empty")}
|
|
</p>
|
|
) : (
|
|
<table className="mt-6 w-full text-left text-sm" data-ui-id="ui.crm.admin.customFields.table">
|
|
<thead>
|
|
<tr className="text-muted-foreground border-b">
|
|
<th className="py-2">{t("crm.admin.customFields.colKey")}</th>
|
|
<th className="py-2">{t("crm.admin.customFields.colLabel")}</th>
|
|
<th className="py-2">{t("crm.admin.customFields.colType")}</th>
|
|
<th className="py-2" />
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{rows.map((row) => (
|
|
<tr key={row.id} className="border-b" data-row-id={row.id}>
|
|
<td className="py-2">{row.key}</td>
|
|
<td className="py-2">
|
|
{row.label}
|
|
{row.isDemoData ? <span className="text-muted-foreground ml-1 text-xs">{t("crm.admin.demoBadge")}</span> : null}
|
|
{row.archiviert ? <span className="text-muted-foreground ml-1 text-xs">{t("crm.admin.archivedBadge")}</span> : null}
|
|
</td>
|
|
<td className="py-2">{t(row.datentypLabelKey)}</td>
|
|
<td className="py-2">
|
|
{!row.archiviert ? (
|
|
<form method="post">
|
|
<input type="hidden" name="intent" value="archive" />
|
|
<input type="hidden" name="definitionId" value={row.id} />
|
|
<button type="submit" className="text-muted-foreground text-xs underline">
|
|
{t("crm.actions.archive")}
|
|
</button>
|
|
</form>
|
|
) : null}
|
|
</td>
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
</table>
|
|
)}
|
|
|
|
<form method="post" className="mt-6 flex flex-wrap items-end gap-2" data-ui-id="ui.crm.admin.customFields.create">
|
|
<input type="hidden" name="intent" value="create" />
|
|
<label className="text-sm">
|
|
<span className="text-muted-foreground block text-xs">{t("crm.admin.customFields.colKey")}</span>
|
|
<input name="key" required className="border-muted-foreground/40 rounded border px-2 py-1" />
|
|
</label>
|
|
<label className="text-sm">
|
|
<span className="text-muted-foreground block text-xs">{t("crm.admin.customFields.colLabel")}</span>
|
|
<input name="label" required className="border-muted-foreground/40 rounded border px-2 py-1" />
|
|
</label>
|
|
<label className="text-sm">
|
|
<span className="text-muted-foreground block text-xs">{t("crm.admin.customFields.colType")}</span>
|
|
<select name="datentyp" defaultValue="text" className="border-muted-foreground/40 rounded border px-2 py-1">
|
|
{DATENTYPEN.map((datentyp) => (
|
|
<option key={datentyp} value={datentyp}>
|
|
{t(`crm.datentyp.${datentyp}`)}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</label>
|
|
<label className="text-sm">
|
|
<span className="text-muted-foreground block text-xs">{t("crm.admin.customFields.colOptions")}</span>
|
|
<input name="optionen" placeholder={t("crm.admin.customFields.optionsPlaceholder")} className="border-muted-foreground/40 rounded border px-2 py-1" />
|
|
</label>
|
|
<button type="submit" className="text-theme-600 text-sm font-medium">
|
|
{t("crm.actions.create")}
|
|
</button>
|
|
</form>
|
|
</main>
|
|
);
|
|
}
|
|
|
|
export function ErrorBoundary() {
|
|
return <ServerError />;
|
|
}
|
|
|
|
export const handle = { moduleKey: CRM_MODULE_KEY };
|