reserve inventory when order placed
This commit is contained in:
@@ -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)
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -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)
|
||||||
|
|||||||
Reference in New Issue
Block a user