Files
tsumugu/backend/app/api/library.py
T
LarsandClaude Sonnet 5.5 d39c7d7999 Liefere Zähler, Rauminhalt und Teilen im Stapel für die neue Ablage.
Akten und Vorgänge melden Unterakten, Vorgänge und die Räume, mit denen sie geteilt sind, jeweils nur aus Lesbarem. Neue Endpunkte zeigen die Räume als Kacheln, den Inhalt eines Raums und teilen mehrere Einträge auf einmal oder gar nicht. Beim Anlegen lässt sich ein Eintrag gleich in einen Raum legen.

Die lokale Suite läuft mit 148 Tests grün.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-22 11:38:02 +02:00

100 lines
3.8 KiB
Python

"""Library endpoints: rooms as tiles, what lies in a room, and sharing several entries at once."""
from __future__ import annotations
from uuid import UUID
from fastapi import APIRouter, Depends
from fastapi.responses import JSONResponse
from psycopg import Connection
from app.access.roles import room_role
from app.api.deps import Principal, get_conn, get_principal
from app.api.schemas import BatchShareIn
from app.core.errors import forbidden
from app.library import case_items, matter_items, room_contents, room_effect
from app.ops import share_with_room
router = APIRouter()
_NO_STORE = {"Cache-Control": "no-store"}
@router.get("/api/v1/rooms/overview")
def rooms_overview(principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn)):
"""The team rooms of the person as tiles. Names of members are only shown to managers."""
actor = principal.account_id
rooms = conn.execute(
"""
SELECT r.id, r.name, r.owner_id, r.default_visibility, m.role,
(SELECT count(*) FROM room_memberships x WHERE x.room_id = r.id AND x.status = 'active') AS members
FROM room_memberships m JOIN data_rooms r ON r.id = m.room_id
WHERE m.subject_id = %s AND m.status = 'active' AND r.kind = 'organization' AND r.status = 'active'
ORDER BY r.name, r.id
""",
(actor,),
).fetchall()
items = []
for room in rooms:
cases, matters = room_contents(conn, actor, room["id"])
people = []
if room["role"] == "manager":
people = [
row["display_name"]
for row in conn.execute(
"""
SELECT a.display_name FROM room_memberships x JOIN auth_accounts a ON a.id = x.subject_id
WHERE x.room_id = %s AND x.status = 'active'
ORDER BY a.display_name, a.id LIMIT 6
""",
(room["id"],),
).fetchall()
]
items.append(
{
"id": str(room["id"]),
"name": room["name"],
"role": room["role"],
"is_owner": room["owner_id"] == actor,
"private": room["members"] <= 1,
"default_visibility": room["default_visibility"],
"member_count": room["members"],
"members": people,
"case_count": sum(1 for case, _ in cases if not case["parent_case_id"]),
"matter_count": sum(1 for matter, _ in matters if not matter["parent_matter_id"]),
}
)
return JSONResponse({"items": items}, headers=_NO_STORE)
@router.get("/api/v1/rooms/{room_id}/contents")
def contents(room_id: UUID, principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn)):
"""Cases and matters shared with the room that the person may read."""
if room_role(conn, principal.account_id, room_id) is None:
raise forbidden()
cases, matters = room_contents(conn, principal.account_id, room_id)
return JSONResponse(
{
"cases": case_items(conn, principal.account_id, cases),
"matters": matter_items(conn, principal.account_id, matters),
},
headers=_NO_STORE,
)
@router.post("/api/v1/rooms/{room_id}/shares")
def share_many(
room_id: UUID,
body: BatchShareIn,
principal: Principal = Depends(get_principal),
conn: Connection = Depends(get_conn),
):
"""Share several own entries with the room. All or none: one failure rolls the request back."""
for item in body.items:
share_with_room(conn, principal.account_id, item.kind, item.id, room_id, body.access)
return {
"shared": len(body.items),
"access": body.access,
"effect": room_effect(conn, room_id, principal.account_id, body.access),
}