Co-authored-by: matst80 <mats.tornberg@gmail.com> Reviewed-on: #8 Co-authored-by: Mats Törnberg <mats@tornberg.me> Co-committed-by: Mats Törnberg <mats@tornberg.me>
45 lines
1.2 KiB
Go
45 lines
1.2 KiB
Go
package checkout
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
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 m.OrderId == "" {
|
|
return fmt.Errorf("InitializeCheckout: missing orderId")
|
|
}
|
|
if m.CartState != nil {
|
|
return fmt.Errorf("InitializeCheckout: checkout already initialized")
|
|
}
|
|
|
|
err := json.Unmarshal(m.CartState.Value, &g.CartState)
|
|
if err != nil {
|
|
return fmt.Errorf("InitializeCheckout: failed to unmarshal cart state: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|