feat(purchase): add filtering for multiple statuses in purchase index and order by status then date
This commit is contained in:
parent
75d3c819d9
commit
c6da71894e
|
|
@ -24,13 +24,22 @@ class PurchaseController extends Controller
|
||||||
{
|
{
|
||||||
public function index(Request $request, Tenant $tenant): JsonResponse
|
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(
|
return PurchaseResource::collection(
|
||||||
Purchase::query()
|
Purchase::query()
|
||||||
->where('tenant_codigo', $tenant->codigo)
|
->where('tenant_codigo', $tenant->codigo)
|
||||||
->where('user_id', $request->user()->id)
|
->where('user_id', $request->user()->id)
|
||||||
->when($request->query('status'), function ($query, $status) {
|
->when($statuses !== [], fn ($query) => $query->whereIn('status', $statuses))
|
||||||
$query->where('status', $status);
|
->orderBy('status')
|
||||||
})
|
|
||||||
->latest()
|
->latest()
|
||||||
->paginateFromRequest()
|
->paginateFromRequest()
|
||||||
)->response();
|
)->response();
|
||||||
|
|
|
||||||
|
|
@ -1230,6 +1230,52 @@ class StorePurchaseTest extends TestCase
|
||||||
->assertJsonPath('data.0.total', '100.00');
|
->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
|
public function test_it_rejects_a_cart_from_another_user(): void
|
||||||
{
|
{
|
||||||
$this->createTenant('sonder', 'Sonder', 'sonder.com.ar');
|
$this->createTenant('sonder', 'Sonder', 'sonder.com.ar');
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue