forked from fahrPool/fahrPool
98 lines
3.9 KiB
TypeScript
98 lines
3.9 KiB
TypeScript
import { ZEITPLANUNG_TYPEN, type ZeitplanungTyp, parseZeitplanungZeitpunkt } from "./zeitplanung";
|
|
|
|
export type AuftragWerte = {
|
|
abholort_id: string;
|
|
zielort_id: string;
|
|
fahrzeug_id: string;
|
|
auftragsnummer: string | null;
|
|
zeitplanung_typ: ZeitplanungTyp;
|
|
zeitplanung_zeitpunkt: string | null;
|
|
};
|
|
|
|
export type AuftragValidierung = { valid: true; werte: AuftragWerte } | { valid: false; error: string };
|
|
|
|
function textOderLeer(wert: FormDataEntryValue | null): string {
|
|
return typeof wert === "string" ? wert : "";
|
|
}
|
|
|
|
type StreckeUndZeit = Pick<AuftragWerte, "abholort_id" | "zielort_id" | "zeitplanung_typ" | "zeitplanung_zeitpunkt">;
|
|
|
|
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 zeitplanungTyp = (ZEITPLANUNG_TYPEN as readonly string[]).includes(typRaw)
|
|
? (typRaw as ZeitplanungTyp)
|
|
: "ab_sofort";
|
|
|
|
let zeitplanungZeitpunkt: string | null = null;
|
|
if (zeitplanungTyp === "am" || zeitplanungTyp === "ab") {
|
|
const zeitpunktRaw = textOderLeer(formData.get("zeitplanung_zeitpunkt"));
|
|
const geparst = zeitpunktRaw ? parseZeitplanungZeitpunkt(zeitpunktRaw) : null;
|
|
if (!geparst || Number.isNaN(geparst.getTime())) {
|
|
return { valid: false, error: "Bitte Datum und Uhrzeit fuer die Zeitplanung angeben." };
|
|
}
|
|
zeitplanungZeitpunkt = geparst.toISOString();
|
|
}
|
|
|
|
const abholortId = textOderLeer(formData.get("abholort_id"));
|
|
if (!abholortId) return { valid: false, error: "Bitte einen Abholort auswaehlen." };
|
|
const zielortId = textOderLeer(formData.get("zielort_id"));
|
|
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"));
|
|
if (!fahrzeugId) return { valid: false, error: "Bitte ein Fahrzeug auswaehlen." };
|
|
|
|
const nummer = textOderLeer(formData.get("auftragsnummer")).trim();
|
|
|
|
return {
|
|
valid: true,
|
|
werte: {
|
|
abholort_id: basis.werte.abholort_id,
|
|
zielort_id: basis.werte.zielort_id,
|
|
fahrzeug_id: fahrzeugId,
|
|
auftragsnummer: nummer || null,
|
|
zeitplanung_typ: basis.werte.zeitplanung_typ,
|
|
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.";
|
|
|
|
// Nach Fahrtbeginn gehoeren die Stammdaten zur Dokumentation und bleiben fest.
|
|
export function istBearbeitbar(status: string): boolean {
|
|
return status === "offen" || status === "zugewiesen";
|
|
}
|