From 20cc7259a3040ec63bd179f3379dae0c64ba17b4 Mon Sep 17 00:00:00 2001 From: batmanisko Date: Wed, 4 Mar 2026 14:11:22 +0100 Subject: [PATCH] chore: test endpoint na push --- server/src/routes/devRoutes.ts | 41 ++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/server/src/routes/devRoutes.ts b/server/src/routes/devRoutes.ts index 066ca6b..b717b5f 100644 --- a/server/src/routes/devRoutes.ts +++ b/server/src/routes/devRoutes.ts @@ -3,6 +3,9 @@ import { getDateForWeekIndex, getData, getRestaurantMenu, getToday, initIfNeeded import { formatDate, getDayOfWeekIndex } from "../utils"; import getStorage from "../storage"; import { getWebsocket } from "../websocket"; +import { getLogin } from "../auth"; +import { parseToken } from "../utils"; +import webpush from 'web-push'; const router = express.Router(); const storage = getStorage(); @@ -153,4 +156,42 @@ router.post("/clear", async (req: Request<{}, any, any>, res, next) => { } }); +/** Vrátí obsah push reminder registry (pro ladění). */ +router.get("/pushRegistry", async (_req, res, next) => { + try { + const registry = await storage.getData('push_reminder_registry') ?? {}; + const sanitized = Object.fromEntries( + Object.entries(registry).map(([login, entry]: [string, any]) => [ + login, + { time: entry.time, endpoint: entry.subscription?.endpoint?.slice(0, 60) + '…' } + ]) + ); + res.status(200).json(sanitized); + } catch (e: any) { next(e) } +}); + +/** Okamžitě odešle test push notifikaci přihlášenému uživateli (pro ladění). */ +router.post("/testPush", async (req, res, next) => { + const login = getLogin(parseToken(req)); + try { + const registry = await storage.getData('push_reminder_registry') ?? {}; + const entry = registry[login]; + if (!entry) { + return res.status(404).json({ error: `Uživatel ${login} nemá uloženou push subscription. Nastav připomínku v nastavení.` }); + } + const publicKey = process.env.VAPID_PUBLIC_KEY; + const privateKey = process.env.VAPID_PRIVATE_KEY; + const subject = process.env.VAPID_SUBJECT; + if (!publicKey || !privateKey || !subject) { + return res.status(503).json({ error: 'VAPID klíče nejsou nastaveny' }); + } + webpush.setVapidDetails(subject, publicKey, privateKey); + await webpush.sendNotification( + entry.subscription, + JSON.stringify({ title: 'Luncher test', body: 'Push notifikace fungují!' }) + ); + res.status(200).json({ ok: true }); + } catch (e: any) { next(e) } +}); + export default router;