Ordne Akten und Vorgaenge nach fachlichem Datum oder eigener Reihenfolge und mache den Inhalt zur Arbeitsansicht.
Deploy Development / deploy (push) Successful in 22s
Test Suite / pytest-backend (push) Successful in 51s
Test Suite / smoke-dev (push) Successful in 1s
Test Suite / frontend-build (push) Successful in 6s

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
LarsandCursor committed 2026-10-17 09:36:48 +02:00
1 parent 27925093e2
commit 52bda91aad
22 files changed
+2079 -258

No files matched your search

+154 -3
View File
@@ -56,6 +56,9 @@ from app.api.schemas import (
MatterIn,
MatterPatch,
MemberIn,
BindingOrderIn,
ContentOrderIn,
DatesPatch,
NoteIn,
NotePatch,
RoomIn,
@@ -102,8 +105,11 @@ from app.ops import (
patch_matter,
patch_task,
revoke_grant,
reorder_bindings,
set_content_order,
set_exception,
unlink_matter_case,
update_dates,
update_note,
)
from app.present import (
@@ -112,6 +118,7 @@ from app.present import (
case_detail,
case_summary,
deadline_payload,
course_items,
matter_detail,
matter_summary,
note_update_payload,
@@ -725,6 +732,7 @@ def post_note(body: NoteIn, principal: Principal = Depends(get_principal), conn:
Ctx(body.context.kind, body.context.id),
_ctx_model(body.placement),
body.chapter_id,
body.dates.document_on if body.dates else None,
)
element = created["element"]
return _created(
@@ -1143,8 +1151,127 @@ def search(q: str, principal: Principal = Depends(get_principal), conn: Connecti
return {"items": items[:50], "next_cursor": None}
@router.patch("/api/v1/cases/{case_id}/content-order")
def patch_case_order(
case_id: UUID,
body: ContentOrderIn,
principal: Principal = Depends(get_principal),
conn: Connection = Depends(get_conn),
):
version = set_content_order(
conn, principal.account_id, "case", case_id, body.expected_version, body.mode, body.direction, None
)
return {"version": version, "mode": body.mode, "direction": body.direction}
@router.post("/api/v1/cases/{case_id}/bindings/order")
def post_case_binding_order(
case_id: UUID,
body: BindingOrderIn,
principal: Principal = Depends(get_principal),
conn: Connection = Depends(get_conn),
):
version = reorder_bindings(
conn, principal.account_id, "case", case_id, body.expected_version, body.binding_ids, None
)
return {"version": version}
@router.patch("/api/v1/matters/{matter_id}/content-order")
def patch_matter_order(
matter_id: UUID,
body: ContentOrderIn,
principal: Principal = Depends(get_principal),
conn: Connection = Depends(get_conn),
):
version = set_content_order(
conn,
principal.account_id,
"matter",
matter_id,
body.expected_version,
body.mode,
body.direction,
_ctx_model(body.context),
)
return {"version": version, "mode": body.mode, "direction": body.direction}
@router.post("/api/v1/matters/{matter_id}/bindings/order")
def post_matter_binding_order(
matter_id: UUID,
body: BindingOrderIn,
principal: Principal = Depends(get_principal),
conn: Connection = Depends(get_conn),
):
version = reorder_bindings(
conn,
principal.account_id,
"matter",
matter_id,
body.expected_version,
body.binding_ids,
_ctx_model(body.context),
)
return {"version": version}
@router.patch("/api/v1/elements/{element_id}/dates")
def patch_element_dates(
element_id: UUID,
body: DatesPatch,
principal: Principal = Depends(get_principal),
conn: Connection = Depends(get_conn),
):
element = update_dates(
conn,
principal.account_id,
element_id,
body.expected_version,
body.document_on,
body.sent_on,
body.received_on,
_ctx_model(body.context),
)
return {"id": str(element["id"]), "version": element["version"]}
@router.get("/api/v1/cases/{case_id}/course")
def case_course(case_id: UUID, principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn)):
case, rights = require_case(conn, principal.account_id, case_id, "read")
detail = case_detail(conn, principal.account_id, case, rights)
return {"items": course_items(detail["bindings"]), "next_cursor": None}
@router.get("/api/v1/matters/{matter_id}/course")
def matter_course(
matter_id: UUID,
context_kind: str | None = None,
context_id: str | None = None,
principal: Principal = Depends(get_principal),
conn: Connection = Depends(get_conn),
):
context = parse_query_context(context_kind, context_id)
matter, rights = require_matter(conn, principal.account_id, matter_id, context, "read")
detail = matter_detail(conn, principal.account_id, matter, rights, context)
return {"items": course_items(detail["bindings"]), "next_cursor": None}
@router.get("/api/v1/views/recent-matters")
def recent_matters(principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn)):
rows = [matter for matter, _contexts in list_matters(conn, principal.account_id)]
rows.sort(key=lambda item: (item["updated_at"], str(item["id"])), reverse=True)
items = [matter_summary(conn, principal.account_id, matter) for matter in rows[:8]]
return {"items": items, "next_cursor": None}
@router.get("/api/v1/cases/{case_id}/chronology")
def case_chronology(case_id: UUID, principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn)):
def case_chronology(
case_id: UUID,
cursor: str | None = None,
principal: Principal = Depends(get_principal),
conn: Connection = Depends(get_conn),
):
case, _rights = require_case(conn, principal.account_id, case_id, "read")
page = Ctx("case", case["id"])
rows = conn.execute(
@@ -1166,7 +1293,7 @@ def case_chronology(case_id: UUID, principal: Principal = Depends(get_principal)
""",
{"case": case_id},
).fetchall()
return {"items": _visible_events(conn, principal.account_id, rows, page)}
return _event_page(_visible_events(conn, principal.account_id, rows, page), cursor)
@router.get("/api/v1/matters/{matter_id}/chronology")
@@ -1174,6 +1301,7 @@ def matter_chronology(
matter_id: UUID,
context_kind: str | None = None,
context_id: str | None = None,
cursor: str | None = None,
principal: Principal = Depends(get_principal),
conn: Connection = Depends(get_conn),
):
@@ -1198,7 +1326,7 @@ def matter_chronology(
""",
{"matter": matter_id},
).fetchall()
return {"items": _visible_events(conn, principal.account_id, rows, page)}
return _event_page(_visible_events(conn, principal.account_id, rows, page), cursor)
def _parse_date(value: str | None) -> date | None:
@@ -1237,18 +1365,41 @@ def _case_contexts(conn, actor, matter) -> list[dict]:
return [item for item in readable_matter_contexts(conn, actor, matter) if item["kind"] == "case"]
def _event_page(items: list[dict], cursor: str | None, limit: int = 40) -> dict:
start = 0
if cursor:
ids = [item["id"] for item in items]
if cursor not in ids:
return {"items": [], "next_cursor": None}
start = ids.index(cursor) + 1
chunk = items[start:start + limit]
next_cursor = chunk[-1]["id"] if chunk and start + limit < len(items) else None
return {"items": chunk, "next_cursor": next_cursor}
def _visible_events(conn, actor: UUID, rows: list[dict], page: Ctx) -> list[dict]:
names: dict = {}
items = []
for event in rows:
title = _event_title(conn, actor, event, page)
if title is None:
continue
actor_id = event["actor_subject_id"]
if actor_id not in names:
row = None
if actor_id is not None:
row = conn.execute(
"SELECT display_name FROM auth_accounts WHERE id = %s",
(actor_id,),
).fetchone()
names[actor_id] = row["display_name"] if row else None
items.append(
{
"id": str(event["id"]),
"action": event["action"],
"label": LABELS.get(event["action"], "Ereignis"),
"title": title,
"actor_display_name": names[actor_id],
"occurred_on": event["occurred_on"].isoformat() if event["occurred_on"] else None,
"occurred_at": event["occurred_at"].astimezone(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ"),
"resource_kind": event["resource_kind"],
+22
View File
@@ -132,6 +132,28 @@ class NoteIn(APIModel):
context: ContextIn
placement: ContextIn | None = None
chapter_id: UUID | None = None
dates: DatesIn | None = None
class ContentOrderIn(APIModel):
expected_version: int = Field(ge=1)
mode: Literal["date", "position"]
direction: Literal["asc", "desc"] = "asc"
context: ContextIn | None = None
class BindingOrderIn(APIModel):
expected_version: int = Field(ge=1)
binding_ids: list[UUID] = Field(min_length=0, max_length=500)
context: ContextIn | None = None
class DatesPatch(APIModel):
expected_version: int = Field(ge=1)
document_on: date | None = None
sent_on: date | None = None
received_on: date | None = None
context: ContextIn | None = None
class NotePatch(APIModel):
+125 -9
View File
@@ -76,9 +76,10 @@ def create_case(conn, actor: UUID, scope_id: UUID, title: str, parent_case_id: U
row = conn.execute(
"""
INSERT INTO cases (
home_scope_id, owner_subject_id, created_by, title, rights_mode
home_scope_id, owner_subject_id, created_by, title, rights_mode,
content_order, content_order_dir
)
VALUES (%s, %s, %s, %s, 'explicit')
VALUES (%s, %s, %s, %s, 'explicit', 'date', 'asc')
RETURNING *
""",
(scope_id, actor, actor, title),
@@ -92,9 +93,10 @@ def create_case(conn, actor: UUID, scope_id: UUID, title: str, parent_case_id: U
row = conn.execute(
"""
INSERT INTO cases (
home_scope_id, owner_subject_id, created_by, title, parent_case_id, rights_mode
home_scope_id, owner_subject_id, created_by, title, parent_case_id, rights_mode,
content_order, content_order_dir
)
VALUES (%s, %s, %s, %s, %s, %s)
VALUES (%s, %s, %s, %s, %s, %s, 'date', 'asc')
RETURNING *
""",
(parent["home_scope_id"], actor, actor, title, parent["id"], rights_mode),
@@ -156,8 +158,10 @@ def create_matter(conn, actor: UUID, scope_id: UUID, title: str, parent_matter_i
if parent_matter_id is None:
row = conn.execute(
"""
INSERT INTO matters (home_scope_id, owner_subject_id, created_by, title)
VALUES (%s, %s, %s, %s) RETURNING *
INSERT INTO matters (
home_scope_id, owner_subject_id, created_by, title, content_order, content_order_dir
)
VALUES (%s, %s, %s, %s, 'date', 'asc') RETURNING *
""",
(scope_id, actor, actor, title),
).fetchone()
@@ -168,9 +172,10 @@ def create_matter(conn, actor: UUID, scope_id: UUID, title: str, parent_matter_i
row = conn.execute(
"""
INSERT INTO matters (
home_scope_id, owner_subject_id, created_by, title, parent_matter_id
home_scope_id, owner_subject_id, created_by, title, parent_matter_id,
content_order, content_order_dir
)
VALUES (%s, %s, %s, %s, %s) RETURNING *
VALUES (%s, %s, %s, %s, %s, 'date', 'asc') RETURNING *
""",
(parent["home_scope_id"], actor, actor, title, parent["id"]),
).fetchone()
@@ -440,7 +445,17 @@ def record_dates(conn, actor: UUID, scope_id: UUID, element_id: UUID, context: C
audit(conn, actor, scope_id, "correspondence_recorded", "element", element_id, context)
def create_note(conn, actor: UUID, scope_id: UUID, title: str, document: dict, context: Ctx, placement: Ctx | None, chapter_id: UUID | None) -> dict:
def create_note(
conn,
actor: UUID,
scope_id: UUID,
title: str,
document: dict,
context: Ctx,
placement: Ctx | None,
chapter_id: UUID | None,
document_on: date | None = None,
) -> dict:
document = clean_document(document)
target, rights_context = _placement(context, placement)
require_container_edit(conn, actor, target, rights_context)
@@ -451,6 +466,8 @@ def create_note(conn, actor: UUID, scope_id: UUID, title: str, document: dict, c
)
binding = insert_binding(conn, element["id"], target.kind, target.id, chapter_id, actor)
audit(conn, actor, scope_id, "note_created", "element", element["id"], rights_context or target)
if document_on is not None:
record_dates(conn, actor, scope_id, element["id"], rights_context or target, document_on, None, None)
return {"element": element, "binding": binding, "document": document}
@@ -782,3 +799,102 @@ def link_task_deadline(conn, actor: UUID, task_id: UUID, deadline_id: UUID, cont
""",
(task_id, deadline_id, actor),
)
def set_content_order(
conn,
actor: UUID,
kind: str,
container_id: UUID,
expected: int,
mode: str,
direction: str,
context: Ctx | None,
) -> int:
if kind == "case":
container, rights = require_case(conn, actor, container_id, "edit")
table = "cases"
else:
container, rights = require_matter(conn, actor, container_id, context, "edit")
table = "matters"
if not rights.edit:
raise forbidden()
version = bump_version(conn, table, container_id, expected)
conn.execute(
f"UPDATE {table} SET content_order = %s, content_order_dir = %s WHERE id = %s",
(mode, direction, container_id),
)
audit(conn, actor, container["home_scope_id"], "content_order", kind, container_id, context)
return version
def reorder_bindings(
conn,
actor: UUID,
kind: str,
container_id: UUID,
expected: int,
binding_ids: list[UUID],
context: Ctx | None,
) -> int:
if kind == "case":
container, rights = require_case(conn, actor, container_id, "edit")
table = "cases"
page = Ctx("case", container_id)
else:
container, rights = require_matter(conn, actor, container_id, context, "edit")
table = "matters"
page = context or Ctx("matter", container_id)
if not rights.edit:
raise forbidden()
version = bump_version(conn, table, container_id, expected)
rows = conn.execute(
"""
SELECT * FROM bindings
WHERE context_kind = %s AND context_id = %s
ORDER BY position, id
""",
(kind, container_id),
).fetchall()
visible_ids = []
for row in rows:
element = get_element(conn, row["element_id"])
if element is not None and element_rights(conn, actor, element, page).read:
visible_ids.append(row["id"])
submitted = list(binding_ids)
if len(submitted) != len(set(submitted)):
raise unprocessable("Dieselbe Einbindung steht mehrfach in der Ordnung.")
if any(item not in set(visible_ids) for item in submitted):
raise not_found()
if set(submitted) != set(visible_ids):
raise unprocessable("Die Ordnung ist unvollständig. Bitte neu laden.")
visible_set = set(visible_ids)
coming = iter(submitted)
sequence = []
for row in rows:
if row["id"] in visible_set:
sequence.append(next(coming))
else:
sequence.append(row["id"])
for index, binding_id in enumerate(sequence):
conn.execute("UPDATE bindings SET position = %s WHERE id = %s", (index, binding_id))
audit(conn, actor, container["home_scope_id"], "content_reordered", kind, container_id, context)
return version
def update_dates(
conn,
actor: UUID,
element_id: UUID,
expected: int,
document_on: date | None,
sent_on: date | None,
received_on: date | None,
context: Ctx | None,
) -> dict:
element, rights = require_element(conn, actor, element_id, context, "edit")
if not rights.edit:
raise forbidden()
bump_version(conn, "elements", element_id, expected)
record_dates(conn, actor, element["home_scope_id"], element_id, context or Ctx("matter", element_id), document_on, sent_on, received_on)
return get_element(conn, element_id)
+25
View File
@@ -0,0 +1,25 @@
"""Display order for bindings. Stored positions stay intact when sorting by date."""
from __future__ import annotations
def arrange(items: list[dict], mode: str, direction: str) -> list[dict]:
"""Return a new list. Undated entries stay last in both date directions."""
if mode == "position":
ordered = sorted(items, key=lambda item: (item["_raw_position"], item["id"]))
else:
dated = [item for item in items if item.get("_document_on")]
undated = [item for item in items if not item.get("_document_on")]
if direction == "desc":
dated.sort(key=lambda item: (item["_raw_position"], item["id"]))
dated.sort(key=lambda item: item["_document_on"], reverse=True)
else:
dated.sort(key=lambda item: (item["_document_on"], item["_raw_position"], item["id"]))
undated.sort(key=lambda item: (item["_raw_position"], item["id"]))
ordered = dated + undated
result = []
for index, item in enumerate(ordered):
visible = {key: value for key, value in item.items() if not key.startswith("_")}
visible["position"] = index
result.append(visible)
return result
+127 -22
View File
@@ -22,6 +22,7 @@ from app.access.policy import (
)
from app.access.policy import SHARED_NOTICE
from app.core.timeutil import dump_date, dump_dt
from app.order import arrange
def rights_dict(rights) -> dict:
@@ -179,6 +180,80 @@ def _grants(conn, kind: str, resource_id: UUID) -> list[dict]:
]
def _ordered_bindings(conn, actor: UUID, kind: str, container_id: UUID, context: Ctx, mode: str, direction: str) -> list[dict]:
rows = conn.execute(
"""
SELECT * FROM bindings
WHERE context_kind = %s AND context_id = %s
ORDER BY position, id
""",
(kind, container_id),
).fetchall()
visible = []
for binding in rows:
item = binding_payload(conn, actor, binding, context)
if item is None:
continue
item["_raw_position"] = binding["position"]
item["_document_on"] = item["element"]["dates"]["document_on"]
visible.append(item)
return arrange(visible, mode or "position", direction or "asc")
def course_items(bindings: list[dict]) -> list[dict]:
"""One fachlicher Eintrag je Unterlage. Das Erfassungsdatum ist kein Belegdatum."""
items = []
for binding in bindings:
dates = binding["element"]["dates"]
items.append(
{
"id": binding["element"]["id"],
"binding_id": binding["id"],
"title": binding["element"]["title"],
"kind": binding["element"]["kind"],
"document_on": dates["document_on"],
"sent_on": dates["sent_on"],
"received_on": dates["received_on"],
"dated": dates["document_on"] is not None,
}
)
dated = [item for item in items if item["dated"]]
undated = [item for item in items if not item["dated"]]
dated.sort(key=lambda item: (item["document_on"], item["id"]))
undated.sort(key=lambda item: item["id"])
return dated + undated
def case_trail(conn, actor: UUID, case: dict) -> list[dict]:
trail = [{"label": "Akten", "href": "/cases"}]
if case["parent_case_id"]:
parent = get_case(conn, case["parent_case_id"])
if parent is not None and case_rights(conn, actor, parent).read:
trail.append({"label": parent["title"], "href": f"/cases/{parent['id']}"})
trail.append({"label": case["title"], "href": None})
return trail
def matter_trail(conn, actor: UUID, matter: dict, context: Ctx | None) -> list[dict]:
if context is not None and context.kind == "case":
case = get_case(conn, context.id)
if case is not None and case_rights(conn, actor, case).read:
trail = case_trail(conn, actor, case)
trail[-1]["href"] = f"/cases/{case['id']}"
trail.append({"label": matter["title"], "href": None})
return trail
trail = [{"label": "Vorgänge", "href": "/matters"}]
if matter["parent_matter_id"]:
parent = get_matter(conn, matter["parent_matter_id"])
if parent is not None and matter_rights(conn, actor, parent, context).read:
href = f"/matters/{parent['id']}"
if context is not None and context.kind == "case":
href += f"?context_kind=case&context_id={context.id}"
trail.append({"label": parent["title"], "href": href})
trail.append({"label": matter["title"], "href": None})
return trail
def case_summary(case: dict, rights) -> dict:
return {
"id": str(case["id"]),
@@ -194,6 +269,10 @@ def case_summary(case: dict, rights) -> dict:
def case_detail(conn, actor: UUID, case: dict, rights) -> dict:
payload = case_summary(case, rights)
payload["content_order"] = case.get("content_order") or "position"
payload["content_order_dir"] = case.get("content_order_dir") or "asc"
payload["trail"] = case_trail(conn, actor, case)
payload["kind_label"] = "Unterakte" if case["parent_case_id"] else "Akte"
payload["home_scope_name"] = room_name(conn, case["home_scope_id"])
payload["is_owner"] = case["owner_subject_id"] == actor
if case["parent_case_id"]:
@@ -238,19 +317,43 @@ def case_detail(conn, actor: UUID, case: dict, rights) -> dict:
)
payload["matters"] = matters
context = Ctx("case", case["id"])
bindings = []
for binding in conn.execute(
payload["bindings"] = _ordered_bindings(
conn, actor, "case", case["id"], context, payload["content_order"], payload["content_order_dir"]
)
contained = []
for link in conn.execute(
"""
SELECT * FROM bindings
WHERE context_kind = 'case' AND context_id = %s
ORDER BY position, created_at
SELECT m.*
FROM matter_case_links l
JOIN matters m ON m.id = l.matter_id
WHERE l.case_id = %s AND m.deleted_at IS NULL
ORDER BY m.title, m.id
""",
(case["id"],),
).fetchall():
item = binding_payload(conn, actor, binding, context)
if item is not None:
bindings.append(item)
payload["bindings"] = bindings
if not matter_rights(conn, actor, link, context).read:
continue
items = _ordered_bindings(
conn,
actor,
"matter",
link["id"],
context,
link.get("content_order") or "position",
link.get("content_order_dir") or "asc",
)
if not items:
continue
contained.append(
{
"matter_id": str(link["id"]),
"title": link["title"],
"content_order": link.get("content_order") or "position",
"content_order_dir": link.get("content_order_dir") or "asc",
"bindings": items,
}
)
payload["contained"] = contained
if rights.share or case["owner_subject_id"] == actor:
payload["grants"] = _grants(conn, "case", case["id"])
return payload
@@ -295,6 +398,10 @@ def matter_detail(conn, actor: UUID, matter: dict, rights, context: Ctx | None)
"home_scope_id": str(matter["home_scope_id"]),
"home_scope_name": room_name(conn, matter["home_scope_id"]),
"is_owner": matter["owner_subject_id"] == actor,
"content_order": matter.get("content_order") or "position",
"content_order_dir": matter.get("content_order_dir") or "asc",
"trail": matter_trail(conn, actor, matter, context),
"kind_label": "Teilvorgang" if matter["parent_matter_id"] else "Vorgang",
"rights": rights.as_dict(),
"context": None if context is None else context.as_dict(),
"chapters": _chapters(conn, "matter", matter["id"]),
@@ -336,19 +443,15 @@ def matter_detail(conn, actor: UUID, matter: dict, rights, context: Ctx | None)
if title is not None:
crosses.append({"id": str(link["id"]), "matter_id": str(link["to_matter_id"]), "title": title})
payload["cross_links"] = crosses
bindings = []
for binding in conn.execute(
"""
SELECT * FROM bindings
WHERE context_kind = 'matter' AND context_id = %s
ORDER BY position, created_at
""",
(matter["id"],),
).fetchall():
item = binding_payload(conn, actor, binding, page)
if item is not None:
bindings.append(item)
payload["bindings"] = bindings
payload["bindings"] = _ordered_bindings(
conn,
actor,
"matter",
matter["id"],
page,
payload["content_order"],
payload["content_order_dir"],
)
payload["tasks"] = []
for task in conn.execute(
"""
@@ -497,4 +600,6 @@ LABELS = {
"correspondence_received": "Zugang",
"correspondence_recorded": "Erfassung",
"chapter_created": "Kapitel angelegt",
"content_order": "Ordnungsart geändert",
"content_reordered": "Eigene Reihenfolge geändert",
}