From 0d8e69073492e44b63d3705450400f6f96053878 Mon Sep 17 00:00:00 2001 From: Jay Hemnani <193022578+jayhemnani9910@users.noreply.github.com> Date: Sun, 1 Nov 2026 03:21:29 +0530 Subject: [PATCH] Show and sort incidents without an occurred time by their created time in the dashboard --- .../Resources/Incidents/IncidentResource.php | 6 ++++-- src/Filament/Widgets/OpenIncidents.php | 3 ++- .../Filament/Resources/IncidentResourceTest.php | 15 +++++++++++++++ .../Filament/Widgets/OpenIncidentsTest.php | 15 +++++++++++++++ 4 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/Filament/Resources/Incidents/IncidentResource.php b/src/Filament/Resources/Incidents/IncidentResource.php index b04bbd6a..7b4f7c17 100644 --- a/src/Filament/Resources/Incidents/IncidentResource.php +++ b/src/Filament/Resources/Incidents/IncidentResource.php @@ -176,8 +176,10 @@ public static function table(Table $table): Table ->boolean(), TextColumn::make('occurred_at') ->label(__('cachet::incident.list.headers.occurred_at')) + ->state(fn (Incident $record) => $record->timestamp) ->dateTime() - ->sortable(), + ->sortable(query: fn (Builder $query, string $direction): Builder => $query + ->orderByRaw('coalesce(occurred_at, created_at) '.($direction === 'asc' ? 'asc' : 'desc'))), TextColumn::make('published_at') ->label(__('cachet::incident.list.headers.published_at')) ->dateTime() @@ -232,7 +234,7 @@ public static function table(Table $table): Table ]) ->defaultSort(fn (Builder $query): Builder => $query ->orderByRaw('case when status = ? then 1 else 0 end', [IncidentStatusEnum::fixed->value]) - ->orderByDesc('occurred_at')) + ->orderByRaw('coalesce(occurred_at, created_at) desc')) ->emptyStateHeading(__('cachet::incident.list.empty_state.heading')) ->emptyStateDescription(__('cachet::incident.list.empty_state.description')); } diff --git a/src/Filament/Widgets/OpenIncidents.php b/src/Filament/Widgets/OpenIncidents.php index 96dcf2be..c220a537 100644 --- a/src/Filament/Widgets/OpenIncidents.php +++ b/src/Filament/Widgets/OpenIncidents.php @@ -26,7 +26,7 @@ public function table(Table $table): Table ->unresolved() ->with('updates') ->withCount('components') - ->orderByDesc('occurred_at') + ->orderByRaw('coalesce(occurred_at, created_at) desc') ) ->heading(__('cachet::dashboard.open_incidents.heading')) ->poll($this->getPollingInterval()) @@ -40,6 +40,7 @@ public function table(Table $table): Table ->badge(), TextColumn::make('occurred_at') ->label(__('cachet::dashboard.open_incidents.headers.occurred_at')) + ->state(fn (Incident $record) => $record->timestamp) ->since() ->tooltip(fn (Incident $record): string => $record->timestamp->toDayDateTimeString()), TextColumn::make('components_count') diff --git a/tests/Feature/Filament/Resources/IncidentResourceTest.php b/tests/Feature/Filament/Resources/IncidentResourceTest.php index 92b5dca8..e4157f74 100644 --- a/tests/Feature/Filament/Resources/IncidentResourceTest.php +++ b/tests/Feature/Filament/Resources/IncidentResourceTest.php @@ -185,3 +185,18 @@ expect(collect($options)->flatten()->all())->not->toContain('Already Attached'); }); + +it('lists incidents without an occurred time by their created time', function () { + $older = Incident::factory()->create([ + 'status' => IncidentStatusEnum::investigating, + 'occurred_at' => now()->subDays(2), + ]); + $newer = Incident::factory()->create([ + 'status' => IncidentStatusEnum::investigating, + 'occurred_at' => null, + ]); + + livewire(ListIncidents::class) + ->assertCanSeeTableRecords([$newer, $older], inOrder: true) + ->assertTableColumnStateSet('occurred_at', $newer->created_at, $newer); +}); diff --git a/tests/Feature/Filament/Widgets/OpenIncidentsTest.php b/tests/Feature/Filament/Widgets/OpenIncidentsTest.php index a6e15afd..51897966 100644 --- a/tests/Feature/Filament/Widgets/OpenIncidentsTest.php +++ b/tests/Feature/Filament/Widgets/OpenIncidentsTest.php @@ -48,3 +48,18 @@ $component->assertSee(__('cachet::dashboard.open_incidents.empty_state.heading')); }); + +it('uses the created time for incidents without an occurred time', function () { + $older = Incident::factory()->create([ + 'status' => IncidentStatusEnum::investigating, + 'occurred_at' => now()->subDays(2), + ]); + $newer = Incident::factory()->create([ + 'status' => IncidentStatusEnum::investigating, + 'occurred_at' => null, + ]); + + livewire(OpenIncidents::class) + ->assertCanSeeTableRecords([$newer, $older], inOrder: true) + ->assertTableColumnStateSet('occurred_at', $newer->created_at, $newer); +});