74 lines
1.8 KiB
Go
74 lines
1.8 KiB
Go
package profile
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
messages "git.k6n.net/mats/go-cart-actor/proto/profile"
|
|
)
|
|
|
|
// HandleLinkCart links a cart grain to this user profile.
|
|
func HandleLinkCart(p *ProfileGrain, m *messages.LinkCart) error {
|
|
if m == nil {
|
|
return fmt.Errorf("LinkCart: nil payload")
|
|
}
|
|
if m.CartId == 0 {
|
|
return fmt.Errorf("LinkCart: cartId is required")
|
|
}
|
|
// Check for duplicate
|
|
for _, c := range p.Carts {
|
|
if c.CartId == m.CartId {
|
|
return nil // already linked; idempotent
|
|
}
|
|
}
|
|
p.Carts = append(p.Carts, LinkedCart{
|
|
CartId: m.CartId,
|
|
Label: m.Label,
|
|
})
|
|
return nil
|
|
}
|
|
|
|
// HandleLinkCheckout links a checkout grain to this user profile.
|
|
func HandleLinkCheckout(p *ProfileGrain, m *messages.LinkCheckout) error {
|
|
if m == nil {
|
|
return fmt.Errorf("LinkCheckout: nil payload")
|
|
}
|
|
if m.CheckoutId == 0 {
|
|
return fmt.Errorf("LinkCheckout: checkoutId is required")
|
|
}
|
|
// Check for duplicate
|
|
for _, ch := range p.Checkouts {
|
|
if ch.CheckoutId == m.CheckoutId {
|
|
return nil // already linked; idempotent
|
|
}
|
|
}
|
|
p.Checkouts = append(p.Checkouts, LinkedCheckout{
|
|
CheckoutId: m.CheckoutId,
|
|
CartId: m.CartId,
|
|
})
|
|
return nil
|
|
}
|
|
|
|
// HandleLinkOrder links an order to this user profile.
|
|
func HandleLinkOrder(p *ProfileGrain, m *messages.LinkOrder) error {
|
|
if m == nil {
|
|
return fmt.Errorf("LinkOrder: nil payload")
|
|
}
|
|
if m.OrderReference == "" {
|
|
return fmt.Errorf("LinkOrder: orderReference is required")
|
|
}
|
|
// Check for duplicate (by index so we can mutate)
|
|
for i := range p.Orders {
|
|
if p.Orders[i].OrderReference == m.OrderReference {
|
|
// Update status snapshot
|
|
p.Orders[i].Status = m.Status
|
|
return nil
|
|
}
|
|
}
|
|
p.Orders = append(p.Orders, LinkedOrder{
|
|
OrderReference: m.OrderReference,
|
|
CartId: m.CartId,
|
|
Status: m.Status,
|
|
})
|
|
return nil
|
|
}
|