diff --git a/cmd/cart/checkout_server.go b/cmd/cart/checkout_server.go index b724fc0..9cdb49f 100644 --- a/cmd/cart/checkout_server.go +++ b/cmd/cart/checkout_server.go @@ -9,6 +9,7 @@ import ( "git.tornberg.me/go-cart-actor/pkg/actor" "git.tornberg.me/go-cart-actor/pkg/cart" + "git.tornberg.me/mats/go-redis-inventory/pkg/inventory" amqp "github.com/rabbitmq/amqp091-go" ) @@ -26,7 +27,7 @@ var tpl = ` ` -func (a *App) HandleCheckoutRequests(amqpUrl string, mux *http.ServeMux) { +func (a *App) HandleCheckoutRequests(amqpUrl string, mux *http.ServeMux, inventoryService inventory.InventoryService) { conn, err := amqp.Dial(amqpUrl) if err != nil { log.Fatalf("failed to connect to RabbitMQ: %v", err) @@ -128,13 +129,25 @@ func (a *App) HandleCheckoutRequests(amqpUrl string, mux *http.ServeMux) { w.WriteHeader(http.StatusBadRequest) } log.Printf("Klarna order validation: %s", order.ID) + cartId, ok := cart.ParseCartId(order.MerchantReference1) + if !ok { + w.WriteHeader(http.StatusBadRequest) + return + } - // 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 - // } + grain, err := a.pool.Get(uint64(cartId)) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + return + } + + inventoryRequests := getInventoryRequests(grain.Items) + err = inventoryService.ReserveInventory(inventoryRequests...) + if err != nil { + logger.WarnContext(r.Context(), "placeorder inventory reservation failed") + w.WriteHeader(http.StatusNotAcceptable) + return + } w.WriteHeader(http.StatusOK) }) diff --git a/cmd/cart/main.go b/cmd/cart/main.go index 8b8f38b..66da5b7 100644 --- a/cmd/cart/main.go +++ b/cmd/cart/main.go @@ -147,7 +147,7 @@ func main() { if amqpUrl == "" { log.Printf("no connection to amqp defined") } else { - app.HandleCheckoutRequests(amqpUrl, mux) + app.HandleCheckoutRequests(amqpUrl, mux, inventoryService) } grpcSrv, err := actor.NewControlServer[*cart.CartGrain](controlPlaneConfig, pool)