Skip to content

Commit 7f5b802

Browse files
authored
Fix webhook causing some checkouts to fail (#464)
1 parent dc9b7b7 commit 7f5b802

File tree

3 files changed

+42
-17
lines changed

3 files changed

+42
-17
lines changed

backend/app/Repository/Eloquent/BaseRepository.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ public function setMaxPerPage(int $maxPerPage): static
6262

6363
public function all(array $columns = self::DEFAULT_COLUMNS): Collection
6464
{
65-
return $this->handleResults($this->model->all($columns));
65+
$models = $this->model->all($columns);
66+
$this->resetModel();
67+
68+
return $this->handleResults($models);
6669
}
6770

6871
public function paginate(
@@ -121,7 +124,10 @@ public function paginateEloquentRelation(
121124
*/
122125
public function findById(int $id, array $columns = self::DEFAULT_COLUMNS): DomainObjectInterface
123126
{
124-
return $this->handleSingleResult($this->model->findOrFail($id, $columns));
127+
$model = $this->model->findOrFail($id, $columns);
128+
$this->resetModel();
129+
130+
return $this->handleSingleResult($model);
125131
}
126132

127133
public function findFirstByField(
@@ -138,7 +144,10 @@ public function findFirstByField(
138144

139145
public function findFirst(int $id, array $columns = self::DEFAULT_COLUMNS): ?DomainObjectInterface
140146
{
141-
return $this->handleSingleResult($this->model->findOrFail($id, $columns));
147+
$model = $this->model->findOrFail($id, $columns);
148+
$this->resetModel();
149+
150+
return $this->handleSingleResult($model);
142151
}
143152

144153
public function findWhere(

backend/app/Services/Application/Handlers/Order/CompleteOrderHandler.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function __construct(
6262
*/
6363
public function handle(string $orderShortId, CompleteOrderDTO $orderData): OrderDomainObject
6464
{
65-
return DB::transaction(function () use ($orderData, $orderShortId) {
65+
$updatedOrder = DB::transaction(function () use ($orderData, $orderShortId) {
6666
$orderDTO = $orderData->order;
6767

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

88-
OrderStatusChangedEvent::dispatch($updatedOrder);
89-
90-
if ($updatedOrder->isOrderCompleted()) {
91-
$this->domainEventDispatcherService->dispatch(
92-
new OrderEvent(
93-
type: DomainEventType::ORDER_CREATED,
94-
orderId: $updatedOrder->getId(),
95-
)
96-
);
97-
}
98-
9988
return $updatedOrder;
10089
});
90+
91+
OrderStatusChangedEvent::dispatch($updatedOrder);
92+
93+
if ($updatedOrder->isOrderCompleted()) {
94+
$this->domainEventDispatcherService->dispatch(
95+
new OrderEvent(
96+
type: DomainEventType::ORDER_CREATED,
97+
orderId: $updatedOrder->getId(),
98+
)
99+
);
100+
}
101+
102+
return $updatedOrder;
101103
}
102104

103105
/**

backend/app/Services/Infrastructure/DomainEvents/DomainEventDispatcherService.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,29 @@
44

55
use HiEvents\Services\Infrastructure\DomainEvents\Events\BaseDomainEvent;
66
use Illuminate\Events\Dispatcher as EventDispatcher;
7+
use Psr\Log\LoggerInterface;
8+
use Throwable;
79

810
class DomainEventDispatcherService
911
{
10-
public function __construct(private readonly EventDispatcher $dispatcher)
12+
public function __construct(
13+
private readonly EventDispatcher $dispatcher,
14+
private readonly LoggerInterface $logger,
15+
)
1116
{
1217
}
1318

19+
/**
20+
* @throws Throwable
21+
*/
1422
public function dispatch(BaseDomainEvent $event): void
1523
{
16-
$this->dispatcher->dispatch($event);
24+
try {
25+
$this->dispatcher->dispatch($event);
26+
} catch (Throwable $e) {
27+
$this->logger->error('Failed to dispatch domain event', ['event' => $event, 'exception' => $e]);
28+
29+
throw $e;
30+
}
1731
}
1832
}

0 commit comments

Comments
 (0)