Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix webhook causing some checkouts to fail #464

Merged
merged 1 commit into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions backend/app/Repository/Eloquent/BaseRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ public function setMaxPerPage(int $maxPerPage): static

public function all(array $columns = self::DEFAULT_COLUMNS): Collection
{
return $this->handleResults($this->model->all($columns));
$models = $this->model->all($columns);
$this->resetModel();

return $this->handleResults($models);
}

public function paginate(
Expand Down Expand Up @@ -121,7 +124,10 @@ public function paginateEloquentRelation(
*/
public function findById(int $id, array $columns = self::DEFAULT_COLUMNS): DomainObjectInterface
{
return $this->handleSingleResult($this->model->findOrFail($id, $columns));
$model = $this->model->findOrFail($id, $columns);
$this->resetModel();

return $this->handleSingleResult($model);
}

public function findFirstByField(
Expand All @@ -138,7 +144,10 @@ public function findFirstByField(

public function findFirst(int $id, array $columns = self::DEFAULT_COLUMNS): ?DomainObjectInterface
{
return $this->handleSingleResult($this->model->findOrFail($id, $columns));
$model = $this->model->findOrFail($id, $columns);
$this->resetModel();

return $this->handleSingleResult($model);
}

public function findWhere(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function __construct(
*/
public function handle(string $orderShortId, CompleteOrderDTO $orderData): OrderDomainObject
{
return DB::transaction(function () use ($orderData, $orderShortId) {
$updatedOrder = DB::transaction(function () use ($orderData, $orderShortId) {
$orderDTO = $orderData->order;

$order = $this->getOrder($orderShortId);
Expand All @@ -85,19 +85,21 @@ public function handle(string $orderShortId, CompleteOrderDTO $orderData): Order
$this->productQuantityUpdateService->updateQuantitiesFromOrder($updatedOrder);
}

OrderStatusChangedEvent::dispatch($updatedOrder);

if ($updatedOrder->isOrderCompleted()) {
$this->domainEventDispatcherService->dispatch(
new OrderEvent(
type: DomainEventType::ORDER_CREATED,
orderId: $updatedOrder->getId(),
)
);
}

return $updatedOrder;
});

OrderStatusChangedEvent::dispatch($updatedOrder);

if ($updatedOrder->isOrderCompleted()) {
$this->domainEventDispatcherService->dispatch(
new OrderEvent(
type: DomainEventType::ORDER_CREATED,
orderId: $updatedOrder->getId(),
)
);
}

return $updatedOrder;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,29 @@

use HiEvents\Services\Infrastructure\DomainEvents\Events\BaseDomainEvent;
use Illuminate\Events\Dispatcher as EventDispatcher;
use Psr\Log\LoggerInterface;
use Throwable;

class DomainEventDispatcherService
{
public function __construct(private readonly EventDispatcher $dispatcher)
public function __construct(
private readonly EventDispatcher $dispatcher,
private readonly LoggerInterface $logger,
)
{
}

/**
* @throws Throwable
*/
public function dispatch(BaseDomainEvent $event): void
{
$this->dispatcher->dispatch($event);
try {
$this->dispatcher->dispatch($event);
} catch (Throwable $e) {
$this->logger->error('Failed to dispatch domain event', ['event' => $event, 'exception' => $e]);

throw $e;
}
}
}
Loading