forked from fahrPool/fahrPool
feat: add validation for Schicht-Korrektur (geht_um after kommt_um)
This commit is contained in:
parent
3fe33c5e0f
commit
d91765bd69
|
|
@ -0,0 +1,33 @@
|
||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
import { validateSchichtKorrektur } from "./validate-korrektur";
|
||||||
|
|
||||||
|
describe("validateSchichtKorrektur", () => {
|
||||||
|
it("akzeptiert eine gueltige Geht-Uhrzeit nach der Kommt-Uhrzeit", () => {
|
||||||
|
const result = validateSchichtKorrektur({
|
||||||
|
gehtUm: "2026-07-13T14:00",
|
||||||
|
kommtUm: "2026-07-13T06:00:00.000Z",
|
||||||
|
});
|
||||||
|
expect(result.valid).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("lehnt eine fehlende Geht-Uhrzeit ab", () => {
|
||||||
|
const result = validateSchichtKorrektur({ gehtUm: "", kommtUm: "2026-07-13T06:00:00.000Z" });
|
||||||
|
expect(result).toEqual({ valid: false, error: "Bitte eine Geht-Uhrzeit angeben." });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("lehnt eine Geht-Uhrzeit vor der Kommt-Uhrzeit ab", () => {
|
||||||
|
const result = validateSchichtKorrektur({
|
||||||
|
gehtUm: "2026-07-13T05:00",
|
||||||
|
kommtUm: "2026-07-13T06:00:00.000Z",
|
||||||
|
});
|
||||||
|
expect(result).toEqual({ valid: false, error: "Die Geht-Uhrzeit muss nach der Kommt-Uhrzeit liegen." });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("lehnt eine Geht-Uhrzeit gleich der Kommt-Uhrzeit ab", () => {
|
||||||
|
const result = validateSchichtKorrektur({
|
||||||
|
gehtUm: "2026-07-13T06:00:00.000Z",
|
||||||
|
kommtUm: "2026-07-13T06:00:00.000Z",
|
||||||
|
});
|
||||||
|
expect(result.valid).toBe(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
@ -0,0 +1,21 @@
|
||||||
|
export type KorrekturValidationResult =
|
||||||
|
| { valid: true; gehtUm: string }
|
||||||
|
| { valid: false; error: string };
|
||||||
|
|
||||||
|
export function validateSchichtKorrektur(input: {
|
||||||
|
gehtUm: FormDataEntryValue | null;
|
||||||
|
kommtUm: string;
|
||||||
|
}): KorrekturValidationResult {
|
||||||
|
if (typeof input.gehtUm !== "string" || input.gehtUm.trim() === "") {
|
||||||
|
return { valid: false, error: "Bitte eine Geht-Uhrzeit angeben." };
|
||||||
|
}
|
||||||
|
const gehtUmDatum = new Date(input.gehtUm);
|
||||||
|
if (Number.isNaN(gehtUmDatum.getTime())) {
|
||||||
|
return { valid: false, error: "Ungueltige Geht-Uhrzeit." };
|
||||||
|
}
|
||||||
|
const kommtUmDatum = new Date(input.kommtUm);
|
||||||
|
if (gehtUmDatum.getTime() <= kommtUmDatum.getTime()) {
|
||||||
|
return { valid: false, error: "Die Geht-Uhrzeit muss nach der Kommt-Uhrzeit liegen." };
|
||||||
|
}
|
||||||
|
return { valid: true, gehtUm: gehtUmDatum.toISOString() };
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue