forked from fahrPool/fahrPool
101 lines
3.4 KiB
TypeScript
101 lines
3.4 KiB
TypeScript
"use server";
|
|
|
|
import { redirect } from "next/navigation";
|
|
import { createClient } from "@/lib/supabase/server";
|
|
import { validateOrt } from "@/lib/orte-fahrzeuge/validate-ort";
|
|
import { insertOrt } from "@/lib/orte-fahrzeuge/mutations";
|
|
|
|
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 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"),
|
|
});
|
|
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,
|
|
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()}`);
|
|
}
|