diff --git a/app/Domains/Purchase/Controllers/PurchaseController.php b/app/Domains/Purchase/Controllers/PurchaseController.php index 483b73f..5db012f 100644 --- a/app/Domains/Purchase/Controllers/PurchaseController.php +++ b/app/Domains/Purchase/Controllers/PurchaseController.php @@ -24,13 +24,22 @@ class PurchaseController extends Controller { public function index(Request $request, Tenant $tenant): JsonResponse { + $statusParam = $request->query('status'); + $statuses = is_string($statusParam) + ? collect(explode(',', $statusParam)) + ->map(fn (string $status): string => trim($status)) + ->filter() + ->unique() + ->values() + ->all() + : []; + return PurchaseResource::collection( Purchase::query() ->where('tenant_codigo', $tenant->codigo) ->where('user_id', $request->user()->id) - ->when($request->query('status'), function ($query, $status) { - $query->where('status', $status); - }) + ->when($statuses !== [], fn ($query) => $query->whereIn('status', $statuses)) + ->orderBy('status') ->latest() ->paginateFromRequest() )->response(); diff --git a/tests/Feature/Purchase/StorePurchaseTest.php b/tests/Feature/Purchase/StorePurchaseTest.php index 9fda55a..91f521a 100644 --- a/tests/Feature/Purchase/StorePurchaseTest.php +++ b/tests/Feature/Purchase/StorePurchaseTest.php @@ -1230,6 +1230,52 @@ class StorePurchaseTest extends TestCase ->assertJsonPath('data.0.total', '100.00'); } + public function test_purchase_index_filters_multiple_comma_separated_statuses_and_orders_by_status_ascending_then_date(): void + { + $this->createTenant('sonder', 'Sonder', 'sonder.com.ar'); + $user = User::factory()->create(); + + $olderPaid = Purchase::query()->create([ + 'tenant_codigo' => 'sonder', + 'user_id' => $user->id, + 'status' => Purchase::STATUS_PAID, + 'total' => '10.00', + ]); + $newerInReview = Purchase::query()->create([ + 'tenant_codigo' => 'sonder', + 'user_id' => $user->id, + 'status' => Purchase::STATUS_IN_REVIEW, + 'total' => '20.00', + ]); + $newerPaid = Purchase::query()->create([ + 'tenant_codigo' => 'sonder', + 'user_id' => $user->id, + 'status' => Purchase::STATUS_PAID, + 'total' => '30.00', + ]); + Purchase::query()->create([ + 'tenant_codigo' => 'sonder', + 'user_id' => $user->id, + 'status' => Purchase::STATUS_CANCELLED, + 'total' => '40.00', + ]); + + $olderPaid->forceFill(['created_at' => now()->subDays(3)])->saveQuietly(); + $newerInReview->forceFill(['created_at' => now()])->saveQuietly(); + $newerPaid->forceFill(['created_at' => now()->subDay()])->saveQuietly(); + + $this->actingAs($user, 'sanctum') + ->getJson('/api/tenants/sonder/compras?status=paid,%20in_review,paid') + ->assertOk() + ->assertJsonCount(3, 'data') + ->assertJsonPath('data.0.id', $newerInReview->id) + ->assertJsonPath('data.0.status', Purchase::STATUS_IN_REVIEW) + ->assertJsonPath('data.1.id', $newerPaid->id) + ->assertJsonPath('data.1.status', Purchase::STATUS_PAID) + ->assertJsonPath('data.2.id', $olderPaid->id) + ->assertJsonPath('data.2.status', Purchase::STATUS_PAID); + } + public function test_it_rejects_a_cart_from_another_user(): void { $this->createTenant('sonder', 'Sonder', 'sonder.com.ar');