forked from fahrPool/fahrPool
165 lines
5.7 KiB
TypeScript
165 lines
5.7 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";
|
|
|
|
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");
|
|
|
|
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,
|
|
vorgaenger_auftrag_id:
|
|
typeof vorgaengerAuftragId === "string" && vorgaengerAuftragId ? vorgaengerAuftragId : null,
|
|
});
|
|
|
|
if (error) {
|
|
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()}`);
|
|
}
|