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/actor"
"git.tornberg.me/go-cart-actor/pkg/cart" "git.tornberg.me/go-cart-actor/pkg/cart"
"git.tornberg.me/mats/go-redis-inventory/pkg/inventory"
amqp "github.com/rabbitmq/amqp091-go" amqp "github.com/rabbitmq/amqp091-go"
) )
@@ -26,7 +27,7 @@ var tpl = `<!DOCTYPE html>
</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) conn, err := amqp.Dial(amqpUrl)
if err != nil { if err != nil {
log.Fatalf("failed to connect to RabbitMQ: %v", err) 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) w.WriteHeader(http.StatusBadRequest)
} }
log.Printf("Klarna order validation: %s", order.ID) 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) grain, err := a.pool.Get(uint64(cartId))
// failingRequest, err := s.inventoryService.ReservationCheck(inventoryRequests...) if err != nil {
// if err != nil { w.WriteHeader(http.StatusInternalServerError)
// logger.WarnContext(ctx, "inventory check failed", string(failingRequest.SKU), string(failingRequest.LocationID)) return
// return nil, err }
// }
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) w.WriteHeader(http.StatusOK)
}) })

View File

@@ -147,7 +147,7 @@ func main() {
if amqpUrl == "" { if amqpUrl == "" {
log.Printf("no connection to amqp defined") log.Printf("no connection to amqp defined")
} else { } else {
app.HandleCheckoutRequests(amqpUrl, mux) app.HandleCheckoutRequests(amqpUrl, mux, inventoryService)
} }
grpcSrv, err := actor.NewControlServer[*cart.CartGrain](controlPlaneConfig, pool) grpcSrv, err := actor.NewControlServer[*cart.CartGrain](controlPlaneConfig, pool)