Files
crewli/api/app/Models/FormBuilder/FormSubmissionActionFailure.php
bert.hausmans 192353f4bc feat(form-builder): admin UI completion — server filters, KPIs, resource expansion (WS-6 sessie 3c)
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>
2026-04-29 00:14:20 +02:00

147 lines
4.5 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Models\FormBuilder;
use App\Enums\FormBuilder\DismissalReasonType;
use App\Models\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Concerns\HasUlids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
/**
* RFC-WS-6 §3 (Q5) — audit table for binding-pipeline failures.
*
* Audit model with no `organisation_id` column. Tenant scope flows via
* the FK chain to `form_submissions.organisation_id`. The
* {@see \App\Policies\FormBuilder\FormSubmissionActionFailurePolicy}
* enforces this at access time per RFC §4 V3 (IDOR-class FK-chain
* pattern). Do NOT register `OrganisationScope` directly on this model.
*
* Resolve and Dismiss are mutually exclusive workflows (RFC V2):
* - Resolved → succeeded via another path (resolved_at + resolved_note)
* - Dismissed → will not be replayed (dismissed_at + reason_type/note)
*/
final class FormSubmissionActionFailure extends Model
{
/** @use HasFactory<\Database\Factories\FormBuilder\FormSubmissionActionFailureFactory> */
use HasFactory;
use HasUlids;
protected $table = 'form_submission_action_failures';
protected $fillable = [
'form_submission_id',
'listener_class',
'binding_id',
'failed_at',
'exception_class',
'exception_message',
'exception_trace',
'context',
'retry_count',
'resolved_at',
'resolved_by_user_id',
'resolved_note',
'dismissed_at',
'dismissed_by_user_id',
'dismissed_reason_type',
'dismissed_reason_note',
];
/** @var array<string, string> */
protected $casts = [
'failed_at' => 'datetime',
'resolved_at' => 'datetime',
'dismissed_at' => 'datetime',
'context' => 'array',
'retry_count' => 'int',
'dismissed_reason_type' => DismissalReasonType::class,
];
/** @return BelongsTo<FormSubmission, $this> */
public function submission(): BelongsTo
{
return $this->belongsTo(FormSubmission::class, 'form_submission_id');
}
/** @return BelongsTo<FormFieldBinding, $this> */
public function binding(): BelongsTo
{
return $this->belongsTo(FormFieldBinding::class, 'binding_id');
}
/** @return BelongsTo<User, $this> */
public function resolvedBy(): BelongsTo
{
return $this->belongsTo(User::class, 'resolved_by_user_id');
}
/** @return BelongsTo<User, $this> */
public function dismissedBy(): BelongsTo
{
return $this->belongsTo(User::class, 'dismissed_by_user_id');
}
/**
* RFC-WS-6 Q5 addendum (sessie 3c) — per-attempt retry history.
* `retry_count` on this model stays as denormalized cache; the
* detail UI consumes this relation for per-attempt timeline.
*
* @return HasMany<FormSubmissionActionFailureRetryAttempt, $this>
*/
public function retryAttempts(): HasMany
{
return $this->hasMany(FormSubmissionActionFailureRetryAttempt::class, 'form_submission_action_failure_id')
->latest('attempted_at');
}
/**
* @param Builder<FormSubmissionActionFailure> $query
* @return Builder<FormSubmissionActionFailure>
*/
protected function scopeOpen(Builder $query): Builder
{
return $query->whereNull('resolved_at')->whereNull('dismissed_at');
}
/**
* @param Builder<FormSubmissionActionFailure> $query
* @return Builder<FormSubmissionActionFailure>
*/
protected function scopeResolved(Builder $query): Builder
{
return $query->whereNotNull('resolved_at');
}
/**
* @param Builder<FormSubmissionActionFailure> $query
* @return Builder<FormSubmissionActionFailure>
*/
protected function scopeDismissed(Builder $query): Builder
{
return $query->whereNotNull('dismissed_at');
}
public function isOpen(): bool
{
return $this->resolved_at === null && $this->dismissed_at === null;
}
/**
* Sessie 3c (Q2 closure): a resolved failure also blocks retry —
* retrying a closed failure would either no-op or trigger a
* spurious state transition. Both are unwanted. Open is the only
* retriable state.
*/
public function canBeRetried(): bool
{
return $this->resolved_at === null && $this->dismissed_at === null;
}
}