diff --git a/web/app/firma/actions.ts b/web/app/firma/actions.ts new file mode 100644 index 0000000..09cfba9 --- /dev/null +++ b/web/app/firma/actions.ts @@ -0,0 +1,72 @@ +"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"; + +export type ActionResult = { success: true } | { success: false; error: string }; + +export async function speichereFirmaEinstellungen(formData: FormData): Promise { + 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 { + 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 { + 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 }; +} diff --git a/web/app/firma/page.tsx b/web/app/firma/page.tsx new file mode 100644 index 0000000..0049784 --- /dev/null +++ b/web/app/firma/page.tsx @@ -0,0 +1,26 @@ +import { redirect } from "next/navigation"; +import { ladeAdminKontext } from "@/lib/firma/admin-kontext"; +import { FirmaEinstellungenForm } from "@/components/firma/FirmaEinstellungenForm"; +import { LogoForm } from "@/components/firma/LogoForm"; +import { Card } from "@/components/ui/Card"; + +export default async function FirmaPage() { + const kontext = await ladeAdminKontext(); + if (!kontext.ok) { + redirect("/dashboard"); + } + + return ( +
+

Firma

+ +

Allgemein

+ +
+ +

Logo

+ +
+
+ ); +} diff --git a/web/components/firma/FirmaEinstellungenForm.tsx b/web/components/firma/FirmaEinstellungenForm.tsx new file mode 100644 index 0000000..716839c --- /dev/null +++ b/web/components/firma/FirmaEinstellungenForm.tsx @@ -0,0 +1,93 @@ +"use client"; + +import { useState, useTransition } from "react"; +import { speichereFirmaEinstellungen } from "@/app/firma/actions"; +import { Alert } from "@/components/ui/Alert"; +import { Button } from "@/components/ui/Button"; +import { FIELD_CLASSES, FormField } from "@/components/ui/FormField"; +import { STANDARD_FARBE_DUNKEL, STANDARD_FARBE_PRIMAER } from "@/lib/firma/farben"; +import type { Firma } from "@/lib/firma/queries"; + +export function FirmaEinstellungenForm({ firma }: { firma: Firma }) { + const [fehler, setFehler] = useState(null); + const [gespeichert, setGespeichert] = useState(false); + const [standard, setStandard] = useState(!firma.farbe_primaer && !firma.farbe_dunkel); + const [isPending, startTransition] = useTransition(); + + function handleSubmit(event: React.FormEvent) { + event.preventDefault(); + const formData = new FormData(event.currentTarget); + startTransition(async () => { + const ergebnis = await speichereFirmaEinstellungen(formData); + setGespeichert(ergebnis.success); + setFehler(ergebnis.success ? null : ergebnis.error); + }); + } + + return ( +
+ {fehler && ( +
+ {fehler} +
+ )} + + + + + + + + {!standard && ( +
+ + + + + + +
+ )} +

+ Beide Farben brauchen genug Kontrast zu weißer Schrift (mindestens 4,5:1). +

+ {gespeichert && ( +

+ Gespeichert. +

+ )} + +
+ ); +} diff --git a/web/components/firma/LogoForm.tsx b/web/components/firma/LogoForm.tsx new file mode 100644 index 0000000..edb7a95 --- /dev/null +++ b/web/components/firma/LogoForm.tsx @@ -0,0 +1,64 @@ +"use client"; + +import { useState, useTransition } from "react"; +import { entferneLogo, ladeLogoHoch } from "@/app/firma/actions"; +import { Alert } from "@/components/ui/Alert"; +import { Button } from "@/components/ui/Button"; +import { FIELD_CLASSES, FormField } from "@/components/ui/FormField"; + +export function LogoForm({ hatLogo }: { hatLogo: boolean }) { + const [fehler, setFehler] = useState(null); + const [isPending, startTransition] = useTransition(); + + function handleSubmit(event: React.FormEvent) { + event.preventDefault(); + const form = event.currentTarget; + const formData = new FormData(form); + startTransition(async () => { + const ergebnis = await ladeLogoHoch(formData); + setFehler(ergebnis.success ? null : ergebnis.error); + if (ergebnis.success) form.reset(); + }); + } + + function handleEntfernen() { + startTransition(async () => { + const ergebnis = await entferneLogo(); + setFehler(ergebnis.success ? null : ergebnis.error); + }); + } + + return ( +
+ {fehler && ( +
+ {fehler} +
+ )} + {hatLogo && ( + // eslint-disable-next-line @next/next/no-img-element -- eigene Logo-Route mit Session-Cookie + Aktuelles Logo + )} + + + +
+ + {hatLogo && ( + + )} +
+
+ ); +} diff --git a/web/lib/firma/admin-kontext.ts b/web/lib/firma/admin-kontext.ts new file mode 100644 index 0000000..8563bec --- /dev/null +++ b/web/lib/firma/admin-kontext.ts @@ -0,0 +1,20 @@ +import { createClient } from "@/lib/supabase/server"; +import { loadProfile } from "@/lib/auth/profile"; +import { ladeFirma, type Firma } from "./queries"; + +export type AdminKontext = { ok: true; firma: Firma; profilId: string } | { ok: false; error: string }; + +// Rolle aus dem eigenen Profil, Firma ueber den RLS-Client (aktiv + eigene). +// Alle Firmen-Actions nehmen die firma_id ausschliesslich von hier, nie aus dem Formular. +export async function ladeAdminKontext(): Promise { + const profil = await loadProfile(); + if (profil.status !== "ok" || profil.profile.role !== "admin" || !profil.profile.firma_id) { + return { ok: false, error: "Nur Admins einer Firma dürfen das." }; + } + const supabase = await createClient(); + const firma = await ladeFirma(supabase, profil.profile.firma_id); + if (!firma) { + return { ok: false, error: "Firma nicht gefunden oder deaktiviert." }; + } + return { ok: true, firma, profilId: profil.profile.id }; +} diff --git a/web/lib/firma/einstellungen.test.ts b/web/lib/firma/einstellungen.test.ts new file mode 100644 index 0000000..f8c6482 --- /dev/null +++ b/web/lib/firma/einstellungen.test.ts @@ -0,0 +1,48 @@ +import { describe, expect, it } from "vitest"; +import { validateFirmaEinstellungen } from "./einstellungen"; + +const BASIS = { name: "Firma B", whatsapp: "", standardFarben: false, farbePrimaer: "#0071ed", farbeDunkel: "#0a185c" }; + +describe("validateFirmaEinstellungen", () => { + it("uebernimmt gueltige Werte", () => { + expect(validateFirmaEinstellungen({ ...BASIS, whatsapp: "0151 2345678" })).toEqual({ + valid: true, + werte: { name: "Firma B", whatsapp_empfaenger: "+491512345678", farbe_primaer: "#0071ed", farbe_dunkel: "#0a185c" }, + }); + }); + + it("Standardfarben setzen beide Farben auf null", () => { + const e = validateFirmaEinstellungen({ ...BASIS, standardFarben: true, farbePrimaer: "#ffff00" }); + expect(e.valid && e.werte.farbe_primaer).toBeNull(); + expect(e.valid && e.werte.farbe_dunkel).toBeNull(); + }); + + it("leerer WhatsApp-Empfaenger wird null", () => { + const e = validateFirmaEinstellungen(BASIS); + expect(e.valid && e.werte.whatsapp_empfaenger).toBeNull(); + }); + + it("lehnt leeren Namen ab", () => { + expect(validateFirmaEinstellungen({ ...BASIS, name: " " })).toEqual({ valid: false, error: "Bitte einen Firmennamen eingeben." }); + }); + + it("lehnt ungueltige WhatsApp-Nummer ab", () => { + expect(validateFirmaEinstellungen({ ...BASIS, whatsapp: "abc" })).toEqual({ + valid: false, + error: "Bitte eine gültige WhatsApp-Nummer eingeben (z. B. +49 151 2345678).", + }); + }); + + it("lehnt ungueltiges Farbformat ab", () => { + expect(validateFirmaEinstellungen({ ...BASIS, farbePrimaer: "blau" })).toEqual({ + valid: false, + error: "Bitte für die Hauptfarbe eine Farbe im Format #rrggbb wählen.", + }); + }); + + it("lehnt zu wenig Kontrast mit dem erreichten Wert ab", () => { + const e = validateFirmaEinstellungen({ ...BASIS, farbeDunkel: "#ffff00" }); + expect(e.valid).toBe(false); + expect(!e.valid && e.error).toMatch(/^Die dunkle Farbe hat zu wenig Kontrast zu Weiß \(1,\d\d:1, mindestens 4,5:1 nötig\)\.$/); + }); +}); diff --git a/web/lib/firma/einstellungen.ts b/web/lib/firma/einstellungen.ts new file mode 100644 index 0000000..1b067ff --- /dev/null +++ b/web/lib/firma/einstellungen.ts @@ -0,0 +1,65 @@ +import { normalisiereTelefon } from "@/lib/nutzerverwaltung/details"; +import { MIN_KONTRAST, kontrastZuWeiss, normalisiereFarbe } from "./farben"; + +export type FirmaEinstellungen = { + name: string; + whatsapp_empfaenger: string | null; + farbe_primaer: string | null; + farbe_dunkel: string | null; +}; + +type Eingabe = { + name: unknown; + whatsapp: unknown; + standardFarben: boolean; + farbePrimaer: unknown; + farbeDunkel: unknown; +}; + +function text(wert: unknown): string { + return typeof wert === "string" ? wert.trim() : ""; +} + +function pruefeFarbe(roh: unknown, bezeichnung: string): { ok: true; farbe: string } | { ok: false; error: string } { + const farbe = normalisiereFarbe(text(roh)); + if (!farbe) { + return { ok: false, error: `Bitte für die ${bezeichnung} eine Farbe im Format #rrggbb wählen.` }; + } + const kontrast = kontrastZuWeiss(farbe); + if (kontrast < MIN_KONTRAST) { + const wert = kontrast.toFixed(2).replace(".", ","); + return { ok: false, error: `Die ${bezeichnung} hat zu wenig Kontrast zu Weiß (${wert}:1, mindestens 4,5:1 nötig).` }; + } + return { ok: true, farbe }; +} + +export function validateFirmaEinstellungen( + input: Eingabe +): { valid: true; werte: FirmaEinstellungen } | { valid: false; error: string } { + const name = text(input.name); + if (!name) return { valid: false, error: "Bitte einen Firmennamen eingeben." }; + if (name.length > 100) return { valid: false, error: "Der Firmenname darf höchstens 100 Zeichen lang sein." }; + + const whatsappRoh = text(input.whatsapp); + let whatsapp: string | null = null; + if (whatsappRoh) { + whatsapp = normalisiereTelefon(whatsappRoh); + if (!whatsapp) { + return { valid: false, error: "Bitte eine gültige WhatsApp-Nummer eingeben (z. B. +49 151 2345678)." }; + } + } + + if (input.standardFarben) { + return { valid: true, werte: { name, whatsapp_empfaenger: whatsapp, farbe_primaer: null, farbe_dunkel: null } }; + } + + const primaer = pruefeFarbe(input.farbePrimaer, "Hauptfarbe"); + if (!primaer.ok) return { valid: false, error: primaer.error }; + const dunkel = pruefeFarbe(input.farbeDunkel, "dunkle Farbe"); + if (!dunkel.ok) return { valid: false, error: dunkel.error }; + + return { + valid: true, + werte: { name, whatsapp_empfaenger: whatsapp, farbe_primaer: primaer.farbe, farbe_dunkel: dunkel.farbe }, + }; +} diff --git a/web/next.config.ts b/web/next.config.ts index 12b611e..9a4525b 100644 --- a/web/next.config.ts +++ b/web/next.config.ts @@ -3,6 +3,12 @@ import type { NextConfig } from "next"; const nextConfig: NextConfig = { output: "standalone", serverExternalPackages: ["@react-pdf/renderer", "sharp"], + // Logo-Upload: Datei bis 1 MB plus Multipart-Overhead (Default-Limit 1 MB). + experimental: { + serverActions: { + bodySizeLimit: "2mb", + }, + }, }; export default nextConfig;