forked from fahrPool/fahrPool
35 lines
1.2 KiB
TypeScript
35 lines
1.2 KiB
TypeScript
"use server";
|
|
|
|
import { revalidatePath } from "next/cache";
|
|
import { createClient } from "@/lib/supabase/server";
|
|
import { loadProfile } from "@/lib/auth/profile";
|
|
import { validateNutzerDetails, leseDetailsAusFormData } from "@/lib/nutzerverwaltung/details";
|
|
import { speichereDetails } from "@/lib/nutzerverwaltung/details-queries";
|
|
|
|
export type ActionResult = { success: true } | { success: false; error: string };
|
|
|
|
export async function speichereMeineDaten(formData: FormData): Promise<ActionResult> {
|
|
const profileResult = await loadProfile();
|
|
if (profileResult.status !== "ok") {
|
|
return { success: false, error: "Bitte neu anmelden." };
|
|
}
|
|
|
|
if (profileResult.profile.role === "superadmin") {
|
|
return { success: false, error: "Für den Plattform-Zugang gibt es keine Kontaktdaten." };
|
|
}
|
|
|
|
const validation = validateNutzerDetails(leseDetailsAusFormData(formData));
|
|
if (!validation.valid) {
|
|
return { success: false, error: validation.error };
|
|
}
|
|
|
|
const supabase = await createClient();
|
|
const gespeichert = await speichereDetails(supabase, profileResult.profile.id, validation.details);
|
|
if (!gespeichert) {
|
|
return { success: false, error: "Speichern fehlgeschlagen." };
|
|
}
|
|
|
|
revalidatePath("/profil");
|
|
return { success: true };
|
|
}
|