forked from fahrPool/fahrPool
190 lines
6.9 KiB
TypeScript
190 lines
6.9 KiB
TypeScript
"use server";
|
|
|
|
import { redirect } from "next/navigation";
|
|
import { createClient } from "@/lib/supabase/server";
|
|
import { validateOrt } from "@/lib/orte-fahrzeuge/validate-ort";
|
|
import { validateFahrzeug } from "@/lib/orte-fahrzeuge/validate-fahrzeug";
|
|
import { insertOrt, insertFahrzeug } from "@/lib/orte-fahrzeuge/mutations";
|
|
import { loadProfile } from "@/lib/auth/profile";
|
|
import { ZEITPLANUNG_TYPEN, type ZeitplanungTyp } from "@/lib/auftraege/zeitplanung";
|
|
|
|
export async function createAuftrag(formData: FormData): Promise<void> {
|
|
const abholortId = formData.get("abholort_id");
|
|
const zielortId = formData.get("zielort_id");
|
|
const fahrzeugId = formData.get("fahrzeug_id");
|
|
const vorgaengerAuftragId = formData.get("vorgaenger_auftrag_id");
|
|
const auftragsnummerRaw = formData.get("auftragsnummer");
|
|
const auftragsnummer =
|
|
typeof auftragsnummerRaw === "string" && auftragsnummerRaw.trim() ? auftragsnummerRaw.trim() : null;
|
|
|
|
const zeitplanungTypRaw = formData.get("zeitplanung_typ");
|
|
const zeitplanungTyp = (ZEITPLANUNG_TYPEN as readonly string[]).includes(zeitplanungTypRaw as string)
|
|
? (zeitplanungTypRaw as ZeitplanungTyp)
|
|
: "ab_sofort";
|
|
|
|
let zeitplanungZeitpunkt: string | null = null;
|
|
if (zeitplanungTyp === "am" || zeitplanungTyp === "ab") {
|
|
const zeitpunktRaw = formData.get("zeitplanung_zeitpunkt");
|
|
const geparst = typeof zeitpunktRaw === "string" && zeitpunktRaw ? new Date(zeitpunktRaw) : null;
|
|
if (!geparst || Number.isNaN(geparst.getTime())) {
|
|
redirect(`/auftraege/neu?error=${encodeURIComponent("Bitte Datum und Uhrzeit fuer die Zeitplanung angeben.")}`);
|
|
}
|
|
zeitplanungZeitpunkt = geparst.toISOString();
|
|
}
|
|
|
|
if (typeof abholortId !== "string" || !abholortId) {
|
|
redirect(`/auftraege/neu?error=${encodeURIComponent("Bitte einen Abholort auswaehlen.")}`);
|
|
}
|
|
if (typeof zielortId !== "string" || !zielortId) {
|
|
redirect(`/auftraege/neu?error=${encodeURIComponent("Bitte einen Zielort auswaehlen.")}`);
|
|
}
|
|
if (typeof fahrzeugId !== "string" || !fahrzeugId) {
|
|
redirect(`/auftraege/neu?error=${encodeURIComponent("Bitte ein Fahrzeug auswaehlen.")}`);
|
|
}
|
|
|
|
const supabase = await createClient();
|
|
const {
|
|
data: { user },
|
|
} = await supabase.auth.getUser();
|
|
|
|
if (!user) {
|
|
redirect("/login");
|
|
}
|
|
|
|
const { error } = await supabase.from("auftraege").insert({
|
|
abholort_id: abholortId,
|
|
zielort_id: zielortId,
|
|
fahrzeug_id: fahrzeugId,
|
|
erstellt_von: user.id,
|
|
auftragsnummer,
|
|
zeitplanung_typ: zeitplanungTyp,
|
|
zeitplanung_zeitpunkt: zeitplanungZeitpunkt,
|
|
vorgaenger_auftrag_id:
|
|
typeof vorgaengerAuftragId === "string" && vorgaengerAuftragId ? vorgaengerAuftragId : null,
|
|
});
|
|
|
|
if (error) {
|
|
if (error.code === "23505") {
|
|
redirect(`/auftraege/neu?error=${encodeURIComponent("Diese Auftragsnummer ist bereits vergeben.")}`);
|
|
}
|
|
redirect(`/auftraege/neu?error=${encodeURIComponent("Auftrag konnte nicht angelegt werden.")}`);
|
|
}
|
|
|
|
redirect("/auftraege");
|
|
}
|
|
|
|
export async function createOrtUndAuswaehlen(ziel: "abholort" | "zielort", formData: FormData): Promise<void> {
|
|
const profileResult = await loadProfile();
|
|
if (
|
|
profileResult.status !== "ok" ||
|
|
(profileResult.profile.role !== "admin" && profileResult.profile.role !== "disponent")
|
|
) {
|
|
redirect(`/auftraege?error=${encodeURIComponent("Nur Disponent/Admin duerfen Auftraege anlegen.")}`);
|
|
}
|
|
|
|
const vorgaengerAuftragId = formData.get("vorgaenger_auftrag_id");
|
|
const abholortId = formData.get("abholort_id");
|
|
const zielortId = formData.get("zielort_id");
|
|
const fahrzeugId = formData.get("fahrzeug_id");
|
|
|
|
const ruecksprungParams = new URLSearchParams();
|
|
if (typeof vorgaengerAuftragId === "string" && vorgaengerAuftragId) {
|
|
ruecksprungParams.set("vorgaenger_auftrag_id", vorgaengerAuftragId);
|
|
}
|
|
if (typeof abholortId === "string" && abholortId) {
|
|
ruecksprungParams.set("abholort_id", abholortId);
|
|
}
|
|
if (typeof zielortId === "string" && zielortId) {
|
|
ruecksprungParams.set("zielort_id", zielortId);
|
|
}
|
|
if (typeof fahrzeugId === "string" && fahrzeugId) {
|
|
ruecksprungParams.set("fahrzeug_id", fahrzeugId);
|
|
}
|
|
|
|
const validation = validateOrt({
|
|
adresse: formData.get("adresse"),
|
|
ansprechpartner: formData.get("ansprechpartner"),
|
|
telefon: formData.get("telefon"),
|
|
email: formData.get("email"),
|
|
});
|
|
if (!validation.valid) {
|
|
ruecksprungParams.set("zeige_ort_formular", ziel);
|
|
ruecksprungParams.set("error", validation.error);
|
|
redirect(`/auftraege/neu?${ruecksprungParams.toString()}`);
|
|
}
|
|
|
|
const supabase = await createClient();
|
|
const {
|
|
data: { user },
|
|
} = await supabase.auth.getUser();
|
|
if (!user) {
|
|
redirect("/login");
|
|
}
|
|
|
|
const result = await insertOrt({
|
|
adresse: validation.adresse,
|
|
ansprechpartner: validation.ansprechpartner,
|
|
telefon: validation.telefon,
|
|
email: validation.email,
|
|
createdBy: user.id,
|
|
});
|
|
if (!result.success) {
|
|
ruecksprungParams.set("zeige_ort_formular", ziel);
|
|
ruecksprungParams.set("error", result.error);
|
|
redirect(`/auftraege/neu?${ruecksprungParams.toString()}`);
|
|
}
|
|
|
|
ruecksprungParams.set(ziel === "abholort" ? "abholort_id" : "zielort_id", result.id);
|
|
redirect(`/auftraege/neu?${ruecksprungParams.toString()}`);
|
|
}
|
|
|
|
export async function createFahrzeugUndAuswaehlen(formData: FormData): Promise<void> {
|
|
const profileResult = await loadProfile();
|
|
if (
|
|
profileResult.status !== "ok" ||
|
|
(profileResult.profile.role !== "admin" && profileResult.profile.role !== "disponent")
|
|
) {
|
|
redirect(`/auftraege?error=${encodeURIComponent("Nur Disponent/Admin duerfen Auftraege anlegen.")}`);
|
|
}
|
|
|
|
const vorgaengerAuftragId = formData.get("vorgaenger_auftrag_id");
|
|
const abholortId = formData.get("abholort_id");
|
|
const zielortId = formData.get("zielort_id");
|
|
|
|
const ruecksprungParams = new URLSearchParams();
|
|
if (typeof vorgaengerAuftragId === "string" && vorgaengerAuftragId) {
|
|
ruecksprungParams.set("vorgaenger_auftrag_id", vorgaengerAuftragId);
|
|
}
|
|
if (typeof abholortId === "string" && abholortId) {
|
|
ruecksprungParams.set("abholort_id", abholortId);
|
|
}
|
|
if (typeof zielortId === "string" && zielortId) {
|
|
ruecksprungParams.set("zielort_id", zielortId);
|
|
}
|
|
|
|
const validation = validateFahrzeug({
|
|
kennzeichen: formData.get("kennzeichen"),
|
|
marke: formData.get("marke"),
|
|
modell: formData.get("modell"),
|
|
});
|
|
if (!validation.valid) {
|
|
ruecksprungParams.set("zeige_fahrzeug_formular", "1");
|
|
ruecksprungParams.set("error", validation.error);
|
|
redirect(`/auftraege/neu?${ruecksprungParams.toString()}`);
|
|
}
|
|
|
|
const result = await insertFahrzeug({
|
|
kennzeichen: validation.kennzeichen,
|
|
marke: validation.marke,
|
|
modell: validation.modell,
|
|
});
|
|
if (!result.success) {
|
|
ruecksprungParams.set("zeige_fahrzeug_formular", "1");
|
|
ruecksprungParams.set("error", result.error);
|
|
redirect(`/auftraege/neu?${ruecksprungParams.toString()}`);
|
|
}
|
|
|
|
ruecksprungParams.set("fahrzeug_id", result.id);
|
|
redirect(`/auftraege/neu?${ruecksprungParams.toString()}`);
|
|
}
|