-
+
+
+ {attention && }
+
diff --git a/src/components/admin/nav.ts b/src/components/admin/nav.ts
index b338ece..c22aa93 100644
--- a/src/components/admin/nav.ts
+++ b/src/components/admin/nav.ts
@@ -1,7 +1,9 @@
import type { IconName } from "../Icon";
import type { Permission } from "@/lib/permissions";
-export type NavItem = { href: string; label: string; icon: IconName; permission: Permission; badge?: "openOrders" };
+export type BadgeKey = "openOrders" | "newCustomers" | "newSubscribers" | "lowStock";
+
+export type NavItem = { href: string; label: string; icon: IconName; permission: Permission; badge?: BadgeKey };
export const ADMIN_NAV: { group: string; items: NavItem[] }[] = [
{
@@ -12,15 +14,15 @@ export const ADMIN_NAV: { group: string; items: NavItem[] }[] = [
group: "Verkauf",
items: [
{ href: "/admin/bestellungen", label: "Bestellungen", icon: "receipt", permission: "orders.view", badge: "openOrders" },
- { href: "/admin/kunden", label: "Kunden", icon: "users", permission: "customers.view" },
+ { href: "/admin/kunden", label: "Kunden", icon: "users", permission: "customers.view", badge: "newCustomers" },
{ href: "/admin/gutscheine", label: "Gutscheine", icon: "ticket", permission: "coupons.edit" },
- { href: "/admin/newsletter", label: "Newsletter", icon: "mail", permission: "newsletter.view" },
+ { href: "/admin/newsletter", label: "Newsletter", icon: "mail", permission: "newsletter.view", badge: "newSubscribers" },
],
},
{
group: "Katalog",
items: [
- { href: "/admin/produkte", label: "Produkte", icon: "box", permission: "products.view" },
+ { href: "/admin/produkte", label: "Produkte", icon: "box", permission: "products.view", badge: "lowStock" },
{ href: "/admin/kategorien", label: "Kategorien", icon: "folder", permission: "categories.edit" },
],
},
diff --git a/src/lib/admin-badges.ts b/src/lib/admin-badges.ts
new file mode 100644
index 0000000..a23b078
--- /dev/null
+++ b/src/lib/admin-badges.ts
@@ -0,0 +1,53 @@
+import "server-only";
+import { db } from "./db";
+import { can, type CurrentUser } from "./auth";
+import { PAYMENT_DEFINITIONS } from "./payments/catalog";
+
+import type { BadgeKey } from "@/components/admin/nav";
+
+export type { BadgeKey };
+export type Badges = Partial>;
+
+export const SEEN_COOKIE = "ad_seen";
+// Bereiche, deren Zähler nach dem Besuch zurückgesetzt wird
+export const SEEN_SECTIONS = { newCustomers: "/admin/kunden", newSubscribers: "/admin/newsletter" } as const;
+
+const MANUAL = PAYMENT_DEFINITIONS.filter((d) => d.provider === "manual").map((d) => d.code);
+const LOW_STOCK = 3;
+
+export function parseSeen(raw: string | undefined): Partial> {
+ try {
+ const v = JSON.parse(decodeURIComponent(raw ?? "")) as Record;
+ return Object.fromEntries(Object.entries(v).filter(([, t]) => typeof t === "number"));
+ } catch {
+ return {};
+ }
+}
+
+export async function getBadges(user: CurrentUser, seenRaw: string | undefined): Promise {
+ const seen = parseSeen(seenRaw);
+ // Ohne Besuch zählen die letzten 7 Tage als neu
+ const since = (k: keyof typeof SEEN_SECTIONS) => new Date(seen[k] ?? Date.now() - 7 * 86400000);
+ const [openOrders, newCustomers, newSubscribers, lowStock] = await Promise.all([
+ can(user, "orders.view")
+ ? db.order.count({
+ where: {
+ OR: [
+ { status: { in: ["PAID", "PROCESSING"] } },
+ // Vorkasse, Rechnung und Nachnahme warten ebenfalls auf uns; offene Online-Zahlungen sind meist abgebrochene Kassenvorgänge
+ { status: "PENDING", paymentMethod: { in: MANUAL } },
+ ],
+ },
+ })
+ : 0,
+ can(user, "customers.view") ? db.user.count({ where: { roleId: null, createdAt: { gt: since("newCustomers") } } }) : 0,
+ can(user, "newsletter.view") ? db.newsletterSubscriber.count({ where: { createdAt: { gt: since("newSubscribers") } } }) : 0,
+ can(user, "products.view")
+ ? Promise.all([
+ db.product.count({ where: { trackStock: true, active: true, stock: { lte: LOW_STOCK }, variants: { none: {} } } }),
+ db.productVariant.count({ where: { stock: { lte: LOW_STOCK }, product: { trackStock: true, active: true } } }),
+ ]).then(([a, b]) => a + b)
+ : 0,
+ ]);
+ return { openOrders, newCustomers, newSubscribers, lowStock };
+}
diff --git a/src/styles/admin.css b/src/styles/admin.css
index fb89afc..dc38953 100644
--- a/src/styles/admin.css
+++ b/src/styles/admin.css
@@ -171,3 +171,8 @@ a.ad-kpi:hover { border-color: var(--line-strong); }
.ad-form .sm-form-row .sm-field + .sm-field { margin-top: 0; }
.ad-upload__bar { display: block; width: 80px; height: 4px; margin: var(--space-1) auto 0; border-radius: 2px; background: var(--line); overflow: hidden; }
.ad-upload__bar > span { display: block; height: 100%; background: var(--gold); transition: width 150ms linear; }
+.ad-nav__badge--strong { background: var(--gold); color: var(--on-gold); }
+.ad-nav__badge--warn { background: var(--danger-soft); color: var(--danger); }
+.ad-nav a[aria-current="page"] .ad-nav__badge { background: var(--gold); color: var(--on-gold); }
+.ad-menu-dot { position: absolute; top: 9px; right: 9px; width: 9px; height: 9px; border-radius: 50%; background: var(--gold); box-shadow: 0 0 0 2px var(--surface); }
+.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }