|
| 1 | +import { Hono } from "hono"; |
| 2 | +import { z } from "zod"; |
| 3 | +import { PushSubscriptionRequestSchema } from "@opencode-manager/shared/schemas"; |
| 4 | +import type { NotificationService } from "../services/notification"; |
| 5 | + |
| 6 | +export function createNotificationRoutes( |
| 7 | + notificationService: NotificationService |
| 8 | +) { |
| 9 | + const app = new Hono(); |
| 10 | + |
| 11 | + app.get("/vapid-public-key", (c) => { |
| 12 | + const publicKey = notificationService.getVapidPublicKey(); |
| 13 | + if (!publicKey) { |
| 14 | + return c.json( |
| 15 | + { error: "Push notifications are not configured" }, |
| 16 | + 503 |
| 17 | + ); |
| 18 | + } |
| 19 | + return c.json({ publicKey }); |
| 20 | + }); |
| 21 | + |
| 22 | + app.post("/subscribe", async (c) => { |
| 23 | + const userId = c.req.query('userId') || 'default' |
| 24 | + const body = await c.req.json(); |
| 25 | + const parsed = PushSubscriptionRequestSchema.safeParse(body); |
| 26 | + |
| 27 | + if (!parsed.success) { |
| 28 | + return c.json({ error: "Invalid subscription data", details: parsed.error.issues }, 400); |
| 29 | + } |
| 30 | + |
| 31 | + const { endpoint, keys, deviceName } = parsed.data; |
| 32 | + |
| 33 | + const subscription = notificationService.saveSubscription( |
| 34 | + userId, |
| 35 | + endpoint, |
| 36 | + keys.p256dh, |
| 37 | + keys.auth, |
| 38 | + deviceName |
| 39 | + ); |
| 40 | + |
| 41 | + return c.json({ subscription }); |
| 42 | + }); |
| 43 | + |
| 44 | + app.delete("/subscribe", async (c) => { |
| 45 | + const userId = c.req.query('userId') || 'default' |
| 46 | + const body = await c.req.json(); |
| 47 | + const parsed = z.object({ endpoint: z.string().url() }).safeParse(body); |
| 48 | + |
| 49 | + if (!parsed.success) { |
| 50 | + return c.json({ error: "Valid endpoint URL is required", details: parsed.error.issues }, 400); |
| 51 | + } |
| 52 | + |
| 53 | + const removed = notificationService.removeSubscription(parsed.data.endpoint, userId); |
| 54 | + return c.json({ success: removed }); |
| 55 | + }); |
| 56 | + |
| 57 | + app.get("/subscriptions", (c) => { |
| 58 | + const userId = c.req.query('userId') || 'default' |
| 59 | + const subscriptions = notificationService.getSubscriptions(userId); |
| 60 | + return c.json({ subscriptions }); |
| 61 | + }); |
| 62 | + |
| 63 | + app.delete("/subscriptions/:id", (c) => { |
| 64 | + const userId = c.req.query('userId') || 'default' |
| 65 | + const id = parseInt(c.req.param("id"), 10); |
| 66 | + |
| 67 | + if (isNaN(id)) { |
| 68 | + return c.json({ error: "Invalid subscription ID" }, 400); |
| 69 | + } |
| 70 | + |
| 71 | + const removed = notificationService.removeSubscriptionById(id, userId); |
| 72 | + return c.json({ success: removed }); |
| 73 | + }); |
| 74 | + |
| 75 | + app.post("/test", async (c) => { |
| 76 | + const userId = c.req.query('userId') || 'default' |
| 77 | + |
| 78 | + if (!notificationService.isConfigured()) { |
| 79 | + return c.json( |
| 80 | + { error: "Push notifications are not configured" }, |
| 81 | + 503 |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + const subscriptions = notificationService.getSubscriptions(userId); |
| 86 | + if (subscriptions.length === 0) { |
| 87 | + return c.json( |
| 88 | + { error: "No push subscriptions registered" }, |
| 89 | + 404 |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + await notificationService.sendTestNotification(userId); |
| 94 | + |
| 95 | + return c.json({ success: true, devicesNotified: subscriptions.length }); |
| 96 | + }); |
| 97 | + |
| 98 | + return app; |
| 99 | +} |
0 commit comments