try to reserve
All checks were successful
Build and Publish / Metadata (push) Successful in 14s
Build and Publish / BuildAndDeployAmd64 (push) Successful in 57s
Build and Publish / BuildAndDeployArm64 (push) Successful in 4m50s

This commit is contained in:
matst80
2025-11-11 09:29:51 +01:00
parent 5d4d917f6a
commit ab5d9cb2b7
3 changed files with 56 additions and 7 deletions

View File

@@ -15,6 +15,7 @@ import (
"git.tornberg.me/go-cart-actor/pkg/cart"
messages "git.tornberg.me/go-cart-actor/pkg/messages"
"git.tornberg.me/go-cart-actor/pkg/voucher"
"git.tornberg.me/mats/go-redis-inventory/pkg/inventory"
"github.com/gogo/protobuf/proto"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
@@ -40,15 +41,17 @@ var (
type PoolServer struct {
actor.GrainPool[*cart.CartGrain]
pod_name string
klarnaClient *KlarnaClient
pod_name string
klarnaClient *KlarnaClient
inventoryService inventory.InventoryService
}
func NewPoolServer(pool actor.GrainPool[*cart.CartGrain], pod_name string, klarnaClient *KlarnaClient) *PoolServer {
func NewPoolServer(pool actor.GrainPool[*cart.CartGrain], pod_name string, klarnaClient *KlarnaClient, inventoryService inventory.InventoryService) *PoolServer {
return &PoolServer{
GrainPool: pool,
pod_name: pod_name,
klarnaClient: klarnaClient,
GrainPool: pool,
pod_name: pod_name,
klarnaClient: klarnaClient,
inventoryService: inventoryService,
}
}
@@ -310,6 +313,25 @@ func getLocale(country string) string {
return "sv-se"
}
func getLocationId(item *cart.CartItem) inventory.LocationID {
if item.StoreId == nil || *item.StoreId == "" {
return "se"
}
return inventory.LocationID(*item.StoreId)
}
func getInventoryRequests(items []*cart.CartItem) []inventory.ReserveRequest {
var requests []inventory.ReserveRequest
for _, item := range items {
requests = append(requests, inventory.ReserveRequest{
SKU: inventory.SKU(item.Sku),
LocationID: getLocationId(item),
Quantity: uint32(item.Quantity),
})
}
return requests
}
func (s *PoolServer) CreateOrUpdateCheckout(ctx context.Context, host string, id cart.CartId) (*CheckoutOrder, error) {
country := getCountryFromHost(host)
meta := &CheckoutMeta{
@@ -329,6 +351,13 @@ func (s *PoolServer) CreateOrUpdateCheckout(ctx context.Context, host string, id
return nil, err
}
inventoryRequests := getInventoryRequests(grain.Items)
failingRequest, err := s.inventoryService.ReservationCheck(inventoryRequests...)
if err != nil {
logger.WarnContext(ctx, "inventory check failed", string(failingRequest.SKU), string(failingRequest.LocationID))
return nil, err
}
// Build pure checkout payload
payload, _, err := BuildCheckoutOrderPayload(grain, meta)
if err != nil {