refactor(checkout): materialize purchase item snapshots at start

This commit is contained in:
2026-08-21 11:03:39 -03:00
parent 3a2dc8b7e0
commit 4e7e42d16e
9 changed files with 74 additions and 325 deletions

View File

@@ -26,6 +26,7 @@ class StartCheckoutService
private readonly CatalogSelectionResolver $selections,
private readonly InsufficientStockMessageBuilder $stockMessages,
private readonly PurchaseResponseLoader $responses,
private readonly PurchaseItemSnapshotFactory $snapshots,
) {}
/** @param array<string, mixed> $purchaseData */
@@ -193,6 +194,10 @@ class StartCheckoutService
$cart->getKey(),
);
$cartItems = $cart->items()->orderBy('id')->lockForUpdate()->get();
$this->loadCartItems($cartItems);
$purchase->items()->createMany($this->snapshots->fromCartItems($cartItems));
foreach ($cartItems as $index => $cartItem) {
$this->reservations->attachToPurchase(
$cartItem,
@@ -259,6 +264,8 @@ class StartCheckoutService
$cart->getTotalAmount(),
$cart->getKey(),
);
$purchase->items()->createMany($this->snapshots->fromCartItems($cartItems));
foreach ($cartItems as $cartItem) {
$this->reservations->attachToPurchase(
$cartItem,