forked from fahrPool/fahrPool
feat: add Standorte server actions (return-based, no redirect)
This commit is contained in:
parent
ca942958c0
commit
587e36f81c
|
|
@ -0,0 +1,69 @@
|
||||||
|
"use server";
|
||||||
|
|
||||||
|
import { createClient } from "@/lib/supabase/server";
|
||||||
|
import { loadProfile } from "@/lib/auth/profile";
|
||||||
|
import { kannStammdatenPflegen } from "@/lib/auth/roles";
|
||||||
|
import { validateOrt } from "@/lib/orte-fahrzeuge/validate-ort";
|
||||||
|
import { insertOrt, updateOrtZeile } from "@/lib/orte-fahrzeuge/mutations";
|
||||||
|
|
||||||
|
export type MutationResult = { success: true } | { success: false; error: string };
|
||||||
|
|
||||||
|
export async function createOrt(formData: FormData): Promise<MutationResult> {
|
||||||
|
const profileResult = await loadProfile();
|
||||||
|
if (profileResult.status !== "ok" || !kannStammdatenPflegen(profileResult.profile.role)) {
|
||||||
|
return { success: false, error: "Nur Disponent/Admin duerfen Stammdaten pflegen." };
|
||||||
|
}
|
||||||
|
|
||||||
|
const validation = validateOrt({
|
||||||
|
adresse: formData.get("adresse"),
|
||||||
|
ansprechpartner: formData.get("ansprechpartner"),
|
||||||
|
});
|
||||||
|
if (!validation.valid) {
|
||||||
|
return { success: false, error: validation.error };
|
||||||
|
}
|
||||||
|
|
||||||
|
const supabase = await createClient();
|
||||||
|
const {
|
||||||
|
data: { user },
|
||||||
|
} = await supabase.auth.getUser();
|
||||||
|
if (!user) {
|
||||||
|
return { success: false, error: "Nicht angemeldet." };
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = await insertOrt({
|
||||||
|
adresse: validation.adresse,
|
||||||
|
ansprechpartner: validation.ansprechpartner,
|
||||||
|
createdBy: user.id,
|
||||||
|
});
|
||||||
|
if (!result.success) {
|
||||||
|
return { success: false, error: result.error };
|
||||||
|
}
|
||||||
|
|
||||||
|
return { success: true };
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function updateOrt(ortId: string, formData: FormData): Promise<MutationResult> {
|
||||||
|
const profileResult = await loadProfile();
|
||||||
|
if (profileResult.status !== "ok" || !kannStammdatenPflegen(profileResult.profile.role)) {
|
||||||
|
return { success: false, error: "Nur Disponent/Admin duerfen Stammdaten pflegen." };
|
||||||
|
}
|
||||||
|
|
||||||
|
const validation = validateOrt({
|
||||||
|
adresse: formData.get("adresse"),
|
||||||
|
ansprechpartner: formData.get("ansprechpartner"),
|
||||||
|
});
|
||||||
|
if (!validation.valid) {
|
||||||
|
return { success: false, error: validation.error };
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = await updateOrtZeile({
|
||||||
|
id: ortId,
|
||||||
|
adresse: validation.adresse,
|
||||||
|
ansprechpartner: validation.ansprechpartner,
|
||||||
|
});
|
||||||
|
if (!result.success) {
|
||||||
|
return { success: false, error: result.error };
|
||||||
|
}
|
||||||
|
|
||||||
|
return { success: true };
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue