"use server"; import { cookies } from "next/headers"; import { redirect } from "next/navigation"; import { revalidatePath } from "next/cache"; import { z } from "zod"; import { db } from "@/lib/db"; import { readCartCookie, writeCartCookie } from "@/lib/cart"; import { findCoupon } from "@/lib/pricing"; import { subscribe } from "@/lib/newsletter"; import { addStockAlert } from "@/lib/stock-alerts"; import { getCurrentUser } from "@/lib/auth"; import { hasCourseAccess, isCourse } from "@/lib/courses"; export async function subscribeNewsletter(_prev: unknown, formData: FormData) { const email = z.email().safeParse(String(formData.get("email") ?? "").trim().toLowerCase()); if (!email.success) return { ok: false, error: "Bitte gib eine gültige E-Mail-Adresse ein." }; const state = await subscribe(email.data); return { ok: true, error: null, pending: state === "pending" }; } export async function subscribeStockAlert(_prev: unknown, formData: FormData) { const email = z.email().safeParse(String(formData.get("email") ?? "").trim().toLowerCase()); if (!email.success) return { ok: false, error: "Bitte gib eine gültige E-Mail-Adresse ein." }; // Verstecktes Feld: von Menschen nie ausgefüllt, von Bots oft if (String(formData.get("website") ?? "")) return { ok: true, error: null }; const r = await addStockAlert(String(formData.get("productId") ?? ""), String(formData.get("variantId") ?? "") || null, email.data); return r.ok ? { ok: true, error: null } : { ok: false, error: r.error }; } export async function addToCart(formData: FormData) { const productId = String(formData.get("productId") ?? ""); const variantId = String(formData.get("variantId") ?? "") || null; const quantity = Math.max(1, Math.min(99, Number(formData.get("quantity") ?? 1) || 1)); const product = await db.product.findFirst({ where: { id: productId, active: true }, include: { variants: true } }); if (!product) return; // Einen Kurs, den man schon hat, kauft man nicht noch einmal if (isCourse(product) && (await hasCourseAccess((await getCurrentUser())?.id, product.id))) redirect(`/konto/kurse/${product.slug}`); if (!isCourse(product) && product.variants.length > 0 && !product.variants.some((v) => v.id === variantId)) { redirect(`/produkt/${product.slug}?variante=fehlt`); } const entries = await readCartCookie(); const existing = entries.find((e) => e.p === productId && e.v === variantId); if (existing) existing.q = Math.min(99, existing.q + quantity); else entries.push({ p: productId, v: variantId, q: quantity }); await writeCartCookie(entries); revalidatePath("/", "layout"); const back = String(formData.get("redirectTo") ?? ""); redirect(back.startsWith("/") ? back : "/warenkorb?hinzugefuegt=1"); } export async function updateCartLine(formData: FormData) { const [productId, variantRaw] = String(formData.get("key") ?? "").split(":"); const variantId = variantRaw || null; const quantity = Number(formData.get("quantity")); let entries = await readCartCookie(); if (!Number.isFinite(quantity) || quantity <= 0) { entries = entries.filter((e) => !(e.p === productId && e.v === variantId)); } else { const e = entries.find((x) => x.p === productId && x.v === variantId); if (e) e.q = Math.min(99, Math.round(quantity)); } await writeCartCookie(entries); revalidatePath("/", "layout"); } export async function applyCoupon(_prev: unknown, formData: FormData) { const code = String(formData.get("code") ?? "").trim().toUpperCase(); const jar = await cookies(); if (!code) { jar.delete("sm_coupon"); revalidatePath("/warenkorb"); return { error: null }; } const { error } = await findCoupon(code, Number.MAX_SAFE_INTEGER); if (error) return { error }; jar.set("sm_coupon", code, { httpOnly: true, sameSite: "lax", path: "/", maxAge: 60 * 60 * 24 * 7 }); revalidatePath("/", "layout"); return { error: null }; } export async function removeCoupon() { (await cookies()).delete("sm_coupon"); revalidatePath("/", "layout"); }