diff --git a/prisma/migrations/20261020190000_stock_alerts/migration.sql b/prisma/migrations/20261020190000_stock_alerts/migration.sql new file mode 100644 index 0000000..dc4605b --- /dev/null +++ b/prisma/migrations/20261020190000_stock_alerts/migration.sql @@ -0,0 +1,24 @@ +-- CreateTable +CREATE TABLE "StockAlert" ( + "id" TEXT NOT NULL, + "productId" TEXT NOT NULL, + "variantId" TEXT, + "email" TEXT NOT NULL, + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "notifiedAt" TIMESTAMP(3), + + CONSTRAINT "StockAlert_pkey" PRIMARY KEY ("id") +); + +-- CreateIndex +CREATE INDEX "StockAlert_productId_notifiedAt_idx" ON "StockAlert"("productId", "notifiedAt"); + +-- CreateIndex +CREATE INDEX "StockAlert_email_idx" ON "StockAlert"("email"); + +-- AddForeignKey +ALTER TABLE "StockAlert" ADD CONSTRAINT "StockAlert_productId_fkey" FOREIGN KEY ("productId") REFERENCES "Product"("id") ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "StockAlert" ADD CONSTRAINT "StockAlert_variantId_fkey" FOREIGN KEY ("variantId") REFERENCES "ProductVariant"("id") ON DELETE CASCADE ON UPDATE CASCADE; + diff --git a/prisma/schema.prisma b/prisma/schema.prisma index b70809d..0167566 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -125,20 +125,38 @@ model Product { category Category? @relation(fields: [categoryId], references: [id], onDelete: SetNull) variants ProductVariant[] orderItems OrderItem[] + stockAlerts StockAlert[] createdAt DateTime @default(now()) updatedAt DateTime @updatedAt } model ProductVariant { - id String @id @default(cuid()) + id String @id @default(cuid()) productId String - product Product @relation(fields: [productId], references: [id], onDelete: Cascade) + product Product @relation(fields: [productId], references: [id], onDelete: Cascade) name String // z. B. "183 cm" oder "Anthrazit" - sku String @default("") + sku String @default("") // Preis in Cent; leer = Produktpreis price Int? - stock Int @default(0) - sort Int @default(0) + stock Int @default(0) + sort Int @default(0) + alerts StockAlert[] +} + +// „Erinnere mich, wenn wieder da“: Kunde trägt sich bei einem ausverkauften Produkt (oder einer Variante) ein +model StockAlert { + id String @id @default(cuid()) + productId String + product Product @relation(fields: [productId], references: [id], onDelete: Cascade) + variantId String? + variant ProductVariant? @relation(fields: [variantId], references: [id], onDelete: Cascade) + email String + createdAt DateTime @default(now()) + // Gesetzt, sobald die Mail „wieder da“ verschickt wurde + notifiedAt DateTime? + + @@index([productId, notifiedAt]) + @@index([email]) } // ---------- Bestellungen ---------- diff --git a/src/app/(shop)/actions.ts b/src/app/(shop)/actions.ts index c4b76db..96605b8 100644 --- a/src/app/(shop)/actions.ts +++ b/src/app/(shop)/actions.ts @@ -7,6 +7,7 @@ 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"; export async function subscribeNewsletter(_prev: unknown, formData: FormData) { const email = z.email().safeParse(String(formData.get("email") ?? "").trim().toLowerCase()); @@ -15,6 +16,15 @@ export async function subscribeNewsletter(_prev: unknown, formData: FormData) { 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; diff --git a/src/app/(shop)/produkt/[slug]/page.tsx b/src/app/(shop)/produkt/[slug]/page.tsx index e1064a4..8188738 100644 --- a/src/app/(shop)/produkt/[slug]/page.tsx +++ b/src/app/(shop)/produkt/[slug]/page.tsx @@ -2,6 +2,7 @@ import { notFound } from "next/navigation"; import Link from "next/link"; import { db } from "@/lib/db"; import { getSettings } from "@/lib/settings"; +import { getCurrentUser } from "@/lib/auth"; import { withSale, withSales } from "@/lib/sales"; import { Icon } from "@/components/Icon"; import { SectionHeading, TrustBar } from "@/components/shop/Blocks"; @@ -29,8 +30,9 @@ export default async function ProductPage({ }); if (!raw || !raw.active) notFound(); const product = await withSale(raw); - const [settings, related] = await Promise.all([ + const [settings, user, related] = await Promise.all([ getSettings(), + getCurrentUser(), db.product.findMany({ where: { active: true, id: { not: product.id }, ...(product.categoryId ? { categoryId: product.categoryId } : {}) }, include: { variants: true }, @@ -66,6 +68,7 @@ export default async function ProductPage({ saleEndsAt={product.sale?.endsAt?.toISOString() ?? null} variantLabel="Variante" missingVariant={variante === "fehlt"} + userEmail={user?.email} />