Skip to content

Commit

Permalink
Prevent async update if existing operation is not finished (#30)
Browse files Browse the repository at this point in the history
* Prevent async update if existing operation is not finished

* SCA
  • Loading branch information
VincentBean authored Feb 3, 2025
1 parent 83d20b7 commit d2e212e
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/Actions/ProcessPrices.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Foundation\Bus\PendingDispatch;
use JustBetter\MagentoAsync\Enums\OperationStatus;
use JustBetter\MagentoClient\Client\Magento;
use JustBetter\MagentoPrices\Contracts\ProcessesPrices;
use JustBetter\MagentoPrices\Jobs\Retrieval\RetrievePriceJob;
Expand Down Expand Up @@ -39,11 +40,16 @@ public function process(): void
->whereHas('product', function (Builder $query): void {
$query->where('exists_in_magento', '=', true);
})
->whereDoesntHave('bulkOperations', function (Builder $query): void {
$query
->where('status', '=', OperationStatus::Open)
->orWhereNull('status');
})
->select(['id', 'sku'])
->take($repository->updateLimit())
->get();

UpdatePricesAsyncJob::dispatch($prices);
UpdatePricesAsyncJob::dispatchIf($prices->isNotEmpty(), $prices);
} else {
Price::query()
->where('sync', '=', true)
Expand Down
71 changes: 70 additions & 1 deletion tests/Actions/ProcessPricesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
namespace JustBetter\MagentoPrices\Tests\Actions;

use Illuminate\Support\Facades\Bus;
use JustBetter\MagentoAsync\Enums\OperationStatus;
use JustBetter\MagentoAsync\Models\BulkRequest;
use JustBetter\MagentoClient\Contracts\ChecksMagento;
use JustBetter\MagentoPrices\Actions\ProcessPrices;
use JustBetter\MagentoPrices\Jobs\Retrieval\RetrievePriceJob;
use JustBetter\MagentoPrices\Jobs\Update\UpdatePriceJob;
use JustBetter\MagentoPrices\Jobs\Update\UpdatePricesAsyncJob;
use JustBetter\MagentoPrices\Models\Price;
use JustBetter\MagentoPrices\Tests\TestCase;
use JustBetter\MagentoProducts\Models\MagentoProduct;
use Mockery\MockInterface;
use PHPUnit\Framework\Attributes\Test;

Expand Down Expand Up @@ -57,6 +60,11 @@ public function it_dispatches_async_update_job(): void
Bus::fake();
config()->set('magento-prices.async', true);

MagentoProduct::query()->create([
'sku' => '::sku::',
'exists_in_magento' => true,
]);

Price::query()->create([
'sku' => '::sku::',
'update' => true,
Expand All @@ -66,7 +74,68 @@ public function it_dispatches_async_update_job(): void
$action = app(ProcessPrices::class);
$action->process();

Bus::assertDispatched(UpdatePricesAsyncJob::class);
Bus::assertDispatched(UpdatePricesAsyncJob::class, function (UpdatePricesAsyncJob $job): bool {
return $job->prices->count() === 1;
});
}

#[Test]
public function it_does_not_dispatch_prices_with_open_async_operations(): void
{
Bus::fake();
config()->set('magento-prices.async', true);

MagentoProduct::query()->create([
'sku' => '::sku_1::',
'exists_in_magento' => true,
]);

MagentoProduct::query()->create([
'sku' => '::sku_2::',
'exists_in_magento' => true,
]);

/** @var Price $price */
$price = Price::query()->create([
'sku' => '::sku_1::',
'update' => true,
]);

/** @var BulkRequest $request */
$request = BulkRequest::query()->create([
'magento_connection' => '::magento-connection::',
'store_code' => '::store-code::',
'method' => 'POST',
'path' => '::path::',
'bulk_uuid' => '::bulk-uuid-1::',
'request' => [
[
'call-1',
],
],
'response' => [],
'created_at' => now(),
]);

$request->operations()->create([
'operation_id' => 0,
'subject_type' => $price->getMorphClass(),
'subject_id' => $price->getKey(),
'status' => OperationStatus::Open,
]);

Price::query()->create([
'sku' => '::sku_2::',
'update' => true,
]);

/** @var ProcessPrices $action */
$action = app(ProcessPrices::class);
$action->process();

Bus::assertDispatched(UpdatePricesAsyncJob::class, function (UpdatePricesAsyncJob $job): bool {
return $job->prices->count() === 1 && $job->prices->first()?->sku === '::sku_2::';
});
}

#[Test]
Expand Down

0 comments on commit d2e212e

Please sign in to comment.