add profile actors
This commit is contained in:
@@ -6,12 +6,14 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"git.k6n.net/mats/go-cart-actor/pkg/checkout"
|
||||
"git.k6n.net/mats/go-cart-actor/pkg/cart"
|
||||
profilePkg "git.k6n.net/mats/go-cart-actor/pkg/profile"
|
||||
checkoutMessages "git.k6n.net/mats/go-cart-actor/proto/checkout"
|
||||
"google.golang.org/protobuf/types/known/anypb"
|
||||
"google.golang.org/protobuf/types/known/timestamppb"
|
||||
@@ -19,8 +21,9 @@ import (
|
||||
|
||||
// CheckoutServer is the UCP REST adapter over the checkout grain pool.
|
||||
type CheckoutServer struct {
|
||||
applier CheckoutApplier
|
||||
orderSvc OrderApplier // optional; when set creates real orders on complete
|
||||
applier CheckoutApplier
|
||||
profileApplier ProfileApplier // optional; when set links checkouts/orders to profiles
|
||||
orderSvc OrderApplier // optional; when set creates real orders on complete
|
||||
}
|
||||
|
||||
// NewCheckoutServer builds a UCP REST adapter over a checkout grain pool.
|
||||
@@ -32,6 +35,11 @@ func NewCheckoutServer(applier CheckoutApplier, orderSvc ...OrderApplier) *Check
|
||||
return s
|
||||
}
|
||||
|
||||
// SetProfileApplier enables identity linking on this server.
|
||||
func (s *CheckoutServer) SetProfileApplier(pa ProfileApplier) {
|
||||
s.profileApplier = pa
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// UCP Checkout endpoints
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -180,6 +188,15 @@ func (s *CheckoutServer) handleCreateCheckout(w http.ResponseWriter, r *http.Req
|
||||
return
|
||||
}
|
||||
|
||||
// Link checkout to customer profile if identity linking is enabled.
|
||||
if s.profileApplier != nil && req.CustomerId != "" {
|
||||
if pid, ok := profilePkg.ParseProfileId(req.CustomerId); ok {
|
||||
if err := linkCheckoutToProfile(s.profileApplier, r.Context(), uint64(pid), uint64(checkoutId), uint64(cartId)); err != nil {
|
||||
log.Printf("identity: failed to link checkout %d to profile %s: %v", uint64(checkoutId), req.CustomerId, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusCreated, checkoutGrainToResponse(checkoutId.String(), g))
|
||||
}
|
||||
|
||||
@@ -336,6 +353,15 @@ func (s *CheckoutServer) handleCompleteCheckout(w http.ResponseWriter, r *http.R
|
||||
return
|
||||
}
|
||||
|
||||
// Auto-link order to customer profile if identity linking is enabled.
|
||||
if s.profileApplier != nil && orderID != "" && req.CustomerId != "" {
|
||||
if pid, ok := profilePkg.ParseProfileId(req.CustomerId); ok {
|
||||
if err := linkOrderToProfile(s.profileApplier, r.Context(), uint64(pid), orderID, uint64(g.CartId), "placed"); err != nil {
|
||||
log.Printf("identity: failed to link order %s to profile %s: %v", orderID, req.CustomerId, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, checkoutGrainToResponse(r.PathValue("id"), g))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user