forked from fahrPool/fahrPool
46 lines
1.4 KiB
TypeScript
46 lines
1.4 KiB
TypeScript
"use server";
|
|
|
|
import { redirect } from "next/navigation";
|
|
import { createClient } from "@/lib/supabase/server";
|
|
|
|
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");
|
|
}
|