feat: web push notifications, mobile-ready (PRD §5.6)

ADR 0003: VAPID self-hosted today, DeviceToken model agnostic to
platform so iOS/Android (Expo/APNs/FCM) plug in as new senders later.

Backend (apps/api/src/modules/notifications)
- sender.ts: NotificationSender interface, WebPushSender via VAPID
- notifications.routes.ts: GET /me/notifications/config, POST/DELETE
  /me/device-tokens, PATCH /me/notifications/settings, POST /me/notifications/test
- scheduler.ts: node-cron Mon 09:00 Europe/Rome for weekly weigh-in
  reminder; auto-cleanup of expired tokens on 404/410
- env: VAPID_PUBLIC_KEY/PRIVATE_KEY/SUBJECT (all optional → push gracefully off)

Frontend
- public/sw.js minimal (push + notificationclick)
- lib/notifications/push-client.ts: subscribe / unsubscribe / getCurrentSubscription
- profile/notifications-{actions,panel}.tsx: editorial panel with toggles,
  device list, "send test", per-device removal
- pushReady requires both permission AND active subscription (covers the
  case where the user revoked the SW but kept the browser permission)

Schema
- DeviceToken { userId, platform, endpoint, p256dh, auth, token, userAgent,
  createdAt, lastSeenAt } with unique(userId, endpoint)
- ExtendedPrismaClient type exported from @ketopath/db
- NotificationSettings zod schema in @ketopath/shared

Tooling
- lint-staged: split .js out of eslint glob so service worker is only
  formatted (it lives outside the TS project)

i18n
- Notifications namespace (it) with typed error keys

Smoke tested: POST /me/device-tokens 201, POST /me/notifications/test 200,
real push delivered to a macOS Chrome device.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
lucianoandClaude Opus 4.7 committed 2026-04-29 21:58:35 +02:00
1 parent cdfc685855
commit bb48357179
26 files changed
+1144 -2

No files matched your search

+34
View File
@@ -165,6 +165,40 @@
"delta": "Variazione",
"emptyHistory": "Nessuna rilevazione registrata. Inserisci la prima a destra: la tendenza apparirà dopo due o tre pesate."
},
"Notifications": {
"eyebrow": "Notifiche",
"title": "Promemoria al momento giusto",
"subtitle": "Decidi tu cosa vuoi ricevere e su quale dispositivo. Niente marketing, niente notifiche di terze parti.",
"thisDevice": "Questo dispositivo",
"thisDeviceActive": "Le notifiche sono attive su questo dispositivo.",
"thisDeviceInactive": "Questo dispositivo non riceve ancora notifiche. Attivalo per il promemoria settimanale.",
"enableHere": "Attiva sul dispositivo",
"sendTest": "Invia notifica di prova",
"weeklyWeighInLabel": "Promemoria pesata settimanale",
"weeklyWeighInHint": "Lunedì alle 09:00 ti ricordiamo la pesata della settimana.",
"fastingMilestonesLabel": "Tappe del digiuno",
"fastingMilestonesHint": "Ti avvisiamo all'ingresso in chetogenesi e all'autofagia. Disponibile a breve.",
"registeredDevices": "Dispositivi registrati",
"subscribed": "Notifiche attivate.",
"unsubscribed": "Notifiche disattivate su questo dispositivo.",
"testSent": "Notifica di prova inviata.",
"blocked": "Hai bloccato le notifiche per questo sito. Cambia l'impostazione dal browser per riattivarle.",
"unsupported": "Il tuo browser non supporta le notifiche push. Su iOS apri prima KetoPath dalla Home.",
"serverDisabled": "Le notifiche non sono ancora attive sul server.",
"configUnavailable": "Impossibile caricare le impostazioni notifiche. Riprova più tardi.",
"working": "Attendere…",
"errors": {
"unsupported": "Il tuo browser non supporta le notifiche push.",
"permission_denied": "Hai negato il permesso. Modificalo dal browser per riprovare.",
"permission_default": "Permesso non concesso. Riprova e seleziona 'Consenti'.",
"no_vapid_key": "Configurazione mancante. Contatta l'amministratore.",
"api_error": "Errore di rete. Riprova fra poco.",
"sw_error": "Impossibile registrare il service worker.",
"no_devices": "Nessun dispositivo registrato.",
"push_not_configured": "Le notifiche non sono ancora abilitate sul server.",
"unknown": "Si è verificato un errore imprevisto."
}
},
"Shopping": {
"title": "La spesa della settimana",
"subtitle": "Tutto ciò che ti serve per il piano in corso, raggruppato per reparto.",