Unterscheide Eingang und Ausgang an der Einbindung und ermoegliche Umbenennen, Archivieren, Loeschen und Verschieben.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
20625b4596
commit
ad6240a5da
27 files changed
+1191
-88
No files matched your search
+169
-2
@@ -257,6 +257,152 @@ def create_matter(conn, actor: UUID, scope_id: UUID, title: str, parent_matter_i
|
||||
return row
|
||||
|
||||
|
||||
def _binding_in_page(conn, binding: dict, context: Ctx) -> bool:
|
||||
if binding["context_kind"] == context.kind and binding["context_id"] == context.id:
|
||||
return True
|
||||
if context.kind != "case" or binding["context_kind"] != "matter":
|
||||
return False
|
||||
current = binding["context_id"]
|
||||
seen: set[UUID] = set()
|
||||
while current and current not in seen:
|
||||
seen.add(current)
|
||||
link = conn.execute(
|
||||
"SELECT 1 FROM matter_case_links WHERE matter_id = %s AND case_id = %s",
|
||||
(current, context.id),
|
||||
).fetchone()
|
||||
if link is not None:
|
||||
return True
|
||||
parent = conn.execute("SELECT parent_matter_id FROM matters WHERE id = %s", (current,)).fetchone()
|
||||
current = parent["parent_matter_id"] if parent else None
|
||||
return False
|
||||
|
||||
|
||||
def set_communication_direction(conn, actor: UUID, binding_id: UUID, expected: int, direction: str | None, context: Ctx | None) -> dict:
|
||||
row = conn.execute("SELECT * FROM bindings WHERE id = %s", (binding_id,)).fetchone()
|
||||
if row is None:
|
||||
raise not_found()
|
||||
page = context or Ctx(row["context_kind"], row["context_id"])
|
||||
if context is not None and not _binding_in_page(conn, row, context):
|
||||
raise not_found()
|
||||
element, rights = require_element(conn, actor, row["element_id"], page, "edit")
|
||||
if not rights.edit:
|
||||
raise forbidden()
|
||||
bump_version(conn, "elements", element["id"], expected)
|
||||
store_direction(conn, row["id"], direction, True)
|
||||
audit(conn, actor, element["home_scope_id"], "communication_direction", "element", element["id"], page)
|
||||
return get_element(conn, element["id"])
|
||||
|
||||
|
||||
def _chapter_container(conn, actor: UUID, chapter_id: UUID, context: Ctx | None):
|
||||
chapter = conn.execute("SELECT * FROM chapters WHERE id = %s", (chapter_id,)).fetchone()
|
||||
if chapter is None:
|
||||
raise not_found()
|
||||
if chapter["case_id"]:
|
||||
container, _rights = require_case(conn, actor, chapter["case_id"], "edit")
|
||||
kind = "case"
|
||||
else:
|
||||
container, _rights = require_matter(conn, actor, chapter["matter_id"], context, "edit")
|
||||
kind = "matter"
|
||||
return chapter, container, kind
|
||||
|
||||
|
||||
def rename_chapter(conn, actor: UUID, chapter_id: UUID, expected: int, title: str, context: Ctx | None) -> int:
|
||||
chapter, container, kind = _chapter_container(conn, actor, chapter_id, context)
|
||||
version = bump_version(conn, "cases" if kind == "case" else "matters", container["id"], expected)
|
||||
conn.execute("UPDATE chapters SET title = %s WHERE id = %s", (title.strip(), chapter["id"]))
|
||||
audit(conn, actor, container["home_scope_id"], "chapter_renamed", kind, container["id"], context)
|
||||
return version
|
||||
|
||||
|
||||
def delete_chapter(conn, actor: UUID, chapter_id: UUID, expected: int, context: Ctx | None) -> int:
|
||||
chapter, container, kind = _chapter_container(conn, actor, chapter_id, context)
|
||||
version = bump_version(conn, "cases" if kind == "case" else "matters", container["id"], expected)
|
||||
conn.execute("UPDATE bindings SET chapter_id = NULL WHERE chapter_id = %s", (chapter["id"],))
|
||||
conn.execute("DELETE FROM chapters WHERE id = %s", (chapter["id"],))
|
||||
audit(conn, actor, container["home_scope_id"], "chapter_deleted", kind, container["id"], context)
|
||||
return version
|
||||
|
||||
|
||||
def delete_case(conn, actor: UUID, case_id: UUID, expected: int) -> None:
|
||||
case, rights = require_case(conn, actor, case_id, "edit")
|
||||
if not rights.edit:
|
||||
raise forbidden()
|
||||
children = conn.execute(
|
||||
"SELECT id FROM cases WHERE parent_case_id = %s AND deleted_at IS NULL",
|
||||
(case_id,),
|
||||
).fetchall()
|
||||
links = conn.execute("SELECT id FROM matter_case_links WHERE case_id = %s", (case_id,)).fetchall()
|
||||
if children or links:
|
||||
raise unprocessable("Diese Akte enthält noch Unterakten oder Vorgänge. Verschiebe oder lösche sie zuerst.")
|
||||
bump_version(conn, "cases", case_id, expected)
|
||||
conn.execute("UPDATE cases SET deleted_at = now(), updated_at = now() WHERE id = %s", (case_id,))
|
||||
audit(conn, actor, case["home_scope_id"], "case_deleted", "case", case_id, None)
|
||||
|
||||
|
||||
def delete_matter(conn, actor: UUID, matter_id: UUID, expected: int, context: Ctx | None) -> None:
|
||||
matter, _rights = require_matter(conn, actor, matter_id, context, "edit")
|
||||
children = conn.execute(
|
||||
"SELECT id FROM matters WHERE parent_matter_id = %s AND deleted_at IS NULL",
|
||||
(matter_id,),
|
||||
).fetchall()
|
||||
if children:
|
||||
raise unprocessable("Dieser Vorgang enthält noch Teilvorgänge. Verschiebe oder lösche sie zuerst.")
|
||||
bump_version(conn, "matters", matter_id, expected)
|
||||
conn.execute("UPDATE matters SET deleted_at = now(), updated_at = now() WHERE id = %s", (matter_id,))
|
||||
audit(conn, actor, matter["home_scope_id"], "matter_deleted", "matter", matter_id, context)
|
||||
|
||||
|
||||
def _matter_parent_chain(conn, matter_id: UUID) -> set[UUID]:
|
||||
seen: set[UUID] = set()
|
||||
current = matter_id
|
||||
while current and current not in seen:
|
||||
seen.add(current)
|
||||
row = conn.execute("SELECT parent_matter_id FROM matters WHERE id = %s", (current,)).fetchone()
|
||||
current = row["parent_matter_id"] if row else None
|
||||
return seen
|
||||
|
||||
|
||||
def move_matter(
|
||||
conn,
|
||||
actor: UUID,
|
||||
matter_id: UUID,
|
||||
expected: int,
|
||||
context: Ctx | None,
|
||||
case_id: UUID | None,
|
||||
parent_matter_id: UUID | None,
|
||||
clear_parent: bool,
|
||||
) -> dict:
|
||||
matter, _rights = require_matter(conn, actor, matter_id, context, "edit")
|
||||
if case_id is None and parent_matter_id is None and not clear_parent:
|
||||
raise unprocessable("Es fehlt ein Ziel.")
|
||||
bump_version(conn, "matters", matter_id, expected)
|
||||
if clear_parent:
|
||||
conn.execute("UPDATE matters SET parent_matter_id = NULL WHERE id = %s", (matter_id,))
|
||||
if parent_matter_id is not None:
|
||||
if parent_matter_id == matter_id or matter_id in _matter_parent_chain(conn, parent_matter_id):
|
||||
raise unprocessable("Ein Vorgang kann nicht in sich selbst oder in einen eigenen Teilvorgang verschoben werden.")
|
||||
parent_context = Ctx("case", case_id) if case_id is not None else context
|
||||
parent, _parent_rights = require_matter(conn, actor, parent_matter_id, parent_context, "edit")
|
||||
if parent["home_scope_id"] != matter["home_scope_id"]:
|
||||
raise unprocessable("Der Zielvorgang liegt in einem anderen Datenraum.")
|
||||
conn.execute("UPDATE matters SET parent_matter_id = %s WHERE id = %s", (parent_matter_id, matter_id))
|
||||
if case_id is not None:
|
||||
require_case(conn, actor, case_id, "edit")
|
||||
exists = conn.execute(
|
||||
"SELECT id FROM matter_case_links WHERE matter_id = %s AND case_id = %s",
|
||||
(matter_id, case_id),
|
||||
).fetchone()
|
||||
if exists is None:
|
||||
link_matter_to_case(conn, actor, matter_id, case_id, context)
|
||||
if context is not None and context.kind == "case" and context.id != case_id:
|
||||
conn.execute(
|
||||
"DELETE FROM matter_case_links WHERE matter_id = %s AND case_id = %s",
|
||||
(matter_id, context.id),
|
||||
)
|
||||
audit(conn, actor, matter["home_scope_id"], "matter_moved", "matter", matter_id, context)
|
||||
return get_matter(conn, matter_id)
|
||||
|
||||
|
||||
def patch_matter(conn, actor: UUID, matter_id: UUID, expected: int, title: str | None, status: str | None, context: Ctx | None) -> dict:
|
||||
if title is None and status is None:
|
||||
raise unprocessable("Es fehlt eine Änderung.")
|
||||
@@ -519,6 +665,21 @@ 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 store_direction(conn, binding_id: UUID, direction: str | None, explicit: bool) -> None:
|
||||
if not explicit:
|
||||
return
|
||||
if direction not in (None, "in", "out"):
|
||||
raise unprocessable("Die Richtung ist ungültig.")
|
||||
conn.execute(
|
||||
"""
|
||||
UPDATE bindings
|
||||
SET communication_direction = %s, direction_explicit = true
|
||||
WHERE id = %s
|
||||
""",
|
||||
(direction, binding_id),
|
||||
)
|
||||
|
||||
|
||||
def create_note(
|
||||
conn,
|
||||
actor: UUID,
|
||||
@@ -531,6 +692,7 @@ def create_note(
|
||||
document_on: date | None = None,
|
||||
sent_on: date | None = None,
|
||||
received_on: date | None = None,
|
||||
communication_direction: str | None = None,
|
||||
) -> dict:
|
||||
document = clean_document(document)
|
||||
target, rights_context = _placement(context, placement)
|
||||
@@ -541,6 +703,7 @@ def create_note(
|
||||
(element["id"], Json(document)),
|
||||
)
|
||||
binding = insert_binding(conn, element["id"], target.kind, target.id, chapter_id, actor)
|
||||
store_direction(conn, binding["id"], communication_direction, communication_direction in ("in", "out"))
|
||||
audit(conn, actor, scope_id, "note_created", "element", element["id"], rights_context or target)
|
||||
if document_on is not None or sent_on is not None or received_on is not None:
|
||||
record_dates(conn, actor, scope_id, element["id"], rights_context or target, document_on, sent_on, received_on)
|
||||
@@ -619,7 +782,7 @@ def add_annotation(conn, actor: UUID, binding_id: UUID, body: str, context: Ctx
|
||||
return row
|
||||
|
||||
|
||||
def create_external(conn, actor: UUID, scope_id: UUID, title: str, source: str, external_id: str, context: Ctx, placement: Ctx | None, chapter_id: UUID | None, document_on, sent_on, received_on, detail: dict | None = None) -> dict:
|
||||
def create_external(conn, actor: UUID, scope_id: UUID, title: str, source: str, external_id: str, context: Ctx, placement: Ctx | None, chapter_id: UUID | None, document_on, sent_on, received_on, detail: dict | None = None, communication_direction: str | None = None) -> dict:
|
||||
target, rights_context = _placement(context, placement)
|
||||
require_container_edit(conn, actor, target, rights_context)
|
||||
element = _new_element(conn, actor, scope_id, title, "external_ref")
|
||||
@@ -628,12 +791,15 @@ def create_external(conn, actor: UUID, scope_id: UUID, title: str, source: str,
|
||||
(element["id"], source, external_id.strip()[:200], Json(detail or {})),
|
||||
)
|
||||
binding = insert_binding(conn, element["id"], target.kind, target.id, chapter_id, actor)
|
||||
adopted = (detail or {}).get("direction") if source == "imap" else None
|
||||
chosen = communication_direction if communication_direction in ("in", "out") else adopted if adopted in ("in", "out") else None
|
||||
store_direction(conn, binding["id"], chosen, chosen in ("in", "out"))
|
||||
record_dates(conn, actor, scope_id, element["id"], rights_context or target, document_on, sent_on, received_on)
|
||||
audit(conn, actor, scope_id, "external_ref_added", "element", element["id"], rights_context or target)
|
||||
return element | {"binding_id": binding["id"]}
|
||||
|
||||
|
||||
def create_file(conn, actor: UUID, scope_id: UUID, title: str, filename: str, data: bytes, context: Ctx, placement: Ctx | None, chapter_id: UUID | None, document_on, sent_on, received_on, media_root: str, max_bytes: int, rollback_files: list[Path]) -> dict:
|
||||
def create_file(conn, actor: UUID, scope_id: UUID, title: str, filename: str, data: bytes, context: Ctx, placement: Ctx | None, chapter_id: UUID | None, document_on, sent_on, received_on, media_root: str, max_bytes: int, rollback_files: list[Path], communication_direction: str | None = None) -> dict:
|
||||
if not data or len(data) > max_bytes:
|
||||
raise unprocessable("Die Datei ist leer oder zu groß.")
|
||||
content_type = sniff(data)
|
||||
@@ -671,6 +837,7 @@ def create_file(conn, actor: UUID, scope_id: UUID, title: str, filename: str, da
|
||||
(element["id"], asset_id),
|
||||
)
|
||||
binding = insert_binding(conn, element["id"], target.kind, target.id, chapter_id, actor)
|
||||
store_direction(conn, binding["id"], communication_direction, communication_direction in ("in", "out"))
|
||||
record_dates(conn, actor, scope_id, element["id"], rights_context or target, document_on, sent_on, received_on)
|
||||
audit(conn, actor, scope_id, "file_added", "element", element["id"], rights_context or target)
|
||||
return element | {"binding_id": binding["id"], "asset_id": asset_id}
|
||||
|
||||
Reference in new issue
Block a user