Skip to content

Commit 35e0144

Browse files
authored
Merge pull request #26 from justbetter/feature/prevent-double-async-updates
Prevent async update if existing operation is not finished
2 parents d7d3cfc + f5f9d0d commit 35e0144

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

src/Actions/ProcessStocks.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Database\Eloquent\Builder;
66
use Illuminate\Foundation\Bus\PendingDispatch;
7+
use JustBetter\MagentoAsync\Enums\OperationStatus;
78
use JustBetter\MagentoClient\Client\Magento;
89
use JustBetter\MagentoStock\Contracts\ProcessesStocks;
910
use JustBetter\MagentoStock\Jobs\Retrieval\RetrieveStockJob;
@@ -39,11 +40,16 @@ public function process(): void
3940
->whereHas('product', function (Builder $query): void {
4041
$query->where('exists_in_magento', '=', true);
4142
})
43+
->whereDoesntHave('bulkOperations', function (Builder $query): void {
44+
$query
45+
->where('status', '=', OperationStatus::Open)
46+
->orWhereNull('status');
47+
})
4248
->select(['id', 'sku'])
4349
->take($repository->updateLimit())
4450
->get();
4551

46-
UpdateStockAsyncJob::dispatch($stocks);
52+
UpdateStockAsyncJob::dispatchIf($stocks->isNotEmpty(), $stocks);
4753
} else {
4854
Stock::query()
4955
->where('sync', '=', true)

tests/Actions/ProcessStocksTest.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
namespace JustBetter\MagentoStock\Tests\Actions;
44

55
use Illuminate\Support\Facades\Bus;
6+
use JustBetter\MagentoAsync\Enums\OperationStatus;
7+
use JustBetter\MagentoAsync\Models\BulkRequest;
68
use JustBetter\MagentoClient\Contracts\ChecksMagento;
9+
use JustBetter\MagentoProducts\Models\MagentoProduct;
710
use JustBetter\MagentoStock\Actions\ProcessStocks;
811
use JustBetter\MagentoStock\Jobs\Retrieval\RetrieveStockJob;
912
use JustBetter\MagentoStock\Jobs\Update\UpdateStockAsyncJob;
@@ -58,6 +61,11 @@ public function it_dispatches_async_update_job(): void
5861
Bus::fake();
5962
config()->set('magento-stock.async', true);
6063

64+
MagentoProduct::query()->create([
65+
'sku' => '::sku::',
66+
'exists_in_magento' => true,
67+
]);
68+
6169
Stock::query()->create([
6270
'sku' => '::sku::',
6371
'update' => true,
@@ -70,6 +78,65 @@ public function it_dispatches_async_update_job(): void
7078
Bus::assertDispatched(UpdateStockAsyncJob::class);
7179
}
7280

81+
#[Test]
82+
public function it_does_not_dispatch_stocks_with_open_async_operations(): void
83+
{
84+
Bus::fake();
85+
config()->set('magento-stock.async', true);
86+
87+
MagentoProduct::query()->create([
88+
'sku' => '::sku_1::',
89+
'exists_in_magento' => true,
90+
]);
91+
92+
MagentoProduct::query()->create([
93+
'sku' => '::sku_2::',
94+
'exists_in_magento' => true,
95+
]);
96+
97+
/** @var Stock $stock */
98+
$stock = Stock::query()->create([
99+
'sku' => '::sku_1::',
100+
'update' => true,
101+
]);
102+
103+
/** @var BulkRequest $request */
104+
$request = BulkRequest::query()->create([
105+
'magento_connection' => '::magento-connection::',
106+
'store_code' => '::store-code::',
107+
'method' => 'POST',
108+
'path' => '::path::',
109+
'bulk_uuid' => '::bulk-uuid-1::',
110+
'request' => [
111+
[
112+
'call-1',
113+
],
114+
],
115+
'response' => [],
116+
'created_at' => now(),
117+
]);
118+
119+
$request->operations()->create([
120+
'operation_id' => 0,
121+
'subject_type' => $stock->getMorphClass(),
122+
'subject_id' => $stock->getKey(),
123+
'status' => OperationStatus::Open,
124+
]);
125+
126+
Stock::query()->create([
127+
'sku' => '::sku_2::',
128+
'update' => true,
129+
]);
130+
131+
/** @var ProcessStocks $action */
132+
$action = app(ProcessStocks::class);
133+
$action->process();
134+
135+
Bus::assertDispatched(UpdateStockAsyncJob::class, function (UpdateStockAsyncJob $job): bool {
136+
return $job->stocks->count() === 1 && $job->stocks->first()?->sku === '::sku_2::';
137+
});
138+
}
139+
73140
#[Test]
74141
public function it_does_not_dispatch_update_jobs_if_magento_is_unavailable(): void
75142
{

0 commit comments

Comments
 (0)