fix(orders): preserve pagination and query parameters in IndexOrdersRequest
This commit is contained in:
parent
30c4483ce9
commit
f84a1a7b8c
3 changed files with 38 additions and 7 deletions
|
|
@ -57,8 +57,7 @@ class IndexOrdersRequest extends FormRequest
|
|||
#[\Override]
|
||||
protected function prepareForValidation(): void
|
||||
{
|
||||
// * Keep only non null values.
|
||||
$inputs = [];
|
||||
// * Remove empty filter values to prevent date and enum validation failures on empty strings.
|
||||
foreach (
|
||||
[
|
||||
'order_date_start',
|
||||
|
|
@ -68,12 +67,11 @@ class IndexOrdersRequest extends FormRequest
|
|||
'status',
|
||||
] as $inputName
|
||||
) {
|
||||
if (! empty($this->input($inputName))) {
|
||||
$inputs[$inputName] = $this->input($inputName);
|
||||
if ($this->has($inputName) && empty($this->input($inputName))) {
|
||||
$this->request->remove($inputName);
|
||||
$this->query->remove($inputName);
|
||||
}
|
||||
}
|
||||
|
||||
$this->replace($inputs);
|
||||
}
|
||||
|
||||
public function getStatus(): ?OrderStatus
|
||||
|
|
|
|||
|
|
@ -62,6 +62,34 @@ describe('Bo\OrdersController', function (): void {
|
|||
$response->assertViewHas('orderStatusList');
|
||||
});
|
||||
|
||||
it('can paginate orders across pages', function (): void {
|
||||
/** @var \Tests\TestCase $this */
|
||||
// GIVEN
|
||||
$shop = Shop::factory()->create(['name' => 'Boutique Pagination']);
|
||||
for ($i = 0; $i < 30; $i++) {
|
||||
Order::forceCreate([
|
||||
'id' => Uuid::uuid7()->toString(),
|
||||
'shop_id' => $shop->id,
|
||||
'reference' => new OrderReference(\Symfony\Component\Uid\Uuid::v7()->toBase58()),
|
||||
'status' => OrderStatus::VALIDATED->value,
|
||||
'total' => new Money(2500, new Currency('EUR')),
|
||||
'employee' => new OrderEmployee(new Matricule((string) (1000100 + $i)), 'Alice', 'Smith', new ShopperEstablishmentCode('FAC01'), new ShopperSiteCode('SITE'), new ShopperServiceCode('101')),
|
||||
'delivery_address' => new DeliveryAddress('Shop Name', 'Address', '75000', 'Paris'),
|
||||
'pickup_day' => Date::now()->toDateString(),
|
||||
]);
|
||||
}
|
||||
|
||||
// WHEN
|
||||
$response = $this->get('/bo/orders?page=2');
|
||||
|
||||
// THEN
|
||||
expect($response->status())->toBe(200);
|
||||
$response->assertViewHas('orders', function ($orders): bool {
|
||||
return $orders instanceof LengthAwarePaginator
|
||||
&& $orders->currentPage() === 2;
|
||||
});
|
||||
});
|
||||
|
||||
it('can filter index by search term', function (): void {
|
||||
/** @var \Tests\TestCase $this */
|
||||
// GIVEN
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@ describe('IndexOrdersRequest', function () {
|
|||
expect($request->authorize())->toBeTrue();
|
||||
});
|
||||
|
||||
it('prepares for validation by keeping only non-null values', function () {
|
||||
it('prepares for validation by removing empty filter values while preserving other parameters', function () {
|
||||
// GIVEN
|
||||
$authService = mock(AppAdminAuthService::class);
|
||||
$request = new IndexOrdersRequest($authService);
|
||||
|
|
@ -36,6 +36,7 @@ describe('IndexOrdersRequest', function () {
|
|||
'order_date_end' => '',
|
||||
'order_pickup_start' => null,
|
||||
'status' => 'pending',
|
||||
'page' => '3',
|
||||
'other_field' => 'value',
|
||||
]);
|
||||
|
||||
|
|
@ -48,6 +49,8 @@ describe('IndexOrdersRequest', function () {
|
|||
expect($request->all())->toBe([
|
||||
'order_date_start' => '2023-01-01',
|
||||
'status' => 'pending',
|
||||
'page' => '3',
|
||||
'other_field' => 'value',
|
||||
]);
|
||||
});
|
||||
|
||||
|
|
@ -163,6 +166,7 @@ describe('IndexOrdersRequest', function () {
|
|||
$authService = mock(AppAdminAuthService::class);
|
||||
$request = new IndexOrdersRequest($authService);
|
||||
$request->initialize(query: [
|
||||
'page' => 3,
|
||||
'search' => 'TEST-REF',
|
||||
'status' => (string) OrderStatus::VALIDATED->value,
|
||||
]);
|
||||
|
|
@ -172,6 +176,7 @@ describe('IndexOrdersRequest', function () {
|
|||
|
||||
// THEN
|
||||
expect($input)->toBeInstanceOf(GetBoPaginatedOrdersQueryInput::class)
|
||||
->and($input->pagination->pageNumber()->value())->toBe(3)
|
||||
->and($input->search)->toBe('TEST-REF')
|
||||
->and($input->status)->toBe(OrderStatus::VALIDATED);
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue