cbaebcf649
- Add notifications table with Alembic migration
- Notification model with user-scoped indexing and partial index on unread
- NotificationService singleton with create/list/count/mark-read/dismiss
- FastAPI router: GET /notifications, GET /unread, PATCH /{id}/read,
POST /mark-all-read, DELETE /{id}
- Mute categories filtering from UserConfig
- 13 unit tests for NotificationService
- 10 integration tests for API endpoints
- Updated test_models.py with new table registration
Quality gates: pytest 23 new passed, ruff clean
44 lines
1.5 KiB
Python
44 lines
1.5 KiB
Python
"""Notification SQLAlchemy model."""
|
|
|
|
from datetime import datetime
|
|
from typing import Any
|
|
import uuid
|
|
|
|
from sqlalchemy import DateTime, ForeignKey, JSON, String, Text
|
|
from sqlalchemy import Uuid as UUID
|
|
from sqlalchemy.orm import Mapped, mapped_column
|
|
from sqlalchemy.sql import func
|
|
|
|
from src.models.base import Base, UUIDPrimaryKeyMixin
|
|
|
|
|
|
class Notification(UUIDPrimaryKeyMixin, Base):
|
|
__tablename__ = "notifications"
|
|
|
|
user_id: Mapped[uuid.UUID] = mapped_column(
|
|
UUID(as_uuid=True),
|
|
ForeignKey("users.id", ondelete="CASCADE"),
|
|
nullable=False,
|
|
index=True,
|
|
)
|
|
category: Mapped[str] = mapped_column(String(32), nullable=False)
|
|
severity: Mapped[str] = mapped_column(String(16), nullable=False)
|
|
title: Mapped[str] = mapped_column(String(255), nullable=False)
|
|
message: Mapped[str | None] = mapped_column(Text, nullable=True)
|
|
source_type: Mapped[str | None] = mapped_column(String(64), nullable=True)
|
|
source_id: Mapped[uuid.UUID | None] = mapped_column(
|
|
UUID(as_uuid=True), nullable=True
|
|
)
|
|
notification_metadata: Mapped[dict[str, Any]] = mapped_column(
|
|
"metadata", JSON, nullable=False, default=dict
|
|
)
|
|
read_at: Mapped[datetime | None] = mapped_column(
|
|
DateTime(timezone=True), nullable=True, index=True
|
|
)
|
|
dismissed_at: Mapped[datetime | None] = mapped_column(
|
|
DateTime(timezone=True), nullable=True
|
|
)
|
|
created_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True), server_default=func.now(), nullable=False, index=True
|
|
)
|