import { notFound } from "next/navigation"; import { db } from "@/lib/db"; import { requireAdmin } from "@/lib/auth"; import { dateFmt } from "@/lib/status"; import { formatPrice } from "@/lib/money"; import { toBerlinLocal } from "@/lib/berlin-time"; import { getNewsletterMailConfig, isMailReady } from "@/lib/mail/config"; import { CAMPAIGN_STATUS, SEGMENTS, cleanBlocks, type CampaignStatus, type Segment } from "@/lib/campaign-blocks"; import { getNewsletterSettings, loadProducts, renderCampaign, sampleNewsletterVars, segmentCounts } from "@/lib/campaigns"; import { ConfirmButton } from "@/components/admin/AdminForm"; import { CampaignEditor } from "@/components/admin/CampaignEditor"; import { Card, PageHead, Pill } from "@/components/admin/ui"; import { AutoRefresh } from "@/components/admin/AutoRefresh"; import { cancelCampaign, deleteCampaign, duplicateCampaign, unscheduleCampaign } from "../actions"; export default async function CampaignPage({ params }: { params: Promise<{ id: string }> }) { const user = await requireAdmin("newsletter.send"); const { id } = await params; const c = await db.newsletterCampaign.findUnique({ where: { id } }); if (!c) notFound(); const blocks = cleanBlocks(c.blocks); const status = (c.status in CAMPAIGN_STATUS ? c.status : "draft") as CampaignStatus; const st = CAMPAIGN_STATUS[status]; const products = await loadProducts(blocks); const preview = await renderCampaign({ subject: c.subject, preheader: c.preheader, blocks }, { vars: await sampleNewsletterVars(), products, unsubscribeUrl: "#" }); const back = { href: "/admin/newsletter", label: "Newsletter" }; const tools = ( <> {status !== "sending" && } ); if (status === "draft" || status === "scheduled") { const all = await db.product.findMany({ where: { active: true }, orderBy: [{ sort: "asc" }, { name: "asc" }], include: { category: { select: { name: true } } } }); return (
{status === "scheduled" && ( <> Geplant )} {tools} ({ id: p.id, name: p.name, detail: [p.category?.name, formatPrice(p.price)].filter(Boolean).join(" · "), image: p.images[0] ?? "" }))} counts={await segmentCounts()} mailReady={isMailReady(await getNewsletterMailConfig())} userEmail={user.email} scheduledFor={status === "scheduled" ? toBerlinLocal(c.scheduledAt) : ""} />
); } // Gestartet: Fortschritt und Statistik const [counts, failed, settings, mailReady] = await Promise.all([ db.newsletterRecipient.groupBy({ by: ["status"], where: { campaignId: c.id }, _count: true }), db.newsletterRecipient.findMany({ where: { campaignId: c.id, status: "failed" }, take: 20, select: { email: true, error: true } }), getNewsletterSettings(), getNewsletterMailConfig().then(isMailReady), ]); const n = (s: string) => counts.find((x) => x.status === s)?._count ?? 0; const total = counts.reduce((a, x) => a + x._count, 0); const sent = n("sent"); const pending = n("pending"); const [opened, clicked, unsubscribed] = await Promise.all([ db.newsletterRecipient.count({ where: { campaignId: c.id, openedAt: { not: null } } }), db.newsletterRecipient.count({ where: { campaignId: c.id, clickedAt: { not: null } } }), db.newsletterRecipient.count({ where: { campaignId: c.id, unsubscribedAt: { not: null } } }), ]); const pct = (a: number) => (sent ? `${Math.round((a / sent) * 100)} %` : "–"); const links = c.links.map((url, i) => ({ url, clicks: c.linkClicks[i] ?? 0 })).filter((l) => l.clicks > 0).sort((a, b) => b.clicks - a.clicks); const minutesLeft = pending ? Math.ceil(pending / settings.perMinute) : 0; return (
{status === "sending" && } {st.label} {status === "sending" && } {tools} {status === "sending" && (

{total - pending} von {total} bearbeitet. {settings.perMinute} Mails pro Minute, noch etwa {minutesLeft} {minutesLeft === 1 ? "Minute" : "Minuten"}. {!mailReady && Der Mail-Versand ist ausgeschaltet, der Newsletter wartet.}

)}
Zugestellt
{sent}
von {total} Empfängern{n("failed") ? `, ${n("failed")} fehlgeschlagen` : ""}
Geöffnet
{settings.tracking || opened ? pct(opened) : "–"}
{opened} Empfänger
Geklickt
{settings.tracking || clicked ? pct(clicked) : "–"}
{clicked} Empfänger
Abgemeldet
{unsubscribed}
über diesen Newsletter

Öffnungen sind ein Richtwert: Manche Mailprogramme laden Bilder nicht oder laden sie automatisch vor.