forked from fahrPool/fahrPool
feat: add inline Fahrzeug quick-add to Auftrag-anlegen page
Adds createFahrzeugUndAuswaehlen (unbound server action) so a new Fahrzeug can be created inline from /auftraege/neu without leaving the page, mirroring the existing Ort quick-add flow from Task 6. Includes an app-layer admin/disponent role check as defense-in-depth alongside RLS, consistent with the pattern already applied to createOrtUndAuswaehlen and the page-level gate. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
parent
e55ab12c2b
commit
116e78fdc6
|
|
@ -3,7 +3,8 @@
|
|||
import { redirect } from "next/navigation";
|
||||
import { createClient } from "@/lib/supabase/server";
|
||||
import { validateOrt } from "@/lib/orte-fahrzeuge/validate-ort";
|
||||
import { insertOrt } from "@/lib/orte-fahrzeuge/mutations";
|
||||
import { validateFahrzeug } from "@/lib/orte-fahrzeuge/validate-fahrzeug";
|
||||
import { insertOrt, insertFahrzeug } from "@/lib/orte-fahrzeuge/mutations";
|
||||
import { loadProfile } from "@/lib/auth/profile";
|
||||
|
||||
export async function createAuftrag(formData: FormData): Promise<void> {
|
||||
|
|
@ -107,3 +108,53 @@ export async function createOrtUndAuswaehlen(ziel: "abholort" | "zielort", formD
|
|||
ruecksprungParams.set(ziel === "abholort" ? "abholort_id" : "zielort_id", result.id);
|
||||
redirect(`/auftraege/neu?${ruecksprungParams.toString()}`);
|
||||
}
|
||||
|
||||
export async function createFahrzeugUndAuswaehlen(formData: FormData): Promise<void> {
|
||||
const profileResult = await loadProfile();
|
||||
if (
|
||||
profileResult.status !== "ok" ||
|
||||
(profileResult.profile.role !== "admin" && profileResult.profile.role !== "disponent")
|
||||
) {
|
||||
redirect(`/auftraege?error=${encodeURIComponent("Nur Disponent/Admin duerfen Auftraege anlegen.")}`);
|
||||
}
|
||||
|
||||
const vorgaengerAuftragId = formData.get("vorgaenger_auftrag_id");
|
||||
const abholortId = formData.get("abholort_id");
|
||||
const zielortId = formData.get("zielort_id");
|
||||
|
||||
const ruecksprungParams = new URLSearchParams();
|
||||
if (typeof vorgaengerAuftragId === "string" && vorgaengerAuftragId) {
|
||||
ruecksprungParams.set("vorgaenger_auftrag_id", vorgaengerAuftragId);
|
||||
}
|
||||
if (typeof abholortId === "string" && abholortId) {
|
||||
ruecksprungParams.set("abholort_id", abholortId);
|
||||
}
|
||||
if (typeof zielortId === "string" && zielortId) {
|
||||
ruecksprungParams.set("zielort_id", zielortId);
|
||||
}
|
||||
|
||||
const validation = validateFahrzeug({
|
||||
kennzeichen: formData.get("kennzeichen"),
|
||||
marke: formData.get("marke"),
|
||||
modell: formData.get("modell"),
|
||||
});
|
||||
if (!validation.valid) {
|
||||
ruecksprungParams.set("zeige_fahrzeug_formular", "1");
|
||||
ruecksprungParams.set("error", validation.error);
|
||||
redirect(`/auftraege/neu?${ruecksprungParams.toString()}`);
|
||||
}
|
||||
|
||||
const result = await insertFahrzeug({
|
||||
kennzeichen: validation.kennzeichen,
|
||||
marke: validation.marke,
|
||||
modell: validation.modell,
|
||||
});
|
||||
if (!result.success) {
|
||||
ruecksprungParams.set("zeige_fahrzeug_formular", "1");
|
||||
ruecksprungParams.set("error", result.error);
|
||||
redirect(`/auftraege/neu?${ruecksprungParams.toString()}`);
|
||||
}
|
||||
|
||||
ruecksprungParams.set("fahrzeug_id", result.id);
|
||||
redirect(`/auftraege/neu?${ruecksprungParams.toString()}`);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import Link from "next/link";
|
||||
import { redirect } from "next/navigation";
|
||||
import { createAuftrag, createOrtUndAuswaehlen } from "./actions";
|
||||
import { createAuftrag, createOrtUndAuswaehlen, createFahrzeugUndAuswaehlen } from "./actions";
|
||||
import { listOrte, listFahrzeuge } from "@/lib/auftraege/queries";
|
||||
import { loadProfile } from "@/lib/auth/profile";
|
||||
import { Card } from "@/components/ui/Card";
|
||||
|
|
@ -20,6 +20,18 @@ function buildOrtFormularUrl(
|
|||
return `/auftraege/neu?${query.toString()}`;
|
||||
}
|
||||
|
||||
function buildFahrzeugFormularUrl(params: {
|
||||
vorgaengerAuftragId?: string;
|
||||
abholortId?: string;
|
||||
zielortId?: string;
|
||||
}): string {
|
||||
const query = new URLSearchParams({ zeige_fahrzeug_formular: "1" });
|
||||
if (params.vorgaengerAuftragId) query.set("vorgaenger_auftrag_id", params.vorgaengerAuftragId);
|
||||
if (params.abholortId) query.set("abholort_id", params.abholortId);
|
||||
if (params.zielortId) query.set("zielort_id", params.zielortId);
|
||||
return `/auftraege/neu?${query.toString()}`;
|
||||
}
|
||||
|
||||
export default async function NeuerAuftragPage({
|
||||
searchParams,
|
||||
}: {
|
||||
|
|
@ -30,14 +42,10 @@ export default async function NeuerAuftragPage({
|
|||
zielort_id?: string;
|
||||
fahrzeug_id?: string;
|
||||
zeige_ort_formular?: string;
|
||||
zeige_fahrzeug_formular?: string;
|
||||
}>;
|
||||
}) {
|
||||
const profileResult = await loadProfile();
|
||||
// Seitenebenen-Gate zusaetzlich zur INSERT-RLS-Policy auf `auftraege`
|
||||
// (admin/disponent-only): ohne diesen Check wuerde die Seite das Formular
|
||||
// auch Fahrern/Readonly anzeigen, obwohl ein Submit dann ohnehin an der
|
||||
// RLS-Policy scheitern wuerde -- das ist verwirrende UX statt eines echten
|
||||
// Sicherheitslecks, aber ein frueher Redirect ist die klarere Loesung.
|
||||
if (
|
||||
profileResult.status !== "ok" ||
|
||||
(profileResult.profile.role !== "admin" && profileResult.profile.role !== "disponent")
|
||||
|
|
@ -45,8 +53,15 @@ export default async function NeuerAuftragPage({
|
|||
redirect(`/auftraege?error=${encodeURIComponent("Nur Disponent/Admin duerfen Auftraege anlegen.")}`);
|
||||
}
|
||||
|
||||
const { error, vorgaenger_auftrag_id, abholort_id, zielort_id, fahrzeug_id, zeige_ort_formular } =
|
||||
await searchParams;
|
||||
const {
|
||||
error,
|
||||
vorgaenger_auftrag_id,
|
||||
abholort_id,
|
||||
zielort_id,
|
||||
fahrzeug_id,
|
||||
zeige_ort_formular,
|
||||
zeige_fahrzeug_formular,
|
||||
} = await searchParams;
|
||||
const [orte, fahrzeuge] = await Promise.all([listOrte(), listFahrzeuge()]);
|
||||
|
||||
const createOrtFuerAbholortAction = createOrtUndAuswaehlen.bind(null, "abholort");
|
||||
|
|
@ -113,6 +128,35 @@ export default async function NeuerAuftragPage({
|
|||
</Card>
|
||||
)}
|
||||
|
||||
{zeige_fahrzeug_formular === "1" && (
|
||||
<Card className="mb-6">
|
||||
<form action={createFahrzeugUndAuswaehlen}>
|
||||
<h2 className="mb-4 text-lg font-semibold text-asphalt-grey">Neues Fahrzeug anlegen</h2>
|
||||
{vorgaenger_auftrag_id && (
|
||||
<input type="hidden" name="vorgaenger_auftrag_id" value={vorgaenger_auftrag_id} />
|
||||
)}
|
||||
{abholort_id && <input type="hidden" name="abholort_id" value={abholort_id} />}
|
||||
{zielort_id && <input type="hidden" name="zielort_id" value={zielort_id} />}
|
||||
<FormField label="Kennzeichen" htmlFor="neues_fahrzeug_kennzeichen">
|
||||
<input
|
||||
id="neues_fahrzeug_kennzeichen"
|
||||
name="kennzeichen"
|
||||
type="text"
|
||||
required
|
||||
className={FIELD_CLASSES}
|
||||
/>
|
||||
</FormField>
|
||||
<FormField label="Marke (optional)" htmlFor="neues_fahrzeug_marke">
|
||||
<input id="neues_fahrzeug_marke" name="marke" type="text" className={FIELD_CLASSES} />
|
||||
</FormField>
|
||||
<FormField label="Modell (optional)" htmlFor="neues_fahrzeug_modell">
|
||||
<input id="neues_fahrzeug_modell" name="modell" type="text" className={FIELD_CLASSES} />
|
||||
</FormField>
|
||||
<Button type="submit">Fahrzeug anlegen</Button>
|
||||
</form>
|
||||
</Card>
|
||||
)}
|
||||
|
||||
<Card>
|
||||
<form action={createAuftrag}>
|
||||
{vorgaenger_auftrag_id && (
|
||||
|
|
@ -194,6 +238,17 @@ export default async function NeuerAuftragPage({
|
|||
))}
|
||||
</select>
|
||||
</FormField>
|
||||
<Link
|
||||
href={buildFahrzeugFormularUrl({
|
||||
vorgaengerAuftragId: vorgaenger_auftrag_id,
|
||||
abholortId: abholort_id,
|
||||
zielortId: zielort_id,
|
||||
})}
|
||||
className="mb-4 inline-block text-sm text-performance-blue hover:underline"
|
||||
>
|
||||
+ Neues Fahrzeug
|
||||
</Link>
|
||||
|
||||
<Button type="submit">Auftrag anlegen</Button>
|
||||
</form>
|
||||
</Card>
|
||||
|
|
|
|||
Loading…
Reference in New Issue