feat: push notifikace pro připomínku výběru oběda
ci/woodpecker/push/workflow Pipeline was successful
ci/woodpecker/push/workflow Pipeline was successful
This commit is contained in:
@@ -2,6 +2,7 @@ import express, { Request } from "express";
|
||||
import { getLogin } from "../auth";
|
||||
import { parseToken } from "../utils";
|
||||
import { getNotificationSettings, saveNotificationSettings } from "../notifikace";
|
||||
import { subscribePush, unsubscribePush, getVapidPublicKey } from "../pushReminder";
|
||||
import { UpdateNotificationSettingsData } from "../../../types";
|
||||
|
||||
const router = express.Router();
|
||||
@@ -24,9 +25,43 @@ router.post("/settings", async (req: Request<{}, any, UpdateNotificationSettings
|
||||
discordWebhookUrl: req.body.discordWebhookUrl,
|
||||
teamsWebhookUrl: req.body.teamsWebhookUrl,
|
||||
enabledEvents: req.body.enabledEvents,
|
||||
reminderTime: req.body.reminderTime,
|
||||
});
|
||||
res.status(200).json(settings);
|
||||
} catch (e: any) { next(e) }
|
||||
});
|
||||
|
||||
/** Vrátí veřejný VAPID klíč pro registraci push notifikací. */
|
||||
router.get("/push/vapidKey", (req, res) => {
|
||||
const key = getVapidPublicKey();
|
||||
if (!key) {
|
||||
return res.status(503).json({ error: "Push notifikace nejsou nakonfigurovány" });
|
||||
}
|
||||
res.status(200).json({ key });
|
||||
});
|
||||
|
||||
/** Přihlásí uživatele k push připomínkám. */
|
||||
router.post("/push/subscribe", async (req, res, next) => {
|
||||
const login = getLogin(parseToken(req));
|
||||
try {
|
||||
if (!req.body.subscription) {
|
||||
return res.status(400).json({ error: "Nebyla předána push subscription" });
|
||||
}
|
||||
if (!req.body.reminderTime) {
|
||||
return res.status(400).json({ error: "Nebyl předán čas připomínky" });
|
||||
}
|
||||
await subscribePush(login, req.body.subscription, req.body.reminderTime);
|
||||
res.status(200).json({});
|
||||
} catch (e: any) { next(e) }
|
||||
});
|
||||
|
||||
/** Odhlásí uživatele z push připomínek. */
|
||||
router.post("/push/unsubscribe", async (req, res, next) => {
|
||||
const login = getLogin(parseToken(req));
|
||||
try {
|
||||
await unsubscribePush(login);
|
||||
res.status(200).json({});
|
||||
} catch (e: any) { next(e) }
|
||||
});
|
||||
|
||||
export default router;
|
||||
|
||||
Reference in New Issue
Block a user