refactor
This commit is contained in:
+18
-5
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
@@ -9,6 +10,7 @@ import (
|
||||
|
||||
"git.k6n.net/mats/go-cart-actor/pkg/cart"
|
||||
"git.k6n.net/mats/go-cart-actor/pkg/checkout"
|
||||
"git.k6n.net/mats/platform/inventory"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/metric"
|
||||
)
|
||||
@@ -77,12 +79,23 @@ func getCountryFromHost(host string) string {
|
||||
}
|
||||
|
||||
func (a *CheckoutPoolServer) reserveInventory(ctx context.Context, grain *checkout.CheckoutGrain) error {
|
||||
if a.inventoryService != nil {
|
||||
inventoryRequests := getInventoryRequests(grain.CartState.Items)
|
||||
_, err := a.inventoryService.ReservationCheck(ctx, inventoryRequests...)
|
||||
if a.reservationPolicy == nil {
|
||||
return nil
|
||||
}
|
||||
for _, item := range grain.CartState.Items {
|
||||
if item == nil || !shouldTrackInventory(item) {
|
||||
continue
|
||||
}
|
||||
loc := inventory.LocationID("se")
|
||||
if item.StoreId != nil && *item.StoreId != "" {
|
||||
loc = inventory.LocationID(*item.StoreId)
|
||||
}
|
||||
avail, err := a.reservationPolicy.Available(ctx, inventory.SKU(item.Sku), loc)
|
||||
if err != nil {
|
||||
logger.WarnContext(ctx, "placeorder inventory check failed")
|
||||
return err
|
||||
return fmt.Errorf("inventory check failed for SKU %s: %w", item.Sku, err)
|
||||
}
|
||||
if int64(item.Quantity) > avail {
|
||||
return fmt.Errorf("insufficient inventory for SKU %s: have %d, need %d", item.Sku, avail, item.Quantity)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user