reserve inventory when order placed
All checks were successful
Build and Publish / Metadata (push) Successful in 10s
Build and Publish / BuildAndDeployAmd64 (push) Successful in 51s
Build and Publish / BuildAndDeployArm64 (push) Successful in 4m45s

This commit is contained in:
matst80
2025-11-11 11:47:05 +01:00
parent 81246fe497
commit 756a43b342
2 changed files with 21 additions and 8 deletions

View File

@@ -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 = `<!DOCTYPE html>
</html>
`
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)
})