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

@@ -129,6 +129,13 @@ func (a *App) HandleCheckoutRequests(amqpUrl string, mux *http.ServeMux) {
} }
log.Printf("Klarna order validation: %s", order.ID) log.Printf("Klarna order validation: %s", order.ID)
// 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
// }
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
}) })
} }

View File

@@ -19,9 +19,11 @@ import (
"git.tornberg.me/go-cart-actor/pkg/promotions" "git.tornberg.me/go-cart-actor/pkg/promotions"
"git.tornberg.me/go-cart-actor/pkg/proxy" "git.tornberg.me/go-cart-actor/pkg/proxy"
"git.tornberg.me/go-cart-actor/pkg/voucher" "git.tornberg.me/go-cart-actor/pkg/voucher"
"git.tornberg.me/mats/go-redis-inventory/pkg/inventory"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto" "github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/redis/go-redis/v9"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp" "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
) )
@@ -45,6 +47,8 @@ type App struct {
var podIp = os.Getenv("POD_IP") var podIp = os.Getenv("POD_IP")
var name = os.Getenv("POD_NAME") var name = os.Getenv("POD_NAME")
var amqpUrl = os.Getenv("AMQP_URL") var amqpUrl = os.Getenv("AMQP_URL")
var redisAddress = os.Getenv("REDIS_ADDRESS")
var redisPassword = os.Getenv("REDIS_PASSWORD")
func getCountryFromHost(host string) string { func getCountryFromHost(host string) string {
if strings.Contains(strings.ToLower(host), "-no") { if strings.Contains(strings.ToLower(host), "-no") {
@@ -119,8 +123,17 @@ func main() {
} }
klarnaClient := NewKlarnaClient(KlarnaPlaygroundUrl, os.Getenv("KLARNA_API_USERNAME"), os.Getenv("KLARNA_API_PASSWORD")) klarnaClient := NewKlarnaClient(KlarnaPlaygroundUrl, os.Getenv("KLARNA_API_USERNAME"), os.Getenv("KLARNA_API_PASSWORD"))
rdb := redis.NewClient(&redis.Options{
Addr: redisAddress,
Password: redisPassword,
DB: 0,
})
inventoryService, err := inventory.NewRedisInventoryService(rdb, context.Background())
if err != nil {
log.Fatalf("Error creating inventory service: %v\n", err)
}
syncedServer := NewPoolServer(pool, fmt.Sprintf("%s, %s", name, podIp), klarnaClient) syncedServer := NewPoolServer(pool, fmt.Sprintf("%s, %s", name, podIp), klarnaClient, inventoryService)
app := &App{ app := &App{
pool: pool, pool: pool,

View File

@@ -15,6 +15,7 @@ import (
"git.tornberg.me/go-cart-actor/pkg/cart" "git.tornberg.me/go-cart-actor/pkg/cart"
messages "git.tornberg.me/go-cart-actor/pkg/messages" messages "git.tornberg.me/go-cart-actor/pkg/messages"
"git.tornberg.me/go-cart-actor/pkg/voucher" "git.tornberg.me/go-cart-actor/pkg/voucher"
"git.tornberg.me/mats/go-redis-inventory/pkg/inventory"
"github.com/gogo/protobuf/proto" "github.com/gogo/protobuf/proto"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto" "github.com/prometheus/client_golang/prometheus/promauto"
@@ -42,13 +43,15 @@ type PoolServer struct {
actor.GrainPool[*cart.CartGrain] actor.GrainPool[*cart.CartGrain]
pod_name string pod_name string
klarnaClient *KlarnaClient 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{ return &PoolServer{
GrainPool: pool, GrainPool: pool,
pod_name: pod_name, pod_name: pod_name,
klarnaClient: klarnaClient, klarnaClient: klarnaClient,
inventoryService: inventoryService,
} }
} }
@@ -310,6 +313,25 @@ func getLocale(country string) string {
return "sv-se" 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) { func (s *PoolServer) CreateOrUpdateCheckout(ctx context.Context, host string, id cart.CartId) (*CheckoutOrder, error) {
country := getCountryFromHost(host) country := getCountryFromHost(host)
meta := &CheckoutMeta{ meta := &CheckoutMeta{
@@ -329,6 +351,13 @@ func (s *PoolServer) CreateOrUpdateCheckout(ctx context.Context, host string, id
return nil, err 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 // Build pure checkout payload
payload, _, err := BuildCheckoutOrderPayload(grain, meta) payload, _, err := BuildCheckoutOrderPayload(grain, meta)
if err != nil { if err != nil {