add profile actors
This commit is contained in:
+20
-3
@@ -3,10 +3,12 @@ package ucp
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
"sync"
|
||||
|
||||
"git.k6n.net/mats/go-cart-actor/pkg/cart"
|
||||
"git.k6n.net/mats/go-cart-actor/pkg/profile"
|
||||
"git.k6n.net/mats/go-cart-actor/pkg/actor"
|
||||
messages "git.k6n.net/mats/go-cart-actor/proto/cart"
|
||||
"google.golang.org/protobuf/proto"
|
||||
@@ -14,8 +16,9 @@ import (
|
||||
|
||||
// CartServer is the UCP REST adapter over the cart grain pool.
|
||||
type CartServer struct {
|
||||
applier CartApplier
|
||||
newCartId func() (cart.CartId, error)
|
||||
applier CartApplier
|
||||
profileApplier ProfileApplier // optional; when set, links carts to customer profiles
|
||||
newCartId func() (cart.CartId, error)
|
||||
}
|
||||
|
||||
// NewCartServer builds a UCP REST adapter over a cart grain pool.
|
||||
@@ -26,6 +29,11 @@ func NewCartServer(applier CartApplier) *CartServer {
|
||||
}
|
||||
}
|
||||
|
||||
// SetProfileApplier enables identity linking on this server.
|
||||
func (s *CartServer) SetProfileApplier(pa ProfileApplier) {
|
||||
s.profileApplier = pa
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// UCP Cart endpoints
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -113,12 +121,21 @@ func (s *CartServer) handleUpdateCart(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
// Set user ID.
|
||||
// Set user ID and auto-link cart to customer profile.
|
||||
if req.UserId != nil && *req.UserId != "" {
|
||||
if _, err := s.applier.Apply(r.Context(), id, &messages.SetUserId{UserId: *req.UserId}); err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "failed to set user: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// Auto-link this cart to the customer's profile if identity linking is enabled.
|
||||
if s.profileApplier != nil {
|
||||
if pid, ok := profile.ParseProfileId(*req.UserId); ok {
|
||||
if err := linkCartToProfile(s.profileApplier, r.Context(), uint64(pid), id); err != nil {
|
||||
log.Printf("identity: failed to link cart %d to profile %d: %v", id, uint64(pid), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
g, err := s.applier.Get(r.Context(), id)
|
||||
|
||||
Reference in New Issue
Block a user