[Debt] Refactor duplicated audit route response assembly #7

Closed
opened 2026-07-13 17:21:06 +02:00 by zemion · 1 comment
Owner

Created from add-ideas/govoplan#3 after triaging /mnt/DATA/git/govoplan/audit-reports/govoplan-full-20260711-1857/jscpd/jscpd-report.json.

Evidence

jscpd reports a 51-line duplicate in src/govoplan_audit/backend/api/v1/routes.py:

  • list_admin_audit: lines 305-355
  • list_admin_audit_delta: lines 434-484

The duplicated block handles scope validation, permission checks, tenant resolution, text filters, sort mapping, counts, and filter echo assembly. This is route behavior, not harmless metadata.

Desired Shape

Extract a small shared query/filter/sort preparation helper for the audit list and delta endpoints. Keep endpoint response contracts and permission behavior unchanged.

Acceptance Criteria

  • The duplicated route setup lives in one helper or service-level function.
  • /admin/audit and /admin/audit/delta keep their current API behavior.
  • Existing audit route tests pass, with a focused regression if behavior is not already covered.
  • A rerun of jscpd no longer reports this route assembly clone, or any remaining duplication is deliberately documented in this issue.
Created from add-ideas/govoplan#3 after triaging `/mnt/DATA/git/govoplan/audit-reports/govoplan-full-20260711-1857/jscpd/jscpd-report.json`. ## Evidence jscpd reports a 51-line duplicate in `src/govoplan_audit/backend/api/v1/routes.py`: - `list_admin_audit`: lines 305-355 - `list_admin_audit_delta`: lines 434-484 The duplicated block handles scope validation, permission checks, tenant resolution, text filters, sort mapping, counts, and filter echo assembly. This is route behavior, not harmless metadata. ## Desired Shape Extract a small shared query/filter/sort preparation helper for the audit list and delta endpoints. Keep endpoint response contracts and permission behavior unchanged. ## Acceptance Criteria - The duplicated route setup lives in one helper or service-level function. - `/admin/audit` and `/admin/audit/delta` keep their current API behavior. - Existing audit route tests pass, with a focused regression if behavior is not already covered. - A rerun of jscpd no longer reports this route assembly clone, or any remaining duplication is deliberately documented in this issue.
Author
Owner

Codex State: done

Summary

  • Extracted duplicated admin audit list/delta query preparation into a shared helper while preserving pagination, filtering, sorting, scope checks, and watermark behavior.
  • Updated the stale audit module contract test expectation from govoplan-core>=0.1.6 to >=0.1.8.

Changed Files

  • src/govoplan_audit/backend/api/v1/routes.py
  • tests/test_audit_module_contract.py

Verification

  • python -m py_compile src/govoplan_audit/backend/api/v1/routes.py
  • python -m ruff check src/govoplan_audit/backend/api/v1/routes.py tests/test_audit_module_contract.py
  • python -m unittest discover -s tests
  • python -m unittest tests.test_api_smoke.ApiSmokeTests.test_admin_audit_delta_tracks_new_events tests.test_api_smoke.ApiSmokeTests.test_admin_audit_delta_supports_filtered_sorted_first_page tests.test_api_smoke.ApiSmokeTests.test_admin_audit_supports_lazy_pagination_sorting_and_filters tests.test_api_smoke.ApiSmokeTests.test_admin_audit_cursor_pagination_is_stable_after_newer_insert
## Codex State: done ### Summary - Extracted duplicated admin audit list/delta query preparation into a shared helper while preserving pagination, filtering, sorting, scope checks, and watermark behavior. - Updated the stale audit module contract test expectation from govoplan-core>=0.1.6 to >=0.1.8. ### Changed Files - `src/govoplan_audit/backend/api/v1/routes.py` - `tests/test_audit_module_contract.py` ### Verification - `python -m py_compile src/govoplan_audit/backend/api/v1/routes.py` - `python -m ruff check src/govoplan_audit/backend/api/v1/routes.py tests/test_audit_module_contract.py` - `python -m unittest discover -s tests` - `python -m unittest tests.test_api_smoke.ApiSmokeTests.test_admin_audit_delta_tracks_new_events tests.test_api_smoke.ApiSmokeTests.test_admin_audit_delta_supports_filtered_sorted_first_page tests.test_api_smoke.ApiSmokeTests.test_admin_audit_supports_lazy_pagination_sorting_and_filters tests.test_api_smoke.ApiSmokeTests.test_admin_audit_cursor_pagination_is_stable_after_newer_insert`
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: add-ideas/govoplan-audit#7
No description provided.