Closes the four production gaps that emerged from sessie 3b's admin UI.
What we ship here is final: no further rework planned before production.
Backend
- IndexFailuresRequest validates state/search/failed_at_from/failed_at_to/
listener_class. orgIndex + platformIndex apply them via a single
applyIndexFilters() helper. Search runs case-insensitive `LIKE` on
exception_message; SQL wildcards in user input are escaped.
- New /kpis aggregate endpoint per scope (orgKpis, platformKpis) returns
open / resolved_30d / dismissed_30d / total_submissions in O(1) COUNTs.
Replaces sessie 3b's client-side bucketing of an oversized list.
- Resource expansion: organisation_name, form_schema_label,
resolved_by_user_name, dismissed_by_user_name, exception_trace,
retry_history[]. Eager-loading via indexEagerLoads()/detailEagerLoads()
prevents N+1 (verified by query-count assertion in test).
- New 2026_04_28_181000 migration adds exception_trace (longtext nullable)
to form_submission_action_failures. ApplyBindingsOnFormSubmit listener
now captures $e->getTraceAsString() at failure time.
- New FormSubmissionActionFailureRetryAttemptResource exposes per-attempt
data (timestamp, actor name, outcome, exception details) inside
retry_history[]. Index payloads omit the field via whenLoaded() to keep
list responses lean.
Frontend (apps/app)
- Types updated to mirror the expanded resource shape and the new KPI
endpoint contract. FormFailuresKpis is now { open, resolved_30d,
dismissed_30d, total_submissions } (server-aggregate).
- useFormFailures composable forwards all 5 server filters via
buildIndexParams() (strips empty/whitespace). useFormFailuresKpis hits
the dedicated /kpis endpoint per scope.
- FormFailuresTable replaces client-side bucketing with server-side
filtering, adds listener_class + date-range filter inputs, and renames
the 4th KPI tile to "Submissions" (was "Totaal").
- FormFailureDetail renders organisation_name + form_schema_label in the
header, surfaces an expandable stack-trace card, names the resolved/
dismissed actor in the timeline, and replaces the "v1 placeholder"
retry-history card with a full per-attempt timeline.
ESLint config gap (apps/app)
- New .eslintrc.cjs adapted from the Vuexy reference, minus Vuexy-internal
rules. `pnpm lint` now runs successfully (was previously broken — the
package.json script referenced a missing config). The 80 baseline
violations across the codebase are pre-existing and out of scope for
this session.
Tests + gates
- 24 new backend tests across filter, kpis, and resource-shape suites.
Backend: 1462 → 1486 passing, 0 → 0 failing. Larastan clean. Rector
dry-run unchanged at 354 (pre-Task-1 baseline from f18b55b).
- 3 new vitest tests in apps/app (filter wiring, KPI endpoint, KPI tile
values from /kpis). Vitest: 38 → 41 passing. tsc clean. Portal
unchanged (113 vitest, tsc clean).
- 5 backfill rollback tests bumped --step counts +1 for the new migration.
- Ws6FoundationMigrationTest down/up chain now includes exception_trace
before the parent table is restored.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
149 lines
5.6 KiB
PHP
149 lines
5.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Feature\FormBuilder\Api;
|
|
|
|
use App\Models\FormBuilder\FormSchema;
|
|
use App\Models\FormBuilder\FormSubmission;
|
|
use App\Models\FormBuilder\FormSubmissionActionFailure;
|
|
use App\Models\Organisation;
|
|
use App\Models\User;
|
|
use Database\Seeders\RoleSeeder;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Laravel\Sanctum\Sanctum;
|
|
use Tests\TestCase;
|
|
|
|
/**
|
|
* Sessie 3c — admin-UI dashboard KPI counters. Both org-scoped and
|
|
* platform-wide endpoints; tenant gate via FK chain (RFC V3).
|
|
*/
|
|
final class FormSubmissionActionFailureKpisTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
private Organisation $orgA;
|
|
|
|
private Organisation $orgB;
|
|
|
|
private User $orgAdminA;
|
|
|
|
private User $orgAdminB;
|
|
|
|
private User $superAdmin;
|
|
|
|
private FormSubmission $submissionA;
|
|
|
|
private FormSubmission $submissionB;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
$this->seed(RoleSeeder::class);
|
|
|
|
$this->orgA = Organisation::factory()->create();
|
|
$this->orgB = Organisation::factory()->create();
|
|
|
|
$schemaA = FormSchema::factory()->create(['organisation_id' => $this->orgA->id]);
|
|
$schemaB = FormSchema::factory()->create(['organisation_id' => $this->orgB->id]);
|
|
$this->submissionA = FormSubmission::factory()->create([
|
|
'form_schema_id' => $schemaA->id,
|
|
'organisation_id' => $this->orgA->id,
|
|
]);
|
|
$this->submissionB = FormSubmission::factory()->create([
|
|
'form_schema_id' => $schemaB->id,
|
|
'organisation_id' => $this->orgB->id,
|
|
]);
|
|
|
|
$this->orgAdminA = User::factory()->create();
|
|
$this->orgA->users()->attach($this->orgAdminA, ['role' => 'org_admin']);
|
|
$this->orgAdminB = User::factory()->create();
|
|
$this->orgB->users()->attach($this->orgAdminB, ['role' => 'org_admin']);
|
|
$this->superAdmin = User::factory()->create();
|
|
$this->superAdmin->assignRole('super_admin');
|
|
}
|
|
|
|
public function test_org_kpis_counts_open_resolved_dismissed_and_total_submissions(): void
|
|
{
|
|
FormSubmissionActionFailure::factory()->for($this->submissionA, 'submission')->create();
|
|
FormSubmissionActionFailure::factory()->for($this->submissionA, 'submission')->create();
|
|
FormSubmissionActionFailure::factory()->for($this->submissionA, 'submission')
|
|
->create(['resolved_at' => now()->subDays(5)]);
|
|
FormSubmissionActionFailure::factory()->for($this->submissionA, 'submission')
|
|
->create(['dismissed_at' => now()->subDays(10), 'dismissed_reason_type' => 'schema_deleted']);
|
|
|
|
// Outside-30d window — must NOT count.
|
|
FormSubmissionActionFailure::factory()->for($this->submissionA, 'submission')
|
|
->create(['resolved_at' => now()->subDays(45)]);
|
|
|
|
// Other tenant — must NOT count.
|
|
FormSubmissionActionFailure::factory()->for($this->submissionB, 'submission')->create();
|
|
|
|
Sanctum::actingAs($this->orgAdminA);
|
|
$response = $this->getJson("/api/v1/organisations/{$this->orgA->id}/form-failures/kpis")->assertOk();
|
|
|
|
$response->assertJsonPath('data.open', 2);
|
|
$response->assertJsonPath('data.resolved_30d', 1);
|
|
$response->assertJsonPath('data.dismissed_30d', 1);
|
|
$response->assertJsonPath('data.total_submissions', 1);
|
|
}
|
|
|
|
public function test_org_kpis_cross_tenant_returns_404(): void
|
|
{
|
|
FormSubmissionActionFailure::factory()->for($this->submissionA, 'submission')->create();
|
|
|
|
Sanctum::actingAs($this->orgAdminB);
|
|
$this->getJson("/api/v1/organisations/{$this->orgA->id}/form-failures/kpis")
|
|
->assertStatus(404);
|
|
}
|
|
|
|
public function test_org_kpis_unauthenticated_returns_401(): void
|
|
{
|
|
$this->getJson("/api/v1/organisations/{$this->orgA->id}/form-failures/kpis")
|
|
->assertStatus(401);
|
|
}
|
|
|
|
public function test_platform_kpis_aggregates_across_all_orgs(): void
|
|
{
|
|
FormSubmissionActionFailure::factory()->for($this->submissionA, 'submission')->create();
|
|
FormSubmissionActionFailure::factory()->for($this->submissionB, 'submission')->create();
|
|
FormSubmissionActionFailure::factory()->for($this->submissionA, 'submission')
|
|
->create(['resolved_at' => now()->subDays(2)]);
|
|
|
|
Sanctum::actingAs($this->superAdmin);
|
|
$response = $this->getJson('/api/v1/admin/form-failures/kpis')->assertOk();
|
|
|
|
$response->assertJsonPath('data.open', 2);
|
|
$response->assertJsonPath('data.resolved_30d', 1);
|
|
$response->assertJsonPath('data.dismissed_30d', 0);
|
|
$response->assertJsonPath('data.total_submissions', 2);
|
|
}
|
|
|
|
public function test_platform_kpis_org_admin_returns_403(): void
|
|
{
|
|
Sanctum::actingAs($this->orgAdminA);
|
|
$this->getJson('/api/v1/admin/form-failures/kpis')
|
|
->assertStatus(403);
|
|
}
|
|
|
|
public function test_platform_kpis_unauthenticated_returns_401(): void
|
|
{
|
|
$this->getJson('/api/v1/admin/form-failures/kpis')->assertStatus(401);
|
|
}
|
|
|
|
public function test_kpis_with_no_data_returns_zeros(): void
|
|
{
|
|
$orgC = Organisation::factory()->create();
|
|
$orgAdminC = User::factory()->create();
|
|
$orgC->users()->attach($orgAdminC, ['role' => 'org_admin']);
|
|
|
|
Sanctum::actingAs($orgAdminC);
|
|
$response = $this->getJson("/api/v1/organisations/{$orgC->id}/form-failures/kpis")->assertOk();
|
|
|
|
$response->assertJsonPath('data.open', 0);
|
|
$response->assertJsonPath('data.resolved_30d', 0);
|
|
$response->assertJsonPath('data.dismissed_30d', 0);
|
|
$response->assertJsonPath('data.total_submissions', 0);
|
|
}
|
|
}
|