Skip to content

Commit

Permalink
Change the stock adjustment webhook handler from async to sync to mak…
Browse files Browse the repository at this point in the history
…e it easier to show errors in Peak WMS interface when delivering webhooks
  • Loading branch information
loevgaard committed Dec 4, 2024
1 parent 1e28702 commit ad84bb1
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 8 deletions.
3 changes: 1 addition & 2 deletions src/Controller/HandleWebhookAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,9 @@ public function __invoke(Request $request): JsonResponse

try {
$webhook = $this->webhookFactory->createFromRequest($request);
$logger = new WebhookLogger($webhook);

if ($this->webhookHandler instanceof LoggerAwareInterface) {
$this->webhookHandler->setLogger($logger);
$this->webhookHandler->setLogger(new WebhookLogger($webhook));
}

$dataClass = WebhookParser::convertNameToDataClass($request->query->getInt('name'));
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/config/services/webhook_handler.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

<service id="Setono\SyliusPeakPlugin\WebhookHandler\StockAdjustmentWebhookHandler">
<argument type="service" id="Setono\SyliusPeakPlugin\Provider\ProductVariantProviderInterface"/>
<argument type="service" id="setono_sylius_peak.command_bus"/>
<argument type="service" id="Setono\SyliusPeakPlugin\Updater\InventoryUpdaterInterface"/>

<tag name="setono_sylius_peak.webhook_handler"/>
</service>
Expand Down
9 changes: 4 additions & 5 deletions src/WebhookHandler/StockAdjustmentWebhookHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,16 @@
use Psr\Log\NullLogger;
use Setono\PeakWMS\DataTransferObject\Webhook\WebhookDataStockAdjust;
use Setono\SyliusPeakPlugin\Exception\UnsupportedWebhookException;
use Setono\SyliusPeakPlugin\Message\Command\UpdateInventory;
use Setono\SyliusPeakPlugin\Provider\ProductVariantProviderInterface;
use Symfony\Component\Messenger\MessageBusInterface;
use Setono\SyliusPeakPlugin\Updater\InventoryUpdaterInterface;

final class StockAdjustmentWebhookHandler implements WebhookHandlerInterface, LoggerAwareInterface
{
private LoggerInterface $logger;

public function __construct(
private readonly ProductVariantProviderInterface $productVariantProvider,
private readonly MessageBusInterface $commandBus,
private readonly InventoryUpdaterInterface $inventoryUpdater,
) {
$this->logger = new NullLogger();
}
Expand All @@ -35,9 +34,9 @@ public function handle(object $data): void
throw new \InvalidArgumentException(sprintf('Product variant with id/code "%s" not found', (string) $data->variantId));
}

$this->logger->debug(sprintf('Dispatching a message onto the message to update the inventory for product variant %s', (string) $productVariant->getCode()));
$this->inventoryUpdater->update($productVariant);

$this->commandBus->dispatch(UpdateInventory::for($productVariant));
$this->logger->debug(sprintf('Updated inventory for product variant %s', (string) $productVariant->getCode()));
}

/**
Expand Down

0 comments on commit ad84bb1

Please sign in to comment.