diff --git a/backend/app/exporting.py b/backend/app/exporting.py index ffba066..809ea3f 100644 --- a/backend/app/exporting.py +++ b/backend/app/exporting.py @@ -235,7 +235,9 @@ def _plan(conn, actor, matter_id, context, selected, assignments, media_root) -> 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 = [ @@ -244,6 +246,21 @@ 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) @@ -684,8 +701,83 @@ def _emit_one(node, prefix, add, entries, loaded, notice_for, generated: str) -> entries.append(_entry(node, reading, [("Gesicherter Text", path), ("Lesefassung", reading)], notice or None)) return if isinstance(payload, FilePayload): - path = add(prefix + [payload.filename], payload.data) - entries.append(_entry(node, path, [(payload.filename, path)], notice or None)) + 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: diff --git a/backend/tests/test_correspondence.py b/backend/tests/test_correspondence.py index 077911c..b398fab 100644 --- a/backend/tests/test_correspondence.py +++ b/backend/tests/test_correspondence.py @@ -384,7 +384,7 @@ def test_dispatch_keeps_the_secured_stand_and_distinct_mail_parts(client, db, mo selected = [node["id"] for node in nodes if node["kind"] in {"dispatch_item", "evidence"}] archive = _zip(ada, matter["id"], selected) names = archive.namelist() - snapshot = next(name for name in names if name.endswith("/Anlagen/lage.pdf")) + snapshot = next(name for name in names if name.endswith("/Anlagen/Lageplan.pdf")) assert archive.read(snapshot) == PDF_B eml_name = next(name for name in names if name.endswith("nachricht.eml")) assert b"Fachlicher Zusatz" in archive.read(eml_name) @@ -517,7 +517,7 @@ def test_dispatch_keeps_the_secured_stand_and_distinct_mail_parts(client, db, mo assert gaps assert all("Später geänderter Text" not in full.read(name).decode("utf-8") for name in gaps) assert all(b"%PDF" not in full.read(name) for name in gaps) - secured = next(name for name in full.namelist() if name.endswith("/Anlagen/lage.pdf")) + secured = next(name for name in full.namelist() if name.endswith("/Anlagen/Lageplan.pdf")) assert full.read(secured) == PDF_B diff --git a/backend/tests/test_export.py b/backend/tests/test_export.py index 08db3dd..c673c9e 100644 --- a/backend/tests/test_export.py +++ b/backend/tests/test_export.py @@ -901,7 +901,76 @@ def test_correspondence_attachments_stay_under_each_host_in_one_chapter(client, assert not any("anlagen-arbeitsstand" in name.lower() for name in names) first_paths = [name for name in names if "Erstes Schreiben/Anlagen/" in name.replace("\\", "/")] second_paths = [name for name in names if "Zweites Schreiben/Anlagen/" in name.replace("\\", "/")] - assert sorted(name.rsplit("/", 1)[-1] for name in first_paths) == ["a1.pdf", "a2.pdf"] - assert sorted(name.rsplit("/", 1)[-1] for name in second_paths) == ["a1.pdf", "b1.pdf"] + assert sorted(name.rsplit("/", 1)[-1] for name in first_paths) == ["Anlage A1.pdf", "Anlage A2.pdf"] + assert sorted(name.rsplit("/", 1)[-1] for name in second_paths) == ["Anlage B1.pdf", "Anlage B2.pdf"] assert any("Erstes Schreiben/Belege/" in name.replace("\\", "/") for name in names) assert not any(name.replace("\\", "/").startswith("01 Schriftwechsel/2026-04") for name in names) + + +def test_export_warns_when_assigned_attachment_is_not_shareable(client, db): + ada = Actor(client, "ada-share-gap@example.test", "Ada Share Gap") + register(db, ada.email, ada.name) + ada.login() + matter = ada.request("POST", "/api/v1/matters", json={"title": "Anlagenfreigabe"}, status=201) + letter = ada.request( + "POST", + "/api/v1/notes", + json={"title": "Schreiben", "document": document("Text"), "context": {"kind": "matter", "id": matter["id"]}}, + status=201, + ) + open_att = ada.request( + "POST", + "/api/v1/files", + data={"title": "Offene Anlage", "context_kind": "matter", "context_id": matter["id"]}, + files={"upload": ("offen.pdf", PDF_A, "application/pdf")}, + status=201, + ) + closed_att = ada.request( + "POST", + "/api/v1/files", + data={"title": "Gesperrte Anlage", "context_kind": "matter", "context_id": matter["id"]}, + files={"upload": ("gesperrt.pdf", PDF_B, "application/pdf")}, + status=201, + ) + detail = ada.request("GET", f"/api/v1/matters/{matter['id']}") + host = next(item for item in detail["bindings"] if item["element"]["title"] == "Schreiben") + ada.request( + "POST", + f"/api/v1/elements/{letter['id']}/attachments", + json={ + "expected_version": host["element"]["version"], + "context": {"kind": "matter", "id": matter["id"]}, + "attachment_element_ids": [open_att["id"], closed_att["id"]], + "fold": "attachment", + }, + status=201, + ) + ada.request( + "PUT", + "/api/v1/exceptions", + json={ + "resource_kind": "element", + "resource_id": closed_att["id"], + "context": {"kind": "matter", "id": matter["id"]}, + "remove_share": True, + }, + ) + tree = ada.request("GET", f"/api/v1/matters/{matter['id']}/export-tree")["tree"] + selected = [node["id"] for node in _walk(tree) if node["exportable"] and node["kind"] in {"binding", "attachment"}] + review = ada.request( + "POST", + f"/api/v1/matters/{matter['id']}/export/review", + json={"context": {"kind": "matter", "id": matter["id"]}, "selected": selected}, + ) + assert review["ready"] is True + assert any("nicht freigegeben" in item["message"] for item in review["notices"]) + response = _post_zip( + ada, + matter["id"], + {"context": {"kind": "matter", "id": matter["id"]}, "selected": selected}, + ) + assert response.status_code == 200, response.text + names = _archive(response.content).namelist() + anlagen = [name for name in names if "Schreiben/Anlagen/" in name.replace("\\", "/")] + assert len(anlagen) == 1 + assert anlagen[0].endswith("/Offene Anlage.pdf")