Skip to content

Commit

Permalink
Only update bulk statusses if there are open operations (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentBean authored Aug 20, 2024
1 parent 6596acc commit 4100f3b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/Actions/UpdateBulkStatuses.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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));
}
Expand Down
27 changes: 23 additions & 4 deletions tests/Actions/UpdateBulkStatusesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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::',
Expand All @@ -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::',
Expand All @@ -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::',
Expand All @@ -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);
}
}

0 comments on commit 4100f3b

Please sign in to comment.