diff --git a/.gitignore b/.gitignore index da64b01..d27b169 100644 --- a/.gitignore +++ b/.gitignore @@ -43,3 +43,6 @@ tmp/ temp/ data/local/ *.db +backend/data/ +backend/.pytest-tmp/ +backend/.pytest-export-tmp/ diff --git a/backend/app/api/correspondence.py b/backend/app/api/correspondence.py new file mode 100644 index 0000000..d962e6c --- /dev/null +++ b/backend/app/api/correspondence.py @@ -0,0 +1,269 @@ +"""HTTP for attachment roles, documented dispatches and secured editions.""" + +from __future__ import annotations + +from uuid import UUID + +from fastapi import APIRouter, Depends, Request, Response +from psycopg import Connection + +from app.access.policy import Ctx +from app.api.deps import Principal, get_conn, get_principal +from app.api.schemas import ( + AttachmentAssignIn, + AttachmentDetachIn, + AttachmentOrderIn, + DispatchCorrectIn, + DispatchEvidenceDetachIn, + DispatchEvidenceIn, + DispatchIn, + FoldIn, +) +from app.correspondence import ( + GapNote, + add_dispatch_evidence, + assign_attachments, + detach_dispatch_evidence, + correct_dispatch, + detach_attachment, + dispatch_view, + open_container, + order_attachments, + read_snapshot, + record_dispatch, + set_fold, +) +from app.core.errors import not_found +from app.media.storage import content_disposition + +router = APIRouter() + + +def _ctx(value) -> Ctx: + return Ctx(value.kind, value.id) + + +def _placement(value) -> Ctx | None: + if value is None: + return None + return Ctx(value.kind, value.id) + + +@router.post("/api/v1/elements/{element_id}/attachments", status_code=201) +def post_attachments( + element_id: UUID, + body: AttachmentAssignIn, + principal: Principal = Depends(get_principal), + conn: Connection = Depends(get_conn), +): + return assign_attachments( + conn, + principal.account_id, + element_id, + body.expected_version, + _ctx(body.context), + _placement(body.placement), + body.attachment_element_ids, + body.fold, + [], + ) + + +@router.post("/api/v1/elements/{element_id}/attachments/order") +def post_attachment_order( + element_id: UUID, + body: AttachmentOrderIn, + principal: Principal = Depends(get_principal), + conn: Connection = Depends(get_conn), +): + return order_attachments( + conn, + principal.account_id, + element_id, + body.expected_version, + _ctx(body.context), + _placement(body.placement), + body.attachment_element_ids, + ) + + +@router.post("/api/v1/elements/{element_id}/attachments/{attachment_id}/detach", status_code=204) +def post_detach( + element_id: UUID, + attachment_id: UUID, + body: AttachmentDetachIn, + principal: Principal = Depends(get_principal), + conn: Connection = Depends(get_conn), +): + detach_attachment( + conn, + principal.account_id, + element_id, + attachment_id, + body.expected_version, + _ctx(body.context), + _placement(body.placement), + ) + + +@router.patch("/api/v1/bindings/{binding_id}/fold") +def patch_fold( + binding_id: UUID, + body: FoldIn, + principal: Principal = Depends(get_principal), + conn: Connection = Depends(get_conn), +): + return set_fold( + conn, + principal.account_id, + binding_id, + body.expected_version, + _ctx(body.context), + _placement(body.placement), + body.fold, + ) + + +@router.post("/api/v1/elements/{element_id}/dispatches", status_code=201) +def post_dispatch( + element_id: UUID, + body: DispatchIn, + request: Request, + principal: Principal = Depends(get_principal), + conn: Connection = Depends(get_conn), +): + request.state.cleanup_files = [] + return record_dispatch( + conn, + principal.account_id, + element_id, + body.expected_version, + _ctx(body.context), + _placement(body.placement), + body.channel, + body.sent_on, + body.counterpart, + body.attachment_element_ids, + [item.model_dump() for item in body.evidence], + body.historical_gap, + body.capture_editions, + request.app.state.settings.media_root, + request.state.cleanup_files, + ) + + +@router.post("/api/v1/dispatches/{dispatch_id}/evidence", status_code=201) +def post_evidence( + dispatch_id: UUID, + body: DispatchEvidenceIn, + request: Request, + principal: Principal = Depends(get_principal), + conn: Connection = Depends(get_conn), +): + request.state.cleanup_files = [] + return add_dispatch_evidence( + conn, + principal.account_id, + dispatch_id, + body.expected_version, + _ctx(body.context), + _placement(body.placement), + body.element_id, + body.role, + body.fold, + request.app.state.settings.media_root, + request.state.cleanup_files, + ) + + +@router.post("/api/v1/dispatches/{dispatch_id}/evidence/{element_id}/detach", status_code=204) +def post_detach_evidence( + dispatch_id: UUID, + element_id: UUID, + body: DispatchEvidenceDetachIn, + principal: Principal = Depends(get_principal), + conn: Connection = Depends(get_conn), +): + detach_dispatch_evidence( + conn, + principal.account_id, + dispatch_id, + element_id, + body.expected_version, + _ctx(body.context), + _placement(body.placement), + ) + + +@router.post("/api/v1/dispatches/{dispatch_id}/correct") +def post_correct( + dispatch_id: UUID, + body: DispatchCorrectIn, + principal: Principal = Depends(get_principal), + conn: Connection = Depends(get_conn), +): + return correct_dispatch( + conn, + principal.account_id, + dispatch_id, + body.expected_version, + _ctx(body.context), + _placement(body.placement), + body.channel, + body.sent_on, + body.clear_sent_on, + body.counterpart, + body.clear_counterpart, + ) + + +@router.get("/api/v1/dispatches/{dispatch_id}") +def get_dispatch( + dispatch_id: UUID, + context_kind: str, + context_id: UUID, + placement_kind: str | None = None, + placement_id: UUID | None = None, + principal: Principal = Depends(get_principal), + conn: Connection = Depends(get_conn), +): + context = Ctx(context_kind, context_id) + placement = Ctx(placement_kind, placement_id) if placement_kind and placement_id else None + _row, page, scope = open_container(conn, principal.account_id, context, placement, "read") + return dispatch_view(conn, principal.account_id, dispatch_id, page, scope) + + +@router.get("/api/v1/dispatches/{dispatch_id}/items/{item_id}/content") +def get_dispatch_item( + dispatch_id: UUID, + item_id: UUID, + context_kind: str, + context_id: UUID, + request: Request, + placement_kind: str | None = None, + placement_id: UUID | None = None, + principal: Principal = Depends(get_principal), + conn: Connection = Depends(get_conn), +): + context = Ctx(context_kind, context_id) + placement = Ctx(placement_kind, placement_id) if placement_kind and placement_id else None + result = read_snapshot( + conn, + principal.account_id, + dispatch_id, + item_id, + context, + placement, + request.app.state.settings.media_root, + ) + if isinstance(result, GapNote): + raise not_found() + return Response( + content=result.data, + media_type=result.media_type or "application/octet-stream", + headers={ + "Content-Disposition": content_disposition(result.filename), + "X-Content-Type-Options": "nosniff", + "Cache-Control": "private, no-store", + }, + ) diff --git a/backend/app/api/routes.py b/backend/app/api/routes.py index 9df0e11..e4aacd3 100644 --- a/backend/app/api/routes.py +++ b/backend/app/api/routes.py @@ -1460,9 +1460,17 @@ def patch_case_order( conn: Connection = Depends(get_conn), ): version = set_content_order( - conn, principal.account_id, "case", case_id, body.expected_version, body.mode, body.direction, None + conn, + principal.account_id, + "case", + case_id, + body.expected_version, + body.mode, + body.direction, + None, + body.view, ) - return {"version": version, "mode": body.mode, "direction": body.direction} + return {"version": version, "mode": body.mode, "direction": body.direction, "view": body.view} @router.post("/api/v1/cases/{case_id}/bindings/order") @@ -1514,8 +1522,9 @@ def patch_matter_order( body.mode, body.direction, _ctx_model(body.context), + body.view, ) - return {"version": version, "mode": body.mode, "direction": body.direction} + return {"version": version, "mode": body.mode, "direction": body.direction, "view": body.view} @router.post("/api/v1/matters/{matter_id}/bindings/order") diff --git a/backend/app/api/schemas.py b/backend/app/api/schemas.py index 0d0022e..628d65e 100644 --- a/backend/app/api/schemas.py +++ b/backend/app/api/schemas.py @@ -197,6 +197,7 @@ class ContentOrderIn(APIModel): expected_version: int = Field(ge=1) mode: Literal["date", "position"] direction: Literal["asc", "desc"] = "asc" + view: Literal["structure", "chronology"] | None = None context: ContextIn | None = None @@ -366,3 +367,76 @@ class ExportIn(APIModel): context: ContextIn | None = None selected: list[str] = Field(default_factory=list, max_length=2000) assignments: dict[str, UUID] = Field(default_factory=dict) + + +class EvidenceIn(APIModel): + element_id: UUID + role: Literal["email", "submission", "delivery", "other"] + fold: Literal["keep", "evidence"] = "keep" + + +class AttachmentAssignIn(APIModel): + expected_version: int = Field(ge=1) + context: ContextIn + placement: ContextIn | None = None + attachment_element_ids: list[UUID] = Field(min_length=1, max_length=100) + fold: Literal["keep", "attachment"] = "keep" + + +class AttachmentOrderIn(APIModel): + expected_version: int = Field(ge=1) + context: ContextIn + placement: ContextIn | None = None + attachment_element_ids: list[UUID] = Field(min_length=1, max_length=100) + + +class AttachmentDetachIn(APIModel): + expected_version: int = Field(ge=1) + context: ContextIn + placement: ContextIn | None = None + + +class FoldIn(APIModel): + expected_version: int = Field(ge=1) + context: ContextIn + placement: ContextIn | None = None + fold: Literal["attachment", "evidence"] | None = None + + +class DispatchIn(APIModel): + expected_version: int = Field(ge=1) + context: ContextIn + placement: ContextIn | None = None + channel: Literal["email", "post", "other"] + sent_on: date | None = None + counterpart: str | None = Field(default=None, max_length=200) + attachment_element_ids: list[UUID] = Field(default_factory=list, max_length=100) + evidence: list[EvidenceIn] = Field(default_factory=list, max_length=20) + historical_gap: bool = False + capture_editions: bool = True + + +class DispatchEvidenceIn(APIModel): + expected_version: int = Field(ge=1) + context: ContextIn + placement: ContextIn | None = None + element_id: UUID + role: Literal["email", "submission", "delivery", "other"] + fold: Literal["keep", "evidence"] = "keep" + + +class DispatchEvidenceDetachIn(APIModel): + expected_version: int = Field(ge=1) + context: ContextIn + placement: ContextIn | None = None + + +class DispatchCorrectIn(APIModel): + expected_version: int = Field(ge=1) + context: ContextIn + placement: ContextIn | None = None + channel: Literal["email", "post", "other"] | None = None + sent_on: date | None = None + clear_sent_on: bool = False + counterpart: str | None = Field(default=None, max_length=200) + clear_counterpart: bool = False diff --git a/backend/app/correspondence.py b/backend/app/correspondence.py new file mode 100644 index 0000000..13a595b --- /dev/null +++ b/backend/app/correspondence.py @@ -0,0 +1,1420 @@ +"""Anlagenverwendung, dokumentierter Versand und gesicherte Fassung. + +An attachment use is a role in one container. It does not copy the file and +does not grant access. A dispatch freezes the transmitted stand. Later edits +of the working papers do not rewrite that stand. A missing historical original +stays a gap and is never filled with today's file. +""" + +from __future__ import annotations + +import json +import re +from dataclasses import dataclass +from datetime import date +from pathlib import Path +from uuid import UUID, uuid4 + +from psycopg.types.json import Json + +from app.access.policy import ( + Ctx, + audit, + bump_version, + element_rights, + get_element, + require_case, + require_matter, +) +from app.core.errors import conflict, not_found, unprocessable +from app.core.timeutil import dump_date, dump_dt +from app.media.storage import asset_path, display_filename, read_bytes, sha256, write_bytes +from app.retrieval import FilePayload, MailPayload, RetrievalFailure, load_external, load_file + +CHANNELS = {"email": "per E-Mail", "post": "per Post", "other": "sonstiger Weg"} +EVIDENCE_ROLES = { + "email": "E-Mail/Versanddokumentation", + "submission": "Einlieferung", + "delivery": "Zustellung", + "other": "sonstiger Nachweis", +} +_HISTORICAL_GAP = ( + "Historische Fassung liegt nicht vor. " + "Die aktuelle Arbeitsfassung wurde nicht als Versandstand übernommen." +) + + +@dataclass(frozen=True) +class GapNote: + reason: str + + +def open_container(conn, actor: UUID, context: Ctx, placement: Ctx | None, need: str) -> tuple[dict, Ctx, Ctx]: + """Rights follow the checked context. The relationship lives on the container.""" + container = placement or context + if container.kind == "case": + if placement is not None and (context.kind != "case" or context.id != placement.id): + raise not_found() + row, _rights = require_case(conn, actor, container.id, need) + page = Ctx("case", container.id) + return row, page, page + if container.kind != "matter": + raise unprocessable("Der Zusammenhang fehlt.") + if context.kind == "matter" and context.id != container.id: + raise not_found() + rights_context = context if context.kind == "case" else None + row, _rights = require_matter(conn, actor, container.id, rights_context, need) + page = rights_context or Ctx("matter", container.id) + return row, page, Ctx("matter", container.id) + + +def assign_attachments( + conn, + actor: UUID, + host_id: UUID, + expected: int, + context: Ctx, + placement: Ctx | None, + attachment_ids: list[UUID], + fold: str, + cleanup: list[Path], +) -> dict: + del cleanup + if fold not in {"keep", "attachment"}: + raise unprocessable("Die Darstellung der Vorkommen ist nicht festgelegt.") + if not attachment_ids: + raise unprocessable("Es ist keine Anlage ausgewählt.") + if len(attachment_ids) != len(set(attachment_ids)): + raise unprocessable("Dieselbe Anlage ist mehrfach ausgewählt.") + container_row, page, scope = open_container(conn, actor, context, placement, "edit") + host = _bound_element(conn, actor, host_id, page, scope) + bump_version(conn, "elements", host_id, expected) + created = [] + for attachment_id in attachment_ids: + created.append(_assign_one(conn, actor, container_row, page, scope, host, attachment_id, fold)) + audit(conn, actor, container_row["home_scope_id"], "attachments_assigned", "element", host_id, page) + return {"attachments": created} + + +def order_attachments( + conn, + actor: UUID, + host_id: UUID, + expected: int, + context: Ctx, + placement: Ctx | None, + attachment_ids: list[UUID], +) -> dict: + if len(attachment_ids) != len(set(attachment_ids)): + raise unprocessable("Dieselbe Anlage steht mehrfach in der Reihenfolge.") + container_row, page, scope = open_container(conn, actor, context, placement, "edit") + _bound_element(conn, actor, host_id, page, scope) + bump_version(conn, "elements", host_id, expected) + rows = _uses(conn, scope, host_id) + current = [row["attachment_element_id"] for row in rows] + if set(attachment_ids) != set(current) or len(attachment_ids) != len(current): + raise unprocessable("Die Anlagenreihenfolge ist unvollständig. Bitte neu laden.") + for position, attachment_id in enumerate(attachment_ids): + conn.execute( + """ + UPDATE attachment_uses + SET position = %s + WHERE context_kind = %s AND context_id = %s + AND host_element_id = %s AND attachment_element_id = %s + """, + (position, scope.kind, scope.id, host_id, attachment_id), + ) + audit(conn, actor, container_row["home_scope_id"], "attachments_ordered", "element", host_id, page) + return {"attachment_element_ids": [str(item) for item in attachment_ids]} + + +def detach_attachment( + conn, + actor: UUID, + host_id: UUID, + attachment_id: UUID, + expected: int, + context: Ctx, + placement: Ctx | None, +) -> None: + container_row, page, scope = open_container(conn, actor, context, placement, "edit") + _bound_element(conn, actor, host_id, page, scope) + bump_version(conn, "elements", host_id, expected) + deleted = conn.execute( + """ + DELETE FROM attachment_uses + WHERE context_kind = %s AND context_id = %s + AND host_element_id = %s AND attachment_element_id = %s + RETURNING id + """, + (scope.kind, scope.id, host_id, attachment_id), + ).fetchone() + if deleted is None: + raise not_found() + _clear_attachment_fold(conn, scope, attachment_id) + audit(conn, actor, container_row["home_scope_id"], "attachment_detached", "element", host_id, page) + + +def set_fold( + conn, + actor: UUID, + binding_id: UUID, + expected: int, + context: Ctx, + placement: Ctx | None, + fold: str | None, +) -> dict: + if fold not in {None, "attachment", "evidence"}: + raise unprocessable("Diese Darstellung ist nicht vorgesehen.") + container_row, page, scope = open_container(conn, actor, context, placement, "edit") + binding = conn.execute("SELECT * FROM bindings WHERE id = %s", (binding_id,)).fetchone() + if binding is None or binding["context_kind"] != scope.kind or binding["context_id"] != scope.id: + raise not_found() + element = _readable(conn, actor, binding["element_id"], page) + if element is None: + raise not_found() + bump_version(conn, "elements", element["id"], expected) + if fold == "attachment" and not _is_attachment(conn, scope, element["id"]): + raise unprocessable("Nur eine zugeordnete Anlage kann so dargestellt werden.") + if fold == "evidence" and not _is_evidence(conn, scope, element["id"]): + raise unprocessable("Nur ein zugeordneter Nachweis kann so dargestellt werden.") + conn.execute("UPDATE bindings SET list_fold = %s WHERE id = %s", (fold, binding_id)) + audit(conn, actor, container_row["home_scope_id"], "binding_folded", "element", element["id"], page) + return {"id": str(binding_id), "fold": fold} + + +def record_dispatch( + conn, + actor: UUID, + host_id: UUID, + expected: int, + context: Ctx, + placement: Ctx | None, + channel: str, + sent_on: date | None, + counterpart: str | None, + attachment_ids: list[UUID], + evidence: list[dict], + historical_gap: bool, + capture_editions: bool, + media_root: str, + cleanup: list[Path], +) -> dict: + if channel not in CHANNELS: + raise unprocessable("Der Versandweg ist nicht vorgesehen.") + if not capture_editions and attachment_ids: + raise unprocessable("Ein Beleg sichert das Schreiben nicht als Versandfassung.") + if not capture_editions and not evidence: + raise unprocessable("Ein Beleg braucht mindestens einen Nachweis.") + if len(attachment_ids) != len(set(attachment_ids)): + raise unprocessable("Dieselbe Anlage ist mehrfach im Versand genannt.") + container_row, page, scope = open_container(conn, actor, context, placement, "edit") + host = _bound_element(conn, actor, host_id, page, scope) + bump_version(conn, "elements", host_id, expected) + known = {row["attachment_element_id"] for row in _uses(conn, scope, host_id)} + if any(item not in known for item in attachment_ids): + raise unprocessable("Mitversendet werden nur Anlagen dieses Schreibens.") + counterpart_text = _counterpart(counterpart) + dispatch = conn.execute( + """ + INSERT INTO dispatches ( + home_scope_id, context_kind, context_id, host_element_id, channel, + sent_on, counterpart, completeness, provenance, recorded_by + ) + VALUES (%s, %s, %s, %s, %s, %s, %s, 'gap', %s, %s) + RETURNING * + """, + ( + container_row["home_scope_id"], + scope.kind, + scope.id, + host_id, + channel, + sent_on, + counterpart_text, + "reconstructed" if historical_gap else "captured", + actor, + ), + ).fetchone() + states = [] + if capture_editions: + states.append(_capture_item(conn, actor, dispatch, host, "host", 0, page, historical_gap, media_root, cleanup)) + ordered = [row for row in _uses(conn, scope, host_id) if row["attachment_element_id"] in set(attachment_ids)] + position_of = {item: index for index, item in enumerate(attachment_ids)} + ordered.sort(key=lambda row: position_of[row["attachment_element_id"]]) + for index, row in enumerate(ordered, start=1): + element = _readable(conn, actor, row["attachment_element_id"], page) + if element is None: + raise not_found() + states.append( + _capture_item(conn, actor, dispatch, element, "attachment", index, page, historical_gap, media_root, cleanup) + ) + evidence_rows = [] + for item in evidence: + evidence_rows.append( + _add_evidence( + conn, + actor, + container_row, + page, + scope, + dispatch, + item["element_id"], + item["role"], + item.get("fold") or "keep", + media_root, + cleanup, + log=False, + ) + ) + completeness = _completeness(states) if capture_editions else "secured" + conn.execute( + "UPDATE dispatches SET completeness = %s, updated_at = now() WHERE id = %s", + (completeness, dispatch["id"]), + ) + audit(conn, actor, container_row["home_scope_id"], "dispatch_recorded", "element", host_id, page) + return dispatch_view(conn, actor, dispatch["id"], page, scope) + + +def add_dispatch_evidence( + conn, + actor: UUID, + dispatch_id: UUID, + expected: int, + context: Ctx, + placement: Ctx | None, + element_id: UUID, + role: str, + fold: str, + media_root: str, + cleanup: list[Path], +) -> dict: + container_row, page, scope = open_container(conn, actor, context, placement, "edit") + dispatch = _dispatch_in(conn, dispatch_id, scope) + _bump_dispatch(conn, dispatch_id, expected) + row = _add_evidence( + conn, actor, container_row, page, scope, dispatch, element_id, role, fold, media_root, cleanup, log=True + ) + return row + + +def detach_dispatch_evidence( + conn, + actor: UUID, + dispatch_id: UUID, + element_id: UUID, + expected: int, + context: Ctx, + placement: Ctx | None, +) -> None: + container_row, page, scope = open_container(conn, actor, context, placement, "edit") + dispatch = _dispatch_in(conn, dispatch_id, scope) + _bump_dispatch(conn, dispatch_id, expected) + deleted = conn.execute( + """ + DELETE FROM dispatch_evidence + WHERE dispatch_id = %s AND element_id = %s + RETURNING id + """, + (dispatch_id, element_id), + ).fetchone() + if deleted is None: + raise not_found() + _clear_evidence_fold(conn, scope, element_id) + leftover_evidence = conn.execute( + "SELECT id FROM dispatch_evidence WHERE dispatch_id = %s", (dispatch_id,) + ).fetchone() + leftover_items = conn.execute( + "SELECT id FROM dispatch_items WHERE dispatch_id = %s", (dispatch_id,) + ).fetchone() + if leftover_evidence is None and leftover_items is None: + conn.execute("DELETE FROM dispatches WHERE id = %s", (dispatch_id,)) + else: + _log(conn, dispatch_id, actor, "Nachweis aus diesem Versand gelöst") + audit(conn, actor, container_row["home_scope_id"], "dispatch_evidence_detached", "element", dispatch["host_element_id"], page) + + +def correct_dispatch( + conn, + actor: UUID, + dispatch_id: UUID, + expected: int, + context: Ctx, + placement: Ctx | None, + channel: str | None, + sent_on: date | None, + clear_sent_on: bool, + counterpart: str | None, + clear_counterpart: bool, +) -> dict: + container_row, page, scope = open_container(conn, actor, context, placement, "edit") + dispatch = _dispatch_in(conn, dispatch_id, scope) + if channel is None and sent_on is None and not clear_sent_on and counterpart is None and not clear_counterpart: + raise unprocessable("Es fehlt eine Berichtigung.") + if channel is not None and channel not in CHANNELS: + raise unprocessable("Der Versandweg ist nicht vorgesehen.") + _bump_dispatch(conn, dispatch_id, expected) + changes = [] + if channel is not None and channel != dispatch["channel"]: + conn.execute("UPDATE dispatches SET channel = %s WHERE id = %s", (channel, dispatch_id)) + changes.append(f"Weg: {CHANNELS[channel]}") + if clear_sent_on: + conn.execute("UPDATE dispatches SET sent_on = NULL WHERE id = %s", (dispatch_id,)) + changes.append("Versanddatum: undatiert") + elif sent_on is not None and sent_on != dispatch["sent_on"]: + conn.execute("UPDATE dispatches SET sent_on = %s WHERE id = %s", (sent_on, dispatch_id)) + changes.append(f"Versanddatum: {sent_on.isoformat()}") + if clear_counterpart: + conn.execute("UPDATE dispatches SET counterpart = NULL WHERE id = %s", (dispatch_id,)) + changes.append("Gegenüber: unbekannt") + elif counterpart is not None: + text = _counterpart(counterpart) + conn.execute("UPDATE dispatches SET counterpart = %s WHERE id = %s", (text, dispatch_id)) + changes.append("Gegenüber berichtigt") + if not changes: + raise unprocessable("Die Angaben sind unverändert.") + conn.execute("UPDATE dispatches SET updated_at = now() WHERE id = %s", (dispatch_id,)) + _log(conn, dispatch_id, actor, "Berichtigung: " + "; ".join(changes)) + audit(conn, actor, container_row["home_scope_id"], "dispatch_corrected", "element", dispatch["host_element_id"], page) + return dispatch_view(conn, actor, dispatch_id, page, scope) + + +def dispatch_view(conn, actor: UUID, dispatch_id: UUID, page: Ctx, scope: Ctx) -> dict: + dispatch = _dispatch_in(conn, dispatch_id, scope) + host = _readable(conn, actor, dispatch["host_element_id"], page) + if host is None: + raise not_found() + return _public_dispatch(conn, actor, dispatch, page) + + +def read_snapshot( + conn, + actor: UUID, + dispatch_id: UUID, + item_id: UUID, + context: Ctx, + placement: Ctx | None, + media_root: str, +) -> FilePayload | GapNote: + _container, page, scope = open_container(conn, actor, context, placement, "read") + dispatch = _dispatch_in(conn, dispatch_id, scope) + if _readable(conn, actor, dispatch["host_element_id"], page) is None: + raise not_found() + item = conn.execute( + "SELECT * FROM dispatch_items WHERE id = %s AND dispatch_id = %s", + (item_id, dispatch_id), + ).fetchone() + if item is None or _readable(conn, actor, item["element_id"], page) is None: + raise not_found() + if item["source_state"] != "snapshot": + return GapNote(item["gap_reason"] or "Für diesen Bestandteil liegt keine gesicherte Fassung vor.") + return _snapshot_file(conn, media_root, dispatch_item_id=item["id"]) + + +def load_snapshot_file(conn, snapshot_id: UUID, media_root: str) -> FilePayload | RetrievalFailure: + row = conn.execute("SELECT * FROM dispatch_snapshots WHERE id = %s", (snapshot_id,)).fetchone() + if row is None: + return RetrievalFailure("missing", "Die gesicherte Fassung ist nicht mehr vorhanden.") + try: + data = read_bytes(_snapshot_path(media_root, row["id"])) + except Exception: + return RetrievalFailure("missing", "Die gesicherte Fassung ist nicht mehr vorhanden.") + if sha256(data) != row["sha256"]: + return RetrievalFailure("missing", "Die gesicherte Fassung lässt sich nicht unverändert lesen.") + return FilePayload(row["original_filename"], row["content_type"], data, row["sha256"]) + + +def export_children(conn, actor: UUID, binding: dict, page: Ctx) -> list[dict]: + """Selectable export nodes for one visible binding. Unreadable parts are omitted.""" + element_id = UUID(binding["element"]["id"]) + scope = Ctx(binding_context_kind(binding), UUID(binding_context_id(binding))) + nodes = [] + host_title = binding["element"]["title"] + for use in _readable_uses(conn, actor, scope, element_id, page): + element = use["element"] + nodes.append( + { + "id": f"attachment:{use['id']}", + "kind": "attachment", + "title": element["title"], + "element": element, + "rights": element["rights"], + "relation": f"Schreiben → Anlage · {host_title}", + "edition": "Aktuelle Arbeitsfassung", + "dates": element.get("dates") or {}, + } + ) + for dispatch in _dispatch_rows(conn, scope, element_id): + if _readable(conn, actor, dispatch["host_element_id"], page) is None: + continue + children = [] + for item in _item_rows(conn, dispatch["id"]): + element = _readable(conn, actor, item["element_id"], page) + if element is None: + continue + rights = element_rights(conn, actor, element, page) + label = "Schreiben → Versand" if item["role"] == "host" else "Schreiben → Anlage → Versand" + sent = dump_date(dispatch["sent_on"]) or "undatiert" + edition = ( + f"Gesicherte Versandfassung · Versanddatum {sent}" + if item["source_state"] == "snapshot" + else "Lücke, keine gesicherte historische Fassung" + ) + children.append( + { + "id": f"dispatch-item:{item['id']}", + "kind": "dispatch_item", + "title": item["title"], + "element_id": str(item["element_id"]), + "rights": rights.as_dict(), + "relation": label, + "edition": edition, + "gap_reason": item["gap_reason"] if item["source_state"] == "gap" else None, + "snapshot_id": _snapshot_id(conn, dispatch_item_id=item["id"]), + "dates": {"document_on": dump_date(item["document_on"]), "sent_on": sent if dispatch["sent_on"] else None}, + "filename": item["filename"], + "content_type": item["content_type"], + } + ) + for proof in _evidence_rows(conn, dispatch["id"]): + element = _readable(conn, actor, proof["element_id"], page) + if element is None: + continue + body = _element_public(conn, element, page) + rights = element_rights(conn, actor, element, page) + children.append( + { + "id": f"evidence:{proof['id']}", + "kind": "evidence", + "title": element["title"], + "element": body, + "rights": rights.as_dict(), + "relation": f"Schreiben → Versand → Nachweis · {EVIDENCE_ROLES[proof['role']]}", + "edition": ( + "Identifiziertes Nachweisoriginal" + if proof["source_state"] == "identified" + else "Nachweis ohne gesicherte Originalfassung" + ), + "gap_reason": proof["gap_reason"] if proof["source_state"] == "gap" else None, + "snapshot_id": _snapshot_id(conn, evidence_id=proof["id"]), + "dates": body.get("dates") or {}, + "role": proof["role"], + } + ) + if not children: + continue + item_rows = _item_rows(conn, dispatch["id"]) + sent = dump_date(dispatch["sent_on"]) + if item_rows: + title = f"Versand {sent or 'undatiert'} · {CHANNELS[dispatch['channel']]}" + label = "Versand" + else: + if not sent: + for proof in _evidence_rows(conn, dispatch["id"]): + dates = _dates(conn, proof["element_id"]) + sent = dump_date(dates["document_on"]) or dump_date(dates["sent_on"]) + if sent: + break + title = sent or "Beleg" + label = "Beleg" + nodes.append( + { + "id": f"dispatch:{dispatch['id']}", + "kind": "dispatch", + "title": title, + "label": label, + "children": children, + "completeness": dispatch["completeness"], + "provenance": dispatch["provenance"], + } + ) + return nodes + + +def attach_correspondence(conn, actor: UUID, bindings: list[dict], page: Ctx) -> None: + if not bindings: + return + scopes: dict[tuple[str, str], list[dict]] = {} + for binding in bindings: + key = (binding_context_kind(binding), binding_context_id(binding)) + scopes.setdefault(key, []).append(binding) + for (kind, raw_id), group in scopes.items(): + scope = Ctx(kind, UUID(raw_id)) + _attach_scope(conn, actor, group, page, scope) + + +def binding_context_kind(binding: dict) -> str: + container = binding.get("container") or {} + return container.get("kind") or binding.get("_context_kind") or "matter" + + +def binding_context_id(binding: dict) -> str: + container = binding.get("container") or {} + return container.get("id") or binding.get("_context_id") or "" + + +def purge_element_dispatch_files(conn, element_id: UUID, media_root: str) -> list[Path]: + """Drop stored bytes for a deleted element. The manifest of a remaining dispatch shows the gap.""" + paths: list[Path] = [] + host_dispatches = conn.execute( + "SELECT id FROM dispatches WHERE host_element_id = %s", + (element_id,), + ).fetchall() + for dispatch in host_dispatches: + paths.extend(_delete_dispatch_files(conn, dispatch["id"], media_root)) + conn.execute("DELETE FROM dispatches WHERE id = %s", (dispatch["id"],)) + item_ids = [ + row["id"] + for row in conn.execute("SELECT id FROM dispatch_items WHERE element_id = %s", (element_id,)).fetchall() + ] + for item_id in item_ids: + snap = conn.execute( + "SELECT id FROM dispatch_snapshots WHERE dispatch_item_id = %s", + (item_id,), + ).fetchone() + if snap is not None: + paths.append(_snapshot_path(media_root, snap["id"])) + conn.execute("DELETE FROM dispatch_snapshots WHERE id = %s", (snap["id"],)) + conn.execute( + """ + UPDATE dispatch_items + SET source_state = 'gap', + gap_reason = 'Die Fassung wurde mit der Unterlage gelöscht.', + note_document = NULL, + content_sha256 = NULL + WHERE id = %s + """, + (item_id,), + ) + evidence_ids = [ + row["id"] + for row in conn.execute("SELECT id FROM dispatch_evidence WHERE element_id = %s", (element_id,)).fetchall() + ] + for evidence_id in evidence_ids: + snap = conn.execute( + "SELECT id FROM dispatch_snapshots WHERE evidence_id = %s", + (evidence_id,), + ).fetchone() + if snap is not None: + paths.append(_snapshot_path(media_root, snap["id"])) + conn.execute("DELETE FROM dispatch_snapshots WHERE id = %s", (snap["id"],)) + conn.execute( + """ + UPDATE dispatch_evidence + SET source_state = 'gap', gap_reason = 'Der Nachweis wurde mit der Unterlage gelöscht.', + identity_sha256 = NULL + WHERE id = %s + """, + (evidence_id,), + ) + affected = conn.execute( + """ + SELECT DISTINCT dispatch_id FROM dispatch_items + WHERE element_id = %s + """, + (element_id,), + ).fetchall() + for row in affected: + states = [ + item["source_state"] + for item in conn.execute( + "SELECT source_state FROM dispatch_items WHERE dispatch_id = %s", + (row["dispatch_id"],), + ).fetchall() + ] + conn.execute( + "UPDATE dispatches SET completeness = %s, updated_at = now() WHERE id = %s", + (_completeness(states), row["dispatch_id"]), + ) + return paths + + +def _assign_one(conn, actor, container_row, page: Ctx, scope: Ctx, host: dict, attachment_id: UUID, fold: str) -> dict: + if attachment_id == host["id"]: + raise unprocessable("Ein Schriftstück kann nicht Anlage seiner selbst sein.") + element = _readable(conn, actor, attachment_id, page) + if element is None: + raise not_found() + existing = conn.execute( + """ + SELECT id FROM attachment_uses + WHERE context_kind = %s AND context_id = %s + AND host_element_id = %s AND attachment_element_id = %s + """, + (scope.kind, scope.id, host["id"], attachment_id), + ).fetchone() + if existing is not None: + raise unprocessable("Diese Anlage ist dem Schreiben bereits zugeordnet.") + if _reaches(conn, scope, attachment_id, host["id"]): + raise unprocessable("Diese Zuordnung würde einen Anlagenkreislauf erzeugen.") + position = conn.execute( + """ + SELECT COALESCE(MAX(position) + 1, 0) AS next_position + FROM attachment_uses + WHERE context_kind = %s AND context_id = %s AND host_element_id = %s + """, + (scope.kind, scope.id, host["id"]), + ).fetchone()["next_position"] + row = conn.execute( + """ + INSERT INTO attachment_uses ( + home_scope_id, context_kind, context_id, host_element_id, + attachment_element_id, position, created_by + ) + VALUES (%s, %s, %s, %s, %s, %s, %s) + RETURNING id, position + """, + (container_row["home_scope_id"], scope.kind, scope.id, host["id"], attachment_id, position, actor), + ).fetchone() + if fold == "attachment": + _set_binding_fold(conn, scope, attachment_id, "attachment") + return {"id": str(row["id"]), "element_id": str(attachment_id), "position": row["position"]} + + +def _add_evidence(conn, actor, container_row, page, scope, dispatch, element_id, role, fold, media_root, cleanup, log: bool) -> dict: + if role not in EVIDENCE_ROLES: + raise unprocessable("Die Rolle des Nachweises ist nicht vorgesehen.") + if fold not in {"keep", "evidence"}: + raise unprocessable("Die Darstellung des Nachweises ist nicht festgelegt.") + element = _readable(conn, actor, element_id, page) + if element is None: + raise not_found() + if conn.execute( + "SELECT id FROM dispatch_evidence WHERE dispatch_id = %s AND element_id = %s", + (dispatch["id"], element_id), + ).fetchone(): + raise unprocessable("Dieser Nachweis ist dem Versand bereits zugeordnet.") + identified, gap_reason, message_id, digest, comparisons, snapshot = _identify_evidence( + conn, actor, dispatch, element, page, media_root, cleanup, container_row["home_scope_id"] + ) + proof = conn.execute( + """ + INSERT INTO dispatch_evidence ( + dispatch_id, element_id, role, source_state, gap_reason, message_id, + identity_sha256, comparisons, manual, created_by + ) + VALUES (%s, %s, %s, %s, %s, %s, %s, %s, true, %s) + RETURNING * + """, + ( + dispatch["id"], + element_id, + role, + "identified" if identified else "gap", + gap_reason, + message_id, + digest, + Json(comparisons), + actor, + ), + ).fetchone() + if snapshot is not None: + _store_snapshot( + conn, + container_row["home_scope_id"], + actor, + snapshot, + media_root, + cleanup, + evidence_id=proof["id"], + ) + if fold == "evidence": + _set_binding_fold(conn, scope, element_id, "evidence") + if log: + _log(conn, dispatch["id"], actor, f"Nachweis ergänzt: {EVIDENCE_ROLES[role]}") + audit(conn, actor, container_row["home_scope_id"], "dispatch_evidence", "element", element_id, page) + return { + "id": str(proof["id"]), + "element_id": str(element_id), + "role": role, + "role_label": EVIDENCE_ROLES[role], + "source_state": proof["source_state"], + "gap_reason": gap_reason, + "manual": True, + "comparisons": comparisons, + } + + +def _identify_evidence(conn, actor, dispatch, element, page, media_root, cleanup, scope_id): + del cleanup, scope_id + comparisons = [] + if element["kind"] == "external_ref": + loaded = load_external(conn, actor, element["id"], None) + if isinstance(loaded, RetrievalFailure): + return False, loaded.message, None, None, [], None + if isinstance(loaded, MailPayload): + comparisons = _compare_mail(conn, actor, dispatch["id"], page, loaded) + payload = FilePayload("nachweis.eml", "message/rfc822", loaded.raw, sha256(loaded.raw)) + return True, None, loaded.parsed.message_id or None, payload.sha256, comparisons, payload + payload = FilePayload(loaded.filename, loaded.media_type, loaded.data, sha256(loaded.data)) + return True, None, None, payload.sha256, [], payload + if element["kind"] == "file": + loaded = load_file(conn, element["id"], media_root) + if isinstance(loaded, RetrievalFailure): + return False, loaded.message, None, None, [], None + payload = FilePayload(loaded.filename, loaded.media_type, loaded.data, sha256(loaded.data)) + return True, None, None, payload.sha256, [], payload + if element["kind"] == "note": + text = _note_bytes(conn, element["id"]) + if text is None: + return False, "Der Nachweistext liegt nicht vor.", None, None, [], None + payload = FilePayload(display_filename(element["title"] + ".json"), "application/json", text, sha256(text)) + return True, None, None, payload.sha256, [], payload + return False, "Für diesen Nachweis liegt kein sicherbares Original vor.", None, None, [], None + + +def _compare_mail(conn, actor: UUID, dispatch_id: UUID, page: Ctx, mail: MailPayload) -> list[dict]: + known = [] + for item in _item_rows(conn, dispatch_id): + element = _readable(conn, actor, item["element_id"], page) + if element is None or not item["content_sha256"]: + continue + known.append( + { + "element_id": str(item["element_id"]), + "title": item["title"], + "sha256": item["content_sha256"], + "filename": item["filename"] or item["title"], + } + ) + return _comparison_rows(mail, known) + + +def _base_filename(filename: str) -> str: + return re.sub(r" \(\d+\)(\.[^.]+)$", r"\1", filename) + + +def _comparison_rows(mail: MailPayload, known: list[dict]) -> list[dict]: + rows = [] + for attachment in mail.parsed.attachments: + base = _base_filename(attachment.filename) + same_bytes = [item for item in known if item["sha256"] == attachment.sha256] + same_name = [ + item + for item in known + if item["sha256"] != attachment.sha256 and (item["filename"] == attachment.filename or item["filename"] == base) + ] + if same_bytes: + disposition = "same_bytes" + matches = same_bytes + elif same_name: + disposition = "same_name_different_bytes" + matches = same_name + else: + disposition = "distinct" + matches = [] + rows.append( + { + "filename": attachment.filename, + "sha256": attachment.sha256, + "disposition": disposition, + "manual": True, + "matches": [{"element_id": item["element_id"], "title": item["title"]} for item in matches], + } + ) + return rows + + +def _capture_item(conn, actor, dispatch, element, role: str, position: int, page: Ctx, historical_gap: bool, media_root: str, cleanup: list[Path]) -> str: + dates = _dates(conn, element["id"]) + title = element["title"] + filename = None + content_type = None + digest = None + byte_size = None + note_document = None + external_source = None + external_id = None + gap_reason = None + payload = None + if historical_gap: + state = "gap" + gap_reason = _HISTORICAL_GAP + else: + payload, gap_reason, note_document, external_source, external_id = _working_bytes( + conn, actor, element, media_root + ) + if payload is None: + state = "gap" + else: + state = "snapshot" + filename = payload.filename + content_type = payload.media_type + digest = sha256(payload.data) + byte_size = len(payload.data) + item = conn.execute( + """ + INSERT INTO dispatch_items ( + dispatch_id, role, position, element_id, title, document_on, filename, + content_type, byte_size, content_sha256, source_state, gap_reason, + note_document, external_source, external_id + ) + VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) + RETURNING id + """, + ( + dispatch["id"], + role, + position, + element["id"], + title, + dates["document_on"], + filename, + content_type, + byte_size, + digest, + state, + gap_reason, + Json(note_document) if note_document is not None else None, + external_source, + external_id, + ), + ).fetchone() + if payload is not None: + _store_snapshot( + conn, + dispatch["home_scope_id"], + actor, + payload, + media_root, + cleanup, + dispatch_item_id=item["id"], + ) + del page + return state + + +def _working_bytes(conn, actor, element, media_root: str): + if element["kind"] == "note": + raw = _note_bytes(conn, element["id"]) + if raw is None: + return None, "Der Text des Schreibens liegt nicht vor.", None, None, None + document = json.loads(raw.decode("utf-8")) + payload = FilePayload(display_filename(element["title"] + ".json"), "application/json", raw, sha256(raw)) + return payload, None, document, None, None + if element["kind"] == "file": + loaded = load_file(conn, element["id"], media_root) + if isinstance(loaded, RetrievalFailure): + return None, loaded.message, None, None, None + return loaded, None, None, None, None + if element["kind"] == "external_ref": + ref = conn.execute( + "SELECT source, external_id FROM external_references WHERE element_id = %s", + (element["id"],), + ).fetchone() + loaded = load_external(conn, actor, element["id"], None) + source = ref["source"] if ref else None + external_id = ref["external_id"] if ref else None + if isinstance(loaded, RetrievalFailure): + return None, loaded.message, None, source, external_id + if isinstance(loaded, MailPayload): + payload = FilePayload("nachricht.eml", "message/rfc822", loaded.raw, sha256(loaded.raw)) + return payload, None, None, source, external_id + return loaded, None, None, source, external_id + return None, "Für diesen Inhalt ist keine sicherbare Fassung vorgesehen.", None, None, None + + +def _store_snapshot(conn, scope_id, actor, payload: FilePayload, media_root: str, cleanup: list[Path], dispatch_item_id: UUID | None = None, evidence_id: UUID | None = None) -> UUID: + snapshot_id = uuid4() + path = _snapshot_path(media_root, snapshot_id) + write_bytes(path, payload.data) + cleanup.append(path) + digest = sha256(payload.data) + conn.execute( + """ + INSERT INTO dispatch_snapshots ( + id, dispatch_item_id, evidence_id, home_scope_id, owner_subject_id, + original_filename, content_type, byte_size, sha256 + ) + VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s) + """, + ( + snapshot_id, + dispatch_item_id, + evidence_id, + scope_id, + actor, + display_filename(payload.filename), + payload.media_type or "application/octet-stream", + len(payload.data), + digest, + ), + ) + return snapshot_id + + +def _snapshot_file(conn, media_root: str, dispatch_item_id: UUID | None = None, evidence_id: UUID | None = None) -> FilePayload: + if dispatch_item_id is not None: + row = conn.execute( + "SELECT * FROM dispatch_snapshots WHERE dispatch_item_id = %s", + (dispatch_item_id,), + ).fetchone() + else: + row = conn.execute( + "SELECT * FROM dispatch_snapshots WHERE evidence_id = %s", + (evidence_id,), + ).fetchone() + if row is None: + raise not_found() + data = read_bytes(_snapshot_path(media_root, row["id"])) + if sha256(data) != row["sha256"]: + raise not_found() + return FilePayload(row["original_filename"], row["content_type"], data, row["sha256"]) + + +def _snapshot_path(media_root: str, snapshot_id: UUID) -> Path: + root = Path(media_root).resolve() + folder = (root / "dispatch").resolve() + folder.mkdir(parents=True, exist_ok=True) + target = (folder / str(snapshot_id)).resolve() + if target.parent != folder: + raise unprocessable("Ungültiger Speicherpfad.") + return target + + +def _snapshot_id(conn, dispatch_item_id: UUID | None = None, evidence_id: UUID | None = None) -> str | None: + if dispatch_item_id is not None: + row = conn.execute( + "SELECT id FROM dispatch_snapshots WHERE dispatch_item_id = %s", + (dispatch_item_id,), + ).fetchone() + else: + row = conn.execute( + "SELECT id FROM dispatch_snapshots WHERE evidence_id = %s", + (evidence_id,), + ).fetchone() + return None if row is None else str(row["id"]) + + +def _public_dispatch(conn, actor: UUID, dispatch, page: Ctx) -> dict: + items = [] + for item in _item_rows(conn, dispatch["id"]): + if _readable(conn, actor, item["element_id"], page) is None: + continue + items.append( + { + "id": str(item["id"]), + "role": item["role"], + "position": item["position"], + "element_id": str(item["element_id"]), + "title": item["title"], + "document_on": dump_date(item["document_on"]), + "filename": item["filename"], + "source_state": item["source_state"], + "gap_reason": item["gap_reason"], + "content_sha256": item["content_sha256"], + } + ) + evidence = [] + for proof in _evidence_rows(conn, dispatch["id"]): + element = _readable(conn, actor, proof["element_id"], page) + if element is None: + continue + evidence.append(_public_evidence(conn, proof, element, page)) + return { + "id": str(dispatch["id"]), + "host_element_id": str(dispatch["host_element_id"]), + "channel": dispatch["channel"], + "channel_label": CHANNELS[dispatch["channel"]], + "sent_on": dump_date(dispatch["sent_on"]), + "undated": dispatch["sent_on"] is None, + "counterpart": dispatch["counterpart"], + "completeness": dispatch["completeness"], + "provenance": dispatch["provenance"], + "recorded_at": dump_dt(dispatch["recorded_at"]), + "version": dispatch["version"], + "items": items, + "evidence": evidence, + "corrections": [ + { + "id": str(row["id"]), + "corrected_at": dump_dt(row["corrected_at"]), + "summary": row["summary"], + } + for row in conn.execute( + """ + SELECT id, corrected_at, summary + FROM dispatch_corrections + WHERE dispatch_id = %s + ORDER BY corrected_at, id + """, + (dispatch["id"],), + ).fetchall() + ], + } + + +def _evidence_source(conn, element_id: UUID) -> str | None: + row = conn.execute( + "SELECT source FROM external_references WHERE element_id = %s", + (element_id,), + ).fetchone() + return row["source"] if row else None + + +def _public_evidence(conn, proof, element, page: Ctx) -> dict: + del page + return { + "id": str(proof["id"]), + "element_id": str(element["id"]), + "title": element["title"], + "kind": element["kind"], + "source": _evidence_source(conn, element["id"]), + "role": proof["role"], + "role_label": EVIDENCE_ROLES[proof["role"]], + "source_state": proof["source_state"], + "gap_reason": proof["gap_reason"], + "manual": proof["manual"], + "comparisons": proof["comparisons"] or [], + "document_on": dump_date(_dates(conn, element["id"])["document_on"]), + } + + +def _attach_scope(conn, actor, bindings: list[dict], page: Ctx, scope: Ctx) -> None: + element_ids = [UUID(item["element"]["id"]) for item in bindings] + folds = { + str(row["id"]): row["list_fold"] + for row in conn.execute( + "SELECT id, list_fold FROM bindings WHERE context_kind = %s AND context_id = %s", + (scope.kind, scope.id), + ).fetchall() + } + uses = conn.execute( + """ + SELECT * FROM attachment_uses + WHERE context_kind = %s AND context_id = %s + ORDER BY position, id + """, + (scope.kind, scope.id), + ).fetchall() + by_host: dict[UUID, list] = {} + hosts_of: dict[UUID, list] = {} + for use in uses: + by_host.setdefault(use["host_element_id"], []).append(use) + hosts_of.setdefault(use["attachment_element_id"], []).append(use) + dispatch_rows = conn.execute( + """ + SELECT * FROM dispatches + WHERE context_kind = %s AND context_id = %s + ORDER BY sent_on NULLS LAST, recorded_at, id + """, + (scope.kind, scope.id), + ).fetchall() + by_dispatch_host: dict[UUID, list] = {} + for row in dispatch_rows: + by_dispatch_host.setdefault(row["host_element_id"], []).append(row) + visible = set(element_ids) + for binding in bindings: + element_id = UUID(binding["element"]["id"]) + attachments = [] + for use in by_host.get(element_id, []): + if use["attachment_element_id"] not in visible and _readable(conn, actor, use["attachment_element_id"], page) is None: + continue + if _readable(conn, actor, use["attachment_element_id"], page) is None: + continue + target = next((item for item in bindings if UUID(item["element"]["id"]) == use["attachment_element_id"]), None) + element = target["element"] if target else _element_public(conn, get_element(conn, use["attachment_element_id"]), page) + attachments.append( + { + "id": str(use["id"]), + "element_id": str(use["attachment_element_id"]), + "binding_id": target["id"] if target else _binding_id(conn, scope, use["attachment_element_id"]), + "title": element["title"], + "kind": element["kind"], + "position": use["position"], + "document_on": (element.get("dates") or {}).get("document_on"), + "label": use["label"], + } + ) + hosts = [] + for use in hosts_of.get(element_id, []): + if _readable(conn, actor, use["host_element_id"], page) is None: + continue + host_binding = next((item for item in bindings if UUID(item["element"]["id"]) == use["host_element_id"]), None) + host_element = host_binding["element"] if host_binding else _element_public(conn, get_element(conn, use["host_element_id"]), page) + hosts.append({"element_id": str(use["host_element_id"]), "title": host_element["title"]}) + dispatches = [] + for dispatch in by_dispatch_host.get(element_id, []): + public = _public_dispatch(conn, actor, dispatch, page) + for proof in public["evidence"]: + match = next((item for item in bindings if item["element"]["id"] == proof["element_id"]), None) + proof["listed_fold"] = folds.get(match["id"]) if match else None + dispatches.append(public) + binding["correspondence"] = { + "fold": folds.get(binding["id"]), + "attachments": attachments, + "hosts": hosts, + "dispatches": dispatches, + } + + +def _element_public(conn, element, page: Ctx) -> dict: + del page + if element is None: + return {"title": "", "kind": "", "dates": {}} + dates = _dates(conn, element["id"]) + body = { + "id": str(element["id"]), + "title": element["title"], + "kind": element["kind"], + "dates": { + "document_on": dump_date(dates["document_on"]), + "sent_on": dump_date(dates["sent_on"]), + "received_on": dump_date(dates["received_on"]), + }, + } + source = _evidence_source(conn, element["id"]) + if source: + body["external"] = {"source": source} + return body + + +def _readable_uses(conn, actor, scope: Ctx, host_id: UUID, page: Ctx) -> list[dict]: + found = [] + for use in _uses(conn, scope, host_id): + element = _readable(conn, actor, use["attachment_element_id"], page) + if element is None: + continue + rights = element_rights(conn, actor, element, page) + body = _element_public(conn, element, page) + body["rights"] = rights.as_dict() + found.append({"id": use["id"], "element": body}) + return found + + +def _bound_element(conn, actor, element_id: UUID, page: Ctx, scope: Ctx) -> dict: + element = _readable(conn, actor, element_id, page) + if element is None: + raise not_found() + binding = conn.execute( + """ + SELECT id FROM bindings + WHERE element_id = %s AND context_kind = %s AND context_id = %s + """, + (element_id, scope.kind, scope.id), + ).fetchone() + if binding is None: + raise unprocessable("Das Schriftstück ist in diesem Zusammenhang nicht eingebunden.") + return element + + +def _readable(conn, actor: UUID, element_id: UUID, page: Ctx): + element = get_element(conn, element_id) + if element is None or not element_rights(conn, actor, element, page).read: + return None + return element + + +def _uses(conn, scope: Ctx, host_id: UUID): + return conn.execute( + """ + SELECT * FROM attachment_uses + WHERE context_kind = %s AND context_id = %s AND host_element_id = %s + ORDER BY position, id + """, + (scope.kind, scope.id, host_id), + ).fetchall() + + +def _dispatch_rows(conn, scope: Ctx, host_id: UUID): + return conn.execute( + """ + SELECT * FROM dispatches + WHERE context_kind = %s AND context_id = %s AND host_element_id = %s + ORDER BY sent_on NULLS LAST, recorded_at, id + """, + (scope.kind, scope.id, host_id), + ).fetchall() + + +def _item_rows(conn, dispatch_id: UUID): + return conn.execute( + "SELECT * FROM dispatch_items WHERE dispatch_id = %s ORDER BY position, id", + (dispatch_id,), + ).fetchall() + + +def _evidence_rows(conn, dispatch_id: UUID): + return conn.execute( + "SELECT * FROM dispatch_evidence WHERE dispatch_id = %s ORDER BY created_at, id", + (dispatch_id,), + ).fetchall() + + +def _dispatch_in(conn, dispatch_id: UUID, scope: Ctx): + row = conn.execute("SELECT * FROM dispatches WHERE id = %s", (dispatch_id,)).fetchone() + if row is None or row["context_kind"] != scope.kind or row["context_id"] != scope.id: + raise not_found() + return row + + +def _bump_dispatch(conn, dispatch_id: UUID, expected: int) -> int: + row = conn.execute( + """ + UPDATE dispatches + SET version = version + 1, updated_at = now() + WHERE id = %s AND version = %s + RETURNING version + """, + (dispatch_id, expected), + ).fetchone() + if row is None: + current = conn.execute("SELECT id FROM dispatches WHERE id = %s", (dispatch_id,)).fetchone() + if current is None: + raise not_found() + raise conflict() + return int(row["version"]) + + +def _reaches(conn, scope: Ctx, start: UUID, target: UUID) -> bool: + seen = set() + stack = [start] + while stack: + current = stack.pop() + if current == target: + return True + if current in seen: + continue + seen.add(current) + rows = conn.execute( + """ + SELECT attachment_element_id FROM attachment_uses + WHERE context_kind = %s AND context_id = %s AND host_element_id = %s + """, + (scope.kind, scope.id, current), + ).fetchall() + stack.extend(row["attachment_element_id"] for row in rows) + return False + + +def _is_attachment(conn, scope: Ctx, element_id: UUID) -> bool: + return ( + conn.execute( + """ + SELECT id FROM attachment_uses + WHERE context_kind = %s AND context_id = %s AND attachment_element_id = %s + """, + (scope.kind, scope.id, element_id), + ).fetchone() + is not None + ) + + +def _is_evidence(conn, scope: Ctx, element_id: UUID) -> bool: + return ( + conn.execute( + """ + SELECT e.id + FROM dispatch_evidence e + JOIN dispatches d ON d.id = e.dispatch_id + WHERE d.context_kind = %s AND d.context_id = %s AND e.element_id = %s + """, + (scope.kind, scope.id, element_id), + ).fetchone() + is not None + ) + + +def _set_binding_fold(conn, scope: Ctx, element_id: UUID, fold: str) -> None: + conn.execute( + """ + UPDATE bindings SET list_fold = %s + WHERE element_id = %s AND context_kind = %s AND context_id = %s + """, + (fold, element_id, scope.kind, scope.id), + ) + + +def _clear_evidence_fold(conn, scope: Ctx, element_id: UUID) -> None: + if _is_evidence(conn, scope, element_id): + return + conn.execute( + """ + UPDATE bindings SET list_fold = NULL + WHERE element_id = %s AND context_kind = %s AND context_id = %s AND list_fold = 'evidence' + """, + (element_id, scope.kind, scope.id), + ) + + +def _clear_attachment_fold(conn, scope: Ctx, element_id: UUID) -> None: + if _is_attachment(conn, scope, element_id): + return + conn.execute( + """ + UPDATE bindings SET list_fold = NULL + WHERE element_id = %s AND context_kind = %s AND context_id = %s AND list_fold = 'attachment' + """, + (element_id, scope.kind, scope.id), + ) + + +def _binding_id(conn, scope: Ctx, element_id: UUID) -> str | None: + row = conn.execute( + """ + SELECT id FROM bindings + WHERE element_id = %s AND context_kind = %s AND context_id = %s + """, + (element_id, scope.kind, scope.id), + ).fetchone() + return None if row is None else str(row["id"]) + + +def _dates(conn, element_id: UUID) -> dict: + row = conn.execute("SELECT document_on, sent_on, received_on FROM correspondence_dates WHERE element_id = %s", (element_id,)).fetchone() + if row is None: + return {"document_on": None, "sent_on": None, "received_on": None} + return row + + +def _note_bytes(conn, element_id: UUID) -> bytes | None: + row = conn.execute("SELECT document_json FROM note_bodies WHERE element_id = %s", (element_id,)).fetchone() + if row is None: + return None + return json.dumps(row["document_json"], ensure_ascii=False, sort_keys=True).encode("utf-8") + + +def _counterpart(value: str | None) -> str | None: + if value is None: + return None + text = value.strip() + if not text: + return None + if len(text) > 200: + raise unprocessable("Das Gegenüber ist zu lang.") + return text + + +def _completeness(states: list[str]) -> str: + if states and all(item == "snapshot" for item in states): + return "secured" + if any(item == "snapshot" for item in states): + return "partial" + return "gap" + + +def _log(conn, dispatch_id: UUID, actor: UUID, summary: str) -> None: + conn.execute( + """ + INSERT INTO dispatch_corrections (dispatch_id, corrected_by, summary) + VALUES (%s, %s, %s) + """, + (dispatch_id, actor, summary[:500]), + ) + + +def _delete_dispatch_files(conn, dispatch_id: UUID, media_root: str) -> list[Path]: + paths = [] + rows = conn.execute( + """ + SELECT s.id + FROM dispatch_snapshots s + LEFT JOIN dispatch_items i ON i.id = s.dispatch_item_id + LEFT JOIN dispatch_evidence e ON e.id = s.evidence_id + WHERE i.dispatch_id = %s OR e.dispatch_id = %s + """, + (dispatch_id, dispatch_id), + ).fetchall() + for row in rows: + paths.append(_snapshot_path(media_root, row["id"])) + return paths diff --git a/backend/app/exporting.py b/backend/app/exporting.py index 9686872..79e614a 100644 --- a/backend/app/exporting.py +++ b/backend/app/exporting.py @@ -21,6 +21,8 @@ from app.access.policy import ( ) from app.core.errors import not_found, unprocessable from app.core.timeutil import dump_dt, utcnow +from app.correspondence import GapNote, export_children, load_snapshot_file +from app.mailpack import parse_mail from app.present import _chapters, _ordered_bindings from app.retrieval import ( FilePayload, @@ -30,7 +32,7 @@ from app.retrieval import ( load_file, ) -_NODE_KINDS = {"binding", "annotation"} +_NODE_KINDS = {"binding", "annotation", "attachment", "dispatch_item", "evidence"} _DIRECTION = {"in": "Eingang", "out": "Ausgang"} _KIND_LABEL = { "note": "Notiz", @@ -62,6 +64,10 @@ class ExportNode: annotation_author: str | None = None annotation_at: str | None = None label: str = "" + relation: str | None = None + edition: str | None = None + gap_reason: str | None = None + snapshot_id: str | None = None def public(self) -> dict: return { @@ -118,6 +124,7 @@ class Entry: notice: str | None replies: list[tuple[str, str]] recorded_at: str | None + notes: tuple[str, ...] = () def public(self) -> dict: return { @@ -129,6 +136,15 @@ class Entry: } +@dataclass +class OverviewNode: + kind: str + title: str + label: str | None = None + entry: Entry | None = None + children: list[OverviewNode] = field(default_factory=list) + + @dataclass class ExportPlan: matter_title: str @@ -202,6 +218,16 @@ def _plan(conn, actor, matter_id, context, selected, assignments, media_root) -> if node.kind == "annotation": loaded[node.id] = node.annotation_body or "" continue + if node.gap_reason and node.kind in {"dispatch_item", "evidence"}: + loaded[node.id] = GapNote(node.gap_reason) + continue + if node.snapshot_id and node.kind in {"dispatch_item", "evidence"}: + result = load_snapshot_file(conn, UUID(node.snapshot_id), media_root) + if isinstance(result, RetrievalFailure): + issues.append(Issue(node.id, node.title, result.code, result.message, [dict(item) for item in result.choices])) + continue + loaded[node.id] = result + continue if node.content_kind == "note": loaded[node.id] = _note_text(conn, UUID(node.element_id)) continue @@ -217,7 +243,10 @@ def _plan(conn, actor, matter_id, context, selected, assignments, media_root) -> loaded[node.id] = result if isinstance(result, MailPayload): notices.extend(_retention_notices(node, result, hashes)) + notices.extend(_bundle_notices(root, chosen)) + notices.extend(_correspondence_export_notices(root, chosen)) issues.extend(_reference_issues(conn, actor, page, leaves)) + issues.extend(_attachment_load_issues(root, chosen, loaded)) generated = dump_dt(utcnow()) or "" if issues: entries = [ @@ -226,9 +255,24 @@ def _plan(conn, actor, matter_id, context, selected, assignments, media_root) -> ] return ExportPlan(matter["title"], generated, False, issues, notices, entries, [], root) files, entries = _layout(root, chosen, loaded, notices, generated) + emit_issues = _attachment_emit_issues(root, chosen, loaded, entries) + if emit_issues: + return ExportPlan( + matter["title"], + generated, + False, + emit_issues, + notices, + [ + Entry(node.id, node.title, _label(node), None, [], node.dates or {}, node.direction, node.summary, None, [], _recorded(node)) + for node in leaves + ], + [], + root, + ) _attach_replies(conn, actor, page, entries, leaves) _stitch_replies(files, entries) - files = _with_overview(matter["title"], generated, entries, files) + files = _with_overview(matter["title"], generated, root, entries, files) return ExportPlan(matter["title"], generated, True, [], notices, entries, files, root) @@ -252,9 +296,11 @@ def _matter_node(conn, actor, matter, page: Ctx, root: bool) -> ExportNode: for binding in bindings: grouped.setdefault(binding.get("chapter_id"), []).append(binding) children: list[ExportNode] = [] - children.extend(_chapter_nodes(chapters, grouped)) + children.extend(_chapter_nodes(conn, actor, page, chapters, grouped)) for binding in grouped.get(None, []): - children.append(_binding_node(binding)) + if not _stands_in_chapter(binding): + continue + children.append(_binding_node(conn, actor, binding, page)) rows = conn.execute( "SELECT * FROM matters WHERE parent_matter_id = %s AND deleted_at IS NULL ORDER BY title, id", (matter["id"],), @@ -275,16 +321,33 @@ def _matter_node(conn, actor, matter, page: Ctx, root: bool) -> ExportNode: ) -def _chapter_nodes(chapters: list[dict], grouped: dict[str | None, list]) -> list[ExportNode]: +def _stands_in_chapter(binding: dict) -> bool: + """A folded attachment or receipt stays under its letter, not as another chapter row.""" + pack = binding.get("correspondence") or {} + if pack.get("fold") in {"attachment", "evidence"}: + return False + if pack.get("hosts"): + return False + return True + + +def _chapter_nodes(conn, actor, page, chapters: list[dict], grouped: dict[str | None, list]) -> list[ExportNode]: by_parent: dict[str | None, list[dict]] = {} for chapter in chapters: by_parent.setdefault(chapter.get("parent_chapter_id"), []).append(chapter) - def build(parent_id: str | None) -> list[ExportNode]: + def build(parent_id: str | None, path: str) -> list[ExportNode]: nodes: list[ExportNode] = [] for chapter in by_parent.get(parent_id, []): - nested = build(chapter["id"]) - items = [_binding_node(item) for item in grouped.get(chapter["id"], [])] + chapter_path = f"{path} / {chapter['title']}" if path else chapter["title"] + nested = build(chapter["id"], chapter_path) + items = [] + for item in grouped.get(chapter["id"], []): + if not _stands_in_chapter(item): + continue + node = _binding_node(conn, actor, item, page) + node.relation = f"Kapitel: {chapter_path}" + items.append(node) if not items and not nested: continue nodes.append( @@ -301,10 +364,10 @@ def _chapter_nodes(chapters: list[dict], grouped: dict[str | None, list]) -> lis ) return nodes - return build(None) + return build(None, "") -def _binding_node(binding: dict) -> ExportNode: +def _binding_node(conn, actor, binding: dict, page) -> ExportNode: element = binding["element"] rights = binding["rights"] content_kind = _content_kind(element) @@ -340,9 +403,58 @@ def _binding_node(binding: dict) -> ExportNode: label="Anmerkung", ) ) + for spec in export_children(conn, actor, binding, page): + node.children.append(_spec_node(spec)) return node +def _spec_node(spec: dict) -> ExportNode: + if spec["kind"] == "dispatch": + return ExportNode( + id=spec["id"], + kind="dispatch", + title=spec["title"], + exportable=True, + reason=None, + content_kind=None, + children=[_spec_node(child) for child in spec["children"]], + label=spec.get("label") or "Versand", + ) + element = spec.get("element") + if spec["kind"] == "dispatch_item": + content_kind = _item_content_kind(spec) + else: + content_kind = _content_kind(element) if element else "file" + rights = spec.get("rights") or {} + exportable = bool(rights.get("read") and rights.get("share")) + return ExportNode( + id=spec["id"], + kind=spec["kind"], + title=spec["title"], + exportable=exportable, + reason=None if exportable else "Für die Übergabe fehlt die Freigabebefugnis.", + content_kind=content_kind, + element_id=(element or {}).get("id") or spec.get("element_id"), + dates=spec.get("dates"), + label={"attachment": "Anlage", "dispatch_item": "Versandfassung", "evidence": "Nachweis"}.get(spec["kind"], "Inhalt"), + relation=spec.get("relation"), + edition=spec.get("edition"), + gap_reason=spec.get("gap_reason"), + snapshot_id=spec.get("snapshot_id"), + ) + + +def _item_content_kind(spec: dict) -> str: + if spec.get("gap_reason"): + return "gap" + media = spec.get("content_type") or "" + if media == "message/rfc822": + return "email" + if media == "application/json": + return "note" + return "file" + + def _content_kind(element: dict) -> str: if element["kind"] == "note": return "note" @@ -464,62 +576,258 @@ def _layout(root, chosen, loaded, notices, generated: str): return def emit(node: ExportNode, prefix: list[str]) -> None: - folders = [child for child in node.children if _is_folder(child) and _included(child, chosen)] - numbers = {child.id: index + 1 for index, child in enumerate(folders)} + numbered = _ordered_numbered_children(node, chosen) + numbers = {child.id: index + 1 for index, child in enumerate(numbered)} for child in node.children: if child.id in numbers: folder = prefix + [_folder_name(child.title, numbers[child.id], prefix)] - if child.kind == "binding" and child.content_kind == "email": - _emit_email(child, folder, add, rewrite, entries, loaded, notice_for, generated) + if child.kind == "binding": + if child.content_kind == "email": + _emit_email(child, folder, add, rewrite, entries, loaded, notice_for, generated, chosen) + else: + _emit_loose(child, folder, add, entries, loaded, notice_for, generated, chosen) else: emit(child, folder) else: - _emit_loose(child, prefix, add, entries, loaded, notice_for, generated) + _emit_loose(child, prefix, add, entries, loaded, notice_for, generated, chosen) emit(root, []) return files, entries -def _emit_email(node, folder, add, rewrite, entries, loaded, notice_for, generated: str) -> None: - mail = loaded[node.id] - assert isinstance(mail, MailPayload) - eml = add(folder + ["nachricht.eml"], mail.raw) - attachments = [] - for attachment in mail.parsed.attachments: - path = add(folder + ["anlagen", attachment.filename], attachment.data) - attachments.append((attachment.filename, path)) - notice = "\n".join(notice_for.get(node.id, [])) - text_path = add(folder + ["lesefassung.md"], b"") - readable = _mail_markdown(node, mail, text_path, eml, attachments, notice, generated) - rewrite(text_path, readable.encode("utf-8")) - links = [("EML-Original", eml), ("Lesefassung", text_path), *attachments] - entries.append(_entry(node, eml, links, notice or None)) - for child in node.children: - if child.kind == "annotation" and child.id in loaded: - path = add(folder + [_annotation_filename(child, folder)], _annotation_markdown(child).encode("utf-8")) - entries.append(_entry(child, path, [], None)) +def _emit_email(node, folder, add, rewrite, entries, loaded, notice_for, generated: str, chosen: set[str]) -> None: + mail = loaded.get(node.id) + if isinstance(mail, MailPayload): + eml = add(folder + ["nachricht.eml"], mail.raw) + attachments = [] + for attachment in mail.parsed.attachments: + path = add(folder + ["anlagen", attachment.filename], attachment.data) + attachments.append((attachment.filename, path)) + notice = "\n".join(notice_for.get(node.id, [])) + text_path = add(folder + ["lesefassung.md"], b"") + readable = _mail_markdown(node, mail, text_path, eml, attachments, notice, generated) + rewrite(text_path, readable.encode("utf-8")) + links = [("EML-Original", eml), ("Lesefassung", text_path), *attachments] + entries.append(_entry(node, eml, links, notice or None)) + for child in node.children: + if child.kind == "annotation" and child.id in loaded: + path = add(folder + [_annotation_filename(child, folder)], _annotation_markdown(child).encode("utf-8")) + entries.append(_entry(child, path, [], None)) + _emit_related(node, folder, add, entries, loaded, notice_for, chosen, generated) -def _emit_loose(node, prefix, add, entries, loaded, notice_for, generated: str) -> None: +def _emit_loose(node, prefix, add, entries, loaded, notice_for, generated: str, chosen: set[str]) -> None: + bundle = _binding_bundle(prefix, node) if node.kind == "binding" else prefix if node.kind == "binding" and node.id in loaded and node.content_kind != "email": payload = loaded[node.id] if node.content_kind == "note": - path = add(prefix + [f"{_segment(node.title)}.md"], _note_markdown(node, str(payload), generated).encode("utf-8")) + path = add(bundle + [f"{_segment(node.title)}.md"], _note_markdown(node, str(payload), generated).encode("utf-8")) entries.append(_entry(node, path, [], None)) elif isinstance(payload, FilePayload): - path = add(prefix + [payload.filename], payload.data) - entries.append(_entry(node, path, [(payload.filename, path)], None)) + name = _handover_filename(node, payload.filename) + path = add(bundle + [name], payload.data) + entries.append(_entry(node, path, [(name, path)], None)) if node.kind == "binding": for child in node.children: if child.kind == "annotation" and child.id in loaded and not (node.content_kind == "email" and node.id in loaded): - path = add(prefix + [_annotation_filename(child, prefix)], _annotation_markdown(child).encode("utf-8")) + path = add(bundle + [_annotation_filename(child, bundle)], _annotation_markdown(child).encode("utf-8")) entries.append(_entry(child, path, [], None)) elif node.kind == "annotation" and node.id in loaded: path = add(prefix + [_annotation_filename(node, prefix)], _annotation_markdown(node).encode("utf-8")) entries.append(_entry(node, path, [], None)) + if node.kind == "binding": + _emit_related(node, prefix, add, entries, loaded, notice_for, chosen, generated) + + +def _binding_bundle(prefix: list[str], _host: ExportNode) -> list[str]: + """Main file and correspondence sit inside the numbered entry folder.""" + return list(prefix) + + +def _handover_filename(node: ExportNode, original: str) -> str: + """Readable name for recipients; file bytes stay unchanged.""" + stem = _segment(node.title) + suffix = PurePosixPath(original or "").suffix + if suffix: + return f"{stem}{suffix}" + return stem or _file_segment(original) + + +def _related_bucket(node: ExportNode) -> str: + if node.kind == "attachment": + return "Anlagen" + if node.kind == "evidence": + return "Belege" + if node.kind == "dispatch_item": + relation = node.relation or "" + if "Anlage → Versand" in relation: + return "Anlagen" + return "Belege" + return "Belege" + + +def _emit_related(node, prefix, add, entries, loaded, notice_for, chosen, generated: str) -> None: + bundle = _binding_bundle(prefix, node) + for child in node.children: + if child.kind == "attachment" and child.id in chosen and child.id in loaded: + _emit_one(child, bundle + ["Anlagen"], add, entries, loaded, notice_for, generated) + if child.kind != "dispatch": + continue + picked = [item for item in child.children if item.id in chosen and item.id in loaded] + for item in picked: + _emit_one(item, bundle + [_related_bucket(item)], add, entries, loaded, notice_for, generated) + + +def _emit_one(node, prefix, add, entries, loaded, notice_for, generated: str) -> None: + payload = loaded[node.id] + notice = "\n".join(notice_for.get(node.id, [])) + if isinstance(payload, GapNote): + path = add(prefix + [f"{_segment(node.title)}-luecke.txt"], payload.reason.encode("utf-8")) + entries.append(_entry(node, path, [( "Lücke", path)], notice or payload.reason)) + return + if isinstance(payload, MailPayload) or (isinstance(payload, FilePayload) and payload.media_type == "message/rfc822"): + raw = payload.raw if isinstance(payload, MailPayload) else payload.data + eml = add(prefix + ["nachricht.eml"], raw) + links = [("EML-Original", eml)] + try: + parsed = payload.parsed if isinstance(payload, MailPayload) else parse_mail(raw) + for attachment in parsed.attachments: + path = add(prefix + ["anlagen", attachment.filename], attachment.data) + links.append((attachment.filename, path)) + except Exception: + links.append(("Hinweis", eml)) + entries.append(_entry(node, eml, links, notice or None)) + return + if isinstance(payload, str): + path = add(prefix + [f"{_segment(node.title)}.md"], _note_markdown(node, payload, generated).encode("utf-8")) + entries.append(_entry(node, path, [], notice or None)) + return + if isinstance(payload, FilePayload) and payload.media_type == "application/json": + path = add(prefix + [f"{_segment(node.title)}.stand.json"], payload.data) + text = _json_note(payload.data) + reading = add(prefix + [f"{_segment(node.title)}.md"], _note_markdown(node, text, generated).encode("utf-8")) + entries.append(_entry(node, reading, [("Gesicherter Text", path), ("Lesefassung", reading)], notice or None)) + return + if isinstance(payload, FilePayload): + name = _handover_filename(node, payload.filename) + path = add(prefix + [name], payload.data) + entries.append(_entry(node, path, [(name, path)], notice or None)) + + +def _correspondence_export_notices(root: ExportNode, chosen: set[str]) -> list[Notice]: + notices: list[Notice] = [] + for node in _walk(root): + if node.kind != "binding": + continue + attachments = [child for child in node.children if child.kind == "attachment"] + if not attachments: + continue + blocked = [child for child in attachments if not child.exportable] + if blocked: + sample = blocked[0] + count = len(blocked) + word = "Anlage ist" if count == 1 else f"{count} Anlagen sind" + notices.append( + Notice( + sample.id, + f"{word} dem Schreiben zugeordnet, aber für die Übergabe nicht freigegeben. " + "Sie erscheinen in der Auswahl nicht und fehlen im Paket.", + ) + ) + if node.id not in chosen: + continue + missing = [child for child in attachments if child.exportable and child.id not in chosen] + if missing: + sample = missing[0] + count = len(missing) + word = "Anlage ist" if count == 1 else f"{count} Anlagen sind" + notices.append( + Notice( + sample.id, + f"{word} dem Schreiben zugeordnet, aber nicht ausgewählt. Die Ausgabe ist unvollständig.", + ) + ) + return notices + + +def _attachment_load_issues(root: ExportNode, chosen: set[str], loaded: dict) -> list[Issue]: + issues: list[Issue] = [] + for node in _walk(root): + if node.kind != "attachment" or node.id not in chosen or not node.exportable: + continue + if node.id not in loaded: + issues.append( + Issue( + node.id, + node.title, + "missing", + "Die zugeordnete Anlage konnte nicht für die Ausgabe geladen werden.", + ) + ) + return issues + + +def _attachment_emit_issues(root: ExportNode, chosen: set[str], loaded: dict, entries: list[Entry]) -> list[Issue]: + emitted = {entry.node_id for entry in entries} + issues: list[Issue] = [] + for node in _walk(root): + if node.kind != "attachment" or node.id not in chosen or not node.exportable: + continue + if node.id not in loaded: + continue + if node.id in emitted: + continue + issues.append( + Issue( + node.id, + node.title, + "missing", + "Die ausgewählte Anlage fehlt in der erzeugten Paketstruktur.", + ) + ) + return issues + + +def _json_note(data: bytes) -> str: + import json + + try: + document = json.loads(data.decode("utf-8")) + except (UnicodeDecodeError, json.JSONDecodeError): + return "" + parts = [] + for block in document.get("blocks") or []: + if isinstance(block, dict): + text = str(block.get("text") or "").strip() + if text: + parts.append(text) + return "\n\n".join(parts) + + +def _bundle_notices(root: ExportNode, chosen: set[str]) -> list[Notice]: + found = [] + for node in _walk(root): + if node.kind != "dispatch": + continue + parts = [child for child in node.children if child.kind in {"dispatch_item", "evidence"}] + picked = [child for child in parts if child.id in chosen] + if not picked: + continue + if len(picked) < len(parts): + found.append( + Notice( + picked[0].id, + "Auszug dieses Versands. Die Ausgabe ist nicht der vollständige Versandstand.", + ) + ) + gaps = [child.gap_reason for child in picked if child.gap_reason] + if gaps: + found.append(Notice(picked[0].id, "Fehlende Fassung: " + " ".join(gaps))) + return found def _entry(node: ExportNode, path: str | None, links, notice: str | None) -> Entry: + notes = tuple(part for part in (node.relation, node.edition, node.gap_reason) if part) return Entry( node.id, node.title, @@ -532,6 +840,7 @@ def _entry(node: ExportNode, path: str | None, links, notice: str | None) -> Ent notice, [], _recorded(node), + notes, ) @@ -566,67 +875,313 @@ def _attach_replies(conn, actor, page, entries: list[Entry], leaves: list[Export entry.replies.append((target["title"], path)) -def _with_overview(title: str, generated: str, entries: list[Entry], files: list[tuple[str, bytes]]): - html_doc = _html_overview(title, generated, entries) - markdown = _markdown_overview(title, generated, entries) +def _with_overview(title: str, generated: str, root: ExportNode, entries: list[Entry], files: list[tuple[str, bytes]]): + by_id = {entry.node_id: entry for entry in entries} + sections = _overview_sections(root, by_id) + html_doc = _html_overview(title, generated, sections) + markdown = _markdown_overview(title, generated, sections) return [("index.html", html_doc.encode("utf-8")), ("inhalt.md", markdown.encode("utf-8")), *files] -def _html_overview(title: str, generated: str, entries: list[Entry]) -> str: - items = [] - for entry in entries: - bits = [f"
{html.escape(entry.kind_label)}
"] - facts = _fact_lines(entry, generated) - if facts: - bits.append("Kurzfassung: {html.escape(entry.summary)}
") - for label, path in _entry_links(entry): - bits.append(f'') - for reply_title, path in entry.replies: - bits.append( - f'Antwort auf {html.escape(reply_title)}
' - ) - if entry.notice: - bits.append(f"{html.escape(entry.notice)}
") - items.append(f"Die bestätigte Auswahl enthält keinen ausgebbaren Inhalt.
" +def _overview_sections(root: ExportNode, entries: dict[str, Entry]) -> list[OverviewNode]: + found: list[OverviewNode] = [] + for child in root.children: + section = _overview_node(child, entries) + if section is not None: + found.append(section) + return found + + +def _overview_node(node: ExportNode, entries: dict[str, Entry]) -> OverviewNode | None: + if node.kind in {"matter", "chapter"}: + children = [] + for child in node.children: + section = _overview_node(child, entries) + if section is not None: + children.append(section) + if not children: + return None + return OverviewNode("container", node.title, node.label or _KIND_LABEL.get(node.kind, ""), None, children) + if node.kind == "binding": + return _overview_binding(node, entries) + return None + + +def _overview_binding(node: ExportNode, entries: dict[str, Entry]) -> OverviewNode | None: + groups: list[OverviewNode] = [] + annotations = [ + OverviewNode("leaf", child.title, _label(child), entries[child.id]) + for child in node.children + if child.kind == "annotation" and child.id in entries + ] + if annotations: + groups.append(OverviewNode("group", "Anmerkungen", None, None, annotations)) + anlagen = [ + OverviewNode("leaf", child.title, _label(child), entries[child.id]) + for child in node.children + if child.kind == "attachment" and child.id in entries + ] + if anlagen: + groups.append(OverviewNode("group", "Anlagen", None, None, anlagen)) + belege: list[OverviewNode] = [] + versand: list[OverviewNode] = [] + for dispatch in node.children: + if dispatch.kind != "dispatch": + continue + items = [ + OverviewNode("leaf", item.title, _label(item), entries[item.id]) + for item in dispatch.children + if item.kind in {"dispatch_item", "evidence"} and item.id in entries + ] + if not items: + continue + bucket = "Belege" if (dispatch.label or "").startswith("Beleg") else (dispatch.label or "Versand") + target = belege if bucket == "Belege" else versand + target.append(OverviewNode("group", dispatch.title, bucket, None, items)) + if belege: + groups.append(OverviewNode("group", "Belege", None, None, belege)) + if versand: + groups.extend(versand) + host = entries.get(node.id) + if host is None and not groups: + return None + return OverviewNode("binding", node.title, node.label or _label(node), host, groups) + + +def _overview_toc(sections: list[OverviewNode], counter: list[int]) -> tuple[list[tuple[str, str]], list[int]]: + items: list[tuple[str, str]] = [] + + def walk(nodes: list[OverviewNode]) -> None: + for node in nodes: + if node.kind not in {"container", "binding"}: + continue + counter[0] += 1 + anchor = f"inhalt-{counter[0]}" + label = node.title + if node.label and node.kind == "container": + label = f"{node.label}: {node.title}" + items.append((anchor, label)) + if node.kind == "container": + walk(node.children) + + walk(sections) + return items, counter + + +def _html_overview(title: str, generated: str, sections: list[OverviewNode]) -> str: + counter = [0] + toc, _counter = _overview_toc(sections, counter) + counter[0] = 0 + toc_html = "" + if toc: + toc_html = ( + '" + ) + body = _html_overview_nodes(sections, generated, counter) or "Die bestätigte Auswahl enthält keinen ausgebbaren Inhalt.
" return ( "" f"Erzeugt am {html.escape(generated)}. Fachliche Daten stehen bei den einzelnen Inhalten und sind kein Erzeugungszeitpunkt.
" - f"{body}" + "" + f"