Skip to content

Commit

Permalink
feat(cart): add cart to ctx when decorating (#492)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristopherSchwab authored Oct 10, 2023
1 parent 3dc51a3 commit b973129
Showing 1 changed file with 42 additions and 23 deletions.
65 changes: 42 additions & 23 deletions cart/domain/decorator/cartDecorator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"sort"

"flamingo.me/flamingo-commerce/v3/cart/domain/cart"
cartDomain "flamingo.me/flamingo-commerce/v3/cart/domain/cart"

"flamingo.me/flamingo/v3/framework/flamingo"

Expand All @@ -21,22 +21,22 @@ type (

// DecoratedCart Decorates Access To a Cart
DecoratedCart struct {
Cart cart.Cart
Cart cartDomain.Cart
DecoratedDeliveries []DecoratedDelivery
Ctx context.Context `json:"-"`
Logger flamingo.Logger `json:"-"`
}

// DecoratedDelivery Decorates a CartItem with its Product
DecoratedDelivery struct {
Delivery cart.Delivery
Delivery cartDomain.Delivery
DecoratedItems []DecoratedCartItem
logger flamingo.Logger
}

// DecoratedCartItem Decorates a CartItem with its Product
DecoratedCartItem struct {
Item cart.Item
Item cartDomain.Item
Product domain.BasicProduct
logger flamingo.Logger
}
Expand All @@ -46,6 +46,8 @@ type (
DecoratedItems []DecoratedCartItem
Group string
}

ctxKey struct{}
)

// Inject dependencies
Expand All @@ -58,54 +60,62 @@ func (df *DecoratedCartFactory) Inject(
}

// Create Factory method to get Decorated Cart
func (df *DecoratedCartFactory) Create(ctx context.Context, Cart cart.Cart) *DecoratedCart {
decoratedCart := DecoratedCart{Cart: Cart, Logger: df.logger}
for _, d := range Cart.Deliveries {
func (df *DecoratedCartFactory) Create(ctx context.Context, cart cartDomain.Cart) *DecoratedCart {
decoratedCart := DecoratedCart{Cart: cart, Logger: df.logger}

for _, d := range cart.Deliveries {
decoratedCart.DecoratedDeliveries = append(decoratedCart.DecoratedDeliveries, DecoratedDelivery{
Delivery: d,
DecoratedItems: df.CreateDecorateCartItems(ctx, d.Cartitems),
DecoratedItems: df.CreateDecorateCartItems(context.WithValue(ctx, ctxKey{}, cart), d.Cartitems),
logger: df.logger,
})
}

decoratedCart.Ctx = ctx

return &decoratedCart
}

// CreateDecorateCartItems Factory method to get Decorated Cart
func (df *DecoratedCartFactory) CreateDecorateCartItems(ctx context.Context, items []cart.Item) []DecoratedCartItem {
func (df *DecoratedCartFactory) CreateDecorateCartItems(ctx context.Context, items []cartDomain.Item) []DecoratedCartItem {
var decoratedItems []DecoratedCartItem
for _, cartitem := range items {
decoratedItem := df.decorateCartItem(ctx, cartitem)

for _, cartItem := range items {
decoratedItem := df.decorateCartItem(ctx, cartItem)
decoratedItems = append(decoratedItems, decoratedItem)
}

return decoratedItems
}

// decorateCartItem factory method
func (df *DecoratedCartFactory) decorateCartItem(ctx context.Context, cartitem cart.Item) DecoratedCartItem {
decorateditem := DecoratedCartItem{Item: cartitem, logger: df.logger}
product, e := df.productService.Get(ctx, cartitem.MarketplaceCode)
func (df *DecoratedCartFactory) decorateCartItem(ctx context.Context, cartItem cartDomain.Item) DecoratedCartItem {
decoratedItem := DecoratedCartItem{Item: cartItem, logger: df.logger}

product, e := df.productService.Get(ctx, cartItem.MarketplaceCode)
if e != nil {
df.logger.WithContext(ctx).Error("cart.decorator - no product for item", e)
if product == nil {
// To avoid errors if consumers want to access the product data
product = domain.SimpleProduct{
BasicProductData: domain.BasicProductData{
Title: cartitem.ProductName + "[outdated]",
Title: cartItem.ProductName + "[outdated]",
},
}

decorateditem.Product = product
decoratedItem.Product = product
}
return decorateditem

return decoratedItem
}

if product.Type() == domain.TypeConfigurable {
if configurable, ok := product.(domain.ConfigurableProduct); ok {
configurableWithVariant, err := configurable.GetConfigurableWithActiveVariant(cartitem.VariantMarketPlaceCode)
configurableWithVariant, err := configurable.GetConfigurableWithActiveVariant(cartItem.VariantMarketPlaceCode)
if err != nil {
product = domain.SimpleProduct{
BasicProductData: domain.BasicProductData{
Title: cartitem.ProductName + "[outdated]",
Title: cartItem.ProductName + "[outdated]",
},
}
} else {
Expand All @@ -116,11 +126,11 @@ func (df *DecoratedCartFactory) decorateCartItem(ctx context.Context, cartitem c

if product.Type() == domain.TypeBundle {
if bundle, ok := product.(domain.BundleProduct); ok {
bundleWithActiveChoices, err := bundle.GetBundleProductWithActiveChoices(cartitem.BundleConfig)
bundleWithActiveChoices, err := bundle.GetBundleProductWithActiveChoices(cartItem.BundleConfig)
if err != nil {
product = domain.SimpleProduct{
BasicProductData: domain.BasicProductData{
Title: cartitem.ProductName + "[outdated]",
Title: cartItem.ProductName + "[outdated]",
},
}
} else {
Expand All @@ -129,8 +139,9 @@ func (df *DecoratedCartFactory) decorateCartItem(ctx context.Context, cartitem c
}
}

decorateditem.Product = product
return decorateditem
decoratedItem.Product = product

return decoratedItem
}

// IsConfigurable - checks if current CartItem is a Configurable Product
Expand Down Expand Up @@ -296,3 +307,11 @@ func (dc DecoratedDelivery) GetDecoratedCartItemByID(ID string) *DecoratedCartIt
}
return nil
}

func CartFromDecoratedCartFactoryContext(ctx context.Context) *cartDomain.Cart {
if cart, ok := ctx.Value(ctxKey{}).(cartDomain.Cart); ok {
return &cart
}

return nil
}

0 comments on commit b973129

Please sign in to comment.