forked from fahrPool/fahrPool
feat: Validierung fuer Auftrags-Klon und Folgeauftrag-URL
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
9224ada4fb
commit
236bd528f3
|
|
@ -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()}`;
|
||||||
|
}
|
||||||
|
|
@ -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