import { prisma } from "@/lib/prisma"; import { OVERDUE_AFTER_MS } from "./stock-math"; export interface ApiCoreStockIssues { /** Seit ueber 15 Minuten nicht bei ApiCore angekommen. */ overdue: number; /** Von ApiCore abgelehnt — braucht einen Menschen. */ rejected: number; /** Aktive Produkte, deren Bestand noch von Hand gepflegt wird. */ unlinked: number; } /** * Stoerungen des Bestandsabgleichs fuer das Admin-Dashboard. Ohne * ApiCore-Konfiguration gibt es nichts zu melden. */ export async function countApiCoreStockIssues(now: Date = new Date()): Promise { const configured = Boolean(process.env.APICORE_API_URL?.trim() && process.env.APICORE_API_KEY?.trim()); if (!configured) return { overdue: 0, rejected: 0, unlinked: 0 }; const [overdue, rejected, unlinked] = await Promise.all([ prisma.apiCoreStockEvent.count({ where: { sentAt: null, rejectedAt: null, createdAt: { lt: new Date(now.getTime() - OVERDUE_AFTER_MS) } }, }), prisma.apiCoreStockEvent.count({ where: { rejectedAt: { not: null } } }), prisma.product.count({ where: { isActive: true, apicoreBottlingId: null, apicoreProductId: null } }), ]); return { overdue, rejected, unlinked }; }