Skip to content

Commit

Permalink
PBC-3241: TaxApp and OMS improvements to support 3rd-party providers.…
Browse files Browse the repository at this point in the history
… (#10704)

PBC-3241 `TaxApp` and `Oms` module improvements to support 3rd-party providers.
  • Loading branch information
bohdan-turchyk-spryker authored Dec 29, 2023
1 parent a8b5a34 commit 4571b83
Show file tree
Hide file tree
Showing 12 changed files with 232 additions and 2 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"spryker/mail": "^4.6.0",
"spryker/mail-extension": "^1.0.0",
"spryker/message-broker": "^1.2.1",
"spryker/oms-extension": "^1.3.0",
"spryker/oms-extension": "^1.4.0",
"spryker/propel": "^3.28.0",
"spryker/propel-orm": "^1.16.0",
"spryker/sales": "^5.0.0 || ^6.0.0 || ^7.0.0 || ^8.0.0 || ^10.0.0 || ^11.0.0",
Expand Down
6 changes: 6 additions & 0 deletions src/Spryker/Shared/Oms/Transfer/oms.transfer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -281,4 +281,10 @@
<property name="image" type="string"/>
</transfer>

<transfer name="OmsEventTriggered" strict="true">
<property name="idEvent" type="string"/>
<property name="orderItemIds" type="array" singular="idOrderItem"/>
<property name="idSalesOrder" type="int"/>
<property name="eventData" type="array" singular="eventData"/>
</transfer>
</transfers>
56 changes: 56 additions & 0 deletions src/Spryker/Zed/Oms/Business/Notifier/EventTriggeredNotifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

/**
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
*/

namespace Spryker\Zed\Oms\Business\Notifier;

use Generated\Shared\Transfer\OmsEventTriggeredTransfer;
use Orm\Zed\Sales\Persistence\SpySalesOrder;
use Orm\Zed\Sales\Persistence\SpySalesOrderItem;
use Spryker\Zed\Oms\Business\Util\ReadOnlyArrayObject;

class EventTriggeredNotifier implements EventTriggeredNotifierInterface
{
/**
* @var array<\Spryker\Zed\OmsExtension\Dependency\Plugin\OmsEventTriggeredListenerPluginInterface>
*/
protected array $omsEventTriggeredListenerPlugins;

/**
* @param array<\Spryker\Zed\OmsExtension\Dependency\Plugin\OmsEventTriggeredListenerPluginInterface> $omsEventTriggeredListenerPlugins
*/
public function __construct(array $omsEventTriggeredListenerPlugins)
{
$this->omsEventTriggeredListenerPlugins = $omsEventTriggeredListenerPlugins;
}

/**
* @param string $idEvent
* @param array<\Orm\Zed\Sales\Persistence\SpySalesOrderItem> $orderItems
* @param \Orm\Zed\Sales\Persistence\SpySalesOrder $orderEntity
* @param \Spryker\Zed\Oms\Business\Util\ReadOnlyArrayObject $data
*
* @return void
*/
public function notifyOmsEventTriggeredListeners(string $idEvent, array $orderItems, SpySalesOrder $orderEntity, ReadOnlyArrayObject $data): void
{
$orderItemIds = array_map(function (SpySalesOrderItem $orderItem) {
return $orderItem->getIdSalesOrderItem();
}, $orderItems);

$omsEventTriggeredTransfer = (new OmsEventTriggeredTransfer())
->setIdEvent($idEvent)
->setOrderItemIds($orderItemIds)
->setIdSalesOrder($orderEntity->getIdSalesOrder())
->setEventData($data->getArrayCopy());

foreach ($this->omsEventTriggeredListenerPlugins as $omsEventTriggeredListener) {
if ($omsEventTriggeredListener->isApplicable($omsEventTriggeredTransfer)) {
$omsEventTriggeredListener->onEventTriggered($omsEventTriggeredTransfer);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/**
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
*/

namespace Spryker\Zed\Oms\Business\Notifier;

use Orm\Zed\Sales\Persistence\SpySalesOrder;
use Spryker\Zed\Oms\Business\Util\ReadOnlyArrayObject;

interface EventTriggeredNotifierInterface
{
/**
* @param string $idEvent
* @param array<\Orm\Zed\Sales\Persistence\SpySalesOrderItem> $orderItems
* @param \Orm\Zed\Sales\Persistence\SpySalesOrder $orderEntity
* @param \Spryker\Zed\Oms\Business\Util\ReadOnlyArrayObject $data
*
* @return void
*/
public function notifyOmsEventTriggeredListeners(string $idEvent, array $orderItems, SpySalesOrder $orderEntity, ReadOnlyArrayObject $data);
}
19 changes: 19 additions & 0 deletions src/Spryker/Zed/Oms/Business/OmsBusinessFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
use Spryker\Zed\Oms\Business\Expander\StateHistoryExpanderInterface;
use Spryker\Zed\Oms\Business\Lock\TriggerLocker;
use Spryker\Zed\Oms\Business\Mail\MailHandler;
use Spryker\Zed\Oms\Business\Notifier\EventTriggeredNotifier;
use Spryker\Zed\Oms\Business\Notifier\EventTriggeredNotifierInterface;
use Spryker\Zed\Oms\Business\OrderStateMachine\Builder;
use Spryker\Zed\Oms\Business\OrderStateMachine\Finder;
use Spryker\Zed\Oms\Business\OrderStateMachine\LockedOrderStateMachine;
Expand Down Expand Up @@ -89,9 +91,18 @@ public function createOrderStateMachine(array $logContext = [])
$this->getProvidedDependency(OmsDependencyProvider::COMMAND_PLUGINS),
$this->createUtilReservation(),
$this->getConfig(),
$this->createEventTriggeredNotifier(),
);
}

/**
* @return \Spryker\Zed\Oms\Business\Notifier\EventTriggeredNotifierInterface
*/
public function createEventTriggeredNotifier(): EventTriggeredNotifierInterface
{
return new EventTriggeredNotifier($this->getOmsEventTriggeredListenerPlugins());
}

/**
* @param array $logContext
*
Expand Down Expand Up @@ -573,4 +584,12 @@ public function createOrderStatusChangedMessageSender(): OrderStatusChangedMessa
{
return new OrderStatusChangedMessageSender($this->getMessageBrokerFacade(), $this->getStoreFacade(), $this->getSalesFacade(), $this->getConfig(), $this->getQueryContainer());
}

/**
* @return array<\Spryker\Zed\OmsExtension\Dependency\Plugin\OmsEventTriggeredListenerPluginInterface>
*/
public function getOmsEventTriggeredListenerPlugins(): array
{
return $this->getProvidedDependency(OmsDependencyProvider::PLUGINS_OMS_EVENT_TRIGGERED_LISTENER);
}
}
6 changes: 6 additions & 0 deletions src/Spryker/Zed/Oms/Business/OmsFacadeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public function isOrderFlaggedExcludeFromCustomer($idOrder);
* - Calls condition plugins
* - Sets timeouts for timeout events
* - Triggers item reservation plugins
* - Notified listeners about event handling
* - Unlocks state machine trigger
* - Returns an array with data aggregated from the state machine plugins and an `OmsEventTriggerResponseTransfer` by the key `\Spryker\Zed\Oms\OmsConfig::OMS_EVENT_TRIGGER_RESPONSE`.
* - If command plugins execution ends without issues `OmsEventTriggerResponse.isSuccessful = true`.
Expand Down Expand Up @@ -113,6 +114,7 @@ public function triggerEventForOrderItems($eventId, array $orderItemIds, array $
* - Calls condition plugins
* - Sets timeouts for timeout events
* - Triggers item reservation plugins
* - Notified listeners about event handling
* - Unlocks state machine trigger
* - Returns an array with data aggregated from the state machine plugins and an `OmsEventTriggerResponseTransfer` by the key `\Spryker\Zed\Oms\OmsConfig::OMS_EVENT_TRIGGER_RESPONSE`.
* - If command plugins execution ends without issues `OmsEventTriggerResponse.isSuccessful = true`.
Expand All @@ -138,6 +140,7 @@ public function triggerEventForNewOrderItems(array $orderItemIds, array $data =
* - Calls condition plugins
* - Sets timeouts for timeout events
* - Triggers item reservation plugins
* - Notified listeners about event handling
* - Unlocks state machine trigger
* - Returns an array with data aggregated from the state machine plugins and an `OmsEventTriggerResponseTransfer` by the key `\Spryker\Zed\Oms\OmsConfig::OMS_EVENT_TRIGGER_RESPONSE`.
* - If command plugins execution ends without issues `OmsEventTriggerResponse.isSuccessful = true`.
Expand Down Expand Up @@ -370,6 +373,7 @@ public function getStateDisplayName(SpySalesOrderItem $orderItem);
* - Calls condition plugins
* - Sets timeouts for timeout events
* - Triggers item reservation plugins
* - Notified listeners about event handling
* - Unlocks state machine trigger
* - Returns an array with data aggregated from the state machine plugins and an `OmsEventTriggerResponseTransfer` by the key `\Spryker\Zed\Oms\OmsConfig::OMS_EVENT_TRIGGER_RESPONSE`.
* - If command plugins execution ends without issues `OmsEventTriggerResponse.isSuccessful = true`.
Expand Down Expand Up @@ -397,6 +401,7 @@ public function triggerEvent($eventId, ObjectCollection $orderItems, array $logC
* - Calls condition plugins
* - Sets timeouts for timeout events
* - Triggers item reservation plugins
* - Notified listeners about event handling
* - Unlocks state machine trigger
* - Returns an array with data aggregated from the state machine plugins and an `OmsEventTriggerResponseTransfer` by the key `\Spryker\Zed\Oms\OmsConfig::OMS_EVENT_TRIGGER_RESPONSE`.
* - If command plugins execution ends without issues `OmsEventTriggerResponse.isSuccessful = true`.
Expand All @@ -423,6 +428,7 @@ public function triggerEventForNewItem(ObjectCollection $orderItems, array $logC
* - Calls condition plugins
* - Sets timeouts for timeout events
* - Triggers item reservation plugins
* - Notified listeners about event handling
* - Unlocks state machine trigger
* - Returns an array with data aggregated from the state machine plugins and an `OmsEventTriggerResponseTransfer` by the key `\Spryker\Zed\Oms\OmsConfig::OMS_EVENT_TRIGGER_RESPONSE`.
* - If command plugins execution ends without issues `OmsEventTriggerResponse.isSuccessful = true`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Orm\Zed\Oms\Persistence\SpyOmsOrderItemStateQuery;
use Orm\Zed\Sales\Persistence\SpySalesOrderItem;
use Spryker\Shared\ErrorHandler\ErrorLogger;
use Spryker\Zed\Oms\Business\Notifier\EventTriggeredNotifierInterface;
use Spryker\Zed\Oms\Business\Process\ProcessInterface;
use Spryker\Zed\Oms\Business\Process\StateInterface;
use Spryker\Zed\Oms\Business\Util\ReadOnlyArrayObject;
Expand Down Expand Up @@ -125,6 +126,11 @@ class OrderStateMachine implements OrderStateMachineInterface
*/
protected $omsConfig;

/**
* @var \Spryker\Zed\Oms\Business\Notifier\EventTriggeredNotifierInterface
*/
protected EventTriggeredNotifierInterface $eventTriggeredNotifier;

/**
* @param \Spryker\Zed\Oms\Persistence\OmsQueryContainerInterface $queryContainer
* @param \Spryker\Zed\Oms\Business\OrderStateMachine\BuilderInterface $builder
Expand All @@ -135,6 +141,7 @@ class OrderStateMachine implements OrderStateMachineInterface
* @param \Spryker\Zed\Oms\Dependency\Plugin\Command\CommandCollectionInterface|array $commands
* @param \Spryker\Zed\Oms\Business\Util\ReservationInterface $reservation
* @param \Spryker\Zed\Oms\OmsConfig $omsConfig
* @param \Spryker\Zed\Oms\Business\Notifier\EventTriggeredNotifierInterface $eventTriggeredNotifier
*/
public function __construct(
OmsQueryContainerInterface $queryContainer,
Expand All @@ -145,7 +152,8 @@ public function __construct(
$conditions,
$commands,
ReservationInterface $reservation,
OmsConfig $omsConfig
OmsConfig $omsConfig,
EventTriggeredNotifierInterface $eventTriggeredNotifier
) {
$this->queryContainer = $queryContainer;
$this->builder = $builder;
Expand All @@ -156,6 +164,7 @@ public function __construct(
$this->setCommands($commands);
$this->reservation = $reservation;
$this->omsConfig = $omsConfig;
$this->eventTriggeredNotifier = $eventTriggeredNotifier;
}

/**
Expand Down Expand Up @@ -238,6 +247,15 @@ public function triggerEvent($eventId, array $orderItems, $data)
}
$sourceStateBuffer = $this->updateStateByEvent($eventId, $processedOrderItems, $sourceStateBuffer, $log);
$this->saveOrderItems($processedOrderItems, $log, $processes, $sourceStateBuffer);

$currentOrderItemEntity = current($processedOrderItems);

if ($currentOrderItemEntity) {
$orderEntity = $currentOrderItemEntity->getOrder();

$this->eventTriggeredNotifier->notifyOmsEventTriggeredListeners($eventId, $processedOrderItems, $orderEntity, $data);
}

$allProcessedOrderItems = array_merge($allProcessedOrderItems, $processedOrderItems);
}

Expand Down
28 changes: 28 additions & 0 deletions src/Spryker/Zed/Oms/OmsDependencyProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ class OmsDependencyProvider extends AbstractBundleDependencyProvider
*/
public const PLUGINS_TIMEOUT_PROCESSOR = 'PLUGINS_TIMEOUT_PROCESSOR';

/**
* @var string
*/
public const PLUGINS_OMS_EVENT_TRIGGERED_LISTENER = 'PLUGINS_OMS_EVENT_TRIGGERED_LISTENER';

/**
* @var string
*/
Expand Down Expand Up @@ -165,6 +170,7 @@ public function provideBusinessLayerDependencies(Container $container)
$container = $this->addOmsReservationWriterStrategyPlugins($container);
$container = $this->addReservationPostSaveTerminationAwareStrategyPlugins($container);
$container = $this->addTimeoutProcessorPlugins($container);
$container = $this->addOmsEventTriggeredListenerPlugins($container);
$container = $this->addMessageBrokerFacade($container);

return $container;
Expand Down Expand Up @@ -603,6 +609,28 @@ protected function getTimeoutProcessorPlugins(): array
return [];
}

/**
* @param \Spryker\Zed\Kernel\Container $container
*
* @return \Spryker\Zed\Kernel\Container
*/
protected function addOmsEventTriggeredListenerPlugins(Container $container): Container
{
$container->set(static::PLUGINS_OMS_EVENT_TRIGGERED_LISTENER, function () {
return $this->getOmsEventTriggeredListenerPlugins();
});

return $container;
}

/**
* @return array<\Spryker\Zed\OmsExtension\Dependency\Plugin\OmsEventTriggeredListenerPluginInterface>
*/
protected function getOmsEventTriggeredListenerPlugins(): array
{
return [];
}

/**
* @param \Spryker\Zed\Kernel\Container $container
*
Expand Down
27 changes: 27 additions & 0 deletions tests/SprykerTest/Zed/Oms/Business/OmsFacadeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,33 @@ public function testIsOrderFlaggedExcludeFromCustomerShouldReturnTrueWhenAllStat
$this->assertTrue($isOrderExcluded);
}

/**
* @group new
*
* @return void
*/
public function testEventTriggeredListenersAreNotifiedWhenEventIsTriggered(): void
{
$testStateMachineProcessName = 'Test01';

$omsEventTriggeredListener = $this->tester->setupEventTriggeredListenerPluginDependency();

$omsFacade = $this->createOmsFacadeWithTestStateMachine([$testStateMachineProcessName]);

$saveOrderTransfer = $this->tester->haveOrder([
'unitPrice' => 100,
'sumPrice' => 100,
], $testStateMachineProcessName);

$idSalesOrder = $saveOrderTransfer->getIdSalesOrder();

$salesOrderEntity = SpySalesOrderQuery::create()->filterByIdSalesOrder($idSalesOrder)->findOne();

$omsFacade->triggerEvent('authorize', $salesOrderEntity->getItems(), []);

$this->assertTrue($omsEventTriggeredListener->wasTriggered);
}

/**
* @return void
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Orm\Zed\Sales\Persistence\SpySalesOrder as ChildSpySalesOrder;
use Orm\Zed\Sales\Persistence\SpySalesOrderItem;
use ReflectionClass;
use Spryker\Zed\Oms\Business\Notifier\EventTriggeredNotifier;
use Spryker\Zed\Oms\Business\OmsBusinessFactory;
use Spryker\Zed\Oms\Business\OrderStateMachine\BuilderInterface;
use Spryker\Zed\Oms\Business\OrderStateMachine\OrderStateMachine;
Expand Down Expand Up @@ -82,6 +83,7 @@ public function testInstantiationConditionsArrayShouldConvertedToCollection(): v
[],
$this->getReservationMock(),
new OmsConfig(),
new EventTriggeredNotifier([]),
);
$reflection = new ReflectionClass(OrderStateMachine::class);
$reflectionProperty = $reflection->getProperty('conditions');
Expand Down Expand Up @@ -110,6 +112,7 @@ public function testInstantiationWithConditionCollection(): void
[],
$this->getReservationMock(),
new OmsConfig(),
new EventTriggeredNotifier([]),
);
$reflection = new ReflectionClass(OrderStateMachine::class);
$reflectionProperty = $reflection->getProperty('conditions');
Expand All @@ -135,6 +138,7 @@ public function testInstantiationCommandsArrayShouldConvertedToCollection(): voi
[static::COMMAND_NAME => $this->getCommandMock()],
$this->getReservationMock(),
new OmsConfig(),
new EventTriggeredNotifier([]),
);
$reflection = new ReflectionClass(OrderStateMachine::class);
$reflectionProperty = $reflection->getProperty('commands');
Expand Down Expand Up @@ -163,6 +167,7 @@ public function testInstantiationWithCommandCollection(): void
$commandCollection,
$this->getReservationMock(),
new OmsConfig(),
new EventTriggeredNotifier([]),
);
$reflection = new ReflectionClass(OrderStateMachine::class);
$reflectionProperty = $reflection->getProperty('commands');
Expand Down Expand Up @@ -273,6 +278,7 @@ protected function getOrderStatemachineMockForConditionsWithCriteriaTest(array $
[],
$omsBusinessFactory->createUtilReservation(),
$omsConfigMock,
$omsBusinessFactory->createEventTriggeredNotifier(),
])
->onlyMethods($methods);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ protected function createOrderStateMachineMock(array $methods): OrderStateMachin
$omsBusinessFactory->getProvidedDependency(OmsDependencyProvider::COMMAND_PLUGINS),
$omsBusinessFactory->createUtilReservation(),
$omsConfigMock,
$omsBusinessFactory->createEventTriggeredNotifier(),
])
->onlyMethods($methods)
->getMock();
Expand Down
Loading

0 comments on commit 4571b83

Please sign in to comment.