Zeige im Breadcrumb die Aktenstruktur und lass Kapitel samt Inhalt an ihren Ort ziehen.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
cafa4e8b92
commit
7ef73ad500
12 files changed
+627
-93
No files matched your search
@@ -42,7 +42,9 @@ from app.api.schemas import (
|
||||
CaseIn,
|
||||
CaseLinkIn,
|
||||
CasePatch,
|
||||
ChapterDateIn,
|
||||
ChapterIn,
|
||||
ChapterOrderIn,
|
||||
CrossLinkIn,
|
||||
DeadlineIn,
|
||||
DeadlinePatch,
|
||||
@@ -79,7 +81,7 @@ from app.auth.security import (
|
||||
verify_password,
|
||||
)
|
||||
from app.core.errors import Problem, forbidden, not_found, too_many, unprocessable
|
||||
from app.core.timeutil import dump_dt, utcnow
|
||||
from app.core.timeutil import dump_date, dump_dt, utcnow
|
||||
from app.media.storage import content_disposition, read_bytes
|
||||
from app.media.storage import asset_path
|
||||
from app.ops import (
|
||||
@@ -109,6 +111,8 @@ from app.ops import (
|
||||
revoke_grant,
|
||||
move_binding_chapter,
|
||||
reorder_bindings,
|
||||
reorder_chapters,
|
||||
set_chapter_date,
|
||||
set_content_order,
|
||||
set_exception,
|
||||
unlink_matter_case,
|
||||
@@ -565,8 +569,22 @@ def post_case_chapter(
|
||||
principal: Principal = Depends(get_principal),
|
||||
conn: Connection = Depends(get_conn),
|
||||
):
|
||||
row = create_chapter(conn, principal.account_id, "case", case_id, body.title, body.position, None)
|
||||
return _created(f"/api/v1/cases/{case_id}/chapters/{row['id']}", {"id": str(row["id"]), "title": row["title"], "position": row["position"]})
|
||||
row = create_chapter(conn, principal.account_id, "case", case_id, body.title, body.position, None, body.document_on)
|
||||
return _created(
|
||||
f"/api/v1/cases/{case_id}/chapters/{row['id']}",
|
||||
{"id": str(row["id"]), "title": row["title"], "position": row["position"], "document_on": dump_date(row["document_on"])},
|
||||
)
|
||||
|
||||
|
||||
@router.post("/api/v1/cases/{case_id}/chapters/order")
|
||||
def post_case_chapter_order(
|
||||
case_id: UUID,
|
||||
body: ChapterOrderIn,
|
||||
principal: Principal = Depends(get_principal),
|
||||
conn: Connection = Depends(get_conn),
|
||||
):
|
||||
version = reorder_chapters(conn, principal.account_id, "case", case_id, body.expected_version, body.chapter_ids, None)
|
||||
return {"version": version}
|
||||
|
||||
|
||||
@router.post("/api/v1/cases/{case_id}/grants")
|
||||
@@ -640,13 +658,41 @@ def post_matter_chapter(
|
||||
conn: Connection = Depends(get_conn),
|
||||
):
|
||||
context = parse_query_context(context_kind, context_id)
|
||||
row = create_chapter(conn, principal.account_id, "matter", matter_id, body.title, body.position, context)
|
||||
row = create_chapter(
|
||||
conn, principal.account_id, "matter", matter_id, body.title, body.position, context, body.document_on
|
||||
)
|
||||
return _created(
|
||||
f"/api/v1/matters/{matter_id}/chapters/{row['id']}",
|
||||
{"id": str(row["id"]), "title": row["title"], "position": row["position"]},
|
||||
{"id": str(row["id"]), "title": row["title"], "position": row["position"], "document_on": dump_date(row["document_on"])},
|
||||
)
|
||||
|
||||
|
||||
@router.post("/api/v1/matters/{matter_id}/chapters/order")
|
||||
def post_matter_chapter_order(
|
||||
matter_id: UUID,
|
||||
body: ChapterOrderIn,
|
||||
principal: Principal = Depends(get_principal),
|
||||
conn: Connection = Depends(get_conn),
|
||||
):
|
||||
version = reorder_chapters(
|
||||
conn, principal.account_id, "matter", matter_id, body.expected_version, body.chapter_ids, _ctx_model(body.context)
|
||||
)
|
||||
return {"version": version}
|
||||
|
||||
|
||||
@router.patch("/api/v1/chapters/{chapter_id}")
|
||||
def patch_chapter_date(
|
||||
chapter_id: UUID,
|
||||
body: ChapterDateIn,
|
||||
principal: Principal = Depends(get_principal),
|
||||
conn: Connection = Depends(get_conn),
|
||||
):
|
||||
version = set_chapter_date(
|
||||
conn, principal.account_id, chapter_id, body.expected_version, body.document_on, _ctx_model(body.context)
|
||||
)
|
||||
return {"version": version, "document_on": dump_date(body.document_on)}
|
||||
|
||||
|
||||
@router.post("/api/v1/matters/{matter_id}/case-links")
|
||||
def post_case_link(
|
||||
matter_id: UUID,
|
||||
|
||||
@@ -78,6 +78,19 @@ class CasePatch(APIModel):
|
||||
class ChapterIn(APIModel):
|
||||
title: str = Field(min_length=1, max_length=200)
|
||||
position: int | None = Field(default=None, ge=0, le=10000)
|
||||
document_on: date | None = None
|
||||
|
||||
|
||||
class ChapterOrderIn(APIModel):
|
||||
expected_version: int = Field(ge=1)
|
||||
chapter_ids: list[UUID] = Field(min_length=0, max_length=200)
|
||||
context: ContextIn | None = None
|
||||
|
||||
|
||||
class ChapterDateIn(APIModel):
|
||||
expected_version: int = Field(ge=1)
|
||||
document_on: date | None = None
|
||||
context: ContextIn | None = None
|
||||
|
||||
|
||||
class MatterIn(APIModel):
|
||||
|
||||
+81
-7
@@ -121,7 +121,16 @@ def patch_case(conn, actor: UUID, case_id: UUID, expected: int, title: str | Non
|
||||
return updated
|
||||
|
||||
|
||||
def create_chapter(conn, actor: UUID, kind: str, container_id: UUID, title: str, position: int | None, context: Ctx | None) -> dict:
|
||||
def create_chapter(
|
||||
conn,
|
||||
actor: UUID,
|
||||
kind: str,
|
||||
container_id: UUID,
|
||||
title: str,
|
||||
position: int | None,
|
||||
context: Ctx | None,
|
||||
document_on: date | None = None,
|
||||
) -> dict:
|
||||
if kind == "case":
|
||||
container, _rights = require_case(conn, actor, container_id, "edit")
|
||||
else:
|
||||
@@ -136,23 +145,88 @@ def create_chapter(conn, actor: UUID, kind: str, container_id: UUID, title: str,
|
||||
if kind == "case":
|
||||
row = conn.execute(
|
||||
"""
|
||||
INSERT INTO chapters (home_scope_id, case_id, title, position)
|
||||
VALUES (%s, %s, %s, %s) RETURNING *
|
||||
INSERT INTO chapters (home_scope_id, case_id, title, position, document_on)
|
||||
VALUES (%s, %s, %s, %s, %s) RETURNING *
|
||||
""",
|
||||
(container["home_scope_id"], container_id, title.strip(), position),
|
||||
(container["home_scope_id"], container_id, title.strip(), position, document_on),
|
||||
).fetchone()
|
||||
else:
|
||||
row = conn.execute(
|
||||
"""
|
||||
INSERT INTO chapters (home_scope_id, matter_id, title, position)
|
||||
VALUES (%s, %s, %s, %s) RETURNING *
|
||||
INSERT INTO chapters (home_scope_id, matter_id, title, position, document_on)
|
||||
VALUES (%s, %s, %s, %s, %s) RETURNING *
|
||||
""",
|
||||
(container["home_scope_id"], container_id, title.strip(), position),
|
||||
(container["home_scope_id"], container_id, title.strip(), position, document_on),
|
||||
).fetchone()
|
||||
audit(conn, actor, container["home_scope_id"], "chapter_created", kind, container_id, context)
|
||||
return row
|
||||
|
||||
|
||||
def reorder_chapters(
|
||||
conn,
|
||||
actor: UUID,
|
||||
kind: str,
|
||||
container_id: UUID,
|
||||
expected: int,
|
||||
chapter_ids: list[UUID],
|
||||
context: Ctx | None,
|
||||
) -> int:
|
||||
if kind == "case":
|
||||
container, rights = require_case(conn, actor, container_id, "edit")
|
||||
table = "cases"
|
||||
column = "case_id"
|
||||
else:
|
||||
container, rights = require_matter(conn, actor, container_id, context, "edit")
|
||||
table = "matters"
|
||||
column = "matter_id"
|
||||
if not rights.edit:
|
||||
raise forbidden()
|
||||
version = bump_version(conn, table, container_id, expected)
|
||||
rows = conn.execute(f"SELECT id FROM chapters WHERE {column} = %s", (container_id,)).fetchall()
|
||||
existing = [row["id"] for row in rows]
|
||||
if len(chapter_ids) != len(set(chapter_ids)):
|
||||
raise unprocessable("Dasselbe Kapitel steht mehrfach in der Ordnung.")
|
||||
if any(item not in set(existing) for item in chapter_ids):
|
||||
raise not_found()
|
||||
if set(chapter_ids) != set(existing):
|
||||
raise unprocessable("Die Kapitelordnung ist unvollständig. Bitte neu laden.")
|
||||
for index, chapter_id in enumerate(chapter_ids):
|
||||
conn.execute("UPDATE chapters SET position = %s WHERE id = %s", (index, chapter_id))
|
||||
audit(conn, actor, container["home_scope_id"], "chapters_reordered", kind, container_id, context)
|
||||
return version
|
||||
|
||||
|
||||
def set_chapter_date(
|
||||
conn,
|
||||
actor: UUID,
|
||||
chapter_id: UUID,
|
||||
expected: int,
|
||||
document_on: date | None,
|
||||
context: Ctx | None,
|
||||
) -> int:
|
||||
row = conn.execute("SELECT * FROM chapters WHERE id = %s", (chapter_id,)).fetchone()
|
||||
if row is None:
|
||||
raise not_found()
|
||||
if row["case_id"] is not None:
|
||||
container, rights = require_case(conn, actor, row["case_id"], "edit")
|
||||
table = "cases"
|
||||
container_id = row["case_id"]
|
||||
kind = "case"
|
||||
page = None
|
||||
else:
|
||||
container, rights = require_matter(conn, actor, row["matter_id"], context, "edit")
|
||||
table = "matters"
|
||||
container_id = row["matter_id"]
|
||||
kind = "matter"
|
||||
page = context
|
||||
if not rights.edit:
|
||||
raise forbidden()
|
||||
version = bump_version(conn, table, container_id, expected)
|
||||
conn.execute("UPDATE chapters SET document_on = %s WHERE id = %s", (document_on, chapter_id))
|
||||
audit(conn, actor, container["home_scope_id"], "chapter_dated", kind, container_id, page)
|
||||
return version
|
||||
|
||||
|
||||
def create_matter(conn, actor: UUID, scope_id: UUID, title: str, parent_matter_id: UUID | None, context: Ctx | None) -> dict:
|
||||
title = title.strip()
|
||||
if parent_matter_id is None:
|
||||
|
||||
+64
-8
@@ -29,13 +29,31 @@ def rights_dict(rights) -> dict:
|
||||
return rights.as_dict()
|
||||
|
||||
|
||||
def _chapters(conn, kind: str, container_id: UUID) -> list[dict]:
|
||||
def _chapters(conn, kind: str, container_id: UUID, mode: str = "position", direction: str = "asc") -> list[dict]:
|
||||
column = "case_id" if kind == "case" else "matter_id"
|
||||
rows = conn.execute(
|
||||
f"SELECT id, title, position FROM chapters WHERE {column} = %s ORDER BY position, created_at",
|
||||
f"SELECT id, title, position, document_on FROM chapters WHERE {column} = %s",
|
||||
(container_id,),
|
||||
).fetchall()
|
||||
return [{"id": str(row["id"]), "title": row["title"], "position": row["position"]} for row in rows]
|
||||
items = [
|
||||
{
|
||||
"id": str(row["id"]),
|
||||
"title": row["title"],
|
||||
"position": row["position"],
|
||||
"document_on": dump_date(row["document_on"]),
|
||||
}
|
||||
for row in rows
|
||||
]
|
||||
if mode == "date":
|
||||
dated = [item for item in items if item["document_on"]]
|
||||
undated = [item for item in items if not item["document_on"]]
|
||||
dated.sort(key=lambda item: (item["document_on"], item["position"], item["id"]))
|
||||
undated.sort(key=lambda item: (item["position"], item["id"]))
|
||||
if direction == "desc":
|
||||
dated.reverse()
|
||||
return dated + undated
|
||||
items.sort(key=lambda item: (item["position"], item["id"]))
|
||||
return items
|
||||
|
||||
|
||||
def _annotations(conn, binding_id: UUID) -> list[dict]:
|
||||
@@ -296,9 +314,31 @@ def case_trail(conn, actor: UUID, case: dict) -> list[dict]:
|
||||
return trail
|
||||
|
||||
|
||||
def _structural_case(conn, actor: UUID, matter: dict, context: Ctx | None) -> dict | None:
|
||||
"""The case that places this matter in the structure. Several placements do not invent a main case."""
|
||||
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:
|
||||
return case
|
||||
return None
|
||||
placements = matter_placements(conn, actor, matter["id"], None)
|
||||
if len(placements) != 1:
|
||||
return None
|
||||
case = get_case(conn, UUID(placements[0]["case_id"]))
|
||||
if case is None or not case_rights(conn, actor, case).read:
|
||||
return None
|
||||
return case
|
||||
|
||||
|
||||
def matter_trail(conn, actor: UUID, matter: dict, context: Ctx | None) -> list[dict]:
|
||||
trail = [{"label": "Vorgänge", "href": "/matters"}]
|
||||
_append_matter_steps(conn, actor, trail, matter, context)
|
||||
case = _structural_case(conn, actor, matter, context)
|
||||
if case is None:
|
||||
trail = [{"label": "Vorgänge", "href": "/matters"}]
|
||||
_append_matter_steps(conn, actor, trail, matter, None)
|
||||
return trail
|
||||
trail = case_trail(conn, actor, case)
|
||||
trail[-1]["href"] = f"/cases/{case['id']}"
|
||||
_append_matter_steps(conn, actor, trail, matter, Ctx("case", case["id"]))
|
||||
return trail
|
||||
|
||||
|
||||
@@ -369,7 +409,13 @@ def _contained_group(conn, actor: UUID, matter: dict, context: Ctx) -> dict | No
|
||||
"parent_title": matter_title_if_readable(conn, actor, matter["parent_matter_id"]),
|
||||
"version": matter["version"],
|
||||
"rights": matter_right.as_dict(),
|
||||
"chapters": _chapters(conn, "matter", matter["id"]),
|
||||
"chapters": _chapters(
|
||||
conn,
|
||||
"matter",
|
||||
matter["id"],
|
||||
matter.get("content_order") or "position",
|
||||
matter.get("content_order_dir") or "asc",
|
||||
),
|
||||
"content_order": matter.get("content_order") or "position",
|
||||
"content_order_dir": matter.get("content_order_dir") or "asc",
|
||||
"bindings": _ordered_bindings(
|
||||
@@ -452,7 +498,9 @@ def case_detail(conn, actor: UUID, case: dict, rights) -> dict:
|
||||
parent = get_case(conn, case["parent_case_id"])
|
||||
if parent is not None and case_rights(conn, actor, parent).read:
|
||||
payload["parent_title"] = parent["title"]
|
||||
payload["chapters"] = _chapters(conn, "case", case["id"])
|
||||
payload["chapters"] = _chapters(
|
||||
conn, "case", case["id"], case.get("content_order") or "position", case.get("content_order_dir") or "asc"
|
||||
)
|
||||
subcases = []
|
||||
for row in conn.execute(
|
||||
"""
|
||||
@@ -586,7 +634,13 @@ def matter_detail(conn, actor: UUID, matter: dict, rights, context: Ctx | None)
|
||||
"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"]),
|
||||
"chapters": _chapters(
|
||||
conn,
|
||||
"matter",
|
||||
matter["id"],
|
||||
matter.get("content_order") or "position",
|
||||
matter.get("content_order_dir") or "asc",
|
||||
),
|
||||
}
|
||||
submatters = []
|
||||
for child in conn.execute(
|
||||
@@ -791,6 +845,8 @@ LABELS = {
|
||||
"chapter_created": "Kapitel angelegt",
|
||||
"summary_changed": "Kurzbeschreibung geändert",
|
||||
"binding_chapter": "In ein Kapitel eingeordnet",
|
||||
"chapters_reordered": "Kapitelreihenfolge geändert",
|
||||
"chapter_dated": "Kapiteldatum geändert",
|
||||
"content_order": "Ordnungsart geändert",
|
||||
"content_reordered": "Eigene Reihenfolge geändert",
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
ALTER TABLE chapters
|
||||
ADD COLUMN document_on date;
|
||||
@@ -359,14 +359,15 @@ def test_direct_matter_trail_invents_no_case(client, db):
|
||||
ben.login()
|
||||
opened = ben.request("GET", f"/api/v1/matters/{matter['id']}?context_kind=case&context_id={child['id']}")
|
||||
labels = [item["label"] for item in opened["trail"]]
|
||||
assert labels == ["Vorgänge", "Vorgang Nebenkosten"]
|
||||
assert labels == ["Akten", "Unterakte Wohnung", "Vorgang Nebenkosten"]
|
||||
assert opened["trail"][-1]["href"] is None
|
||||
assert opened["context"]["id"] == child["id"]
|
||||
assert [item["label"] for item in opened["placements"]] == ["Unterakte Wohnung"]
|
||||
assert opened["placements"][0]["current"] is True
|
||||
assert all(item["title"] != "Geheim" for item in opened["case_links"])
|
||||
ada.login()
|
||||
direct = ada.request("GET", f"/api/v1/matters/{matter['id']}")
|
||||
assert [item["label"] for item in direct["trail"]] == labels
|
||||
assert [item["label"] for item in direct["trail"]] == ["Vorgänge", "Vorgang Nebenkosten"]
|
||||
assert "Geheim" not in " ".join(item["label"] for item in opened["placements"])
|
||||
assert any(item["label"] == "Akte Geheim" for item in direct["placements"])
|
||||
|
||||
@@ -387,8 +388,15 @@ def test_teilvorgang_keeps_its_parent_in_the_trail(client, db):
|
||||
case = case_of(ada, "Sammelakte")
|
||||
ada.request("POST", f"/api/v1/matters/{parent['id']}/case-links", json={"case_id": case["id"]}, status=201)
|
||||
opened = ada.request("GET", f"/api/v1/matters/{child['id']}?context_kind=case&context_id={case['id']}")
|
||||
assert [item["label"] for item in opened["trail"]] == [item["label"] for item in direct["trail"]]
|
||||
assert opened["trail"][1]["href"].endswith(f"context_id={case['id']}")
|
||||
assert [item["label"] for item in opened["trail"]] == [
|
||||
"Akten",
|
||||
"Akte Sammelakte",
|
||||
"Vorgang Hauptvorgang",
|
||||
"Teilvorgang Prüfung",
|
||||
]
|
||||
assert opened["trail"][2]["href"].endswith(f"context_id={case['id']}")
|
||||
placed = ada.request("GET", f"/api/v1/matters/{child['id']}")
|
||||
assert [item["label"] for item in placed["trail"]] == [item["label"] for item in opened["trail"]]
|
||||
assert opened["placements"][0]["label"] == "Akte Sammelakte"
|
||||
assert opened["placements"][0]["current"] is True
|
||||
listed = ada.request("GET", "/api/v1/matters")
|
||||
@@ -551,3 +559,57 @@ def test_summary_and_preview_follow_the_read_right(client, db):
|
||||
ben.login()
|
||||
ben.request("GET", f"/api/v1/matters/{matter['id']}", status=404)
|
||||
ben.request("GET", f"/api/v1/files/{by_title['Scan']['id']}/content", status=404)
|
||||
|
||||
|
||||
def test_chapters_move_with_their_entries_and_follow_their_date(client, db):
|
||||
ada = Actor(client, "ada-kapitel-ordnung@example.test", "Ada Kapitel")
|
||||
ben = Actor(client, "ben-kapitel-ordnung@example.test", "Ben Kapitel")
|
||||
register(db, ada.email, ada.name)
|
||||
register(db, ben.email, ben.name)
|
||||
ada.login()
|
||||
matter = matter_of(ada, "Nebenkosten")
|
||||
first = ada.request("POST", f"/api/v1/matters/{matter['id']}/chapters", json={"title": "Abrechnung", "document_on": "2025-03-01"}, status=201)
|
||||
second = ada.request("POST", f"/api/v1/matters/{matter['id']}/chapters", json={"title": "Schriftverkehr", "document_on": "2025-01-15"}, status=201)
|
||||
_note(ada, "Schreiben", matter, document_on="2025-06-02")
|
||||
detail = _matter(ada, matter["id"])
|
||||
binding = binding_for(detail, "Schreiben")
|
||||
ada.request(
|
||||
"PATCH",
|
||||
f"/api/v1/bindings/{binding['id']}/chapter",
|
||||
json={"expected_version": detail["version"], "chapter_id": first["id"]},
|
||||
)
|
||||
detail = _matter(ada, matter["id"])
|
||||
assert detail["content_order"] == "date"
|
||||
assert [item["title"] for item in detail["chapters"]] == ["Schriftverkehr", "Abrechnung"]
|
||||
dated = detail
|
||||
assert binding_for(dated, "Schreiben")["chapter_id"] == first["id"]
|
||||
assert binding_for(dated, "Schreiben")["element"]["excerpt"] == "Schreiben"
|
||||
ada.request(
|
||||
"PATCH",
|
||||
f"/api/v1/matters/{matter['id']}/content-order",
|
||||
json={"expected_version": dated["version"], "mode": "position", "direction": "asc"},
|
||||
)
|
||||
positioned = _matter(ada, matter["id"])
|
||||
ada.request(
|
||||
"POST",
|
||||
f"/api/v1/matters/{matter['id']}/chapters/order",
|
||||
json={"expected_version": positioned["version"], "chapter_ids": [first["id"], second["id"]]},
|
||||
)
|
||||
ordered = _matter(ada, matter["id"])
|
||||
assert [item["title"] for item in ordered["chapters"]] == ["Abrechnung", "Schriftverkehr"]
|
||||
assert binding_for(ordered, "Schreiben")["chapter_id"] == first["id"]
|
||||
assert len(ordered["bindings"]) == 1
|
||||
case = case_of(ada, "Wohnung")
|
||||
ada.request("POST", f"/api/v1/matters/{matter['id']}/case-links", json={"case_id": case["id"]}, status=201)
|
||||
grant(ada, case["id"], ben.email)
|
||||
ben.login()
|
||||
ben.request(
|
||||
"POST",
|
||||
f"/api/v1/matters/{matter['id']}/chapters/order",
|
||||
json={
|
||||
"expected_version": ordered["version"],
|
||||
"chapter_ids": [second["id"], first["id"]],
|
||||
"context": {"kind": "case", "id": case["id"]},
|
||||
},
|
||||
status=403,
|
||||
)
|
||||
Reference in new issue
Block a user