1
0
Fork 0

feat: add validation for Schicht-Korrektur (geht_um after kommt_um)

This commit is contained in:
Jonny 2026-07-13 16:44:30 +02:00
parent 3fe33c5e0f
commit d91765bd69
2 changed files with 54 additions and 0 deletions

View File

@ -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);
});
});

View File

@ -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() };
}