chore: test endpoint na push
This commit is contained in:
@@ -3,6 +3,9 @@ import { getDateForWeekIndex, getData, getRestaurantMenu, getToday, initIfNeeded
|
|||||||
import { formatDate, getDayOfWeekIndex } from "../utils";
|
import { formatDate, getDayOfWeekIndex } from "../utils";
|
||||||
import getStorage from "../storage";
|
import getStorage from "../storage";
|
||||||
import { getWebsocket } from "../websocket";
|
import { getWebsocket } from "../websocket";
|
||||||
|
import { getLogin } from "../auth";
|
||||||
|
import { parseToken } from "../utils";
|
||||||
|
import webpush from 'web-push';
|
||||||
|
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
const storage = getStorage();
|
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<any>('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<any>('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;
|
export default router;
|
||||||
|
|||||||
Reference in New Issue
Block a user