all the refactor
This commit is contained in:
@@ -181,36 +181,12 @@ func (s *CheckoutPoolServer) KlarnaPushHandler(w http.ResponseWriter, r *http.Re
|
||||
return
|
||||
}
|
||||
|
||||
if s.inventoryService != nil {
|
||||
inventoryRequests := getInventoryRequests(grain.CartState.Items)
|
||||
invStatus := "success"
|
||||
if err = s.inventoryService.ReserveInventory(r.Context(), inventoryRequests...); err != nil {
|
||||
// The payment is already settled at Klarna (checkout_complete), so the
|
||||
// order MUST be created. Inventory is best-effort at this point — a
|
||||
// reservation failure (unstocked / drop-ship / non-tracked items)
|
||||
// must not block order creation. Log it and flag the grain so
|
||||
// fulfillment can reconcile.
|
||||
logger.WarnContext(r.Context(), "klarna push: inventory reservation failed; creating order anyway",
|
||||
"err", err, "checkoutId", grain.Id.String())
|
||||
invStatus = "failed"
|
||||
} else {
|
||||
// Commit step: successfully decremented stock permanently, now release the temporary cart reservations
|
||||
if s.reservationService != nil {
|
||||
for _, item := range grain.CartState.Items {
|
||||
if item == nil || !shouldTrackInventory(item) {
|
||||
continue
|
||||
}
|
||||
if err := s.reservationService.ReleaseForCart(r.Context(), inventory.SKU(item.Sku), getLocationId(item), inventory.CartID(grain.CartId.String())); err != nil {
|
||||
logger.WarnContext(r.Context(), "klarna push: failed to release cart reservation", "sku", item.Sku, "err", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
s.Apply(r.Context(), uint64(grain.Id), &messages.InventoryReserved{
|
||||
Id: grain.Id.String(),
|
||||
Status: invStatus,
|
||||
})
|
||||
}
|
||||
// Inventory is NOT committed here. Checkout announces a completed sale; the
|
||||
// order saga emits order.created and the inventory service reacts (commit +
|
||||
// release the cart hold + emit a level crossing), idempotently. This keeps a
|
||||
// down inventory service from blocking the revenue path — the payment is
|
||||
// already settled at Klarna, so the sale is a fact, not a request. See
|
||||
// docs/inventory-shape.md.
|
||||
|
||||
s.ApplyAnywhere(r.Context(), grain.Id, &messages.PaymentCompleted{
|
||||
PaymentId: orderId,
|
||||
@@ -266,8 +242,6 @@ func createOrderFromCheckout(ctx context.Context, s *CheckoutPoolServer, grain *
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
|
||||
func getLocationId(item *cart.CartItem) inventory.LocationID {
|
||||
if item.StoreId == nil || *item.StoreId == "" {
|
||||
return "se"
|
||||
@@ -282,7 +256,7 @@ func shouldTrackInventory(item *cart.CartItem) bool {
|
||||
if item.InventoryTracked {
|
||||
return true
|
||||
}
|
||||
return item.Cgm == "55010"
|
||||
return false
|
||||
}
|
||||
|
||||
func getInventoryRequests(items []*cart.CartItem) []inventory.ReserveRequest {
|
||||
|
||||
Reference in New Issue
Block a user