1
0
Fork 0

feat: Auftraege bearbeiten (Disponent/Admin) mit gemeinsamem Formular

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Jonny 2026-11-02 09:34:01 +01:00
parent aee766e1c1
commit da4d0f6afa
7 changed files with 284 additions and 99 deletions

View File

@ -0,0 +1,43 @@
"use server";
import { redirect } from "next/navigation";
import { createClient } from "@/lib/supabase/server";
import { loadProfile } from "@/lib/auth/profile";
import { kannAuftraegeVerwalten } from "@/lib/auth/roles";
import { NICHT_BEARBEITBAR_MELDUNG, validateAuftragEingaben } from "@/lib/auftraege/validate-auftrag";
export async function aktualisiereAuftrag(auftragId: string, formData: FormData): Promise<void> {
const profileResult = await loadProfile();
if (profileResult.status !== "ok" || !kannAuftraegeVerwalten(profileResult.profile.role)) {
redirect(`/auftraege/${auftragId}?error=${encodeURIComponent("Nur Disponent/Admin duerfen Auftraege bearbeiten.")}`);
}
const validierung = validateAuftragEingaben(formData);
if (!validierung.valid) {
redirect(`/auftraege/${auftragId}/bearbeiten?error=${encodeURIComponent(validierung.error)}`);
}
// Leeres Nummernfeld behaelt die bisherige Nummer (Spalte wird nicht gesendet).
const { auftragsnummer, ...rest } = validierung.werte;
const aenderungen = auftragsnummer === null ? rest : { ...rest, auftragsnummer };
const supabase = await createClient();
const { data, error } = await supabase
.from("auftraege")
.update(aenderungen)
.eq("id", auftragId)
.in("status", ["offen", "zugewiesen"])
.select("id");
if (error?.code === "23505") {
redirect(`/auftraege/${auftragId}/bearbeiten?error=${encodeURIComponent("Diese Auftragsnummer ist bereits vergeben.")}`);
}
if (error) {
redirect(`/auftraege/${auftragId}/bearbeiten?error=${encodeURIComponent("Auftrag konnte nicht gespeichert werden.")}`);
}
if (!data || data.length === 0) {
redirect(`/auftraege/${auftragId}?error=${encodeURIComponent(NICHT_BEARBEITBAR_MELDUNG)}`);
}
redirect(`/auftraege/${auftragId}`);
}

View File

@ -0,0 +1,78 @@
import Link from "next/link";
import { notFound, redirect } from "next/navigation";
import { aktualisiereAuftrag } from "./actions";
import { listFahrzeuge, listOrte, loadAuftrag } from "@/lib/auftraege/queries";
import { loadProfile } from "@/lib/auth/profile";
import { kannAuftraegeVerwalten } from "@/lib/auth/roles";
import { istBearbeitbar, NICHT_BEARBEITBAR_MELDUNG } from "@/lib/auftraege/validate-auftrag";
import { alsDatetimeLocal, berechneVorbefuelltenZeitpunkt } from "@/lib/auftraege/zeitplanung";
import { AuftragStammFelder } from "@/components/auftraege/AuftragStammFelder";
import { NetzSicheresFormular } from "@/components/pwa/NetzSicheresFormular";
import { Card } from "@/components/ui/Card";
import { Button, buttonClasses } from "@/components/ui/Button";
import { Alert } from "@/components/ui/Alert";
export default async function AuftragBearbeitenPage({
params,
searchParams,
}: {
params: Promise<{ id: string }>;
searchParams: Promise<{ error?: string }>;
}) {
const { id } = await params;
const { error } = await searchParams;
const profileResult = await loadProfile();
if (profileResult.status !== "ok" || !kannAuftraegeVerwalten(profileResult.profile.role)) {
redirect(`/auftraege/${id}?error=${encodeURIComponent("Nur Disponent/Admin duerfen Auftraege bearbeiten.")}`);
}
const [auftrag, orte, fahrzeuge] = await Promise.all([loadAuftrag(id), listOrte(), listFahrzeuge()]);
if (!auftrag) {
notFound();
}
if (!istBearbeitbar(auftrag.status)) {
redirect(`/auftraege/${id}?error=${encodeURIComponent(NICHT_BEARBEITBAR_MELDUNG)}`);
}
const aktion = aktualisiereAuftrag.bind(null, auftrag.id);
return (
<div className="mx-auto max-w-xl p-4 md:p-8">
<h1 className="mb-1 text-xl font-semibold text-asphalt-grey">Auftrag bearbeiten</h1>
<p className="mb-6 text-sm text-asphalt-grey/70">{auftrag.auftragsnummer}</p>
{error && (
<div className="mb-6">
<Alert>{error}</Alert>
</div>
)}
<Card>
<NetzSicheresFormular action={aktion}>
<AuftragStammFelder
orte={orte}
fahrzeuge={fahrzeuge}
werte={{
abholortId: auftrag.abholort.id,
zielortId: auftrag.zielort.id,
fahrzeugId: auftrag.fahrzeug.id,
auftragsnummer: auftrag.auftragsnummer,
zeitplanungTyp: auftrag.zeitplanung_typ,
zeitplanungZeitpunkt: auftrag.zeitplanung_zeitpunkt
? alsDatetimeLocal(auftrag.zeitplanung_zeitpunkt)
: undefined,
}}
vorbefuelltesDatum={berechneVorbefuelltenZeitpunkt()}
neuLinks={null}
nummerLabel="Auftragsnummer"
/>
<div className="flex flex-col gap-3 sm:flex-row">
<Button type="submit">Speichern</Button>
<Link href={`/auftraege/${auftrag.id}`} className={buttonClasses("secondary", true)}>
Abbrechen
</Link>
</div>
</NetzSicheresFormular>
</Card>
</div>
);
}

View File

@ -1,14 +1,12 @@
import Link from "next/link";
import { redirect } from "next/navigation";
import { createAuftrag, createOrtUndAuswaehlen, createFahrzeugUndAuswaehlen } from "./actions";
import { listOrte, listFahrzeuge } from "@/lib/auftraege/queries";
import { loadProfile } from "@/lib/auth/profile";
import { fahrzeugBezeichnung } from "@/lib/orte-fahrzeuge/fahrzeug-bezeichnung";
import { Card } from "@/components/ui/Card";
import { Button } from "@/components/ui/Button";
import { Alert } from "@/components/ui/Alert";
import { FormField, FIELD_CLASSES } from "@/components/ui/FormField";
import { ZeitplanungFeld } from "@/components/auftraege/ZeitplanungFeld";
import { AuftragStammFelder } from "@/components/auftraege/AuftragStammFelder";
import { FinFeld } from "@/components/fahrzeuge/FinFeld";
import { berechneVorbefuelltenZeitpunkt } from "@/lib/auftraege/zeitplanung";
@ -179,98 +177,30 @@ export default async function NeuerAuftragPage({
{vorgaenger_auftrag_id && (
<input type="hidden" name="vorgaenger_auftrag_id" value={vorgaenger_auftrag_id} />
)}
<FormField label="Abholort" htmlFor="abholort_id">
<select
id="abholort_id"
name="abholort_id"
required
defaultValue={abholort_id ?? ""}
className={FIELD_CLASSES}
>
<option value="" disabled>
Bitte waehlen
</option>
{orte.map((ort) => (
<option key={ort.id} value={ort.id}>
{ort.adresse} ({ort.ansprechpartner})
</option>
))}
</select>
</FormField>
<Link
href={buildOrtFormularUrl("abholort", {
<AuftragStammFelder
orte={orte}
fahrzeuge={fahrzeuge}
werte={{ abholortId: abholort_id, zielortId: zielort_id, fahrzeugId: fahrzeug_id }}
vorbefuelltesDatum={vorbefuelltesDatum}
neuLinks={{
abholort: buildOrtFormularUrl("abholort", {
vorgaengerAuftragId: vorgaenger_auftrag_id,
zielortId: zielort_id,
fahrzeugId: fahrzeug_id,
})}
className="mb-4 inline-block text-sm text-performance-blue hover:underline"
>
+ Neuer Abholort
</Link>
<FormField label="Zielort" htmlFor="zielort_id">
<select
id="zielort_id"
name="zielort_id"
required
defaultValue={zielort_id ?? ""}
className={FIELD_CLASSES}
>
<option value="" disabled>
Bitte waehlen
</option>
{orte.map((ort) => (
<option key={ort.id} value={ort.id}>
{ort.adresse} ({ort.ansprechpartner})
</option>
))}
</select>
</FormField>
<Link
href={buildOrtFormularUrl("zielort", {
}),
zielort: buildOrtFormularUrl("zielort", {
vorgaengerAuftragId: vorgaenger_auftrag_id,
abholortId: abholort_id,
fahrzeugId: fahrzeug_id,
})}
className="mb-4 inline-block text-sm text-performance-blue hover:underline"
>
+ Neuer Zielort
</Link>
<FormField label="Fahrzeug" htmlFor="fahrzeug_id">
<select
id="fahrzeug_id"
name="fahrzeug_id"
required
defaultValue={fahrzeug_id ?? ""}
className={FIELD_CLASSES}
>
<option value="" disabled>
Bitte waehlen
</option>
{fahrzeuge.map((fahrzeug) => (
<option key={fahrzeug.id} value={fahrzeug.id}>
{fahrzeugBezeichnung(fahrzeug)}
</option>
))}
</select>
</FormField>
<Link
href={buildFahrzeugFormularUrl({
}),
fahrzeug: 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>
<ZeitplanungFeld vorbefuelltesDatum={vorbefuelltesDatum} />
<FormField label="Auftragsnummer (optional, wird sonst automatisch vergeben)" htmlFor="auftragsnummer">
<input id="auftragsnummer" name="auftragsnummer" type="text" className={FIELD_CLASSES} />
</FormField>
}),
}}
nummerLabel="Auftragsnummer (optional, wird sonst automatisch vergeben)"
/>
<Button type="submit">Auftrag anlegen</Button>
</form>
</Card>

View File

@ -0,0 +1,109 @@
import Link from "next/link";
import type { Fahrzeug, Ort } from "@/lib/auftraege/queries";
import type { ZeitplanungTyp } from "@/lib/auftraege/zeitplanung";
import { fahrzeugBezeichnung } from "@/lib/orte-fahrzeuge/fahrzeug-bezeichnung";
import { FormField, FIELD_CLASSES } from "@/components/ui/FormField";
import { ZeitplanungFeld } from "@/components/auftraege/ZeitplanungFeld";
export type AuftragStammWerte = {
abholortId?: string;
zielortId?: string;
fahrzeugId?: string;
auftragsnummer?: string;
zeitplanungTyp?: ZeitplanungTyp;
zeitplanungZeitpunkt?: string;
};
const LINK_CLASSES = "mb-4 inline-flex min-h-11 items-center text-sm text-performance-blue hover:underline";
// Gemeinsame Felder fuer "Auftrag anlegen" und "Auftrag bearbeiten". Formular
// und Absende-Knopf bleiben in der jeweiligen Seite.
export function AuftragStammFelder({
orte,
fahrzeuge,
werte,
vorbefuelltesDatum,
neuLinks,
nummerLabel,
}: {
orte: Ort[];
fahrzeuge: Fahrzeug[];
werte: AuftragStammWerte;
vorbefuelltesDatum: string;
neuLinks: { abholort: string; zielort: string; fahrzeug: string } | null;
nummerLabel: string;
}) {
return (
<>
<FormField label="Abholort" htmlFor="abholort_id">
<select id="abholort_id" name="abholort_id" required defaultValue={werte.abholortId ?? ""} className={FIELD_CLASSES}>
<option value="" disabled>
Bitte waehlen
</option>
{orte.map((ort) => (
<option key={ort.id} value={ort.id}>
{ort.adresse} ({ort.ansprechpartner})
</option>
))}
</select>
</FormField>
{neuLinks && (
<Link href={neuLinks.abholort} className={LINK_CLASSES}>
+ Neuer Abholort
</Link>
)}
<FormField label="Zielort" htmlFor="zielort_id">
<select id="zielort_id" name="zielort_id" required defaultValue={werte.zielortId ?? ""} className={FIELD_CLASSES}>
<option value="" disabled>
Bitte waehlen
</option>
{orte.map((ort) => (
<option key={ort.id} value={ort.id}>
{ort.adresse} ({ort.ansprechpartner})
</option>
))}
</select>
</FormField>
{neuLinks && (
<Link href={neuLinks.zielort} className={LINK_CLASSES}>
+ Neuer Zielort
</Link>
)}
<FormField label="Fahrzeug" htmlFor="fahrzeug_id">
<select id="fahrzeug_id" name="fahrzeug_id" required defaultValue={werte.fahrzeugId ?? ""} className={FIELD_CLASSES}>
<option value="" disabled>
Bitte waehlen
</option>
{fahrzeuge.map((fahrzeug) => (
<option key={fahrzeug.id} value={fahrzeug.id}>
{fahrzeugBezeichnung(fahrzeug)}
</option>
))}
</select>
</FormField>
{neuLinks && (
<Link href={neuLinks.fahrzeug} className={LINK_CLASSES}>
+ Neues Fahrzeug
</Link>
)}
<ZeitplanungFeld
vorbefuelltesDatum={vorbefuelltesDatum}
startTyp={werte.zeitplanungTyp}
startZeitpunkt={werte.zeitplanungZeitpunkt}
/>
<FormField label={nummerLabel} htmlFor="auftragsnummer">
<input
id="auftragsnummer"
name="auftragsnummer"
type="text"
defaultValue={werte.auftragsnummer ?? ""}
className={FIELD_CLASSES}
/>
</FormField>
</>
);
}

View File

@ -12,8 +12,16 @@ const LABELS: Record<ZeitplanungTyp, string> = {
sobald_moeglich: "Sobald als möglich",
};
export function ZeitplanungFeld({ vorbefuelltesDatum }: { vorbefuelltesDatum: string }) {
const [typ, setTyp] = useState<ZeitplanungTyp>("ab_sofort");
export function ZeitplanungFeld({
vorbefuelltesDatum,
startTyp = "ab_sofort",
startZeitpunkt,
}: {
vorbefuelltesDatum: string;
startTyp?: ZeitplanungTyp;
startZeitpunkt?: string;
}) {
const [typ, setTyp] = useState<ZeitplanungTyp>(startTyp);
const zeigeZeitpunkt = typ === "am" || typ === "ab";
return (
@ -41,7 +49,7 @@ export function ZeitplanungFeld({ vorbefuelltesDatum }: { vorbefuelltesDatum: st
name="zeitplanung_zeitpunkt"
type="datetime-local"
required
defaultValue={vorbefuelltesDatum}
defaultValue={startZeitpunkt ?? vorbefuelltesDatum}
className={FIELD_CLASSES}
/>
</FormField>

View File

@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
import { validateAuftragEingaben } from "./validate-auftrag";
import { istBearbeitbar, validateAuftragEingaben } from "./validate-auftrag";
import { parseZeitplanungZeitpunkt } from "./zeitplanung";
function formular(werte: Record<string, string>): FormData {
@ -69,3 +69,13 @@ describe("validateAuftragEingaben", () => {
});
});
});
describe("istBearbeitbar", () => {
it("erlaubt nur offen und zugewiesen", () => {
expect(istBearbeitbar("offen")).toBe(true);
expect(istBearbeitbar("zugewiesen")).toBe(true);
expect(istBearbeitbar("unterwegs")).toBe(false);
expect(istBearbeitbar("abgeschlossen")).toBe(false);
expect(istBearbeitbar("storniert")).toBe(false);
});
});

View File

@ -54,3 +54,10 @@ export function validateAuftragEingaben(formData: FormData): AuftragValidierung
},
};
}
export const NICHT_BEARBEITBAR_MELDUNG = "Der Auftrag kann in diesem Status nicht mehr bearbeitet werden.";
// Nach Fahrtbeginn gehoeren die Stammdaten zur Dokumentation und bleiben fest.
export function istBearbeitbar(status: string): boolean {
return status === "offen" || status === "zugewiesen";
}