From f925661937e511c22a90473281b288b3601138cb Mon Sep 17 00:00:00 2001 From: Stommer Date: Sun, 25 Oct 2026 10:06:52 +0100 Subject: [PATCH] =?UTF-8?q?Warne=20vor=20bereits=20eingef=C3=BCgten=20Quel?= =?UTF-8?q?ldokumenten=20und=20biete=20Nicht=20einf=C3=BCgen,=20erneut=20e?= =?UTF-8?q?inf=C3=BCgen=20oder=20zus=C3=A4tzliches=20Einbinden=20an.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Prüfstufen hier, darin, Umfeld stehen als Kontoeinstellung bereit. Der Server antwortet bei einem sicheren Treffer mit 409 und bindet auf Wunsch das vorhandene Element zusätzlich ein. Co-Authored-By: Claude Sonnet 5.5 --- backend/app/api/externals.py | 52 +++++ backend/app/api/routes.py | 18 +- backend/app/api/schemas.py | 16 ++ backend/app/api/sources.py | 22 +-- backend/app/duplicates.py | 166 ++++++++++++++++ backend/app/main.py | 2 + backend/app/ops.py | 56 +++++- backend/app/preferences.py | 2 +- backend/app/present.py | 1 + .../20261025T090000Z_account_preferences.sql | 3 + backend/tests/test_duplicates.py | 126 ++++++++++++ backend/tests/test_preferences.py | 26 +-- docs/architecture/API_REFERENZ.md | 5 +- frontend/src/admin/DuplicatePreference.jsx | 6 +- frontend/src/pages/Cases.jsx | 39 ++-- frontend/src/pages/Sources.jsx | 181 +++++++++++++----- frontend/src/sources/duplicates.jsx | 122 ++++++++++++ frontend/src/sources/registry.jsx | 24 ++- frontend/src/styles.css | 14 ++ 19 files changed, 748 insertions(+), 133 deletions(-) create mode 100644 backend/app/api/externals.py create mode 100644 backend/app/duplicates.py create mode 100644 backend/tests/test_duplicates.py create mode 100644 frontend/src/sources/duplicates.jsx diff --git a/backend/app/api/externals.py b/backend/app/api/externals.py new file mode 100644 index 0000000..0f9d5ef --- /dev/null +++ b/backend/app/api/externals.py @@ -0,0 +1,52 @@ +"""Placing a source document into a container: a new reference, or one more binding of an existing element.""" + +from __future__ import annotations + +from fastapi import APIRouter, Depends +from fastapi.responses import JSONResponse +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 ExistingIn, ExternalIn +from app.ops import bind_existing_external, create_external, existing_externals + +router = APIRouter() + + +def _ctx_model(value) -> Ctx | None: + return None if value is None else Ctx(value.kind, value.id) + + +def place_external(conn, principal: Principal, body: ExternalIn) -> dict: + context = Ctx(body.context.kind, body.context.id) + placement = _ctx_model(body.placement) + if body.bind_element_id is not None: + return bind_existing_external(conn, principal.account_id, principal.scope_id, body.bind_element_id, context, placement, body.chapter_id) + return create_external( + conn, + principal.account_id, + principal.scope_id, + body.title, + body.source, + body.external_id, + context, + placement, + body.chapter_id, + body.dates.document_on, + body.dates.sent_on, + body.dates.received_on, + body.detail.model_dump(mode="json", exclude_none=True) if body.detail else None, + duplicate=body.duplicate, + ) + + +@router.post("/api/v1/external-refs/existing") +def existing(body: ExistingIn, principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn)): + """Read only: which of these documents are already present around the target. Nothing is written.""" + items = [ + {"source": item.source, "external_id": item.external_id, "connector_id": str(item.connector_id) if item.connector_id else None} + for item in body.items + ] + result = existing_externals(conn, principal.account_id, Ctx(body.context.kind, body.context.id), _ctx_model(body.placement), items) + return JSONResponse(result, headers={"Cache-Control": "no-store"}) diff --git a/backend/app/api/routes.py b/backend/app/api/routes.py index b40adab..14908c4 100644 --- a/backend/app/api/routes.py +++ b/backend/app/api/routes.py @@ -34,6 +34,7 @@ from app.access.policy import ( ) from app.access.limits import ensure_can_add_member from app.api.deps import Principal, get_conn, get_principal, get_session +from app.api.externals import place_external from app.api.schemas import ( AcceptInviteIn, AnnotationIn, @@ -113,7 +114,6 @@ from app.ops import ( create_chapter, create_cross_link, create_deadline, - create_external, create_file, create_matter, create_note, @@ -860,21 +860,7 @@ def post_annotation( @router.post("/api/v1/external-refs") def post_external(body: ExternalIn, principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn)): - element = create_external( - conn, - principal.account_id, - principal.scope_id, - body.title, - body.source, - body.external_id, - Ctx(body.context.kind, body.context.id), - _ctx_model(body.placement), - body.chapter_id, - body.dates.document_on, - body.dates.sent_on, - body.dates.received_on, - body.detail.model_dump(mode="json", exclude_none=True) if body.detail else None, - ) + element = place_external(conn, principal, body) return _created(f"/api/v1/elements/{element['id']}", {"id": str(element["id"]), "title": element["title"]}) diff --git a/backend/app/api/schemas.py b/backend/app/api/schemas.py index b381d64..f8d5dec 100644 --- a/backend/app/api/schemas.py +++ b/backend/app/api/schemas.py @@ -352,6 +352,22 @@ class ExternalIn(APIModel): chapter_id: UUID | None = None dates: DatesIn = DatesIn() detail: ExternalDetail | None = None + # "ask": a document that is already present at the account's check level answers 409. + # "insert": the user chose to insert it again. `bind_element_id` binds that element instead of creating one. + duplicate: Literal["ask", "insert"] = "ask" + bind_element_id: UUID | None = None + + +class ExistingItemIn(APIModel): + source: str = Field(min_length=1, max_length=40) + external_id: str = Field(min_length=1, max_length=200) + connector_id: UUID | None = None + + +class ExistingIn(APIModel): + context: ContextIn + placement: ContextIn | None = None + items: list[ExistingItemIn] = Field(min_length=1, max_length=100) class ConnectorIn(APIModel): diff --git a/backend/app/api/sources.py b/backend/app/api/sources.py index e8c7f37..6e4d70a 100644 --- a/backend/app/api/sources.py +++ b/backend/app/api/sources.py @@ -8,15 +8,15 @@ from fastapi import APIRouter, Depends, Request from fastapi.responses import JSONResponse, Response from psycopg import Connection -from app.access.policy import Ctx, element_rights, get_element, parse_query_context +from app.access.policy import element_rights, get_element, parse_query_context from app.api.deps import Principal, get_conn, get_principal -from app.api.routes import _created, _ctx_model +from app.api.routes import _created from app.api.schemas import ConnectorIn, ExternalIn, SourceAssignmentIn from app.core.errors import DOWNLOAD_REFUSED, forbidden, not_found, unprocessable, unsupported from app.mailpack import _inline_images, parse_mail, sanitize_html from app.remoteimages import RemoteImages from app.media.storage import content_disposition -from app.ops import create_external +from app.api.externals import place_external from app.retrieval import RetrievalFailure from app.sourcebind import assign_reference from app.sourceport import capability_payload, with_capabilities @@ -370,21 +370,7 @@ def read_mail( def link_source(body: ExternalIn, principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn)): if get_source(body.source) is None: raise unprocessable("Verknüpft werden registrierte Quellen.") - element = create_external( - conn, - principal.account_id, - principal.scope_id, - body.title, - body.source, - body.external_id, - Ctx(body.context.kind, body.context.id), - _ctx_model(body.placement), - body.chapter_id, - body.dates.document_on, - body.dates.sent_on, - body.dates.received_on, - body.detail.model_dump(mode="json", exclude_none=True) if body.detail else None, - ) + element = place_external(conn, principal, body) return _created(f"/api/v1/elements/{element['id']}", {"id": str(element["id"]), "title": element["title"]}) diff --git a/backend/app/duplicates.py b/backend/app/duplicates.py new file mode 100644 index 0000000..4ad1990 --- /dev/null +++ b/backend/app/duplicates.py @@ -0,0 +1,166 @@ +"""Documents of a source that are inserted into a container again. + +The same document in different Vorgänge stays allowed. This module only finds where a document is +already present around the target, so the dialog can warn and the user can decide. Levels (account +preference `duplicate_check`): + +- here: bound directly to the target. +- inside: also what the target shows: Teilvorgänge of a Vorgang, the linked Vorgänge of an Akte. +- around: also where the target lies: its Vorgang, the Akten it is linked to, the parent Akte. + +Only elements the actor may read are reported. A hit outside the actor's rights does not exist here. +""" + +from __future__ import annotations + +from uuid import UUID + +from app.access.policy import ( + Ctx, + case_rights, + element_rights, + get_case, + get_matter, + matter_rights, +) +from app.core.errors import Problem + +LEVELS = {"off": (), "here": ("here",), "inside": ("here", "inside"), "around": ("here", "inside", "around")} +_ORDER = {"here": 0, "inside": 1, "around": 2} + + +def _stored_connector(row: dict) -> str | None: + if row["connector_id"] is not None: + return str(row["connector_id"]) + stored = (row["detail"] or {}).get("connector_id") + return str(stored) if stored else None + + +def _candidates(conn, source: str, external_id: str, connector_id: str | None) -> list[dict]: + """Elements for the same document. `certain` is False when only one side knows the connection.""" + rows = conn.execute( + """ + SELECT e.id, e.title, e.owner_subject_id, er.connector_id, er.detail + FROM external_references er + JOIN elements e ON e.id = er.element_id + WHERE er.source = %s AND er.external_id = %s AND e.deleted_at IS NULL + """, + (source, external_id.strip()[:200]), + ).fetchall() + found = [] + for row in rows: + stored = _stored_connector(row) + if stored is not None and connector_id is not None and stored != connector_id: + continue + found.append(row | {"certain": stored == connector_id}) + return found + + +def _children(conn, matter_id: UUID) -> list[UUID]: + rows = conn.execute("SELECT id FROM matters WHERE parent_matter_id = %s AND deleted_at IS NULL", (matter_id,)).fetchall() + return [row["id"] for row in rows] + + +def _zones(conn, target: Ctx) -> dict[str, list[Ctx]]: + inside: list[Ctx] = [] + around: list[Ctx] = [] + if target.kind == "matter": + inside = [Ctx("matter", child) for child in _children(conn, target.id)] + matter = get_matter(conn, target.id) + anchors = [target.id] + if matter is not None and matter["parent_matter_id"] is not None: + around.append(Ctx("matter", matter["parent_matter_id"])) + anchors.append(matter["parent_matter_id"]) + links = conn.execute("SELECT DISTINCT case_id FROM matter_case_links WHERE matter_id = ANY(%s)", (anchors,)).fetchall() + around += [Ctx("case", row["case_id"]) for row in links] + else: + links = conn.execute("SELECT matter_id FROM matter_case_links WHERE case_id = %s", (target.id,)).fetchall() + for row in links: + inside.append(Ctx("matter", row["matter_id"])) + inside += [Ctx("matter", child) for child in _children(conn, row["matter_id"])] + case = get_case(conn, target.id) + if case is not None and case["parent_case_id"] is not None: + around.append(Ctx("case", case["parent_case_id"])) + return {"here": [target], "inside": inside, "around": around} + + +def _blocked(conn, case_id: UUID, element_id: UUID) -> bool: + row = conn.execute( + """ + SELECT 1 FROM case_link_blocks b JOIN matter_case_links l ON l.id = b.link_id + WHERE l.case_id = %s AND b.element_id = %s + """, + (case_id, element_id), + ).fetchone() + return row is not None + + +def _title_if_readable(conn, actor: UUID, container: Ctx) -> str | None: + if container.kind == "case": + case = get_case(conn, container.id) + return case["title"] if case is not None and case_rights(conn, actor, case).read else None + matter = get_matter(conn, container.id) + return matter["title"] if matter is not None and matter_rights(conn, actor, matter, None).read else None + + +def find_existing( + conn, + actor: UUID, + source: str, + external_id: str, + connector_id: str | None, + target: Ctx, + rights_context: Ctx | None, + level: str, +) -> list[dict]: + """Matches of one document at the chosen level, readable by the actor, nearest zone first.""" + zones = LEVELS.get(level, ()) + if not zones: + return [] + candidates = _candidates(conn, source, external_id, connector_id) + if not candidates: + return [] + layout = _zones(conn, target) + by_element: dict[UUID, dict] = {} + for candidate in candidates: + bindings = conn.execute( + "SELECT b.context_kind, b.context_id, c.title AS chapter FROM bindings b LEFT JOIN chapters c ON c.id = b.chapter_id WHERE b.element_id = %s", + (candidate["id"],), + ).fetchall() + for zone in zones: + for container in layout[zone]: + hit = next((b for b in bindings if b["context_kind"] == container.kind and b["context_id"] == container.id), None) + if hit is None: + continue + if target.kind == "case" and zone == "inside" and _blocked(conn, target.id, candidate["id"]): + continue + seen_in = [container] if zone == "around" else [c for c in (target, rights_context) if c is not None] + rights = [element_rights(conn, actor, candidate, ctx) for ctx in seen_in] + if not any(r.read for r in rights): + continue + match = { + "element_id": str(candidate["id"]), + "title": candidate["title"], + "where": zone, + "certain": candidate["certain"], + "chapter": hit["chapter"] if zone == "here" else None, + "container": { + "kind": container.kind, + "title": _title_if_readable(conn, actor, container) if zone != "here" else None, + }, + "can_bind": any(r.share for r in rights), + } + known = by_element.get(candidate["id"]) + if known is None or _ORDER[zone] < _ORDER[known["where"]]: + by_element[candidate["id"]] = match + return sorted(by_element.values(), key=lambda item: (_ORDER[item["where"]], item["title"])) + + +def already_present(matches: list[dict]) -> Problem: + where = {"here": "hier", "inside": "in diesem Bestand", "around": "im Umfeld"}[matches[0]["where"]] + return Problem( + 409, + "Bereits vorhanden", + f"Dieses Dokument ist {where} schon vorhanden. Füge es nur dann erneut ein, wenn das gewollt ist.", + "already_present", + ) diff --git a/backend/app/main.py b/backend/app/main.py index 681da58..8e23e67 100644 --- a/backend/app/main.py +++ b/backend/app/main.py @@ -13,6 +13,7 @@ from app.api.accounts import router as accounts_router from app.api.correspondence import router as correspondence_router from app.api.downloads import router as downloads_router from app.api.export import router as export_router +from app.api.externals import router as externals_router from app.api.library import router as library_router from app.api.preferences import router as preferences_router from app.api.rooms import router as rooms_router @@ -132,6 +133,7 @@ def create_app() -> FastAPI: app.include_router(export_router) app.include_router(downloads_router) app.include_router(preferences_router) + app.include_router(externals_router) app.include_router(correspondence_router) return app diff --git a/backend/app/ops.py b/backend/app/ops.py index 8b13ad0..f2b6eaa 100644 --- a/backend/app/ops.py +++ b/backend/app/ops.py @@ -36,6 +36,8 @@ from app.access.policy import ( from app.content.documents import clean_document from app.core.errors import forbidden, not_found, unprocessable, unsupported from app.core.timeutil import utcnow +from app.preferences import effective_preference +from app.duplicates import already_present, find_existing from app.media.port import LEGACY_PROFILE, StoredHandle, adapter_for, instance_for_write, publish, removal_for from app.media.storage import display_filename, sha256, sniff @@ -1142,7 +1144,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, communication_direction: str | 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, duplicate: str = "insert") -> dict: ensure_can_create(conn, actor, scope_id) from app.sourceregistry import accepts_reference @@ -1150,6 +1152,13 @@ def create_external(conn, actor: UUID, scope_id: UUID, title: str, source: str, raise unprocessable("Diese Quelle ist nicht registriert.") target, rights_context = _placement(context, placement) require_container_edit(conn, actor, target, rights_context) + if duplicate == "ask": + # The level is the account's choice. Only a certain match warns: an unclear connection never blocks. + connector = (detail or {}).get("connector_id") + level = effective_preference(conn, actor, "duplicate_check") + found = [item for item in find_existing(conn, actor, source, external_id, str(connector) if connector else None, target, rights_context, level) if item["certain"]] + if found: + raise already_present(found) element = _new_element(conn, actor, scope_id, title, "external_ref") conn.execute( "INSERT INTO external_references (element_id, source, external_id, detail) VALUES (%s, %s, %s, %s)", @@ -1167,6 +1176,51 @@ def create_external(conn, actor: UUID, scope_id: UUID, title: str, source: str, return element | {"binding_id": binding["id"]} +def existing_externals(conn, actor: UUID, context: Ctx, placement: Ctx | None, items: list[dict]) -> dict: + """For each document: where it is already present around the target, at the account's check level.""" + target, rights_context = _placement(context, placement) + require_container_edit(conn, actor, target, rights_context) + level = effective_preference(conn, actor, "duplicate_check") + return { + "level": level, + "items": [ + item + | {"matches": find_existing(conn, actor, item["source"], item["external_id"], item.get("connector_id"), target, rights_context, level)} + for item in items + ], + } + + +def bind_existing_external(conn, actor: UUID, scope_id: UUID, element_id: UUID, context: Ctx, placement: Ctx | None, chapter_id: UUID | None) -> dict: + """Bind an element that already holds the document into a further container: one element, one more binding. + + The element must be a certain match around the target, readable for the actor, and the actor may share it + there. Nothing is copied and nothing is handed over: rights stay those of each context. + """ + ensure_can_create(conn, actor, scope_id) + target, rights_context = _placement(context, placement) + require_container_edit(conn, actor, target, rights_context) + ref = conn.execute( + "SELECT source, external_id, connector_id, detail FROM external_references WHERE element_id = %s", + (element_id,), + ).fetchone() + if ref is None: + raise not_found() + connector = ref["connector_id"] or (ref["detail"] or {}).get("connector_id") + matches = find_existing(conn, actor, ref["source"], ref["external_id"], str(connector) if connector else None, target, rights_context, "around") + match = next((item for item in matches if item["element_id"] == str(element_id) and item["certain"]), None) + if match is None: + raise not_found() + if match["where"] == "here": + raise unprocessable("Das Dokument ist hier bereits eingebunden.") + if not match["can_bind"]: + raise forbidden("Du darfst dieses Element nicht in einen weiteren Zusammenhang einbinden.") + element = get_element(conn, element_id) + binding = insert_binding(conn, element_id, target.kind, target.id, chapter_id, actor) + audit(conn, actor, scope_id, "external_ref_bound", "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], communication_direction: str | None = None) -> dict: ensure_can_create(conn, actor, scope_id) if not data or len(data) > max_bytes: diff --git a/backend/app/preferences.py b/backend/app/preferences.py index 1130a18..3e0bce1 100644 --- a/backend/app/preferences.py +++ b/backend/app/preferences.py @@ -25,7 +25,7 @@ class Preference: PREFERENCES: dict[str, Preference] = { # Where an element that is inserted again counts as already present. - "duplicate_check": Preference("case", ("off", "case", "case_matters", "case_tree")), + "duplicate_check": Preference("here", ("off", "here", "inside", "around")), } diff --git a/backend/app/present.py b/backend/app/present.py index 7d6278c..fe8879c 100644 --- a/backend/app/present.py +++ b/backend/app/present.py @@ -1083,4 +1083,5 @@ LABELS = { "room_download_policy_changed": "Download-Regel des Datenraums geändert", "download_defaults_changed": "Standard für Downloads geändert", "preference_changed": "Einstellung des Kontos geändert", + "external_ref_bound": "Vorhandene Referenz zusätzlich eingebunden", } diff --git a/backend/migrations/20261025T090000Z_account_preferences.sql b/backend/migrations/20261025T090000Z_account_preferences.sql index 83ec1b5..8569356 100644 --- a/backend/migrations/20261025T090000Z_account_preferences.sql +++ b/backend/migrations/20261025T090000Z_account_preferences.sql @@ -13,3 +13,6 @@ CREATE TABLE account_preferences ( CONSTRAINT account_preferences_key_len CHECK (char_length(key) BETWEEN 1 AND 60), CONSTRAINT account_preferences_team_mode CHECK (team_mode IN ('free', 'locked')) ); + +-- Dieselbe Quelle und Kennung: Suche nach bereits eingefügten Dokumenten (app/duplicates.py). +CREATE INDEX external_references_identity ON external_references (source, external_id); diff --git a/backend/tests/test_duplicates.py b/backend/tests/test_duplicates.py new file mode 100644 index 0000000..f4a4ef4 --- /dev/null +++ b/backend/tests/test_duplicates.py @@ -0,0 +1,126 @@ +"""Bereits eingefügte Quelldokumente: Vorabfrage, Warnung (409), erneutes Einfügen und zusätzliches Einbinden.""" + +from __future__ import annotations + +from uuid import uuid4 + +import pytest + +from tests.support import matter_of +from tests.test_team import _main + +PREF = "/api/v1/auth/preferences/duplicate_check" +EXISTING = "/api/v1/external-refs/existing" +CONNECTOR = str(uuid4()) + + +@pytest.fixture +def ada(client, db): + return _main(client, db, "ada", main=True) + + +def _ref(actor, kind, container, external_id="41", connector=CONNECTOR, status=201, **extra): + body = { + "title": f"Dokument {external_id}", + "source": "paperless", + "external_id": external_id, + "context": {"kind": kind, "id": container}, + "detail": {"connector_id": connector}, + **extra, + } + return actor.request("POST", "/api/v1/external-refs", json=body, status=status) + + +def _ask(actor, kind, container, external_id="41", connector=CONNECTOR): + body = { + "context": {"kind": kind, "id": container}, + "items": [{"source": "paperless", "external_id": external_id, "connector_id": connector}], + } + return actor.request("POST", EXISTING, json=body)["items"][0]["matches"] + + +def _level(actor, value): + actor.request("PUT", PREF, json={"value": value}) + + +def test_a_document_in_the_same_container_warns_and_can_be_inserted_anyway(ada): + matter = matter_of(ada, "Mietminderung")["id"] + first = _ref(ada, "matter", matter) + found = _ask(ada, "matter", matter) + assert [(m["element_id"], m["where"], m["certain"]) for m in found] == [(first["id"], "here", True)] + + refused = _ref(ada, "matter", matter, status=409) + assert refused["code"] == "already_present" + # A deliberate second insert goes through. Another document or another connection is no duplicate. + _ref(ada, "matter", matter, duplicate="insert") + _ref(ada, "matter", matter, external_id="42") + _ref(ada, "matter", matter, connector=str(uuid4())) + assert _ask(ada, "matter", matter, external_id="42")[0]["where"] == "here" + + +def test_the_same_document_in_another_vorgang_is_no_duplicate(ada): + one = matter_of(ada, "Eins")["id"] + two = matter_of(ada, "Zwei")["id"] + _ref(ada, "matter", one) + assert _ask(ada, "matter", two) == [] + _ref(ada, "matter", two) + + +def test_the_check_can_be_switched_off(ada): + matter = matter_of(ada, "Mietminderung")["id"] + _ref(ada, "matter", matter) + _level(ada, "off") + assert _ask(ada, "matter", matter) == [] + _ref(ada, "matter", matter) + + +def test_levels_reach_into_teilvorgang_vorgang_and_akte(ada): + parent = matter_of(ada, "Hauptvorgang")["id"] + child = ada.request("POST", "/api/v1/matters", json={"title": "Teil", "parent_matter_id": parent}, status=201)["id"] + case = ada.request("POST", "/api/v1/cases", json={"title": "Akte"}, status=201)["id"] + ada.request("POST", f"/api/v1/matters/{parent}/case-links", json={"case_id": case}, status=201) + _ref(ada, "matter", child, external_id="1") + _ref(ada, "matter", parent, external_id="2") + _ref(ada, "case", case, external_id="3") + + def where(kind, container, external_id): + return [m["where"] for m in _ask(ada, kind, container, external_id)] + + # Standard: only what sits directly in the target. + assert where("matter", parent, "1") == [] + _level(ada, "inside") + assert where("matter", parent, "1") == ["inside"] + assert where("matter", child, "2") == [] + assert where("case", case, "2") == ["inside"] + assert where("case", case, "3") == ["here"] + _level(ada, "around") + assert where("matter", child, "2") == ["around"] + assert where("matter", parent, "3") == ["around"] + assert where("matter", child, "3") == ["around"] + # The server warns at the same level. + _ref(ada, "matter", child, external_id="3", status=409) + + +def test_an_existing_element_can_be_bound_instead_of_a_new_one(ada): + parent = matter_of(ada, "Hauptvorgang")["id"] + child = ada.request("POST", "/api/v1/matters", json={"title": "Teil", "parent_matter_id": parent}, status=201)["id"] + original = _ref(ada, "matter", child) + _level(ada, "inside") + assert _ask(ada, "matter", parent)[0]["can_bind"] is True + + bound = _ref(ada, "matter", parent, bind_element_id=original["id"]) + assert bound["id"] == original["id"] + assert [m["where"] for m in _ask(ada, "matter", parent)] == ["here"] + refused = _ref(ada, "matter", parent, bind_element_id=original["id"], status=422) + assert "bereits" in refused["detail"] + + +def test_a_stranger_neither_sees_nor_binds_my_element(client, db, ada): + mine = matter_of(ada, "Mein Vorgang")["id"] + element = _ref(ada, "matter", mine) + ben = _main(client, db, "ben") + theirs = matter_of(ben, "Ihr Vorgang")["id"] + _level(ben, "around") + assert _ask(ben, "matter", theirs) == [] + _ref(ben, "matter", theirs) + _ref(ben, "matter", theirs, bind_element_id=element["id"], status=404) diff --git a/backend/tests/test_preferences.py b/backend/tests/test_preferences.py index 3248abd..78ae411 100644 --- a/backend/tests/test_preferences.py +++ b/backend/tests/test_preferences.py @@ -21,45 +21,45 @@ def world(client, db): def test_the_default_applies_without_any_choice(world): for person in (world["ada"], world["emp"]): state = person.request("GET", PATH) - assert (state["value"], state["source"]) == ("case", "default") + assert (state["value"], state["source"]) == ("here", "default") assert world["ada"].request("GET", PATH)["can_lock"] is True assert world["emp"].request("GET", PATH)["can_lock"] is False def test_an_employee_inherits_the_main_user_and_may_override(world): ada, emp = world["ada"], world["emp"] - ada.request("PUT", PATH, json={"value": "case_matters"}) + ada.request("PUT", PATH, json={"value": "inside"}) inherited = emp.request("GET", PATH) - assert (inherited["value"], inherited["source"]) == ("case_matters", "team") + assert (inherited["value"], inherited["source"]) == ("inside", "team") own = emp.request("PUT", PATH, json={"value": "off"}) assert (own["value"], own["source"]) == ("off", "own") # The main user's change does not touch an own choice. - ada.request("PUT", PATH, json={"value": "case_tree"}) + ada.request("PUT", PATH, json={"value": "around"}) assert emp.request("GET", PATH)["value"] == "off" - assert ada.request("GET", PATH)["value"] == "case_tree" + assert ada.request("GET", PATH)["value"] == "around" # Inheriting again. back = emp.request("PUT", PATH, json={"value": None}) - assert (back["value"], back["source"]) == ("case_tree", "team") + assert (back["value"], back["source"]) == ("around", "team") def test_a_binding_overrides_an_own_value_without_deleting_it(world): ada, emp = world["ada"], world["emp"] emp.request("PUT", PATH, json={"value": "off"}) - ada.request("PUT", PATH, json={"value": "case_matters", "team_mode": "locked"}) + ada.request("PUT", PATH, json={"value": "inside", "team_mode": "locked"}) locked = emp.request("GET", PATH) - assert (locked["value"], locked["source"], locked["locked"]) == ("case_matters", "locked", True) - refused = emp.request("PUT", PATH, json={"value": "case"}, status=403) + assert (locked["value"], locked["source"], locked["locked"]) == ("inside", "locked", True) + refused = emp.request("PUT", PATH, json={"value": "here"}, status=403) assert "vorgegeben" in refused["detail"] # The main user is not bound by the own binding. - assert ada.request("PUT", PATH, json={"value": "case"})["value"] == "case" + assert ada.request("PUT", PATH, json={"value": "here"})["value"] == "here" assert ada.request("GET", PATH)["team_mode"] == "locked" - assert emp.request("GET", PATH)["value"] == "case" + assert emp.request("GET", PATH)["value"] == "here" - ada.request("PUT", PATH, json={"value": "case", "team_mode": "free"}) + ada.request("PUT", PATH, json={"value": "here", "team_mode": "free"}) again = emp.request("GET", PATH) assert (again["value"], again["source"]) == ("off", "own") @@ -76,4 +76,4 @@ def test_only_the_main_user_binds_and_values_are_checked(world): def test_a_binding_of_a_stranger_does_not_reach_my_employee(client, db, world): ben = _main(client, db, "ben") ben.request("PUT", PATH, json={"value": "off", "team_mode": "locked"}) - assert world["emp"].request("GET", PATH)["value"] == "case" + assert world["emp"].request("GET", PATH)["value"] == "here" diff --git a/docs/architecture/API_REFERENZ.md b/docs/architecture/API_REFERENZ.md index 0fa2f5d..abf3ac9 100644 --- a/docs/architecture/API_REFERENZ.md +++ b/docs/architecture/API_REFERENZ.md @@ -34,7 +34,7 @@ Legende Spalte „Auth“: **–** öffentlich, **S** gültige Session, **S+R** | POST | `/api/v1/auth/login` | – | E-Mail, Passwort → Session-Sicht und Cookie. 8 Fehlversuche je Bucket (Hash aus IP und E-Mail) in 10 min, gezählt in `auth_login_attempts` | | POST | `/api/v1/auth/logout` | S | Session widerrufen, Cookie löschen, 204 | | GET / PUT | `/api/v1/auth/download-defaults` | S+R, Hauptnutzer | Voreinstellung je Rolle und `direct` (direkt freigegebene Personen); neue Räume übernehmen die drei Rollenwerte. Audit `download_defaults_changed` | -| GET / PUT | `/api/v1/auth/preferences/{key}` | S+R | Einstellung des Kontos mit Erbung. GET liefert `value` (wirksam), `source` (`locked`/`own`/`team`/`default`), `own`, `inherited`, `locked`, `allowed`, `can_lock`, `team_mode`. PUT setzt den eigenen Wert (`value`, `null` = erben) und beim Hauptnutzer die Verbindlichkeit für das Team (`team_mode` `free`/`locked`). Ein Mitarbeiter erhält bei verbindlicher Vorgabe 403. Schlüssel: `duplicate_check` (`off`/`case`/`case_matters`/`case_tree`, Standard `case`). Audit `preference_changed` | +| GET / PUT | `/api/v1/auth/preferences/{key}` | S+R | Einstellung des Kontos mit Erbung. GET liefert `value` (wirksam), `source` (`locked`/`own`/`team`/`default`), `own`, `inherited`, `locked`, `allowed`, `can_lock`, `team_mode`. PUT setzt den eigenen Wert (`value`, `null` = erben) und beim Hauptnutzer die Verbindlichkeit für das Team (`team_mode` `free`/`locked`). Ein Mitarbeiter erhält bei verbindlicher Vorgabe 403. Schlüssel: `duplicate_check` (`off`/`here`/`inside`/`around`, Standard `here`). Audit `preference_changed` | | GET | `/api/v1/auth/session` | S | Konto, bestätigter Datenraum, Räume, CSRF-Token, Epoche | | POST | `/api/v1/auth/session/scope` | S | Datenraum wechseln (`kind`, `id`); erhöht die Epoche | | GET | `/api/v1/auth/bootstrap` | – | `{available: true}`, solange kein Betreiber existiert, sonst 404 | @@ -146,7 +146,8 @@ Legende Spalte „Auth“: **–** öffentlich, **S** gültige Session, **S+R** | PATCH | `/api/v1/notes/{id}` | gemeinsamen Text ändern; Antwort mit `visible_bindings` und `notice` | | POST | `/api/v1/files` | Datei hochladen (Multipart: `title`, `context_kind`, `context_id`, `upload`, optional Platzierung, Kapitel, Daten, Richtung); PDF, PNG, JPEG, WebP bis 20 MiB | | GET | `/api/v1/files/{id}/content` | Datei als `attachment` mit Dateiname (UTF-8, `filename*`), `no-store`. Mit `save=true` zusätzlich das Download-Recht, sonst 403 | -| POST | `/api/v1/external-refs` | externe Referenz (`paperless`, `imap`, `url`) mit `detail` | +| POST | `/api/v1/external-refs` | externe Referenz (`paperless`, `imap`, `url`) mit `detail`. `duplicate` (`ask` Standard, `insert`): bei `ask` antwortet ein bereits vorhandenes Dokument mit 409 `already_present`, wenn nur sichere Treffer auf der Prüfstufe des Kontos zählen (Einstellung `duplicate_check`). `bind_element_id` bindet stattdessen das vorhandene Element zusätzlich in den Zielcontainer (eine Referenz, eine weitere Einbindung; Audit `external_ref_bound`). Nur ein sicherer, lesbarer Treffer im Umfeld des Ziels, mit Weitergaberecht (sonst 403 bzw. 404) | +| POST | `/api/v1/external-refs/existing` | nur lesend: `context`, optional `placement`, `items` (`source`, `external_id`, `connector_id`) → je Dokument die Treffer (`element_id`, `title`, `where` `here`/`inside`/`around`, `certain`, `chapter`, `container`, `can_bind`) auf der Prüfstufe des Kontos. Nur Lesbares, ein Treffer außerhalb der Rechte gilt als nicht vorhanden | | POST | `/api/v1/sources/link` | wie oben, nur `paperless`/`imap` (doppelter Weg, siehe Connectoren-Befund) | | GET | `/api/v1/elements` | eigene, nicht eingebundene Elemente (max. 100) | | POST | `/api/v1/elements/{id}/delete` | überall löschen; nur Element-Owner | diff --git a/frontend/src/admin/DuplicatePreference.jsx b/frontend/src/admin/DuplicatePreference.jsx index b55360c..ca2bc2f 100644 --- a/frontend/src/admin/DuplicatePreference.jsx +++ b/frontend/src/admin/DuplicatePreference.jsx @@ -6,9 +6,9 @@ const PATH = "/api/v1/auth/preferences/duplicate_check"; export const DUPLICATE_LEVELS = { off: "Keine Prüfung", - case: "Nur dieser Vorgang", - case_matters: "Vorgang und verknüpfte Akten", - case_tree: "Vorgang mit Ober- und Unterordnung", + here: "Nur hier (direkt in diesem Vorgang oder dieser Akte)", + inside: "Hier und was darin angezeigt wird (Teilvorgänge, zugeordnete Vorgänge)", + around: "Zusätzlich dort, wo es liegt (übergeordneter Vorgang, zugeordnete Akten)", }; // Where a document that is inserted again counts as already present. Only a warning, never a block. diff --git a/frontend/src/pages/Cases.jsx b/frontend/src/pages/Cases.jsx index 377d477..581cba5 100644 --- a/frontend/src/pages/Cases.jsx +++ b/frontend/src/pages/Cases.jsx @@ -15,6 +15,7 @@ import { Workspace, rememberScroll, restoreScroll, useWorkspaceLayout } from "./ import { useLibraryFocus } from "../library/LibraryLayout.jsx"; import { outlineSignature } from "../library/library-core.js"; import { NavIcon } from "./icons.jsx"; +import { useExternalPlacement } from "../sources/duplicates.jsx"; function matterOrigin(group) { const kind = group.kind_label === "Teilvorgang" ? "Teilvorgang" : "Vorgang"; @@ -905,6 +906,15 @@ function ExternalForm({ context, placement, chapters = [], initialChapter = "", const [sentOn, setSentOn] = useState(""); const [receivedOn, setReceivedOn] = useState(""); const [error, setError] = useState(""); + const { place, panel } = useExternalPlacement({ + context, + placement, + onPlaced: () => { + setTitle(""); + setExternalId(""); + onChange(); + }, + }); useEffect(() => { setChapterId(initialChapter || ""); }, [initialChapter]); @@ -912,25 +922,19 @@ function ExternalForm({ context, placement, chapters = [], initialChapter = "", event.preventDefault(); setError(""); try { - await api("/api/v1/external-refs", { - method: "POST", - body: { - title, - source, - external_id: externalId, - context, - ...(placement ? { placement } : {}), - ...(chapterId ? { chapter_id: chapterId } : {}), - dates: { - document_on: documentOn || null, - sent_on: sentOn || null, - received_on: receivedOn || null, - }, + await place({ + title, + source, + external_id: externalId, + context, + ...(placement ? { placement } : {}), + ...(chapterId ? { chapter_id: chapterId } : {}), + dates: { + document_on: documentOn || null, + sent_on: sentOn || null, + received_on: receivedOn || null, }, }); - setTitle(""); - setExternalId(""); - onChange(); } catch (err) { setError(err.message); } @@ -951,6 +955,7 @@ function ExternalForm({ context, placement, chapters = [], initialChapter = "", {chapterSelect(chapters, chapterId, setChapterId)} + {panel} ); } diff --git a/frontend/src/pages/Sources.jsx b/frontend/src/pages/Sources.jsx index 4d40b5e..78e4ab8 100644 --- a/frontend/src/pages/Sources.jsx +++ b/frontend/src/pages/Sources.jsx @@ -5,6 +5,7 @@ import { api, fetchBlob } from "../api.js"; import { MailBody } from "../mail-view.jsx"; import { NewConnector, useSourceModules } from "../sources/connector-config.jsx"; import { SourceFrame, registerSourceModule } from "../sources/registry.jsx"; +import { PresentNote, bindable, describeMatch, lookupPresent, presentKey, useExternalPlacement } from "../sources/duplicates.jsx"; const SEARCH_LIMITS = { paperless: "Die Suche läuft in Paperless selbst, über Titel und Inhalt, den Titel allein, den Inhalt allein oder den Volltextindex. Filter nach Korrespondent, Dokumenttyp, Speicherpfad, Tags, Dokumentdatum, Hinzugefügt-Datum und Archivnummer grenzen sie ein.", @@ -303,6 +304,9 @@ export function PaperlessPicker({ context, placement, chapterId = "", chapterTit const [total, setTotal] = useState(null); const [searched, setSearched] = useState(false); const [picked, setPicked] = useState(() => new Map()); + // Documents that are already present around the target, by presentKey. `review` holds the choices while the user decides. + const [present, setPresent] = useState(() => new Map()); + const [review, setReview] = useState(null); const [previewKey, setPreviewKey] = useState(""); const [previewUrl, setPreviewUrl] = useState(""); const [pending, setPending] = useState(false); @@ -417,12 +421,33 @@ export function PaperlessPicker({ context, placement, chapterId = "", chapterTit setTotal(typeof data.total === "number" ? data.total : null); setSearched(true); if (!asked && data.connector_id) setConnectorId(data.connector_id); + refreshPresent(data.items || [], asked || data.connector_id || ""); } catch (err) { if (token !== searchRef.current) return; setError(err.message); } } + function presentEntry(item, bound = connectorId) { + return { source: "paperless", external_id: String(item.id), connector_id: item.connector_id || bound }; + } + + function matchesOf(item) { + const entry = presentEntry(item); + return present.get(presentKey(entry.source, entry.connector_id, entry.external_id)) || []; + } + + async function refreshPresent(list, bound) { + const entries = list.map((item) => presentEntry(item, bound)); + const found = await lookupPresent(context, placement, entries); + setPresent((current) => { + const next = new Map(current); + entries.forEach((entry) => next.delete(presentKey(entry.source, entry.connector_id, entry.external_id))); + found.forEach((value, key) => next.set(key, value)); + return next; + }); + } + function resetFilters() { invalidate(); closePreview(); @@ -469,52 +494,82 @@ export function PaperlessPicker({ context, placement, chapterId = "", chapterTit } } - async function linkMany() { + async function linkMany(choices = null) { const chosen = [...picked.values()]; if (!chosen.length) return; setError(""); + const keyOf = (item) => `${item.connector_id || connectorId}:${item.id}`; + if (!choices) { + const known = chosen.filter((item) => matchesOf(item).length); + if (known.length) { + setReview(new Map(known.map((item) => [keyOf(item), "skip"]))); + return; + } + } + setReview(null); setPending(true); const inserted = []; + const skipped = []; + const conflicts = []; try { for (const item of chosen) { - await api("/api/v1/external-refs", { - method: "POST", - body: { - title: item.title, - source: "paperless", - external_id: String(item.id), - context, - ...(placement ? { placement } : {}), - ...(chapterId ? { chapter_id: chapterId } : {}), - ...(item.document_on || item.correspondent || item.open_url || connectorId - ? { - dates: { document_on: item.document_on || null, sent_on: null, received_on: null }, - detail: { - ...(item.connector_id || connectorId ? { connector_id: item.connector_id || connectorId } : {}), - ...(item.correspondent ? { correspondent: item.correspondent } : {}), - ...(item.open_url ? { open_url: item.open_url } : {}), - }, - } - : {}), - }, - }); - inserted.push(`${item.connector_id || connectorId}:${item.id}`); + const key = keyOf(item); + const choice = choices?.get(key); + if (choice === "skip") { + skipped.push(key); + continue; + } + const existing = choice === "bind" ? bindable(matchesOf(item)) : null; + try { + await api("/api/v1/external-refs", { + method: "POST", + body: { + title: item.title, + source: "paperless", + external_id: String(item.id), + context, + ...(placement ? { placement } : {}), + ...(chapterId ? { chapter_id: chapterId } : {}), + ...(choice === "insert" ? { duplicate: "insert" } : {}), + ...(existing ? { bind_element_id: existing.element_id } : {}), + ...(item.document_on || item.correspondent || item.open_url || connectorId + ? { + dates: { document_on: item.document_on || null, sent_on: null, received_on: null }, + detail: { + ...(item.connector_id || connectorId ? { connector_id: item.connector_id || connectorId } : {}), + ...(item.correspondent ? { correspondent: item.correspondent } : {}), + ...(item.open_url ? { open_url: item.open_url } : {}), + }, + } + : {}), + }, + }); + inserted.push(key); + } catch (err) { + // Inserted by someone else in the meantime: ask again instead of failing. + if (err.code !== "already_present") throw err; + conflicts.push(item); + } } } catch (err) { setError(err.message); } finally { setPending(false); } + if (conflicts.length) { + await refreshPresent(conflicts, connectorId); + setReview(new Map(conflicts.map((item) => [keyOf(item), "skip"]))); + } if (inserted.length) { setItems((current) => current.filter((item) => !inserted.includes(`${connectorId}:${item.id}`))); - setPicked((current) => { - const next = new Map(current); - inserted.forEach((id) => next.delete(id)); - return next; - }); onInserted?.(); } - if (inserted.length === chosen.length) onDone(); + setPicked((current) => { + const next = new Map(current); + [...inserted, ...skipped].forEach((id) => next.delete(id)); + return next; + }); + if (inserted.length && inserted.length + skipped.length === chosen.length) onDone(); } function setFilter(name, value) { @@ -706,8 +761,37 @@ export function PaperlessPicker({ context, placement, chapterId = "", chapterTit

{SEARCH_LIMITS.paperless}

Die Verbindung richtest du unter Quellen ein. Ausgewählte Belege bleiben erhalten, wenn du weiterblätterst oder neu suchst.

+ {review ? ( +
+ Diese Belege sind schon vorhanden. Was soll geschehen? +
    + {[...picked.values()].filter((item) => review.has(`${item.connector_id || connectorId}:${item.id}`)).map((item) => { + const key = `${item.connector_id || connectorId}:${item.id}`; + const matches = matchesOf(item); + return ( +
  • + {item.title}: {matches.length ? describeMatch(matches[0]) : "schon vorhanden"} + +
  • + ); + })} +
+
+ + +
+
+ ) : null} {items.length || picked.size ? ( - ) : null} @@ -745,6 +829,7 @@ export function PaperlessPicker({ context, placement, chapterId = "", chapterTit
{item.title} + {facts.length ? {facts.join(" · ")} : null} {item.tags?.length ? (
    @@ -901,6 +986,7 @@ export function MailPicker({ context, placement, chapterId = "", chapterTitle = const [preview, setPreview] = useState(null); const [previewError, setPreviewError] = useState(""); const [adopting, setAdopting] = useState(false); + const { place, panel } = useExternalPlacement({ context, placement, onPlaced: () => onDone() }); const [loading, setLoading] = useState(""); const [error, setError] = useState(""); const requestRef = useRef(0); @@ -1083,26 +1169,22 @@ export function MailPicker({ context, placement, chapterId = "", chapterTitle = if (cc) detail.message_cc = cc; if (attachments.length) detail.attachments = attachments; try { - await api("/api/v1/external-refs", { - method: "POST", - body: { - title: item.title, - source: "imap", - external_id: item.id, - context, - ...(placement ? { placement } : {}), - ...(chapterId ? { chapter_id: chapterId } : {}), - ...(item.document_on ? { - dates: { - document_on: item.document_on, - sent_on: direction === "out" ? item.document_on : null, - received_on: direction === "in" ? item.document_on : null, - }, - } : {}), - detail, - }, + await place({ + title: item.title, + source: "imap", + external_id: item.id, + context, + ...(placement ? { placement } : {}), + ...(chapterId ? { chapter_id: chapterId } : {}), + ...(item.document_on ? { + dates: { + document_on: item.document_on, + sent_on: direction === "out" ? item.document_on : null, + received_on: direction === "in" ? item.document_on : null, + }, + } : {}), + detail, }); - onDone(); } catch (err) { setError(err.message); } finally { @@ -1183,6 +1265,7 @@ export function MailPicker({ context, placement, chapterId = "", chapterTitle = {selected && !preview && !previewError ?

    Vorschau wird geladen …

    : null} {preview ? : null} + {panel} )} diff --git a/frontend/src/sources/duplicates.jsx b/frontend/src/sources/duplicates.jsx new file mode 100644 index 0000000..f8de92a --- /dev/null +++ b/frontend/src/sources/duplicates.jsx @@ -0,0 +1,122 @@ +import { useState } from "react"; +import { api } from "../api.js"; +import { ErrorNote } from "../pages/widgets.jsx"; + +// A document that is already present around the target. The server decides; this only asks and explains. +export function presentKey(source, connectorId, externalId) { + return `${source}:${connectorId || ""}:${externalId}`; +} + +export function describeMatch(match) { + const title = match.container?.title; + const maybe = match.certain ? "" : "möglicherweise "; + if (match.where === "here") return `${maybe}schon hier${match.chapter ? ` (Kapitel „${match.chapter}“)` : ""}`; + if (match.where === "inside") return `${maybe}schon ${title ? `in „${title}“` : "in einem zugehörigen Teil"}`; + const kind = match.container?.kind === "case" ? "in der Akte" : "im Vorgang"; + return `${maybe}schon ${kind}${title ? ` „${title}“` : ""}`; +} + +export function bindable(matches) { + return (matches || []).find((item) => item.certain && item.can_bind && item.where !== "here") || null; +} + +// entries: [{ source, external_id, connector_id }]. Answers a Map of presentKey -> matches, only for documents that are present. +// A failed lookup answers nothing: the warning is a help, the server still protects on insert. +export async function lookupPresent(context, placement, entries) { + const found = new Map(); + if (!entries.length) return found; + try { + const data = await api("/api/v1/external-refs/existing", { + method: "POST", + body: { + context, + ...(placement ? { placement } : {}), + items: entries.map((item) => ({ source: item.source, external_id: item.external_id, ...(item.connector_id ? { connector_id: item.connector_id } : {}) })), + }, + }); + for (const entry of data.items || []) { + if (entry.matches?.length) found.set(presentKey(entry.source, entry.connector_id, entry.external_id), entry.matches); + } + } catch { + return found; + } + return found; +} + +export function PresentNote({ matches }) { + if (!matches?.length) return null; + return

    Schon vorhanden: {matches.map(describeMatch).join(", ")}.

    ; +} + +// Choices when a document is already present: do not insert, insert again, or bind the existing element as well. +export function DuplicatePanel({ matches, busy, onCancel, onInsert, onBind }) { + const existing = bindable(matches); + return ( +
    + Dieses Dokument ist schon vorhanden. +
      + {matches.map((item) =>
    • {item.title}: {describeMatch(item)}
    • )} +
    +
    + + + {existing ? : null} +
    +
    + ); +} + +// One document at a time. `place(body)` posts it. If the server finds it already present, `panel` offers the choices +// and `onPlaced` runs once the document is in, whichever way. +export function useExternalPlacement({ context, placement, onPlaced }) { + const [conflict, setConflict] = useState(null); + const [busy, setBusy] = useState(false); + const [error, setError] = useState(""); + + async function send(body) { + await api("/api/v1/external-refs", { method: "POST", body }); + setConflict(null); + await onPlaced?.(); + } + + async function place(body) { + setError(""); + try { + await send(body); + } catch (err) { + if (err.code !== "already_present") throw err; + const entry = { source: body.source, external_id: body.external_id, connector_id: body.detail?.connector_id }; + const found = await lookupPresent(context, placement, [entry]); + const matches = found.get(presentKey(entry.source, entry.connector_id, entry.external_id)) || []; + if (!matches.length) throw err; + setConflict({ body, matches }); + } + } + + async function resolve(extra) { + if (!conflict) return; + setBusy(true); + setError(""); + try { + await send({ ...conflict.body, ...extra }); + } catch (err) { + setError(err.message); + } finally { + setBusy(false); + } + } + + const panel = conflict ? ( + <> + setConflict(null)} + onInsert={() => resolve({ duplicate: "insert" })} + onBind={(elementId) => resolve({ bind_element_id: elementId })} + /> + + + ) : null; + return { place, panel }; +} diff --git a/frontend/src/sources/registry.jsx b/frontend/src/sources/registry.jsx index 121731a..938d163 100644 --- a/frontend/src/sources/registry.jsx +++ b/frontend/src/sources/registry.jsx @@ -2,6 +2,7 @@ import { useEffect, useRef, useState } from "react"; import { api } from "../api.js"; import { TargetLine } from "../pages/content.jsx"; import { ErrorNote } from "../pages/widgets.jsx"; +import { useExternalPlacement } from "./duplicates.jsx"; import { defineSourceModules, moduleFor, registerSourceModule, sourceModules, unregisterSourceModule } from "./registry-core.js"; export { defineSourceModules, moduleFor, registerSourceModule, sourceModules, unregisterSourceModule }; @@ -71,6 +72,7 @@ export function StandardSourcePicker({ sourceKind, context, placement, chapterId const [error, setError] = useState(""); const [pending, setPending] = useState(false); const searchRef = useRef(0); + const { place, panel } = useExternalPlacement({ context, placement, onPlaced: () => { onInserted?.(); onDone?.(); } }); const hint = module?.searchHint || "Suche im heute unterstützten Umfang dieser Quelle."; useEffect(() => { @@ -111,20 +113,15 @@ export function StandardSourcePicker({ sourceKind, context, placement, chapterId setPending(true); setError(""); try { - await api("/api/v1/external-refs", { - method: "POST", - body: { - title: item.title, - source: sourceKind, - external_id: String(item.id), - context, - ...(placement ? { placement } : {}), - ...(chapterId ? { chapter_id: chapterId } : {}), - ...(connectorId ? { detail: { connector_id: connectorId } } : {}), - }, + await place({ + title: item.title, + source: sourceKind, + external_id: String(item.id), + context, + ...(placement ? { placement } : {}), + ...(chapterId ? { chapter_id: chapterId } : {}), + ...(connectorId ? { detail: { connector_id: connectorId } } : {}), }); - onInserted?.(); - onDone?.(); } catch (err) { setError(err.message); } finally { @@ -149,6 +146,7 @@ export function StandardSourcePicker({ sourceKind, context, placement, chapterId + {panel}

    {hint}

    {items.map((item) => ( diff --git a/frontend/src/styles.css b/frontend/src/styles.css index 3eef4f5..4b23c78 100644 --- a/frontend/src/styles.css +++ b/frontend/src/styles.css @@ -1837,3 +1837,17 @@ button.pick-tree__twist { .mini-icon--link { color: var(--kind-room); } .structure__links { margin-top: 12px; padding-top: 8px; border-top: 1px solid var(--border); } .structure__links h3 { font-size: 11px; letter-spacing: 0.06em; text-transform: uppercase; color: var(--text2); margin: 0 8px 4px; } + +.duplicate-note { margin: 0; color: var(--text); font-size: 13px; font-weight: 600; } +.duplicate-panel { border: 1px solid var(--border); background: var(--surface2); } +.duplicate-panel ul { margin: 0; padding-left: 18px; } +.duplicate-list { list-style: none; padding-left: 0 !important; display: grid; gap: 8px; } +.duplicate-list li { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; justify-content: space-between; } +.duplicate-list select { max-width: 100%; } + +.duplicate-note { margin: 0; color: var(--text); font-size: 13px; font-weight: 600; } +.duplicate-panel { border: 1px solid var(--border); background: var(--surface2); } +.duplicate-panel ul { margin: 0; padding-left: 18px; } +.duplicate-list { list-style: none; padding-left: 0 !important; display: grid; gap: 8px; } +.duplicate-list li { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; justify-content: space-between; } +.duplicate-list select { max-width: 100 \ No newline at end of file