Skip to content

Commit

Permalink
Merge pull request #12 from justbetter/feature/adjust-product-creation
Browse files Browse the repository at this point in the history
Adjust product creation
  • Loading branch information
VincentBean authored Jul 24, 2024
2 parents 3776cac + 5b32100 commit 76878c9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Actions/CheckKnownProducts.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function handle(array $skus = []): void
{
$productChunks = MagentoProduct::query()
->where('exists_in_magento', false)
->when(count($skus), fn (Builder $query): Builder => $query->whereIn('sku', $skus)) /** @phpstan-ignore-line */
->when(count($skus), fn (Builder $query): Builder => $query->whereIn('sku', $skus))
->get()
->chunk(static::CHUNK_SIZE);

Expand Down
14 changes: 9 additions & 5 deletions src/Actions/ProcessMagentoSkus.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,17 @@ public function process(Enumerable $skus): void

$knownProductsThatDontExistQuery->update(['exists_in_magento' => true, 'updated_at' => now()]);

$missingProducts = $skus->diff($knownProductsThatDontExist->pluck('sku'))
->map(fn (string $sku) => ['sku' => $sku, 'exists_in_magento' => true, 'created_at' => now(), 'updated_at' => now()]);
$missingProducts = $skus->diff($knownProductsThatDontExist->pluck('sku'));

MagentoProduct::query()
->insert($missingProducts->toArray());
$missingProducts
->each(fn (string $sku) => MagentoProduct::query()->updateOrCreate(
['sku' => $sku],
['exists_in_magento' => true]
));

$knownProductsThatDontExist->pluck('sku')->merge($missingProducts->pluck('sku'))
$knownProductsThatDontExist
->pluck('sku')
->merge($missingProducts)
->each(fn (string $sku) => event(new ProductCreatedInMagentoEvent($sku)));
}

Expand Down

0 comments on commit 76878c9

Please sign in to comment.