Note (2026-04-24): After this document was written, legal_entity was renamed to tenant and the old tenant was renamed to organization. Read references to these terms with the pre-rename meaning.

Documents Module Split Design

Split documents.clj (2,574 lines) and document_management.clj (1,424 lines) into a com.getorcha.erp.http.documents.* namespace tree with grouped handlers, unified URL structure, and deduplicated shared code.

Namespace Layout

com.getorcha.erp.http.documents                        — route assembly + /documents → 404
com.getorcha.erp.http.documents.shared                 — shared helpers, UI components, parameterized list-events SSE
com.getorcha.erp.http.documents.upload                 — single upload handler (all doc types)
com.getorcha.erp.http.documents.accounts-payable       — AP list, search, bulk DATEV export, list SSE
com.getorcha.erp.http.documents.management             — DM list, search, type filtering, list SSE
com.getorcha.erp.http.documents.view                   — route assembly for /documents/view/:uuid, type dispatch
com.getorcha.erp.http.documents.view.shared            — shared detail layout, detail SSE, navigation, reingest
com.getorcha.erp.http.documents.view.invoice           — invoice detail + DATEV export + QA dataset
com.getorcha.erp.http.documents.view.notice            — MAESN notice detail
com.getorcha.erp.http.documents.view.contract          — contract detail
com.getorcha.erp.http.documents.view.purchase-order    — PO detail
com.getorcha.erp.http.documents.view.goods-received-note — GRN detail

URL Structure

/documents                                    → 404 (placeholder for future)
/documents/upload                             → POST (all document types)
/documents/accounts-payable                   → GET list
/documents/accounts-payable/search            → GET search
/documents/accounts-payable/events            → GET SSE (list updates)
/documents/accounts-payable/bulk/toggle/:id   → POST
/documents/accounts-payable/bulk/toggle-all   → POST
/documents/accounts-payable/bulk/deselect-all → POST
/documents/accounts-payable/bulk/export-datev → POST
/documents/management                         → GET list
/documents/management/search                  → GET search
/documents/management/events                  → GET SSE (list updates)
/documents/view/:uuid                         → GET detail (dispatches by document type)
/documents/view/:uuid/events                  → GET SSE (detail updates)
/documents/view/:uuid/reingest                → POST
/documents/view/:uuid/export/datev            → POST (invoice-only)
/documents/view/:uuid/export/datev/status     → GET (invoice-only)
/documents/view/:uuid/add-to-qa-dataset       → POST (invoice-only)

Route Assembly

Following existing convention (approach B), each submodule owns its full route prefix. The parent concatenates:

;; com.getorcha.erp.http.documents
(defn routes [config]
  ["/documents"
   ["" {:get {:handler (constantly {:status 404})}}]
   (upload/routes config)
   (accounts-payable/routes config)
   (management/routes config)
   (view/routes config)])

The erp/http.clj router replaces both erp.http.documents/routes and erp.http.document-management/routes with a single erp.http.documents/routes call.

Shared Code (documents.shared)

Deduplicated from both existing files:

Detail View Dispatch (documents.view)

The get-document handler in view loads the document, determines its type, and delegates rendering to the appropriate submodule:

Detail SSE (view.shared)

Single detail-events handler replacing two nearly-identical handlers:

List SSE (documents.shared)

Parameterized handler used by both AP and management lists:

Upload (documents.upload)

Single upload handler serving all document types. Currently duplicated nearly identically in both files — merged into one at /documents/upload.

Bulk Operations

DATEV bulk export (selection toggling, toggle-all, deselect-all, export) stays exclusively in accounts-payable. Not shared.

Test Structure

com.getorcha.erp.http.documents-test                — route assembly tests
com.getorcha.erp.http.documents.upload-test         — upload handler tests
com.getorcha.erp.http.documents.accounts-payable-test — AP list, search, bulk, list SSE
com.getorcha.erp.http.documents.management-test     — DM list, search, list SSE
com.getorcha.erp.http.documents.view-test           — detail dispatch, shared detail SSE, reingest
com.getorcha.erp.http.documents.view.invoice-test   — invoice detail, DATEV export, QA dataset
com.getorcha.erp.http.documents.view.contract-test  — contract detail

Simpler view types (notice, PO, GRN) grouped into view-test initially, split if they grow.

Existing tests in documents_test.clj and document_management_test.clj redistributed to match.