@@ -98,6 +98,7 @@ public function it_does_not_dispatch_stocks_with_open_async_operations(): void
9898 $ stock = Stock::query ()->create ([
9999 'sku ' => '::sku_1:: ' ,
100100 'update ' => true ,
101+ 'created_at ' => now ()->subMinutes (10 ),
101102 ]);
102103
103104 /** @var BulkRequest $request */
@@ -137,6 +138,57 @@ public function it_does_not_dispatch_stocks_with_open_async_operations(): void
137138 });
138139 }
139140
141+ #[Test]
142+ public function it_dispatches_stocks_with_stale_async_operations (): void
143+ {
144+ Bus::fake ();
145+ config ()->set ('magento-stock.async ' , true );
146+ config ()->set ('magento-stock.async_stale_hours ' , 24 );
147+
148+ MagentoProduct::query ()->create ([
149+ 'sku ' => '::sku:: ' ,
150+ 'exists_in_magento ' => true ,
151+ ]);
152+
153+ /** @var Stock $stock */
154+ $ stock = Stock::query ()->create ([
155+ 'sku ' => '::sku:: ' ,
156+ 'update ' => true ,
157+ ]);
158+
159+ /** @var BulkRequest $request */
160+ $ request = BulkRequest::query ()->create ([
161+ 'magento_connection ' => '::magento-connection:: ' ,
162+ 'store_code ' => '::store-code:: ' ,
163+ 'method ' => 'POST ' ,
164+ 'path ' => '::path:: ' ,
165+ 'bulk_uuid ' => '::bulk-uuid:: ' ,
166+ 'request ' => [
167+ [
168+ 'call-1 ' ,
169+ ],
170+ ],
171+ 'response ' => [],
172+ 'created_at ' => now ()->subHours (25 ),
173+ ]);
174+
175+ $ request ->operations ()->create ([
176+ 'operation_id ' => 0 ,
177+ 'subject_type ' => $ stock ->getMorphClass (),
178+ 'subject_id ' => $ stock ->getKey (),
179+ 'status ' => OperationStatus::Open,
180+ 'created_at ' => now ()->subHours (25 ),
181+ ]);
182+
183+ /** @var ProcessStocks $action */
184+ $ action = app (ProcessStocks::class);
185+ $ action ->process ();
186+
187+ Bus::assertDispatched (UpdateStockAsyncJob::class, function (UpdateStockAsyncJob $ job ): bool {
188+ return $ job ->stocks ->count () === 1 && $ job ->stocks ->first ()?->sku === '::sku:: ' ;
189+ });
190+ }
191+
140192 #[Test]
141193 public function it_does_not_dispatch_update_jobs_if_magento_is_unavailable (): void
142194 {
0 commit comments