53 lines
1.4 KiB
Go
53 lines
1.4 KiB
Go
package checkout
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
"git.k6n.net/go-cart-actor/pkg/cart"
|
|
messages "git.k6n.net/go-cart-actor/proto/checkout"
|
|
)
|
|
|
|
// mutation_initialize_checkout.go
|
|
//
|
|
// Registers the InitializeCheckout mutation.
|
|
// This mutation is invoked AFTER an external checkout session
|
|
// has been successfully created or updated. It persists the
|
|
// order reference / status and marks the checkout as having a payment in progress.
|
|
//
|
|
// Behavior:
|
|
// - Sets OrderId to the order ID.
|
|
// - Sets Status to the current status.
|
|
// - Sets PaymentInProgress flag.
|
|
// - Assumes inventory is already reserved.
|
|
//
|
|
// Validation:
|
|
// - Returns an error if payload is nil.
|
|
// - Returns an error if orderId is empty.
|
|
|
|
func HandleInitializeCheckout(g *CheckoutGrain, m *messages.InitializeCheckout) error {
|
|
if m == nil {
|
|
return fmt.Errorf("InitializeCheckout: nil payload")
|
|
}
|
|
// if g.OrderId == "" {
|
|
// return fmt.Errorf("InitializeCheckout: missing orderId")
|
|
// }
|
|
if g.CartState != nil {
|
|
if g.CartId != cart.CartId(m.CartId) {
|
|
return fmt.Errorf("InitializeCheckout: cart ID mismatch")
|
|
}
|
|
if g.PaymentInProgress > 0 || g.OrderId != nil {
|
|
return fmt.Errorf("InitializeCheckout: payment already in progress")
|
|
}
|
|
}
|
|
|
|
err := json.Unmarshal(m.CartState.Value, &g.CartState)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
g.CartTotalPrice = g.CartState.TotalPrice
|
|
|
|
return nil
|
|
}
|