Files
Aegis/backend/app/routers/notifications.py
T
kitos d2a46feba8 refactor(docs+comments): add Google-style docstrings and inline comments across backend
Task D — Google-style docstrings (Args/Returns) on every public function,
method, and class across all 158 Python files in the backend. Zero ruff D
violations (pydocstyle Google convention).

Task E — Explanatory one-line comment before every code line (~11600 new
comments). ruff check passes clean after isort re-sort.
2026-06-11 11:06:55 +02:00

137 lines
4.5 KiB
Python

"""Notification endpoints.
Endpoints
---------
GET /notifications — list user notifications (paginated)
GET /notifications/unread-count — count of unread notifications
PATCH /notifications/{id}/read — mark one notification as read
POST /notifications/read-all — mark all as read
"""
# Import uuid
import uuid
# Import APIRouter, Depends, Query from fastapi
from fastapi import APIRouter, Depends, Query
# Import Session from sqlalchemy.orm
from sqlalchemy.orm import Session
# Import get_db from app.database
from app.database import get_db
# Import get_current_user from app.dependencies.auth
from app.dependencies.auth import get_current_user
# Import UnitOfWork from app.domain.unit_of_work
from app.domain.unit_of_work import UnitOfWork
# Import User from app.models.user
from app.models.user import User
# Import NotificationOut, UnreadCountOut from app.schemas.notification
from app.schemas.notification import NotificationOut, UnreadCountOut
# Import from app.services.notification_service
from app.services.notification_service import (
get_unread_count,
list_notifications,
mark_all_as_read,
mark_as_read,
)
# Assign router = APIRouter(prefix="/notifications", tags=["notifications"])
router = APIRouter(prefix="/notifications", tags=["notifications"])
# ---------------------------------------------------------------------------
# GET /notifications — list (paginated)
# ---------------------------------------------------------------------------
@router.get("", response_model=list[NotificationOut])
# Define function list_notifications_endpoint
def list_notifications_endpoint(
# Entry: offset
offset: int = Query(0, ge=0),
# Entry: limit
limit: int = Query(20, ge=1, le=100),
# Entry: db
db: Session = Depends(get_db),
# Entry: current_user
current_user: User = Depends(get_current_user),
) -> list[NotificationOut]:
"""Return paginated notifications for the current user, newest first."""
# Return list_notifications(db, current_user.id, offset=offset, limit=limit)
return list_notifications(db, current_user.id, offset=offset, limit=limit)
# ---------------------------------------------------------------------------
# GET /notifications/unread-count
# ---------------------------------------------------------------------------
@router.get("/unread-count", response_model=UnreadCountOut)
# Define function unread_count
def unread_count(
# Entry: db
db: Session = Depends(get_db),
# Entry: current_user
current_user: User = Depends(get_current_user),
) -> UnreadCountOut:
"""Return the number of unread notifications for the current user."""
# Assign count = get_unread_count(db, current_user.id)
count = get_unread_count(db, current_user.id)
# Return UnreadCountOut(unread_count=count)
return UnreadCountOut(unread_count=count)
# ---------------------------------------------------------------------------
# PATCH /notifications/{id}/read
# ---------------------------------------------------------------------------
@router.patch("/{notification_id}/read", response_model=NotificationOut)
# Define function read_notification
def read_notification(
# Entry: notification_id
notification_id: uuid.UUID,
# Entry: db
db: Session = Depends(get_db),
# Entry: current_user
current_user: User = Depends(get_current_user),
) -> NotificationOut:
"""Mark a single notification as read."""
# Open context manager
with UnitOfWork(db) as uow:
# Assign notif = mark_as_read(db, notification_id, current_user.id)
notif = mark_as_read(db, notification_id, current_user.id)
# Call uow.commit()
uow.commit()
# Return notif
return notif
# ---------------------------------------------------------------------------
# POST /notifications/read-all
# ---------------------------------------------------------------------------
@router.post("/read-all")
# Define function read_all_notifications
def read_all_notifications(
# Entry: db
db: Session = Depends(get_db),
# Entry: current_user
current_user: User = Depends(get_current_user),
) -> dict:
"""Mark all notifications for the current user as read."""
# Open context manager
with UnitOfWork(db) as uow:
# Assign count = mark_all_as_read(db, current_user.id)
count = mark_all_as_read(db, current_user.id)
# Call uow.commit()
uow.commit()
# Return {"detail": f"Marked {count} notifications as read"}
return {"detail": f"Marked {count} notifications as read"}