forked from fahrPool/fahrPool
115 lines
5.1 KiB
TypeScript
115 lines
5.1 KiB
TypeScript
"use server";
|
|
|
|
import { revalidatePath } from "next/cache";
|
|
import { createClient } from "@/lib/supabase/server";
|
|
import { ladeAdminKontext } from "@/lib/firma/admin-kontext";
|
|
import { validateFirmaEinstellungen } from "@/lib/firma/einstellungen";
|
|
import { logoPfad, validateLogo } from "@/lib/firma/logo";
|
|
import { appIconPfad, validateAppIcon } from "@/lib/pwa/app-icon";
|
|
|
|
export type ActionResult = { success: true } | { success: false; error: string };
|
|
|
|
export async function speichereFirmaEinstellungen(formData: FormData): Promise<ActionResult> {
|
|
const kontext = await ladeAdminKontext();
|
|
if (!kontext.ok) return { success: false, error: kontext.error };
|
|
|
|
const validation = validateFirmaEinstellungen({
|
|
name: formData.get("name"),
|
|
whatsapp: formData.get("whatsapp_empfaenger"),
|
|
standardFarben: formData.get("standard_farben") === "on",
|
|
farbePrimaer: formData.get("farbe_primaer"),
|
|
farbeDunkel: formData.get("farbe_dunkel"),
|
|
});
|
|
if (!validation.valid) return { success: false, error: validation.error };
|
|
|
|
const supabase = await createClient();
|
|
const { data, error } = await supabase.from("firmen").update(validation.werte).eq("id", kontext.firma.id).select("id");
|
|
if (error || !data || data.length === 0) {
|
|
return { success: false, error: "Speichern fehlgeschlagen." };
|
|
}
|
|
revalidatePath("/", "layout");
|
|
return { success: true };
|
|
}
|
|
|
|
export async function ladeLogoHoch(formData: FormData): Promise<ActionResult> {
|
|
const kontext = await ladeAdminKontext();
|
|
if (!kontext.ok) return { success: false, error: kontext.error };
|
|
|
|
const datei = formData.get("logo");
|
|
if (!(datei instanceof File)) return { success: false, error: "Bitte eine Datei auswählen." };
|
|
const validation = validateLogo({ type: datei.type, size: datei.size });
|
|
if (!validation.valid) return { success: false, error: validation.error };
|
|
|
|
const supabase = await createClient();
|
|
const pfad = logoPfad(kontext.firma.id, validation.endung);
|
|
const { error: uploadFehler } = await supabase.storage
|
|
.from("firmen-logos")
|
|
.upload(pfad, datei, { contentType: datei.type });
|
|
if (uploadFehler) return { success: false, error: "Logo-Upload fehlgeschlagen." };
|
|
|
|
const { error } = await supabase.from("firmen").update({ logo_pfad: pfad }).eq("id", kontext.firma.id);
|
|
if (error) {
|
|
await supabase.storage.from("firmen-logos").remove([pfad]);
|
|
return { success: false, error: "Logo konnte nicht gespeichert werden." };
|
|
}
|
|
if (kontext.firma.logo_pfad) {
|
|
await supabase.storage.from("firmen-logos").remove([kontext.firma.logo_pfad]);
|
|
}
|
|
revalidatePath("/", "layout");
|
|
return { success: true };
|
|
}
|
|
|
|
export async function entferneLogo(): Promise<ActionResult> {
|
|
const kontext = await ladeAdminKontext();
|
|
if (!kontext.ok) return { success: false, error: kontext.error };
|
|
if (!kontext.firma.logo_pfad) return { success: true };
|
|
|
|
const supabase = await createClient();
|
|
const { error } = await supabase.from("firmen").update({ logo_pfad: null }).eq("id", kontext.firma.id);
|
|
if (error) return { success: false, error: "Logo konnte nicht entfernt werden." };
|
|
await supabase.storage.from("firmen-logos").remove([kontext.firma.logo_pfad]);
|
|
revalidatePath("/", "layout");
|
|
return { success: true };
|
|
}
|
|
|
|
export async function ladeAppIconHoch(formData: FormData): Promise<ActionResult> {
|
|
const kontext = await ladeAdminKontext();
|
|
if (!kontext.ok) return { success: false, error: kontext.error };
|
|
|
|
const datei = formData.get("app_icon");
|
|
if (!(datei instanceof File)) return { success: false, error: "Bitte eine Datei auswählen." };
|
|
const validation = validateAppIcon({ type: datei.type, size: datei.size });
|
|
if (!validation.valid) return { success: false, error: validation.error };
|
|
|
|
const supabase = await createClient();
|
|
const pfad = appIconPfad(kontext.firma.id, validation.endung);
|
|
const { error: uploadFehler } = await supabase.storage
|
|
.from("firmen-logos")
|
|
.upload(pfad, datei, { contentType: datei.type });
|
|
if (uploadFehler) return { success: false, error: "App-Icon-Upload fehlgeschlagen." };
|
|
|
|
const { error } = await supabase.from("firmen").update({ app_icon_pfad: pfad }).eq("id", kontext.firma.id);
|
|
if (error) {
|
|
await supabase.storage.from("firmen-logos").remove([pfad]);
|
|
return { success: false, error: "App-Icon konnte nicht gespeichert werden." };
|
|
}
|
|
if (kontext.firma.app_icon_pfad) {
|
|
await supabase.storage.from("firmen-logos").remove([kontext.firma.app_icon_pfad]);
|
|
}
|
|
revalidatePath("/", "layout");
|
|
return { success: true };
|
|
}
|
|
|
|
export async function entferneAppIcon(): Promise<ActionResult> {
|
|
const kontext = await ladeAdminKontext();
|
|
if (!kontext.ok) return { success: false, error: kontext.error };
|
|
if (!kontext.firma.app_icon_pfad) return { success: true };
|
|
|
|
const supabase = await createClient();
|
|
const { error } = await supabase.from("firmen").update({ app_icon_pfad: null }).eq("id", kontext.firma.id);
|
|
if (error) return { success: false, error: "App-Icon konnte nicht entfernt werden." };
|
|
await supabase.storage.from("firmen-logos").remove([kontext.firma.app_icon_pfad]);
|
|
revalidatePath("/", "layout");
|
|
return { success: true };
|
|
}
|