From 51889222539bf40371b903cffb458ef3dd016e06 Mon Sep 17 00:00:00 2001 From: Morris Jencen Chavez Date: Sun, 6 Sep 2026 19:21:01 +0000 Subject: [PATCH] fix: apply event_name and date-range filters on the deliveries dashboard page The Deliveries index page collects event_name, from_date, and to_date filters in the UI, but DashboardController::deliveries() never read them, so results silently ignored those filters while still showing the empty-state copy suggesting the user adjust their filters. Apply the missing filters server-side (matching the convention already used by the API's DeliveryController::index()) and include them in the filters prop returned to the page so the UI reflects what's actually active. Fixes #81 --- app/Http/Controllers/DashboardController.php | 14 ++- .../Feature/DashboardDeliveriesFilterTest.php | 91 +++++++++++++++++++ 2 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 tests/Feature/DashboardDeliveriesFilterTest.php diff --git a/app/Http/Controllers/DashboardController.php b/app/Http/Controllers/DashboardController.php index 3a6e787..8c87c58 100644 --- a/app/Http/Controllers/DashboardController.php +++ b/app/Http/Controllers/DashboardController.php @@ -94,6 +94,18 @@ public function deliveries(Request $request): Response ->find($request->event_id, ['id', 'name']); } + if ($request->has('event_name') && $request->event_name) { + $query->whereHas('event', fn ($q) => $q->where('name', 'like', '%'.$request->event_name.'%')); + } + + if ($request->has('from_date') && $request->from_date) { + $query->whereDate('created_at', '>=', $request->from_date); + } + + if ($request->has('to_date') && $request->to_date) { + $query->whereDate('created_at', '<=', $request->to_date); + } + $deliveries = $query->latest()->paginate(20); // Get filter options @@ -102,7 +114,7 @@ public function deliveries(Request $request): Response return Inertia::render('Deliveries/Index', [ 'deliveries' => $deliveries, 'endpoints' => $endpoints, - 'filters' => $request->only(['status', 'endpoint_id', 'event_id']), + 'filters' => $request->only(['status', 'endpoint_id', 'event_id', 'event_name', 'from_date', 'to_date']), 'filteredEvent' => $filteredEvent, ]); } diff --git a/tests/Feature/DashboardDeliveriesFilterTest.php b/tests/Feature/DashboardDeliveriesFilterTest.php new file mode 100644 index 0000000..a7ba565 --- /dev/null +++ b/tests/Feature/DashboardDeliveriesFilterTest.php @@ -0,0 +1,91 @@ +withPersonalTeam()->create(); + $endpoint = Endpoint::factory()->for($user)->create(); + + $targetEvent = Event::factory()->for($user)->create(['name' => 'user.created']); + $otherEvent = Event::factory()->for($user)->create(['name' => 'order.updated']); + + $targetDelivery = Delivery::factory()->for($targetEvent)->for($endpoint)->create(); + Delivery::factory()->for($otherEvent)->for($endpoint)->create(); + + $response = $this->actingAs($user)->get(route('deliveries', ['event_name' => 'user.cre'])); + + $response->assertOk(); + $response->assertInertia( + fn ($page) => $page + ->component('Deliveries/Index') + ->has('deliveries.data', 1) + ->where('deliveries.data.0.id', $targetDelivery->id) + ->where('filters.event_name', 'user.cre') + ); + } + + public function test_date_range_filters_only_return_deliveries_within_range(): void + { + $user = User::factory()->withPersonalTeam()->create(); + $endpoint = Endpoint::factory()->for($user)->create(); + $event = Event::factory()->for($user)->create(); + + $withinRange = Delivery::factory()->for($event)->for($endpoint)->create([ + 'created_at' => '2026-06-15 12:00:00', + ]); + Delivery::factory()->for($event)->for($endpoint)->create([ + 'created_at' => '2026-05-01 12:00:00', + ]); + Delivery::factory()->for($event)->for($endpoint)->create([ + 'created_at' => '2026-07-01 12:00:00', + ]); + + $response = $this->actingAs($user)->get(route('deliveries', [ + 'from_date' => '2026-06-01', + 'to_date' => '2026-06-30', + ])); + + $response->assertOk(); + $response->assertInertia( + fn ($page) => $page + ->component('Deliveries/Index') + ->has('deliveries.data', 1) + ->where('deliveries.data.0.id', $withinRange->id) + ->where('filters.from_date', '2026-06-01') + ->where('filters.to_date', '2026-06-30') + ); + } + + public function test_event_name_filter_cannot_be_used_to_view_another_users_deliveries(): void + { + $user = User::factory()->withPersonalTeam()->create(); + $endpoint = Endpoint::factory()->for($user)->create(); + Event::factory()->for($user)->create(['name' => 'user.created']); + + $otherUser = User::factory()->withPersonalTeam()->create(); + $otherEndpoint = Endpoint::factory()->for($otherUser)->create(); + $otherEvent = Event::factory()->for($otherUser)->create(['name' => 'user.created']); + Delivery::factory()->for($otherEvent)->for($otherEndpoint)->create(); + + $response = $this->actingAs($user)->get(route('deliveries', ['event_name' => 'user.created'])); + + $response->assertOk(); + $response->assertInertia( + fn ($page) => $page + ->component('Deliveries/Index') + ->has('deliveries.data', 0) + ); + } +}