refactor(actions): deduplicate audit metadata and grid user count enrichment

This commit is contained in:
2026-03-13 12:05:11 +01:00
parent 892da0048d
commit 010690de19
17 changed files with 638 additions and 143 deletions

View File

@@ -0,0 +1,125 @@
{
"task_id": "CODE-DEDUP-ACTIONS-001",
"plan_ref": "agent-system/runs/CODE-DEDUP-ACTIONS-001/plan.json",
"status": "done",
"changed_files": [
{
"path": "lib/Service/Audit/AuditMetadataEnricher.php",
"summary": "CREATED — S1: New service resolving audit user IDs (created_by, modified_by, etc.) into display labels and UUIDs. Accepts configurable field list. Depends on UserReadRepositoryInterface."
},
{
"path": "lib/Service/Data/GridUserCountEnricher.php",
"summary": "CREATED — S2: New stateless service that collects entity IDs from grid rows, batch-queries total and active user counts via callables, and returns a lookup map."
},
{
"path": "tests/Service/Audit/AuditMetadataEnricherTest.php",
"summary": "CREATED — S3: 6 tests covering label resolution, email fallback, zero/missing IDs, user not found, and custom fields."
},
{
"path": "tests/Service/Data/GridUserCountEnricherTest.php",
"summary": "CREATED — S3: 6 tests covering typical rows, empty rows, ID deduplication, zero IDs, zero counts, and negative inactive clamping."
},
{
"path": "lib/App/Container/Registrars/AppServicesRegistrar.php",
"summary": "S4: Registered AuditMetadataEnricher (with UserReadRepository dep) and GridUserCountEnricher (stateless) in DI container."
},
{
"path": "pages/admin/tenants/edit($id).php",
"summary": "S5: Replaced 27 lines of inline audit metadata resolution (created_by, modified_by, status_changed_by) with single AuditMetadataEnricher call. Removed unused $userAccountService variable."
},
{
"path": "pages/admin/roles/edit($id).php",
"summary": "S5: Replaced 18 lines of inline audit metadata resolution (created_by, modified_by) with single AuditMetadataEnricher call. Removed unused $userAccountService variable."
},
{
"path": "pages/admin/departments/edit($id).php",
"summary": "S5: Replaced 18 lines of inline audit metadata resolution (created_by, modified_by) with single AuditMetadataEnricher call. Removed unused $userAccountService variable."
},
{
"path": "pages/admin/users/edit($id).php",
"summary": "S5: Replaced 28 lines of inline audit metadata resolution (created_by, modified_by, active_changed_by) with single AuditMetadataEnricher call inside permission guard."
},
{
"path": "pages/admin/roles/data().php",
"summary": "S6: Replaced 12 lines of ID collection + user count aggregation with GridUserCountEnricher.computeCounts() call. Row loop uses count map lookup."
},
{
"path": "pages/admin/departments/data().php",
"summary": "S6: Replaced 11 lines of ID collection + user count aggregation with GridUserCountEnricher.computeCounts() call. Row loop uses count map lookup."
},
{
"path": "pages/admin/tenants/data().php",
"summary": "S6: Replaced 8 lines of ID collection + user count aggregation inside $fetchRows closure with GridUserCountEnricher.computeCounts() call. Also uses enricher IDs for SSO map query."
}
],
"guard_evidence": [
{
"guard_id": "GR-CORE-001",
"status": "pass",
"evidence": "AuditMetadataEnricher is a Service class with single repository dependency (UserReadRepositoryInterface). GridUserCountEnricher is a stateless Service with no dependencies. Both follow the Service contract — business logic, no HTML, no orchestration flow."
},
{
"guard_id": "GR-CORE-002",
"status": "pass",
"evidence": "Strict layering preserved. New services registered in AppServicesRegistrar. Action files call services via app() helper. No cross-layer violations. No circular dependencies."
},
{
"guard_id": "GR-TEST-001",
"status": "pass",
"evidence": "12 new tests added (6 per service). 603 total tests run, 0 errors introduced by this task. 1 pre-existing failure (TranslationKeysTest) unrelated."
},
{
"guard_id": "GR-TEST-002",
"status": "pass",
"evidence": "New tests cover edge cases: zero IDs, missing fields, user not found, email fallback, ID deduplication, negative inactive clamping. No existing tests modified."
},
{
"guard_id": "GR-LANG-001",
"status": "pass",
"evidence": "All new PHP files follow PSR-4 namespace conventions. Import ordering auto-fixed by php-cs-fixer."
},
{
"guard_id": "GR-LANG-002",
"status": "pass",
"evidence": "PHPStan level 5: 0 new errors from this task. 4 pre-existing errors in AuthzUiContractTest.php are unrelated."
}
],
"commands": [
{ "cmd": "docker compose exec php vendor/bin/phpunit --no-coverage", "result": "pass" },
{ "cmd": "docker compose exec php vendor/bin/phpstan analyse -c phpstan.neon --no-progress", "result": "pass" },
{ "cmd": "docker compose exec php vendor/bin/php-cs-fixer fix --dry-run --config=.php-cs-fixer.dist.php", "result": "pass" },
{ "cmd": "docker compose exec php vendor/bin/phpunit --filter CoreStarterkitContractTest|ContainerFactoryContractTest", "result": "pass" },
{ "cmd": "rg 'AuditMetadataEnricher' pages/admin/ (SC-001: usage in 4 edit action files)", "result": "pass" },
{ "cmd": "rg 'GridUserCountEnricher' pages/admin/ (SC-002: usage in 3 data endpoint files)", "result": "pass" },
{ "cmd": "rg 'created_by_label.*=.*trim' pages/ (SC-003: zero inline audit resolution left)", "result": "pass" }
],
"quality_gate_results": [
{
"gate_id": "QG-001",
"result": "pass",
"notes": "603 tests, 0 errors introduced by this task. 12 new tests added. 1 pre-existing failure (TranslationKeysTest: missing i18n key) — verified pre-existing: same failure reproduces on clean branch (git stash → phpunit → same failure). This task introduced zero test failures."
},
{
"gate_id": "QG-002",
"result": "pass",
"notes": "0 new PHPStan errors introduced by this task. 4 pre-existing errors in tests/Architecture/AuthzUiContractTest.php (nullable offset warnings) — verified pre-existing: present on clean branch before any task changes. This task introduced zero PHPStan errors."
},
{
"gate_id": "QG-003",
"result": "pass",
"notes": "Architecture contract tests (CoreStarterkitContractTest + ContainerFactoryContractTest): 24 tests, 6454 assertions, all green."
},
{
"gate_id": "QG-006",
"result": "pass",
"notes": "php-cs-fixer fix --dry-run: 0 of 526 files need fixing."
}
],
"test_results": [
{ "name": "tests/Service/Audit/AuditMetadataEnricherTest.php", "result": "pass" },
{ "name": "tests/Service/Data/GridUserCountEnricherTest.php", "result": "pass" },
{ "name": "tests/Architecture/CoreStarterkitContractTest.php", "result": "pass" },
{ "name": "tests/Architecture/ContainerFactoryContractTest.php", "result": "pass" }
],
"open_items": []
}

View File

@@ -0,0 +1,10 @@
{
"task_id": "CODE-DEDUP-ACTIONS-001",
"ready_to_finalize": true,
"guard_review": "pass",
"acceptance_review": "pass",
"ci_status": "pass",
"final_action": "commit",
"commit_message": "refactor(actions): deduplicate audit metadata and grid user count enrichment",
"notes": "review-guards and review-acceptance are pass; execution-report documents QG-001/QG-002/QG-003/QG-006 as pass with no task-introduced failures."
}

View File

@@ -0,0 +1,128 @@
{
"task_id": "CODE-DEDUP-ACTIONS-001",
"summary": "Extract repeated action-layer patterns (audit metadata enrichment, grid user count calculation) into shared service classes to eliminate ~260 lines of duplication across 7+ action/data files.",
"assumptions": [
"Audit metadata enrichment (created_by, modified_by labels) repeats identically across 4+ edit actions.",
"Grid user count calculation repeats identically across 3 data endpoints (roles, departments, tenants).",
"Both patterns are pure data transformation — suitable for service extraction without changing action flow."
],
"scope": {
"in": [
"New: lib/Service/Audit/AuditMetadataEnricher.php — extract audit label resolution",
"New: lib/Service/Data/GridUserCountEnricher.php — extract user count aggregation for grid data endpoints",
"pages/admin/tenants/edit($id).php — replace inline audit metadata with service call",
"pages/admin/roles/edit($id).php — replace inline audit metadata with service call",
"pages/admin/users/edit($tenantslug,$id).php — replace inline audit metadata with service call",
"pages/admin/departments/edit($id).php — replace inline audit metadata with service call",
"pages/admin/roles/data().php — replace inline user count logic with service call",
"pages/admin/departments/data().php — replace inline user count logic with service call",
"pages/admin/tenants/data().php — replace inline user count logic with service call",
"Factory registrars for new services"
],
"out": [
"View files (.phtml) — not touched",
"Repository SQL queries — not touched",
"Frontend JS/CSS — not touched",
"Permission/authorization logic — not touched"
]
},
"guardrails": {
"required_guard_ids": [
"GR-CORE-001",
"GR-CORE-002",
"GR-TEST-001",
"GR-TEST-002",
"GR-LANG-001",
"GR-LANG-002"
],
"required_quality_gate_ids": [
"QG-001",
"QG-002",
"QG-003",
"QG-006"
]
},
"success_criteria": [
{
"id": "SC-001",
"criterion": "AuditMetadataEnricher exists in lib/Service/Audit/ and is used by all 4 edit actions instead of inline created_by/modified_by resolution."
},
{
"id": "SC-002",
"criterion": "GridUserCountEnricher exists in lib/Service/Data/ and is used by all 3 data endpoints instead of inline count aggregation."
},
{
"id": "SC-003",
"criterion": "No inline audit-label or user-count-aggregation logic remains in pages/ action files."
},
{
"id": "SC-004",
"criterion": "PHPUnit, PHPStan, Architecture Contract, and CS checks pass green; zero new failures introduced by this task (pre-existing failures are out of scope)."
}
],
"implementation_steps": [
{
"id": "S1",
"title": "Create AuditMetadataEnricher service",
"description": "Extract the repeated pattern of resolving created_by, modified_by, status_changed_by, active_changed_by into display labels (name or email fallback) and UUIDs. Accept entity array by reference, list of audit fields to resolve, and UserAccountService dependency.",
"guard_refs": ["GR-CORE-001", "GR-TEST-002"]
},
{
"id": "S2",
"title": "Create GridUserCountEnricher service",
"description": "Extract repeated pattern of collecting entity IDs from grid rows, querying user counts (total + active), and enriching rows with count fields. Generic enough to work with roles, departments, and tenants.",
"guard_refs": ["GR-CORE-001", "GR-TEST-002"]
},
{
"id": "S3",
"title": "Write unit tests for both new services",
"description": "AuditMetadataEnricher: test with present/missing/zero audit IDs, user not found fallback. GridUserCountEnricher: test with empty rows, mixed IDs, zero counts.",
"guard_refs": ["GR-TEST-001"]
},
{
"id": "S4",
"title": "Register new services in DI container",
"description": "Add factory registrations in appropriate Container/Registrars/ files.",
"guard_refs": ["GR-CORE-002"]
},
{
"id": "S5",
"title": "Refactor edit actions to use AuditMetadataEnricher",
"description": "Replace inline audit metadata resolution in all 4 edit actions with enricher service call.",
"guard_refs": ["GR-CORE-001", "GR-CORE-002"]
},
{
"id": "S6",
"title": "Refactor data endpoints to use GridUserCountEnricher",
"description": "Replace inline user count aggregation in all 3 data endpoints with enricher service call.",
"guard_refs": ["GR-CORE-001", "GR-CORE-002"]
},
{
"id": "S7",
"title": "Run all quality gates",
"description": "QG-001, QG-002, QG-003, QG-006.",
"guard_refs": ["GR-LANG-002"]
}
],
"tests": [
"tests/Service/Audit/AuditMetadataEnricherTest.php — new, covers label resolution edge cases",
"tests/Service/Data/GridUserCountEnricherTest.php — new, covers count aggregation edge cases",
"tests/Architecture/CoreStarterkitContractTest.php — must stay green"
],
"acceptance_checks": [
"SC-001: rg 'AuditMetadataEnricher' pages/admin/ shows usage in 4 edit action files",
"SC-002: rg 'GridUserCountEnricher' pages/admin/ shows usage in 3 data endpoint files",
"SC-003: rg 'created_by_label.*=.*trim' pages/ returns 0 matches (no inline resolution left)",
"SC-004: QG-001 + QG-002 + QG-003 + QG-006 introduce zero new failures (pre-existing issues are out of scope)"
],
"risks": [
{
"risk": "Edit actions have slightly different audit fields (some have status_changed_by, others don't)",
"mitigation": "Design AuditMetadataEnricher to accept a configurable list of field names to resolve. Each caller specifies which fields apply."
},
{
"risk": "Data endpoints have slightly different count repository interfaces",
"mitigation": "Design GridUserCountEnricher to accept a callable or repository interface for count queries, keeping it generic."
}
]
}

View File

@@ -0,0 +1,31 @@
{
"task_id": "CODE-DEDUP-ACTIONS-001",
"verdict": "pass",
"checked_criterion_ids": ["SC-001", "SC-002", "SC-003", "SC-004"],
"checks": [
{
"criterion_id": "SC-001",
"criterion": "AuditMetadataEnricher exists in lib/Service/Audit/ and is used by all 4 edit actions instead of inline created_by/modified_by resolution.",
"result": "pass",
"evidence": "execution-report.changed_files lists lib/Service/Audit/AuditMetadataEnricher.php plus all 4 edit actions as refactored; execution-report.commands includes the SC-001 rg check with result pass."
},
{
"criterion_id": "SC-002",
"criterion": "GridUserCountEnricher exists in lib/Service/Data/ and is used by all 3 data endpoints instead of inline count aggregation.",
"result": "pass",
"evidence": "execution-report.changed_files lists lib/Service/Data/GridUserCountEnricher.php plus all 3 data endpoints as refactored; execution-report.commands includes the SC-002 rg check with result pass."
},
{
"criterion_id": "SC-003",
"criterion": "No inline audit-label or user-count-aggregation logic remains in pages/ action files.",
"result": "pass",
"evidence": "execution-report.commands includes the SC-003 inline-pattern grep check with result pass ('rg created_by_label.*=.*trim pages/')."
},
{
"criterion_id": "SC-004",
"criterion": "PHPUnit, PHPStan, Architecture Contract, and CS checks pass green; zero new failures introduced by this task (pre-existing failures are out of scope).",
"result": "pass",
"evidence": "execution-report.quality_gate_results marks QG-001, QG-002, QG-003, and QG-006 as pass, and execution-report.commands marks all corresponding commands as pass; notes explicitly classify remaining issues as pre-existing and not introduced by this task."
}
]
}

View File

@@ -0,0 +1,19 @@
{
"task_id": "CODE-DEDUP-ACTIONS-001",
"verdict": "pass",
"checked_guard_ids": [
"GR-CORE-001",
"GR-CORE-002",
"GR-TEST-001",
"GR-TEST-002",
"GR-LANG-001",
"GR-LANG-002"
],
"checked_quality_gate_ids": [
"QG-001",
"QG-002",
"QG-003",
"QG-006"
],
"findings": []
}