diff --git a/src/modules/settings/actions/business-settings-actions.ts b/src/modules/settings/actions/business-settings-actions.ts new file mode 100644 index 0000000..77a66d3 --- /dev/null +++ b/src/modules/settings/actions/business-settings-actions.ts @@ -0,0 +1,55 @@ +"use server"; + +import { prisma } from "@/src/lib/prisma"; +import { requireManager } from "@/src/lib/auth/getUser"; +import { revalidateTag } from "next/cache"; + +type ActionResult = { success: boolean; error?: string }; + +export async function updateBusinessInfo(data: { + businessId: string; + address: string; + email: string; + title: string; + ustId?: string; + banking?: string; +}): Promise { + try { + const manager = await requireManager(); + + const business = await prisma.business.findUnique({ + where: { id: data.businessId }, + select: { managerId: true }, + }); + + if (!business || business.managerId !== manager.id) { + return { success: false, error: "Nicht berechtigt." }; + } + + const email = data.email.trim(); + + if (!data.address.trim() || !email || !data.title.trim()) { + return { success: false, error: "Bitte alle Pflichtfelder ausfüllen." }; + } + + if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) { + return { success: false, error: "Ungültige E-Mail-Adresse." }; + } + + await prisma.business.update({ + where: { id: data.businessId }, + data: { + address: data.address.trim(), + email, + title: data.title.trim(), + ustId: data.ustId?.trim() || null, + banking: data.banking?.trim() || null, + }, + }); + + (revalidateTag as (tag: string, type: "max") => void)(`user-data-${manager.email}`, "max"); + return { success: true }; + } catch { + return { success: false, error: "Fehler beim Speichern der Business-Daten." }; + } +} diff --git a/src/modules/settings/ui/components/BusinessSettingsForm.tsx b/src/modules/settings/ui/components/BusinessSettingsForm.tsx new file mode 100644 index 0000000..bdc9bce --- /dev/null +++ b/src/modules/settings/ui/components/BusinessSettingsForm.tsx @@ -0,0 +1,202 @@ +"use client"; + +import { type ReactNode, useState, useTransition } from "react"; +import InputComponent from "@/src/components/layout/InputComponent"; +import { Button } from "@/src/components/ui/button"; +import { updateBusinessInfo } from "../../actions/business-settings-actions"; +import { updateProfileInfo } from "../../actions/personal-settings-actions"; +import { toast } from "sonner"; + +export type BusinessSettingsFormProps = { + businessId: string; + name: string; + address: string; + email: string; + title: string; + ustId: string | null; + banking: string | null; + managerTel: string | null; +}; + +function Card({ + title, + subtitle, + children, +}: { + title: string; + subtitle?: string; + children: ReactNode; +}) { + return ( +
+
+

{title}

+ {subtitle &&

{subtitle}

} +
+ {children} +
+ ); +} + +const BusinessSettingsForm = ({ + businessId, + name, + address: initialAddress, + email: initialEmail, + title: initialTitle, + ustId: initialUstId, + banking: initialBanking, + managerTel: initialTel, +}: BusinessSettingsFormProps) => { + const [pending, startTransition] = useTransition(); + + const [address, setAddress] = useState(initialAddress); + const [email, setEmail] = useState(initialEmail); + const [title, setTitle] = useState(initialTitle); + const [ustId, setUstId] = useState(initialUstId ?? ""); + const [banking, setBanking] = useState(initialBanking ?? ""); + const [tel, setTel] = useState(initialTel ?? ""); + + const handleBusinessSave = () => { + startTransition(async () => { + try { + const result = await updateBusinessInfo({ + businessId, + address, + email, + title, + ustId, + banking, + }); + if (result.success) { + toast.success("Business-Daten erfolgreich gespeichert."); + } else { + toast.error(result.error ?? "Fehler beim Speichern."); + } + } catch { + toast.error("Fehler beim Speichern."); + } + }); + }; + + const handleTelSave = () => { + startTransition(async () => { + try { + const result = await updateProfileInfo({ tel }); + if (result.success) { + toast.success("Geschäftsnummer erfolgreich gespeichert."); + } else { + toast.error(result.error ?? "Fehler beim Speichern."); + } + } catch { + toast.error("Fehler beim Speichern."); + } + }); + }; + + return ( +
+ {/* Read-only Unternehmensname */} +
+

+ Unternehmensname +

+

{name}

+

+ Der Unternehmensname kann nach der Registrierung nicht mehr geändert werden. +

+
+ +
+ +
+ setTitle(e.target.value)} + /> + setAddress(e.target.value)} + /> +
+
+ +
+
+ + +
+ setEmail(e.target.value)} + /> + setUstId(e.target.value)} + /> + setBanking(e.target.value)} + /> +
+
+ +
+
+ + + setTel(e.target.value)} + /> +
+ +
+
+
+
+ ); +}; + +export default BusinessSettingsForm; diff --git a/src/modules/settings/ui/views/settings-business-page-view.tsx b/src/modules/settings/ui/views/settings-business-page-view.tsx index b5e54d7..1c8e648 100644 --- a/src/modules/settings/ui/views/settings-business-page-view.tsx +++ b/src/modules/settings/ui/views/settings-business-page-view.tsx @@ -1,5 +1,52 @@ -const SettingsBusinessPageView = () => { - return
; +import { requireManager } from "@/src/lib/auth/getUser"; +import { prisma } from "@/src/lib/prisma"; +import BusinessSettingsForm from "../components/BusinessSettingsForm"; + +const SettingsBusinessPageView = async () => { + const manager = await requireManager(); + + const business = await prisma.business.findFirst({ + where: { managerId: manager.id }, + select: { + id: true, + name: true, + address: true, + email: true, + title: true, + ustId: true, + banking: true, + }, + }); + + if (!business) { + return ( +
+
+

Business Einstellungen

+

Kein Business gefunden.

+
+
+ ); + } + + return ( +
+
+

Business Einstellungen

+

Verwalte die Daten deines Unternehmens

+
+ +
+ ); }; export default SettingsBusinessPageView;