From e76fba3b2e63482c20fe01d4ff385cf2e9a7f2e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Witold=20Wis=CC=81niewski?= Date: Fri, 25 Oct 2024 13:43:55 +0200 Subject: [PATCH 1/2] Added search to payment methods, price maps and shipping methods --- app/Criteria/WhereHasNameLike.php | 14 +++++++++++++ .../Controllers/PriceMapController.php | 5 +++-- .../Dtos/PaymentMethodIndexDto.php | 1 + .../PaymentMethods/Models/PaymentMethod.php | 2 ++ src/Domain/PriceMap/Dtos/PriceMapIndexDto.php | 15 ++++++++++++++ src/Domain/PriceMap/PriceMap.php | 8 ++++++++ src/Domain/PriceMap/PriceMapService.php | 5 +++-- .../Dtos/ShippingMethodIndexDto.php | 1 + .../ShippingMethod/Models/ShippingMethod.php | 2 ++ .../Services/ShippingMethodService.php | 1 + tests/Feature/PaymentMethodTest.php | 14 +++++++++++++ tests/Feature/PriceMap/PriceMapTest.php | 12 +++++++++++ tests/Feature/ShippingMethodTest.php | 20 +++++++++++++++++++ 13 files changed, 96 insertions(+), 4 deletions(-) create mode 100644 app/Criteria/WhereHasNameLike.php create mode 100644 src/Domain/PriceMap/Dtos/PriceMapIndexDto.php diff --git a/app/Criteria/WhereHasNameLike.php b/app/Criteria/WhereHasNameLike.php new file mode 100644 index 000000000..fef017d31 --- /dev/null +++ b/app/Criteria/WhereHasNameLike.php @@ -0,0 +1,14 @@ +where('name', 'LIKE', "%{$this->value}%"); + } +} diff --git a/src/Application/PriceMap/Controllers/PriceMapController.php b/src/Application/PriceMap/Controllers/PriceMapController.php index bc82ac6a1..98e9aba69 100644 --- a/src/Application/PriceMap/Controllers/PriceMapController.php +++ b/src/Application/PriceMap/Controllers/PriceMapController.php @@ -8,6 +8,7 @@ use App\Models\Product; use Application\PriceMap\Requests\PriceMapListPricesRequest; use Domain\PriceMap\Dtos\PriceMapCreateDto; +use Domain\PriceMap\Dtos\PriceMapIndexDto; use Domain\PriceMap\Dtos\PriceMapPricesUpdateDto; use Domain\PriceMap\Dtos\PriceMapProductPricesUpdateDto; use Domain\PriceMap\Dtos\PriceMapSchemaPricesUpdateDto; @@ -28,9 +29,9 @@ public function __construct( private PriceMapService $priceMapService, ) {} - public function index(Request $request): HttpResponse + public function index(Request $request, PriceMapIndexDto $dto): HttpResponse { - return $this->priceMapService->list()->toResponse($request); + return $this->priceMapService->list($dto)->toResponse($request); } public function store(Request $request, PriceMapCreateDto $dto): HttpResponse diff --git a/src/Domain/PaymentMethods/Dtos/PaymentMethodIndexDto.php b/src/Domain/PaymentMethods/Dtos/PaymentMethodIndexDto.php index 0a299e4d3..2d18bb441 100644 --- a/src/Domain/PaymentMethods/Dtos/PaymentMethodIndexDto.php +++ b/src/Domain/PaymentMethods/Dtos/PaymentMethodIndexDto.php @@ -25,6 +25,7 @@ public function __construct( public readonly array|Optional $ids, #[Uuid, Exists('sales_channels', 'id')] public readonly Optional|string $sales_channel_id, + public readonly Optional|string $search, ) {} /** diff --git a/src/Domain/PaymentMethods/Models/PaymentMethod.php b/src/Domain/PaymentMethods/Models/PaymentMethod.php index a87b6f2e0..3d2aefad5 100644 --- a/src/Domain/PaymentMethods/Models/PaymentMethod.php +++ b/src/Domain/PaymentMethods/Models/PaymentMethod.php @@ -4,6 +4,7 @@ namespace Domain\PaymentMethods\Models; +use App\Criteria\WhereHasNameLike; use App\Criteria\WhereHasOrderWithCode; use App\Criteria\WhereHasSalesChannel; use App\Criteria\WhereHasShippingMethod; @@ -56,6 +57,7 @@ final class PaymentMethod extends Model 'alias', 'ids' => WhereInIds::class, 'sales_channel_id' => WhereHasSalesChannel::class, + 'search' => WhereHasNameLike::class, ]; /** diff --git a/src/Domain/PriceMap/Dtos/PriceMapIndexDto.php b/src/Domain/PriceMap/Dtos/PriceMapIndexDto.php new file mode 100644 index 000000000..237d99aca --- /dev/null +++ b/src/Domain/PriceMap/Dtos/PriceMapIndexDto.php @@ -0,0 +1,15 @@ + @@ -38,6 +41,11 @@ final class PriceMap extends Model 'prices_generated' => 'bool', ]; + /** @var string[] */ + protected array $criteria = [ + 'search' => WhereHasNameLike::class, + ]; + /** * @return HasMany */ diff --git a/src/Domain/PriceMap/PriceMapService.php b/src/Domain/PriceMap/PriceMapService.php index 2c526e791..d71d6a45a 100644 --- a/src/Domain/PriceMap/PriceMapService.php +++ b/src/Domain/PriceMap/PriceMapService.php @@ -13,6 +13,7 @@ use Domain\Currency\Currency; use Domain\Price\Dtos\PriceDto; use Domain\PriceMap\Dtos\PriceMapCreateDto; +use Domain\PriceMap\Dtos\PriceMapIndexDto; use Domain\PriceMap\Dtos\PriceMapPricesUpdateDto; use Domain\PriceMap\Dtos\PriceMapPriceUpdateDto; use Domain\PriceMap\Dtos\PriceMapProductPricesUpdateDto; @@ -53,9 +54,9 @@ /** * @return PaginatedDataCollection */ - public function list(): PaginatedDataCollection + public function list(PriceMapIndexDto $dto): PaginatedDataCollection { - return PriceMapData::collection(PriceMap::query()->paginate(Config::get('pagination.per_page'))); + return PriceMapData::collection(PriceMap::searchByCriteria($dto->toArray())->paginate(Config::get('pagination.per_page'))); } public function create(PriceMapCreateDto $dto): PriceMap diff --git a/src/Domain/ShippingMethod/Dtos/ShippingMethodIndexDto.php b/src/Domain/ShippingMethod/Dtos/ShippingMethodIndexDto.php index 76af2ff02..ead124f5e 100644 --- a/src/Domain/ShippingMethod/Dtos/ShippingMethodIndexDto.php +++ b/src/Domain/ShippingMethod/Dtos/ShippingMethodIndexDto.php @@ -37,6 +37,7 @@ public function __construct( public array|Optional $metadata_private, #[Uuid, Exists('sales_channels', 'id')] public Optional|string $sales_channel_id, + public Optional|string $search, #[ArrayType] public array $items = [], ) {} diff --git a/src/Domain/ShippingMethod/Models/ShippingMethod.php b/src/Domain/ShippingMethod/Models/ShippingMethod.php index 32a9c60a4..64b7bf559 100644 --- a/src/Domain/ShippingMethod/Models/ShippingMethod.php +++ b/src/Domain/ShippingMethod/Models/ShippingMethod.php @@ -7,6 +7,7 @@ use App\Criteria\MetadataPrivateSearch; use App\Criteria\MetadataSearch; use App\Criteria\ShippingMethodItems; +use App\Criteria\WhereHasNameLike; use App\Criteria\WhereHasSalesChannel; use App\Criteria\WhereInIds; use App\Enums\ShippingType; @@ -85,6 +86,7 @@ final class ShippingMethod extends Model 'ids' => WhereInIds::class, 'items' => ShippingMethodItems::class, 'sales_channel_id' => WhereHasSalesChannel::class, + 'search' => WhereHasNameLike::class, ]; /** diff --git a/src/Domain/ShippingMethod/Services/ShippingMethodService.php b/src/Domain/ShippingMethod/Services/ShippingMethodService.php index 680f77da1..583c196d6 100644 --- a/src/Domain/ShippingMethod/Services/ShippingMethodService.php +++ b/src/Domain/ShippingMethod/Services/ShippingMethodService.php @@ -37,6 +37,7 @@ public function index(ShippingMethodIndexDto $dto, ?Money $cartValue): LengthAwa 'ids', 'items', 'sales_channel_id', + 'search', )->toArray(); $query = ShippingMethod::query() diff --git a/tests/Feature/PaymentMethodTest.php b/tests/Feature/PaymentMethodTest.php index 14e2940c9..adf4b6d45 100644 --- a/tests/Feature/PaymentMethodTest.php +++ b/tests/Feature/PaymentMethodTest.php @@ -60,6 +60,20 @@ public function testIndex($user): void { $this->{$user}->givePermissionTo('payment_methods.show'); + $response = $this->actingAs($this->{$user})->json('GET', '/payment-methods', ['search' => $this->payment_method->name]); + $response + ->assertOk() + ->assertJsonCount(1, 'data') + ->assertJsonFragment(['id' => $this->payment_method->getKey()]); + } + + /** + * @dataProvider authProvider + */ + public function testIndexSearch($user): void + { + $this->{$user}->givePermissionTo('payment_methods.show'); + $response = $this->actingAs($this->{$user})->getJson('/payment-methods'); $response ->assertOk() diff --git a/tests/Feature/PriceMap/PriceMapTest.php b/tests/Feature/PriceMap/PriceMapTest.php index f0ba1d5bf..4ec19954d 100644 --- a/tests/Feature/PriceMap/PriceMapTest.php +++ b/tests/Feature/PriceMap/PriceMapTest.php @@ -34,6 +34,18 @@ public function testIndex(string $user): void $response->assertOk()->assertJsonCount(1 + count(Currency::cases()), 'data'); } + /** + * @dataProvider authProvider + */ + public function testIndexSearch(string $user): void + { + $this->{$user}->givePermissionTo('price-maps.show'); + + $response = $this->actingAs($this->{$user})->json('GET', '/price-maps', ['search' => $this->priceMap->name]); + + $response->assertOk()->assertJsonCount(1, 'data'); + } + public function testCreateUnauthorized(): void { $this diff --git a/tests/Feature/ShippingMethodTest.php b/tests/Feature/ShippingMethodTest.php index 984ab7d74..b1a76035e 100644 --- a/tests/Feature/ShippingMethodTest.php +++ b/tests/Feature/ShippingMethodTest.php @@ -138,6 +138,26 @@ public function testIndex($user): void ]); } + /** + * @dataProvider authProvider + */ + public function testIndexSearch($user): void + { + $this->{$user}->givePermissionTo('shipping_methods.show'); + + $shippingMethod = ShippingMethod::factory()->create([ + 'public' => true, + 'name' => 'Searched value', + ]); + + $this->actingAs($this->{$user})->json('GET', '/shipping-methods', ['search' => 'Searched value']) + ->assertOk() + ->assertJsonCount(1, 'data') + ->assertJsonFragment([ + 'id' => $shippingMethod->getKey(), + ]); + } + /** * @dataProvider authProvider */ From 9dbea9757c0b103eb87474a41d22407a2074130f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Witold=20Wis=CC=81niewski?= Date: Mon, 28 Oct 2024 07:10:02 +0100 Subject: [PATCH 2/2] Fix tests --- tests/Feature/PaymentMethodTest.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/Feature/PaymentMethodTest.php b/tests/Feature/PaymentMethodTest.php index adf4b6d45..2eec1fbb5 100644 --- a/tests/Feature/PaymentMethodTest.php +++ b/tests/Feature/PaymentMethodTest.php @@ -60,6 +60,10 @@ public function testIndex($user): void { $this->{$user}->givePermissionTo('payment_methods.show'); + $this->payment_method->update([ + 'name' => 'Searched value', + ]); + $response = $this->actingAs($this->{$user})->json('GET', '/payment-methods', ['search' => $this->payment_method->name]); $response ->assertOk()