Update hivegrid-cloud reference to latest main
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e98d38e716
commit
7b21524c86
3781 files changed
+373484
-225607
No files matched your search
+278
@@ -0,0 +1,278 @@
|
||||
// R-P6 (P6 CRM core, Variante A) — create-contact route (`/contacts/new`).
|
||||
// `intent=checkDuplicates` (fetcher) surfaces hard/soft duplicate hits live while
|
||||
// typing (DoD 4); `intent=create` persists the typed Kontakt (+ audit + activity)
|
||||
// and redirects to the detail. Module off -> locked; missing crm.write -> 404.
|
||||
|
||||
import { useRef, useState } from "react";
|
||||
import { type ActionFunctionArgs, type LoaderFunctionArgs, redirect, useActionData, useFetcher, useLoaderData } from "react-router";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { buildKontaktDisplayName } from "@hivegrid/view-models";
|
||||
import { getTranslations } from "~/locale/i18next.server";
|
||||
import {
|
||||
badRequestResponse,
|
||||
crmActionErrorResponse,
|
||||
loadCrmContext,
|
||||
notFoundResponse,
|
||||
requireCrmPermission,
|
||||
} from "~/utils/integrations/crm/crmAccess.server";
|
||||
import { createKontakt, findDuplicateKontakte, listAssignableUsers, type KontaktTyp, type OrgMerkmal, type KontaktStatus } from "~/utils/integrations/crm/crmService.server";
|
||||
import { auditCrmEvent, logKontaktActivity } from "~/utils/integrations/crm/crmAudit.server";
|
||||
import { companyDetailPath, contactDetailPath, contactsPath, CRM_MODULE_KEY } from "~/utils/integrations/crm/crmRoutePaths";
|
||||
import ServerError from "~/components/ui/errors/ServerError";
|
||||
|
||||
type DuplicateCard = { id: string; displayName: string; primaerEmail: string | null; primaerTelefon: string | null; detailHref: string };
|
||||
type LoaderData = { title: string; moduleActive: boolean; ownerOptions: { id: string; label: string }[]; defaultOwnerId: string; backHref: string };
|
||||
|
||||
const crmFieldLabelClass = "block text-body-small-regular-0 font-body-small-regular-0 text-text-neutral-secondary";
|
||||
const crmFieldControlClass =
|
||||
"mt-1 h-9 w-full rounded-sm border border-solid border-border-strong bg-surface-neutral-primary px-3 text-body-default-regular-0 font-body-default-regular-0 text-text-neutral-secondary outline-none placeholder:text-text-neutral-subtle focus:shadow-focus";
|
||||
|
||||
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
||||
const { t } = await getTranslations(request);
|
||||
const ctx = await loadCrmContext({ request, params });
|
||||
const tenantSlug = params.tenant ?? "";
|
||||
const base = { title: `${t("crm.new.title")} | ${process.env.APP_NAME}`, backHref: contactsPath(tenantSlug) };
|
||||
if (!ctx.hasModule) {
|
||||
return Response.json({ ...base, moduleActive: false, ownerOptions: [], defaultOwnerId: ctx.userId } satisfies LoaderData);
|
||||
}
|
||||
try {
|
||||
requireCrmPermission(ctx, "crm.write");
|
||||
} catch {
|
||||
throw notFoundResponse();
|
||||
}
|
||||
const ownerOptions = await listAssignableUsers(ctx.tenantId);
|
||||
return Response.json({ ...base, moduleActive: true, ownerOptions, defaultOwnerId: ctx.userId } satisfies LoaderData);
|
||||
};
|
||||
|
||||
export const action = async ({ request, params }: ActionFunctionArgs) => {
|
||||
const ctx = await loadCrmContext({ request, params, requireModule: true });
|
||||
const tenantSlug = params.tenant ?? "";
|
||||
const form = await request.formData();
|
||||
const intent = form.get("intent")?.toString();
|
||||
const str = (name: string) => form.get(name)?.toString().trim() || null;
|
||||
|
||||
try {
|
||||
requireCrmPermission(ctx, "crm.write");
|
||||
const kontaktTyp = (str("kontaktTyp") ?? "privatperson") as KontaktTyp;
|
||||
|
||||
if (intent === "checkDuplicates") {
|
||||
const { hard, soft } = await findDuplicateKontakte({
|
||||
tenantId: ctx.tenantId,
|
||||
email: str("email"),
|
||||
telefon: str("telefon"),
|
||||
vorname: str("vorname"),
|
||||
nachname: str("nachname"),
|
||||
orgName: str("orgName"),
|
||||
});
|
||||
const toCard = (row: (typeof hard)[number]): DuplicateCard => ({
|
||||
id: row.id,
|
||||
displayName: buildKontaktDisplayName(row),
|
||||
primaerEmail: row.primaerEmail,
|
||||
primaerTelefon: row.primaerTelefon,
|
||||
detailHref: row.kontaktTyp === "organisation" ? companyDetailPath(tenantSlug, row.id) : contactDetailPath(tenantSlug, row.id),
|
||||
});
|
||||
return Response.json({ hard: hard.map(toCard), soft: soft.map(toCard) });
|
||||
}
|
||||
|
||||
if (intent === "create") {
|
||||
const email = str("email");
|
||||
const telefon = str("telefon");
|
||||
const created = await createKontakt({
|
||||
tenantId: ctx.tenantId,
|
||||
userId: ctx.userId,
|
||||
kontaktTyp,
|
||||
kontaktStatus: (str("kontaktStatus") as KontaktStatus | null) ?? undefined,
|
||||
vorname: str("vorname"),
|
||||
nachname: str("nachname"),
|
||||
orgName: str("orgName"),
|
||||
orgMerkmal: (str("orgMerkmal") as OrgMerkmal | null) ?? undefined,
|
||||
emails: email ? [{ email, primaer: true }] : [],
|
||||
telefone: telefon ? [{ telefon, primaer: true }] : [],
|
||||
zustaendigerUserId: str("zustaendigerUserId"),
|
||||
});
|
||||
await auditCrmEvent({ ctx, action: "kontakt.created", targetType: "kontakt", targetId: created.id });
|
||||
await logKontaktActivity({ ctx, kontaktId: created.id, kontaktTyp, activityType: "contact_created", summaryText: "Kontakt angelegt" });
|
||||
return redirect(kontaktTyp === "organisation" ? companyDetailPath(tenantSlug, created.id) : contactDetailPath(tenantSlug, created.id));
|
||||
}
|
||||
|
||||
throw badRequestResponse();
|
||||
} catch (error) {
|
||||
if (error instanceof Response) {
|
||||
throw error;
|
||||
}
|
||||
return crmActionErrorResponse(error);
|
||||
}
|
||||
};
|
||||
|
||||
export default function ContactNewRoute() {
|
||||
const { t } = useTranslation();
|
||||
const { moduleActive, ownerOptions, defaultOwnerId, backHref } = useLoaderData<LoaderData>();
|
||||
const actionData = useActionData<{ error?: string }>();
|
||||
const dupFetcher = useFetcher<{ hard: DuplicateCard[]; soft: DuplicateCard[] }>();
|
||||
const formRef = useRef<HTMLFormElement>(null);
|
||||
const [kontaktTyp, setKontaktTyp] = useState<KontaktTyp>("privatperson");
|
||||
|
||||
if (!moduleActive) {
|
||||
return (
|
||||
<main className="mx-auto max-w-3xl px-4 py-10" data-ui-id="ui.crm.new">
|
||||
<div className="mt-10 text-center" data-ui-id="ui.crm.new.locked">
|
||||
<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 checkDuplicates = () => {
|
||||
if (!formRef.current) return;
|
||||
const fd = new FormData(formRef.current);
|
||||
fd.set("intent", "checkDuplicates");
|
||||
dupFetcher.submit(fd, { method: "post" });
|
||||
};
|
||||
|
||||
const dup = dupFetcher.data;
|
||||
|
||||
return (
|
||||
<main className="mx-auto max-w-3xl px-4 py-10" data-ui-id="ui.crm.new">
|
||||
<nav className="text-muted-foreground mb-4 text-sm">
|
||||
<a href={backHref} className="hover:text-foreground">
|
||||
{t("crm.contacts.title")}
|
||||
</a>
|
||||
</nav>
|
||||
<h1 className="text-foreground text-xl font-semibold">{t("crm.new.title")}</h1>
|
||||
|
||||
{actionData?.error ? (
|
||||
<p className="border-destructive text-destructive mt-4 rounded border px-3 py-2 text-sm" role="alert" data-ui-id="ui.crm.new.error">
|
||||
{t(actionData.error)}
|
||||
</p>
|
||||
) : null}
|
||||
|
||||
{dup && (dup.hard.length > 0 || dup.soft.length > 0) ? (
|
||||
<div className="mt-4 space-y-2" data-ui-id="ui.crm.new.duplicates">
|
||||
{dup.hard.length > 0 ? (
|
||||
<div className="border-destructive rounded border px-3 py-2 text-sm" data-ui-id="ui.crm.new.duplicates.hard">
|
||||
<p className="text-destructive font-medium">{t("crm.new.duplicates.hard")}</p>
|
||||
<ul className="mt-1">
|
||||
{dup.hard.map((card) => (
|
||||
<li key={card.id}>
|
||||
<a href={card.detailHref} className="text-theme-600">
|
||||
{card.displayName}
|
||||
</a>
|
||||
{card.primaerEmail ? ` · ${card.primaerEmail}` : ""}
|
||||
{card.primaerTelefon ? ` · ${card.primaerTelefon}` : ""}
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
</div>
|
||||
) : null}
|
||||
{dup.soft.length > 0 ? (
|
||||
<div className="border-border rounded border px-3 py-2 text-sm" data-ui-id="ui.crm.new.duplicates.soft">
|
||||
<p className="font-medium">{t("crm.new.duplicates.soft")}</p>
|
||||
<ul className="mt-1">
|
||||
{dup.soft.map((card) => (
|
||||
<li key={card.id}>
|
||||
<a href={card.detailHref} className="text-theme-600">
|
||||
{card.displayName}
|
||||
</a>
|
||||
{card.primaerEmail ? ` · ${card.primaerEmail}` : ""}
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
) : null}
|
||||
|
||||
<form method="post" ref={formRef} className="mt-6 grid gap-3 sm:grid-cols-2" data-ui-id="ui.crm.new.form">
|
||||
<input type="hidden" name="intent" value="create" />
|
||||
<label className="text-sm">
|
||||
<span className={crmFieldLabelClass}>{t("crm.fields.type")}</span>
|
||||
<select
|
||||
name="kontaktTyp"
|
||||
value={kontaktTyp}
|
||||
onChange={(event) => setKontaktTyp(event.target.value as KontaktTyp)}
|
||||
className={crmFieldControlClass}
|
||||
>
|
||||
<option value="privatperson">{t("crm.kontaktTyp.privatperson")}</option>
|
||||
<option value="organisation">{t("crm.kontaktTyp.organisation")}</option>
|
||||
</select>
|
||||
</label>
|
||||
<label className="text-sm">
|
||||
<span className={crmFieldLabelClass}>{t("crm.fields.status")}</span>
|
||||
<select name="kontaktStatus" defaultValue="kontakt" className={crmFieldControlClass}>
|
||||
{(["kontakt", "kunde", "bestandskunde"] as const).map((status) => (
|
||||
<option key={status} value={status}>
|
||||
{t(`crm.kontaktStatus.${status}`)}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
|
||||
{kontaktTyp === "organisation" ? (
|
||||
<>
|
||||
<label className="text-sm">
|
||||
<span className={crmFieldLabelClass}>{t("crm.fields.orgName")}</span>
|
||||
<input name="orgName" onBlur={checkDuplicates} className={crmFieldControlClass} />
|
||||
</label>
|
||||
<label className="text-sm">
|
||||
<span className={crmFieldLabelClass}>{t("crm.fields.orgMerkmal")}</span>
|
||||
<select name="orgMerkmal" defaultValue="" className={crmFieldControlClass}>
|
||||
<option value="">{t("crm.fields.none")}</option>
|
||||
{(["gmbh", "kg", "oeffentliche_einrichtung", "sonstige"] as const).map((merkmal) => (
|
||||
<option key={merkmal} value={merkmal}>
|
||||
{t(`crm.orgMerkmal.${merkmal}`)}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<label className="text-sm">
|
||||
<span className={crmFieldLabelClass}>{t("crm.fields.vorname")}</span>
|
||||
<input name="vorname" onBlur={checkDuplicates} className={crmFieldControlClass} />
|
||||
</label>
|
||||
<label className="text-sm">
|
||||
<span className={crmFieldLabelClass}>{t("crm.fields.nachname")}</span>
|
||||
<input name="nachname" onBlur={checkDuplicates} className={crmFieldControlClass} />
|
||||
</label>
|
||||
</>
|
||||
)}
|
||||
|
||||
<label className="text-sm">
|
||||
<span className={crmFieldLabelClass}>{t("crm.fields.email")}</span>
|
||||
<input name="email" type="email" onBlur={checkDuplicates} className={crmFieldControlClass} />
|
||||
</label>
|
||||
<label className="text-sm">
|
||||
<span className={crmFieldLabelClass}>{t("crm.fields.telefon")}</span>
|
||||
<input name="telefon" onBlur={checkDuplicates} className={crmFieldControlClass} />
|
||||
</label>
|
||||
<label className="text-sm">
|
||||
<span className={crmFieldLabelClass}>{t("crm.fields.owner")}</span>
|
||||
<select name="zustaendigerUserId" defaultValue={defaultOwnerId} className={crmFieldControlClass}>
|
||||
{ownerOptions.map((option) => (
|
||||
<option key={option.id} value={option.id}>
|
||||
{option.label}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
|
||||
<div className="flex items-center gap-3 sm:col-span-2">
|
||||
<button type="submit" className="text-theme-600 text-sm font-medium" data-ui-id="ui.crm.new.submit">
|
||||
{t("crm.actions.create")}
|
||||
</button>
|
||||
<button type="button" onClick={checkDuplicates} className="text-muted-foreground text-sm underline" data-ui-id="ui.crm.new.checkDuplicates">
|
||||
{t("crm.actions.checkDuplicates")}
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</main>
|
||||
);
|
||||
}
|
||||
|
||||
export function ErrorBoundary() {
|
||||
return <ServerError />;
|
||||
}
|
||||
|
||||
export const handle = { moduleKey: CRM_MODULE_KEY };
|
||||
Reference in new issue
Block a user