forked from fahrPool/fahrPool
Merge feature/auftrag-klonen: Auftraege klonen und Folgeauftrag ab Zielort
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
beed834ccd
|
|
@ -0,0 +1,44 @@
|
||||||
|
"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 { validateKlonEingaben } from "@/lib/auftraege/validate-auftrag";
|
||||||
|
|
||||||
|
// Legt je gewaehltem Fahrzeug einen neuen, offenen Auftrag auf derselben
|
||||||
|
// Strecke an. Ein einziges Insert-Statement: alle oder keiner. Nummer und
|
||||||
|
// firma_id setzen die bestehenden Trigger; RLS erlaubt das Insert nur
|
||||||
|
// admin/disponent mit erstellt_von = auth.uid() (eigentliche Sperre).
|
||||||
|
export async function auftragKlonen(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 klonen.")}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const validierung = validateKlonEingaben(formData);
|
||||||
|
if (!validierung.valid) {
|
||||||
|
redirect(`/auftraege/${auftragId}/klonen?error=${encodeURIComponent(validierung.error)}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const supabase = await createClient();
|
||||||
|
const {
|
||||||
|
data: { user },
|
||||||
|
} = await supabase.auth.getUser();
|
||||||
|
if (!user) {
|
||||||
|
redirect("/login");
|
||||||
|
}
|
||||||
|
|
||||||
|
const { fahrzeug_ids, ...strecke } = validierung.werte;
|
||||||
|
const { error } = await supabase
|
||||||
|
.from("auftraege")
|
||||||
|
.insert(fahrzeug_ids.map((fahrzeug_id) => ({ ...strecke, fahrzeug_id, erstellt_von: user.id })));
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
redirect(
|
||||||
|
`/auftraege/${auftragId}/klonen?error=${encodeURIComponent("Auftraege konnten nicht angelegt werden.")}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
redirect("/auftraege");
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,78 @@
|
||||||
|
import { notFound, redirect } from "next/navigation";
|
||||||
|
import Link from "next/link";
|
||||||
|
import { loadAuftrag, listOrte, listFahrzeuge } from "@/lib/auftraege/queries";
|
||||||
|
import { alsDatetimeLocal, berechneVorbefuelltenZeitpunkt } from "@/lib/auftraege/zeitplanung";
|
||||||
|
import { loadProfile } from "@/lib/auth/profile";
|
||||||
|
import { kannAuftraegeVerwalten } from "@/lib/auth/roles";
|
||||||
|
import { auftragKlonen } from "./actions";
|
||||||
|
import { AuftragStammFelder } from "@/components/auftraege/AuftragStammFelder";
|
||||||
|
import { FahrzeugMehrfachAuswahl } from "@/components/auftraege/FahrzeugMehrfachAuswahl";
|
||||||
|
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 AuftragKlonenPage({
|
||||||
|
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 klonen.")}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const [auftrag, orte, fahrzeuge] = await Promise.all([loadAuftrag(id), listOrte(), listFahrzeuge()]);
|
||||||
|
if (!auftrag) {
|
||||||
|
notFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
const aktion = auftragKlonen.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 klonen</h1>
|
||||||
|
<p className="mb-6 text-sm text-asphalt-grey/70">
|
||||||
|
Vorlage {auftrag.auftragsnummer}: {auftrag.abholort.adresse} → {auftrag.zielort.adresse}
|
||||||
|
</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,
|
||||||
|
zeitplanungTyp: auftrag.zeitplanung_typ,
|
||||||
|
zeitplanungZeitpunkt: auftrag.zeitplanung_zeitpunkt
|
||||||
|
? alsDatetimeLocal(auftrag.zeitplanung_zeitpunkt)
|
||||||
|
: undefined,
|
||||||
|
}}
|
||||||
|
vorbefuelltesDatum={berechneVorbefuelltenZeitpunkt()}
|
||||||
|
neuLinks={null}
|
||||||
|
nummerLabel="Auftragsnummer"
|
||||||
|
ohneFahrzeugUndNummer
|
||||||
|
/>
|
||||||
|
<FahrzeugMehrfachAuswahl fahrzeuge={fahrzeuge} />
|
||||||
|
<p className="mb-4 text-sm text-asphalt-grey/70">Je angehaktem Fahrzeug wird ein eigener Auftrag angelegt.</p>
|
||||||
|
<div className="flex flex-col gap-3 sm:flex-row">
|
||||||
|
<Button type="submit">Auftraege anlegen</Button>
|
||||||
|
<Link href={`/auftraege/${auftrag.id}`} className={buttonClasses("secondary", true)}>
|
||||||
|
Abbrechen
|
||||||
|
</Link>
|
||||||
|
</div>
|
||||||
|
</NetzSicheresFormular>
|
||||||
|
</Card>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
@ -2,6 +2,7 @@ import { notFound } from "next/navigation";
|
||||||
import Link from "next/link";
|
import Link from "next/link";
|
||||||
import { loadAuftrag, findeFolgeauftrag } from "@/lib/auftraege/queries";
|
import { loadAuftrag, findeFolgeauftrag } from "@/lib/auftraege/queries";
|
||||||
import { formatZeitplanung } from "@/lib/auftraege/zeitplanung";
|
import { formatZeitplanung } from "@/lib/auftraege/zeitplanung";
|
||||||
|
import { folgeauftragUrl } from "@/lib/auftraege/klonen";
|
||||||
import { loadProfile } from "@/lib/auth/profile";
|
import { loadProfile } from "@/lib/auth/profile";
|
||||||
import { istEinsatzrolle, kannAuftraegeVerwalten, personBezeichnung } from "@/lib/auth/roles";
|
import { istEinsatzrolle, kannAuftraegeVerwalten, personBezeichnung } from "@/lib/auth/roles";
|
||||||
import { listEinsatzPersonen } from "@/lib/dashboard/queries";
|
import { listEinsatzPersonen } from "@/lib/dashboard/queries";
|
||||||
|
|
@ -360,6 +361,20 @@ export default async function AuftragDetailPage({
|
||||||
</Card>
|
</Card>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
|
{kannAuftraegeVerwalten(profileResult.profile.role) && (
|
||||||
|
<Card className="mb-6">
|
||||||
|
<h2 className="mb-4 text-lg font-semibold text-asphalt-grey">Als Vorlage nutzen</h2>
|
||||||
|
<div className="flex flex-col gap-3 sm:flex-row">
|
||||||
|
<Link href={`/auftraege/${auftrag.id}/klonen`} className={buttonClasses("secondary", true)}>
|
||||||
|
Klonen – gleiche Strecke
|
||||||
|
</Link>
|
||||||
|
<Link href={folgeauftragUrl(auftrag)} className={buttonClasses("secondary", true)}>
|
||||||
|
Folgeauftrag – ab Zielort
|
||||||
|
</Link>
|
||||||
|
</div>
|
||||||
|
</Card>
|
||||||
|
)}
|
||||||
|
|
||||||
{kannStornieren && auftrag.status !== "abgeschlossen" && auftrag.status !== "storniert" && (
|
{kannStornieren && auftrag.status !== "abgeschlossen" && auftrag.status !== "storniert" && (
|
||||||
<Card>
|
<Card>
|
||||||
<NetzSicheresFormular action={stornierenAction}>
|
<NetzSicheresFormular action={stornierenAction}>
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,7 @@ export function AuftragStammFelder({
|
||||||
vorbefuelltesDatum,
|
vorbefuelltesDatum,
|
||||||
neuLinks,
|
neuLinks,
|
||||||
nummerLabel,
|
nummerLabel,
|
||||||
|
ohneFahrzeugUndNummer = false,
|
||||||
}: {
|
}: {
|
||||||
orte: Ort[];
|
orte: Ort[];
|
||||||
fahrzeuge: Fahrzeug[];
|
fahrzeuge: Fahrzeug[];
|
||||||
|
|
@ -32,6 +33,9 @@ export function AuftragStammFelder({
|
||||||
vorbefuelltesDatum: string;
|
vorbefuelltesDatum: string;
|
||||||
neuLinks: { abholort: string; zielort: string; fahrzeug: string } | null;
|
neuLinks: { abholort: string; zielort: string; fahrzeug: string } | null;
|
||||||
nummerLabel: string;
|
nummerLabel: string;
|
||||||
|
// Klonen: Fahrzeuge kommen aus einer Mehrfachauswahl, die Nummer vergibt
|
||||||
|
// der Trigger -- beides wird hier dann nicht gerendert.
|
||||||
|
ohneFahrzeugUndNummer?: boolean;
|
||||||
}) {
|
}) {
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
|
@ -71,22 +75,26 @@ export function AuftragStammFelder({
|
||||||
</Link>
|
</Link>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
<FormField label="Fahrzeug" htmlFor="fahrzeug_id">
|
{!ohneFahrzeugUndNummer && (
|
||||||
<select id="fahrzeug_id" name="fahrzeug_id" required defaultValue={werte.fahrzeugId ?? ""} className={FIELD_CLASSES}>
|
<>
|
||||||
<option value="" disabled>
|
<FormField label="Fahrzeug" htmlFor="fahrzeug_id">
|
||||||
Bitte waehlen
|
<select id="fahrzeug_id" name="fahrzeug_id" required defaultValue={werte.fahrzeugId ?? ""} className={FIELD_CLASSES}>
|
||||||
</option>
|
<option value="" disabled>
|
||||||
{fahrzeuge.map((fahrzeug) => (
|
Bitte waehlen
|
||||||
<option key={fahrzeug.id} value={fahrzeug.id}>
|
</option>
|
||||||
{fahrzeugBezeichnung(fahrzeug)}
|
{fahrzeuge.map((fahrzeug) => (
|
||||||
</option>
|
<option key={fahrzeug.id} value={fahrzeug.id}>
|
||||||
))}
|
{fahrzeugBezeichnung(fahrzeug)}
|
||||||
</select>
|
</option>
|
||||||
</FormField>
|
))}
|
||||||
{neuLinks && (
|
</select>
|
||||||
<Link href={neuLinks.fahrzeug} className={LINK_CLASSES}>
|
</FormField>
|
||||||
+ Neues Fahrzeug
|
{neuLinks && (
|
||||||
</Link>
|
<Link href={neuLinks.fahrzeug} className={LINK_CLASSES}>
|
||||||
|
+ Neues Fahrzeug
|
||||||
|
</Link>
|
||||||
|
)}
|
||||||
|
</>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
<ZeitplanungFeld
|
<ZeitplanungFeld
|
||||||
|
|
@ -95,15 +103,17 @@ export function AuftragStammFelder({
|
||||||
startZeitpunkt={werte.zeitplanungZeitpunkt}
|
startZeitpunkt={werte.zeitplanungZeitpunkt}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<FormField label={nummerLabel} htmlFor="auftragsnummer">
|
{!ohneFahrzeugUndNummer && (
|
||||||
<input
|
<FormField label={nummerLabel} htmlFor="auftragsnummer">
|
||||||
id="auftragsnummer"
|
<input
|
||||||
name="auftragsnummer"
|
id="auftragsnummer"
|
||||||
type="text"
|
name="auftragsnummer"
|
||||||
defaultValue={werte.auftragsnummer ?? ""}
|
type="text"
|
||||||
className={FIELD_CLASSES}
|
defaultValue={werte.auftragsnummer ?? ""}
|
||||||
/>
|
className={FIELD_CLASSES}
|
||||||
</FormField>
|
/>
|
||||||
|
</FormField>
|
||||||
|
)}
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,26 @@
|
||||||
|
import type { Fahrzeug } from "@/lib/auftraege/queries";
|
||||||
|
import { fahrzeugBezeichnung } from "@/lib/orte-fahrzeuge/fahrzeug-bezeichnung";
|
||||||
|
|
||||||
|
// Mehrfachauswahl fuers Klonen: je angehaktem Fahrzeug entsteht ein Auftrag.
|
||||||
|
// Ganze Zeile ist klickbar (mobile Bedienung), nichts ist vorab angehakt.
|
||||||
|
export function FahrzeugMehrfachAuswahl({ fahrzeuge }: { fahrzeuge: Fahrzeug[] }) {
|
||||||
|
return (
|
||||||
|
<fieldset className="mb-4">
|
||||||
|
<legend className="mb-2 block text-sm font-medium text-asphalt-grey">Fahrzeuge</legend>
|
||||||
|
{fahrzeuge.length === 0 ? (
|
||||||
|
<p className="text-sm text-asphalt-grey/70">Noch keine Fahrzeuge angelegt.</p>
|
||||||
|
) : (
|
||||||
|
<ul className="divide-y divide-asphalt-grey/10 rounded-md border border-asphalt-grey/20">
|
||||||
|
{fahrzeuge.map((fahrzeug) => (
|
||||||
|
<li key={fahrzeug.id}>
|
||||||
|
<label className="flex min-h-11 cursor-pointer items-center gap-3 px-3 py-2 text-asphalt-grey">
|
||||||
|
<input type="checkbox" name="fahrzeug_ids" value={fahrzeug.id} className="h-5 w-5 shrink-0" />
|
||||||
|
<span>{fahrzeugBezeichnung(fahrzeug)}</span>
|
||||||
|
</label>
|
||||||
|
</li>
|
||||||
|
))}
|
||||||
|
</ul>
|
||||||
|
)}
|
||||||
|
</fieldset>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,14 @@
|
||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
import { folgeauftragUrl } from "./klonen";
|
||||||
|
|
||||||
|
describe("folgeauftragUrl", () => {
|
||||||
|
it("setzt alten Zielort als Abholort, Fahrzeug und Vorgaenger", () => {
|
||||||
|
const url = folgeauftragUrl({ id: "a1", zielort: { id: "o2" }, fahrzeug: { id: "f1" } });
|
||||||
|
expect(url.startsWith("/auftraege/neu?")).toBe(true);
|
||||||
|
const params = new URLSearchParams(url.split("?")[1]);
|
||||||
|
expect(params.get("abholort_id")).toBe("o2");
|
||||||
|
expect(params.get("fahrzeug_id")).toBe("f1");
|
||||||
|
expect(params.get("vorgaenger_auftrag_id")).toBe("a1");
|
||||||
|
expect(params.has("zielort_id")).toBe(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
@ -0,0 +1,10 @@
|
||||||
|
// Folgeauftrag: Das Fahrzeug faehrt vom bisherigen Zielort weiter. Das
|
||||||
|
// bestehende Formular /auftraege/neu uebernimmt Vorbelegung und Verknuepfung.
|
||||||
|
export function folgeauftragUrl(auftrag: { id: string; zielort: { id: string }; fahrzeug: { id: string } }): string {
|
||||||
|
const query = new URLSearchParams({
|
||||||
|
abholort_id: auftrag.zielort.id,
|
||||||
|
fahrzeug_id: auftrag.fahrzeug.id,
|
||||||
|
vorgaenger_auftrag_id: auftrag.id,
|
||||||
|
});
|
||||||
|
return `/auftraege/neu?${query.toString()}`;
|
||||||
|
}
|
||||||
|
|
@ -132,6 +132,9 @@ export async function findeFolgeauftrag(vorgaengerId: string): Promise<{ id: str
|
||||||
.from("auftraege")
|
.from("auftraege")
|
||||||
.select("id")
|
.select("id")
|
||||||
.eq("vorgaenger_auftrag_id", vorgaengerId)
|
.eq("vorgaenger_auftrag_id", vorgaengerId)
|
||||||
.maybeSingle();
|
// Seit dem Folgeauftrag-Button kann es mehrere Folgeauftraege geben;
|
||||||
return data;
|
// maybeSingle() wuerde dann scheitern und die Anzeige verschwinden.
|
||||||
|
.order("created_at", { ascending: true })
|
||||||
|
.limit(1);
|
||||||
|
return data?.[0] ?? null;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import { describe, expect, it } from "vitest";
|
import { describe, expect, it } from "vitest";
|
||||||
import { istBearbeitbar, validateAuftragEingaben } from "./validate-auftrag";
|
import { istBearbeitbar, validateAuftragEingaben, validateKlonEingaben } from "./validate-auftrag";
|
||||||
import { parseZeitplanungZeitpunkt } from "./zeitplanung";
|
import { parseZeitplanungZeitpunkt } from "./zeitplanung";
|
||||||
|
|
||||||
function formular(werte: Record<string, string>): FormData {
|
function formular(werte: Record<string, string>): FormData {
|
||||||
|
|
@ -79,3 +79,55 @@ describe("istBearbeitbar", () => {
|
||||||
expect(istBearbeitbar("storniert")).toBe(false);
|
expect(istBearbeitbar("storniert")).toBe(false);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function klonFormular(werte: Record<string, string>, fahrzeugIds: string[]): FormData {
|
||||||
|
const fd = formular(werte);
|
||||||
|
for (const id of fahrzeugIds) fd.append("fahrzeug_ids", id);
|
||||||
|
return fd;
|
||||||
|
}
|
||||||
|
|
||||||
|
const KLON_BASIS = { abholort_id: "o1", zielort_id: "o2", zeitplanung_typ: "ab_sofort" };
|
||||||
|
|
||||||
|
describe("validateKlonEingaben", () => {
|
||||||
|
it("liefert die Werte mit mehreren Fahrzeugen", () => {
|
||||||
|
expect(validateKlonEingaben(klonFormular(KLON_BASIS, ["f1", "f2"]))).toEqual({
|
||||||
|
valid: true,
|
||||||
|
werte: {
|
||||||
|
abholort_id: "o1",
|
||||||
|
zielort_id: "o2",
|
||||||
|
fahrzeug_ids: ["f1", "f2"],
|
||||||
|
zeitplanung_typ: "ab_sofort",
|
||||||
|
zeitplanung_zeitpunkt: null,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("entfernt doppelte Fahrzeuge", () => {
|
||||||
|
const ergebnis = validateKlonEingaben(klonFormular(KLON_BASIS, ["f1", "f1", "f2"]));
|
||||||
|
expect(ergebnis.valid && ergebnis.werte.fahrzeug_ids).toEqual(["f1", "f2"]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("verlangt mindestens ein Fahrzeug", () => {
|
||||||
|
const fehler = { valid: false, error: "Bitte mindestens ein Fahrzeug auswaehlen." };
|
||||||
|
expect(validateKlonEingaben(klonFormular(KLON_BASIS, []))).toEqual(fehler);
|
||||||
|
expect(validateKlonEingaben(klonFormular(KLON_BASIS, ["", ""]))).toEqual(fehler);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("verlangt Abholort und Zielort", () => {
|
||||||
|
expect(validateKlonEingaben(klonFormular({ ...KLON_BASIS, abholort_id: "" }, ["f1"]))).toEqual({
|
||||||
|
valid: false,
|
||||||
|
error: "Bitte einen Abholort auswaehlen.",
|
||||||
|
});
|
||||||
|
expect(validateKlonEingaben(klonFormular({ ...KLON_BASIS, zielort_id: "" }, ["f1"]))).toEqual({
|
||||||
|
valid: false,
|
||||||
|
error: "Bitte einen Zielort auswaehlen.",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("verlangt bei am einen Zeitpunkt", () => {
|
||||||
|
expect(validateKlonEingaben(klonFormular({ ...KLON_BASIS, zeitplanung_typ: "am" }, ["f1"]))).toEqual({
|
||||||
|
valid: false,
|
||||||
|
error: "Bitte Datum und Uhrzeit fuer die Zeitplanung angeben.",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
|
||||||
|
|
@ -15,9 +15,13 @@ function textOderLeer(wert: FormDataEntryValue | null): string {
|
||||||
return typeof wert === "string" ? wert : "";
|
return typeof wert === "string" ? wert : "";
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gemeinsame Pruefung fuer Anlegen und Bearbeiten. Reihenfolge und Meldungen
|
type StreckeUndZeit = Pick<AuftragWerte, "abholort_id" | "zielort_id" | "zeitplanung_typ" | "zeitplanung_zeitpunkt">;
|
||||||
// entsprechen bewusst dem bisherigen Verhalten von createAuftrag.
|
|
||||||
export function validateAuftragEingaben(formData: FormData): AuftragValidierung {
|
export type KlonWerte = StreckeUndZeit & { fahrzeug_ids: string[] };
|
||||||
|
|
||||||
|
export type KlonValidierung = { valid: true; werte: KlonWerte } | { valid: false; error: string };
|
||||||
|
|
||||||
|
function pruefeStreckeUndZeit(formData: FormData): { valid: true; werte: StreckeUndZeit } | { valid: false; error: string } {
|
||||||
const typRaw = textOderLeer(formData.get("zeitplanung_typ"));
|
const typRaw = textOderLeer(formData.get("zeitplanung_typ"));
|
||||||
const zeitplanungTyp = (ZEITPLANUNG_TYPEN as readonly string[]).includes(typRaw)
|
const zeitplanungTyp = (ZEITPLANUNG_TYPEN as readonly string[]).includes(typRaw)
|
||||||
? (typRaw as ZeitplanungTyp)
|
? (typRaw as ZeitplanungTyp)
|
||||||
|
|
@ -37,6 +41,23 @@ export function validateAuftragEingaben(formData: FormData): AuftragValidierung
|
||||||
if (!abholortId) return { valid: false, error: "Bitte einen Abholort auswaehlen." };
|
if (!abholortId) return { valid: false, error: "Bitte einen Abholort auswaehlen." };
|
||||||
const zielortId = textOderLeer(formData.get("zielort_id"));
|
const zielortId = textOderLeer(formData.get("zielort_id"));
|
||||||
if (!zielortId) return { valid: false, error: "Bitte einen Zielort auswaehlen." };
|
if (!zielortId) return { valid: false, error: "Bitte einen Zielort auswaehlen." };
|
||||||
|
|
||||||
|
return {
|
||||||
|
valid: true,
|
||||||
|
werte: {
|
||||||
|
abholort_id: abholortId,
|
||||||
|
zielort_id: zielortId,
|
||||||
|
zeitplanung_typ: zeitplanungTyp,
|
||||||
|
zeitplanung_zeitpunkt: zeitplanungZeitpunkt,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Gemeinsame Pruefung fuer Anlegen und Bearbeiten. Reihenfolge und Meldungen
|
||||||
|
// entsprechen bewusst dem bisherigen Verhalten von createAuftrag.
|
||||||
|
export function validateAuftragEingaben(formData: FormData): AuftragValidierung {
|
||||||
|
const basis = pruefeStreckeUndZeit(formData);
|
||||||
|
if (!basis.valid) return basis;
|
||||||
const fahrzeugId = textOderLeer(formData.get("fahrzeug_id"));
|
const fahrzeugId = textOderLeer(formData.get("fahrzeug_id"));
|
||||||
if (!fahrzeugId) return { valid: false, error: "Bitte ein Fahrzeug auswaehlen." };
|
if (!fahrzeugId) return { valid: false, error: "Bitte ein Fahrzeug auswaehlen." };
|
||||||
|
|
||||||
|
|
@ -45,16 +66,29 @@ export function validateAuftragEingaben(formData: FormData): AuftragValidierung
|
||||||
return {
|
return {
|
||||||
valid: true,
|
valid: true,
|
||||||
werte: {
|
werte: {
|
||||||
abholort_id: abholortId,
|
abholort_id: basis.werte.abholort_id,
|
||||||
zielort_id: zielortId,
|
zielort_id: basis.werte.zielort_id,
|
||||||
fahrzeug_id: fahrzeugId,
|
fahrzeug_id: fahrzeugId,
|
||||||
auftragsnummer: nummer || null,
|
auftragsnummer: nummer || null,
|
||||||
zeitplanung_typ: zeitplanungTyp,
|
zeitplanung_typ: basis.werte.zeitplanung_typ,
|
||||||
zeitplanung_zeitpunkt: zeitplanungZeitpunkt,
|
zeitplanung_zeitpunkt: basis.werte.zeitplanung_zeitpunkt,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Klonen auf gleicher Strecke: ein Auftrag je gewaehltem Fahrzeug. Doppelte
|
||||||
|
// IDs (z.B. manipulierter POST) werden entfernt, damit nie zwei identische
|
||||||
|
// Auftraege entstehen.
|
||||||
|
export function validateKlonEingaben(formData: FormData): KlonValidierung {
|
||||||
|
const basis = pruefeStreckeUndZeit(formData);
|
||||||
|
if (!basis.valid) return basis;
|
||||||
|
const fahrzeugIds = [
|
||||||
|
...new Set(formData.getAll("fahrzeug_ids").map(textOderLeer).filter((id) => id !== "")),
|
||||||
|
];
|
||||||
|
if (fahrzeugIds.length === 0) return { valid: false, error: "Bitte mindestens ein Fahrzeug auswaehlen." };
|
||||||
|
return { valid: true, werte: { ...basis.werte, fahrzeug_ids: fahrzeugIds } };
|
||||||
|
}
|
||||||
|
|
||||||
export const NICHT_BEARBEITBAR_MELDUNG = "Der Auftrag kann in diesem Status nicht mehr bearbeitet werden.";
|
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.
|
// Nach Fahrtbeginn gehoeren die Stammdaten zur Dokumentation und bleiben fest.
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue