1
0
Fork 0
fahrPool/web/app/zeiterfassung/actions.ts

127 lines
4.4 KiB
TypeScript

"use server";
import { createClient } from "@/lib/supabase/server";
import { loadProfile } from "@/lib/auth/profile";
import { istEinsatzrolle } from "@/lib/auth/roles";
import { validateSchichtKorrektur } from "@/lib/zeiterfassung/validate-korrektur";
import { findeOffeneSchicht, listSchichten } from "@/lib/zeiterfassung/queries";
import { pauseMinuten, nettoMinuten } from "@/lib/zeiterfassung/zeit-berechnung";
import { schichtenZuCsv } from "@/lib/zeiterfassung/csv";
export type MutationResult = { success: true } | { success: false; error: string };
export async function kommen(): Promise<MutationResult> {
const profileResult = await loadProfile();
if (profileResult.status !== "ok" || !istEinsatzrolle(profileResult.profile.role)) {
return { success: false, error: "Diese Rolle kann sich nicht einstempeln." };
}
const offeneSchicht = await findeOffeneSchicht(profileResult.profile.id);
if (offeneSchicht) {
return { success: false, error: "Du bist bereits eingestempelt." };
}
const supabase = await createClient();
const { error } = await supabase
.from("arbeitszeiten")
.insert({ fahrer_id: profileResult.profile.id, kommt_um: new Date().toISOString() });
if (error) {
return { success: false, error: "Einstempeln fehlgeschlagen." };
}
return { success: true };
}
export async function gehen(schichtId: string): Promise<MutationResult> {
const profileResult = await loadProfile();
if (profileResult.status !== "ok" || !istEinsatzrolle(profileResult.profile.role)) {
return { success: false, error: "Diese Rolle kann sich nicht ausstempeln." };
}
const supabase = await createClient();
const { data, error } = await supabase
.from("arbeitszeiten")
.update({ geht_um: new Date().toISOString() })
.eq("id", schichtId)
.eq("fahrer_id", profileResult.profile.id)
.is("geht_um", null)
.select("id");
if (error || !data || data.length === 0) {
return { success: false, error: "Ausstempeln fehlgeschlagen (keine offene eigene Schicht gefunden)." };
}
return { success: true };
}
export async function schichtKorrigieren(schichtId: string, formData: FormData): Promise<MutationResult> {
const profileResult = await loadProfile();
if (
profileResult.status !== "ok" ||
(profileResult.profile.role !== "admin" && profileResult.profile.role !== "disponent")
) {
return { success: false, error: "Nur Disponent/Admin duerfen Arbeitszeiten korrigieren." };
}
const supabase = await createClient();
const { data: schicht } = await supabase.from("arbeitszeiten").select("kommt_um").eq("id", schichtId).single();
if (!schicht) {
return { success: false, error: "Schicht nicht gefunden." };
}
const validation = validateSchichtKorrektur({ gehtUm: formData.get("geht_um"), kommtUm: schicht.kommt_um });
if (!validation.valid) {
return { success: false, error: validation.error };
}
const { data, error } = await supabase
.from("arbeitszeiten")
.update({
geht_um: validation.gehtUm,
korrigiert_von: profileResult.profile.id,
korrigiert_am: new Date().toISOString(),
})
.eq("id", schichtId)
.select("id");
if (error || !data || data.length === 0) {
return { success: false, error: "Korrektur fehlgeschlagen." };
}
return { success: true };
}
export async function exportiereCsv(
zeitraum: { von: string; bis: string },
fahrerId?: string
): Promise<{ success: true; csv: string } | { success: false; error: string }> {
const profileResult = await loadProfile();
if (
profileResult.status !== "ok" ||
(profileResult.profile.role !== "admin" && profileResult.profile.role !== "disponent")
) {
return { success: false, error: "Nur Disponent/Admin duerfen exportieren." };
}
const schichten = await listSchichten(zeitraum, fahrerId);
const zeilen = schichten
.filter((schicht): schicht is typeof schicht & { geht_um: string } => schicht.geht_um !== null)
.map((schicht) => {
const bruttoMinuten = Math.round(
(new Date(schicht.geht_um).getTime() - new Date(schicht.kommt_um).getTime()) / 60000
);
return {
fahrerName: schicht.fahrer.name,
datum: schicht.kommt_um.slice(0, 10),
kommtUm: schicht.kommt_um,
gehtUm: schicht.geht_um,
bruttoMinuten,
pauseMinuten: pauseMinuten(bruttoMinuten),
nettoMinuten: nettoMinuten(schicht.kommt_um, schicht.geht_um),
};
});
return { success: true, csv: schichtenZuCsv(zeilen) };
}