import { Router, Request, Response } from "express"; import { donorNotifications } from "../lib/mockDb.js"; const router = Router(); // ─── GET /notifications ─────────────────────────────────────────────────────── // Full queue (most recent first). Useful for debugging/inspection. router.get("/notifications", (_req: Request, res: Response): void => { res.json([...donorNotifications].reverse()); }); // ─── GET /notifications/pending ─────────────────────────────────────────────── // Rows the external poller (OpenClaw) still needs to send via WhatsApp. router.get("/notifications/pending", (_req: Request, res: Response): void => { res.json(donorNotifications.filter((n) => n.status === "pending")); }); // ─── POST /notifications/:id/sent ───────────────────────────────────────────── // Marks a queued notification as delivered (or failed). Called by the poller // after it actually sends the WhatsApp message from the platform number. router.post( "/notifications/:id/sent", (req: Request, res: Response): void => { const item = donorNotifications.find((n) => n.id === req.params.id); if (!item) { res.status(404).json({ error: "Not found" }); return; } const failed = req.body?.status === "failed"; item.status = failed ? "failed" : "sent"; item.sentAt = failed ? null : new Date().toISOString(); res.json(item); } ); export default router;