From dff7309ef7fcba6b8b8951425afbe5f530e171c8 Mon Sep 17 00:00:00 2001 From: Vincent Boon Date: Tue, 20 Aug 2024 15:35:05 +0200 Subject: [PATCH 1/3] Only update bulk statusses if there are open operations --- src/Actions/UpdateBulkStatuses.php | 5 +++++ tests/Actions/UpdateBulkStatusesTest.php | 27 ++++++++++++++++++++---- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/Actions/UpdateBulkStatuses.php b/src/Actions/UpdateBulkStatuses.php index c805f06..4d14790 100644 --- a/src/Actions/UpdateBulkStatuses.php +++ b/src/Actions/UpdateBulkStatuses.php @@ -2,8 +2,10 @@ namespace JustBetter\MagentoAsync\Actions; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Foundation\Bus\PendingDispatch; use JustBetter\MagentoAsync\Contracts\UpdatesBulkStatuses; +use JustBetter\MagentoAsync\Enums\OperationStatus; use JustBetter\MagentoAsync\Jobs\UpdateBulkStatusJob; use JustBetter\MagentoAsync\Models\BulkRequest; @@ -12,6 +14,9 @@ class UpdateBulkStatuses implements UpdatesBulkStatuses public function update(): void { BulkRequest::query() + ->whereHas('operations', function (Builder $query): void { + $query->where('status', '=', OperationStatus::Open); + }) ->get() ->each(fn (BulkRequest $bulkRequest): PendingDispatch => UpdateBulkStatusJob::dispatch($bulkRequest)); } diff --git a/tests/Actions/UpdateBulkStatusesTest.php b/tests/Actions/UpdateBulkStatusesTest.php index 5ef1ad0..d1af8b9 100644 --- a/tests/Actions/UpdateBulkStatusesTest.php +++ b/tests/Actions/UpdateBulkStatusesTest.php @@ -4,6 +4,7 @@ use Illuminate\Support\Facades\Bus; use JustBetter\MagentoAsync\Actions\UpdateBulkStatuses; +use JustBetter\MagentoAsync\Enums\OperationStatus; use JustBetter\MagentoAsync\Jobs\UpdateBulkStatusJob; use JustBetter\MagentoAsync\Models\BulkRequest; use JustBetter\MagentoAsync\Tests\TestCase; @@ -16,7 +17,8 @@ public function it_can_update_bulk_statuses(): void { Bus::fake(); - BulkRequest::query()->create([ + /** @var BulkRequest $status1 */ + $status1 = BulkRequest::query()->create([ 'magento_connection' => '::magento-connection::', 'store_code' => '::store-code::', 'path' => '::path::', @@ -25,7 +27,8 @@ public function it_can_update_bulk_statuses(): void 'response' => [], ]); - BulkRequest::query()->create([ + /** @var BulkRequest $status2 */ + $status2 = BulkRequest::query()->create([ 'magento_connection' => '::magento-connection::', 'store_code' => '::store-code::', 'path' => '::path::', @@ -34,7 +37,8 @@ public function it_can_update_bulk_statuses(): void 'response' => [], ]); - BulkRequest::query()->create([ + /** @var BulkRequest $status3 */ + $status3 = BulkRequest::query()->create([ 'magento_connection' => '::magento-connection::', 'store_code' => '::store-code::', 'path' => '::path::', @@ -43,10 +47,25 @@ public function it_can_update_bulk_statuses(): void 'response' => [], ]); + $status1->operations()->create([ + 'operation_id' => 1, + 'status' => OperationStatus::Open, + ]); + + $status2->operations()->create([ + 'operation_id' => 1, + 'status' => OperationStatus::Open, + ]); + + $status3->operations()->create([ + 'operation_id' => 1, + 'status' => OperationStatus::Complete, + ]); + /** @var UpdateBulkStatuses $action */ $action = app(UpdateBulkStatuses::class); $action->update(); - Bus::assertDispatched(UpdateBulkStatusJob::class, 3); + Bus::assertDispatched(UpdateBulkStatusJob::class, 2); } } From ff5208220ef118285e3f3675ac32cff776b0b7c3 Mon Sep 17 00:00:00 2001 From: Vincent Boon Date: Tue, 27 Aug 2024 09:38:18 +0200 Subject: [PATCH 2/3] Allow null --- src/Actions/UpdateBulkStatuses.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Actions/UpdateBulkStatuses.php b/src/Actions/UpdateBulkStatuses.php index 4d14790..2d25cee 100644 --- a/src/Actions/UpdateBulkStatuses.php +++ b/src/Actions/UpdateBulkStatuses.php @@ -15,7 +15,9 @@ public function update(): void { BulkRequest::query() ->whereHas('operations', function (Builder $query): void { - $query->where('status', '=', OperationStatus::Open); + $query + ->where('status', '=', OperationStatus::Open) + ->orWhereNull('status'); }) ->get() ->each(fn (BulkRequest $bulkRequest): PendingDispatch => UpdateBulkStatusJob::dispatch($bulkRequest)); From 77da69fe2ce8caab022f9b0cf9a721e483354700 Mon Sep 17 00:00:00 2001 From: Vincent Boon Date: Tue, 27 Aug 2024 09:39:17 +0200 Subject: [PATCH 3/3] Adjust test --- tests/Actions/UpdateBulkStatusesTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Actions/UpdateBulkStatusesTest.php b/tests/Actions/UpdateBulkStatusesTest.php index d1af8b9..933f9ed 100644 --- a/tests/Actions/UpdateBulkStatusesTest.php +++ b/tests/Actions/UpdateBulkStatusesTest.php @@ -54,7 +54,7 @@ public function it_can_update_bulk_statuses(): void $status2->operations()->create([ 'operation_id' => 1, - 'status' => OperationStatus::Open, + 'status' => null, ]); $status3->operations()->create([