Files
personal-ai-assistant/backend/app/api/v1/notifications.py
dolgolyov.alexei ada7e82961 Phase 5: Notifications — WebSocket, APScheduler, AI tool, health review
Backend:
- Notification model + Alembic migration
- Notification service: CRUD, mark read, unread count, pending scheduled
- WebSocket manager singleton for real-time push
- WebSocket endpoint /ws/notifications with JWT auth via query param
- APScheduler integration: periodic notification sender (every 60s),
  daily proactive health review job (8 AM)
- AI tool: schedule_notification (immediate or scheduled)
- Health review worker: analyzes user memory via Claude, creates
  ai_generated notifications with WebSocket push

Frontend:
- Notification API client + Zustand store
- WebSocket hook with auto-reconnect (exponential backoff)
- Notification bell in header with unread count badge + dropdown
- Notifications page with type badges, mark read, mark all read
- WebSocket initialized in AppLayout for app-wide real-time updates
- Enabled notifications nav in sidebar
- English + Russian translations

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-19 13:57:25 +03:00

58 lines
2.1 KiB
Python

import uuid
from typing import Annotated
from fastapi import APIRouter, Depends, Query, status
from sqlalchemy.ext.asyncio import AsyncSession
from app.api.deps import get_current_user
from app.database import get_db
from app.models.user import User
from app.schemas.notification import NotificationListResponse, NotificationResponse, UnreadCountResponse
from app.services import notification_service
router = APIRouter(prefix="/notifications", tags=["notifications"])
@router.get("/", response_model=NotificationListResponse)
async def list_notifications(
user: Annotated[User, Depends(get_current_user)],
db: Annotated[AsyncSession, Depends(get_db)],
status_filter: str | None = Query(default=None, alias="status"),
limit: int = Query(default=50, le=200),
offset: int = Query(default=0),
):
notifications = await notification_service.get_user_notifications(db, user.id, status_filter, limit, offset)
unread = await notification_service.get_unread_count(db, user.id)
return NotificationListResponse(
notifications=[NotificationResponse.model_validate(n) for n in notifications],
unread_count=unread,
)
@router.get("/unread-count", response_model=UnreadCountResponse)
async def unread_count(
user: Annotated[User, Depends(get_current_user)],
db: Annotated[AsyncSession, Depends(get_db)],
):
count = await notification_service.get_unread_count(db, user.id)
return UnreadCountResponse(count=count)
@router.patch("/{notification_id}/read", response_model=NotificationResponse)
async def mark_read(
notification_id: uuid.UUID,
user: Annotated[User, Depends(get_current_user)],
db: Annotated[AsyncSession, Depends(get_db)],
):
notif = await notification_service.mark_as_read(db, notification_id, user.id)
return NotificationResponse.model_validate(notif)
@router.post("/mark-all-read", status_code=status.HTTP_200_OK)
async def mark_all_read(
user: Annotated[User, Depends(get_current_user)],
db: Annotated[AsyncSession, Depends(get_db)],
):
count = await notification_service.mark_all_read(db, user.id)
return {"marked": count}