Coverage for app / router / app / notifications / router.py: 100%
45 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-10-31 11:21 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-10-31 11:21 +0000
1"""
2Notifications — `/app/notifications`: the person's inbox (cost-control plan
3A2).
5A person's, across every workspace they belong to — like their invites —
6not one workspace's: an organisation's budget alert reaches its admins
7wherever they are working. Each notification names its workspace, and fe-core
8opens its link there. See ``app/services/notifications.py``.
9"""
11from __future__ import annotations
13from typing import Any
14from uuid import UUID
16from fastapi import APIRouter, Depends, HTTPException
17from pydantic import BaseModel, Field
18from sqlalchemy.orm import Session
20from app.db.postgres.models import Notification, User
21from app.dependency.base import get_db
22from app.dependency.user.context import get_current_user
23from app.services import notifications as notification_service
24from app.util.time import UTCDateTime
26notifications_router = APIRouter(prefix="/notifications", tags=["App - Notifications"])
29class NotificationRead(BaseModel):
30 id: UUID
31 #: A registered kind (``notifications.kind_keys``).
32 kind: str
33 title: str
34 body: str | None = None
35 #: A page written against ``/user/…``, to open in ``workspace_key``.
36 link: str | None = None
37 workspace_key: str | None = None
38 workspace_name: str | None = None
39 data: dict[str, Any] | None = None
40 created_at: UTCDateTime
41 read_at: UTCDateTime | None = None
43 class Config:
44 from_attributes = True
47class Inbox(BaseModel):
48 #: Newest first.
49 items: list[NotificationRead]
50 unread: int
53@notifications_router.get("", response_model=Inbox)
54def list_notifications(
55 unread: bool = False,
56 limit: int = 30,
57 db: Session = Depends(get_db),
58 current_user: User = Depends(get_current_user),
59):
60 """The caller's notifications, newest first, and how many are unread."""
61 items = notification_service.for_user(
62 db, current_user.id, unread_only=unread, limit=max(1, min(limit, 100))
63 )
64 return {
65 "items": items,
66 "unread": notification_service.unread_count(db, current_user.id),
67 }
70class InboxCount(BaseModel):
71 unread: int = Field(ge=0)
74@notifications_router.post("/{notification_id}/read", response_model=InboxCount)
75def read_notification(
76 notification_id: UUID,
77 db: Session = Depends(get_db),
78 current_user: User = Depends(get_current_user),
79):
80 """Mark one of the caller's notifications read. Someone else's reads as
81 not found."""
82 exists = (
83 db.query(Notification.id)
84 .filter(
85 Notification.id == notification_id,
86 Notification.recipient_user_id == current_user.id,
87 )
88 .first()
89 )
90 if exists is None:
91 raise HTTPException(status_code=404, detail="Notification not found.")
92 notification_service.mark_read(db, current_user.id, notification_id)
93 return {"unread": notification_service.unread_count(db, current_user.id)}
96@notifications_router.post("/read-all", response_model=InboxCount)
97def read_all_notifications(
98 db: Session = Depends(get_db),
99 current_user: User = Depends(get_current_user),
100):
101 """Mark every one of the caller's notifications read."""
102 notification_service.mark_read(db, current_user.id)
103 return {"unread": 0}