1
0
Fork 0

feat: Zeitplanung-Typen, Formatierung und Datumsvorbefuellung

This commit is contained in:
Jonny 2026-08-20 19:46:59 +02:00
parent 9c00e0bb14
commit a592054ef6
2 changed files with 68 additions and 0 deletions

View File

@ -0,0 +1,34 @@
import { describe, expect, it } from "vitest";
import { berechneVorbefuelltenZeitpunkt, formatZeitplanung } from "./zeitplanung";
describe("formatZeitplanung", () => {
it("formatiert ab_sofort", () => {
expect(formatZeitplanung("ab_sofort", null)).toBe("Ab sofort");
});
it("formatiert diese_woche", () => {
expect(formatZeitplanung("diese_woche", null)).toBe("Diese Woche");
});
it("formatiert sobald_moeglich", () => {
expect(formatZeitplanung("sobald_moeglich", null)).toBe("Sobald als möglich");
});
it("formatiert am mit Datum/Uhrzeit in Europe/Berlin-Zeit", () => {
expect(formatZeitplanung("am", "2026-08-21T13:00:00.000Z")).toBe("Am 21.08.2026, 15:00 Uhr");
});
it("formatiert ab mit Datum/Uhrzeit in Europe/Berlin-Zeit", () => {
expect(formatZeitplanung("ab", "2026-08-21T13:00:00.000Z")).toBe("Ab 21.08.2026, 15:00 Uhr");
});
});
describe("berechneVorbefuelltenZeitpunkt", () => {
it("berechnet morgen 15:00 Uhr basierend auf dem uebergebenen Zeitpunkt", () => {
expect(berechneVorbefuelltenZeitpunkt(new Date(2026, 7, 20, 10, 30))).toBe("2026-08-21T15:00");
});
it("behandelt Monatswechsel korrekt", () => {
expect(berechneVorbefuelltenZeitpunkt(new Date(2026, 7, 31, 8, 0))).toBe("2026-09-01T15:00");
});
});

View File

@ -0,0 +1,34 @@
export const ZEITPLANUNG_TYPEN = ["ab_sofort", "am", "ab", "diese_woche", "sobald_moeglich"] as const;
export type ZeitplanungTyp = (typeof ZEITPLANUNG_TYPEN)[number];
export function formatZeitplanung(typ: ZeitplanungTyp, zeitpunkt: string | null): string {
if (typ === "ab_sofort") return "Ab sofort";
if (typ === "diese_woche") return "Diese Woche";
if (typ === "sobald_moeglich") return "Sobald als möglich";
// DB-CHECK-Constraint garantiert zeitpunkt bei am/ab, TS kennt das nicht.
const datum = new Date(zeitpunkt!);
const formatiertesDatum = datum.toLocaleDateString("de-DE", {
day: "2-digit",
month: "2-digit",
year: "numeric",
timeZone: "Europe/Berlin",
});
const formatierteUhrzeit = datum.toLocaleTimeString("de-DE", {
hour: "2-digit",
minute: "2-digit",
timeZone: "Europe/Berlin",
});
const praefix = typ === "am" ? "Am" : "Ab";
return `${praefix} ${formatiertesDatum}, ${formatierteUhrzeit} Uhr`;
}
export function berechneVorbefuelltenZeitpunkt(jetzt: Date = new Date()): string {
const morgen = new Date(jetzt);
morgen.setDate(morgen.getDate() + 1);
morgen.setHours(15, 0, 0, 0);
const jahr = morgen.getFullYear();
const monat = String(morgen.getMonth() + 1).padStart(2, "0");
const tag = String(morgen.getDate()).padStart(2, "0");
return `${jahr}-${monat}-${tag}T15:00`;
}