Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions app/Http/Controllers/Api/DeliveryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Models\Delivery;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;

Expand Down Expand Up @@ -52,13 +53,15 @@ public function index(Request $request): JsonResponse
$query->where('event_id', $request->input('event_id'));
}

// Filter by date range
// Filter by date range. Comparing against a plain timestamp range
// (rather than wrapping created_at in whereDate()) keeps the query
// sargable so it can use the index on created_at.
if ($request->has('from_date')) {
$query->whereDate('created_at', '>=', $request->input('from_date'));
$query->where('created_at', '>=', Carbon::parse($request->input('from_date'))->startOfDay());
}

if ($request->has('to_date')) {
$query->whereDate('created_at', '<=', $request->input('to_date'));
$query->where('created_at', '<', Carbon::parse($request->input('to_date'))->addDay()->startOfDay());
}

$deliveries = $query->orderBy('created_at', 'desc')
Expand Down
8 changes: 6 additions & 2 deletions app/Http/Controllers/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Models\Delivery;
use App\Models\Event;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Inertia\Inertia;
use Inertia\Response;

Expand Down Expand Up @@ -133,12 +134,15 @@ public function deliveries(Request $request): Response
$query->whereHas('event', fn ($q) => $q->where('name', 'like', '%'.$request->event_name.'%'));
}

// Comparing against a plain timestamp range (rather than wrapping
// created_at in whereDate()) keeps the query sargable so it can use
// the index on created_at.
if ($request->has('from_date') && $request->from_date) {
$query->whereDate('created_at', '>=', $request->from_date);
$query->where('created_at', '>=', Carbon::parse($request->from_date)->startOfDay());
}

if ($request->has('to_date') && $request->to_date) {
$query->whereDate('created_at', '<=', $request->to_date);
$query->where('created_at', '<', Carbon::parse($request->to_date)->addDay()->startOfDay());
}

$statusCounts = (clone $query)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class () extends Migration {
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('deliveries', function (Blueprint $table) {
$table->index('created_at');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('deliveries', function (Blueprint $table) {
$table->dropIndex(['created_at']);
});
}
};
93 changes: 93 additions & 0 deletions tests/Feature/DeliveryDateRangeFilterBoundaryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace Tests\Feature;

use App\Models\Delivery;
use App\Models\Endpoint;
use App\Models\Event;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Schema;
use Tests\TestCase;

class DeliveryDateRangeFilterBoundaryTest extends TestCase
{
use RefreshDatabase;

public function test_deliveries_table_has_an_index_on_created_at(): void
{
$indexedColumns = collect(Schema::getIndexes('deliveries'))
->pluck('columns')
->flatten()
->all();

$this->assertContains('created_at', $indexedColumns);
}

public function test_api_date_range_filter_includes_the_full_first_and_last_day(): void
{
$user = User::factory()->withPersonalTeam()->create();
$endpoint = Endpoint::factory()->for($user)->create();
$event = Event::factory()->for($user)->create();

$startOfFirstDay = Delivery::factory()->for($event)->for($endpoint)->create([
'created_at' => '2026-06-01 00:00:00',
]);
$endOfLastDay = Delivery::factory()->for($event)->for($endpoint)->create([
'created_at' => '2026-06-30 23:59:59',
]);
$beforeRange = Delivery::factory()->for($event)->for($endpoint)->create([
'created_at' => '2026-05-31 23:59:59',
]);
$afterRange = Delivery::factory()->for($event)->for($endpoint)->create([
'created_at' => '2026-07-01 00:00:00',
]);

$response = $this->actingAs($user)->getJson(
'/api/v1/deliveries?from_date=2026-06-01&to_date=2026-06-30'
);

$response->assertOk();
$ids = collect($response->json('data'))->pluck('id');

$this->assertTrue($ids->contains($startOfFirstDay->id));
$this->assertTrue($ids->contains($endOfLastDay->id));
$this->assertFalse($ids->contains($beforeRange->id));
$this->assertFalse($ids->contains($afterRange->id));
}

public function test_dashboard_date_range_filter_includes_the_full_first_and_last_day(): void
{
$user = User::factory()->withPersonalTeam()->create();
$endpoint = Endpoint::factory()->for($user)->create();
$event = Event::factory()->for($user)->create();

$startOfFirstDay = Delivery::factory()->for($event)->for($endpoint)->create([
'created_at' => '2026-06-01 00:00:00',
]);
$endOfLastDay = Delivery::factory()->for($event)->for($endpoint)->create([
'created_at' => '2026-06-30 23:59:59',
]);
$beforeRange = Delivery::factory()->for($event)->for($endpoint)->create([
'created_at' => '2026-05-31 23:59:59',
]);
$afterRange = Delivery::factory()->for($event)->for($endpoint)->create([
'created_at' => '2026-07-01 00: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', 2)
// Ordered by latest() first, so the end-of-range delivery sorts before the start-of-range one.
->where('deliveries.data.0.id', $endOfLastDay->id)
->where('deliveries.data.1.id', $startOfFirstDay->id)
);
}
}
Loading