flamingo.me/flamingo-commerce/v3@v3.11.0/cart/interfaces/graphql/resolvers.go (about) 1 package graphql 2 3 import ( 4 "context" 5 6 "flamingo.me/flamingo/v3/framework/web" 7 8 "flamingo.me/flamingo-commerce/v3/cart/application" 9 "flamingo.me/flamingo-commerce/v3/cart/domain/cart" 10 "flamingo.me/flamingo-commerce/v3/cart/domain/validation" 11 "flamingo.me/flamingo-commerce/v3/cart/interfaces/graphql/dto" 12 "flamingo.me/flamingo-commerce/v3/product/domain" 13 ) 14 15 // CommerceCartQueryResolver resolver for carts 16 type CommerceCartQueryResolver struct { 17 applicationCartReceiverService *application.CartReceiverService 18 applicationCartService *application.CartService 19 restrictionService *validation.RestrictionService 20 productService domain.ProductService 21 } 22 23 // Inject dependencies 24 func (r *CommerceCartQueryResolver) Inject( 25 applicationCartReceiverService *application.CartReceiverService, 26 cartService *application.CartService, 27 restrictionService *validation.RestrictionService, 28 productService domain.ProductService, 29 ) { 30 r.applicationCartReceiverService = applicationCartReceiverService 31 r.applicationCartService = cartService 32 r.restrictionService = restrictionService 33 r.productService = productService 34 } 35 36 // CommerceCart getter for queries 37 func (r *CommerceCartQueryResolver) CommerceCart(ctx context.Context) (*dto.DecoratedCart, error) { 38 req := web.RequestFromContext(ctx) 39 dc, err := r.applicationCartReceiverService.ViewDecoratedCart(ctx, req.Session()) 40 if err != nil { 41 return nil, err 42 } 43 44 return dto.NewDecoratedCart(dc), nil 45 } 46 47 // CommerceCartValidator to trigger the cart validation service 48 func (r *CommerceCartQueryResolver) CommerceCartValidator(ctx context.Context) (*validation.Result, error) { 49 session := web.SessionFromContext(ctx) 50 51 decoratedCart, err := r.applicationCartReceiverService.ViewDecoratedCart(ctx, session) 52 if err != nil { 53 return nil, err 54 } 55 56 result := r.applicationCartService.ValidateCart(ctx, session, decoratedCart) 57 58 return &result, nil 59 } 60 61 // CommerceCartQtyRestriction checks if given sku is restricted in terms of qty 62 func (r *CommerceCartQueryResolver) CommerceCartQtyRestriction(ctx context.Context, marketplaceCode string, variantCode *string, deliveryCode string) (*validation.RestrictionResult, error) { 63 session := web.SessionFromContext(ctx) 64 65 product, err := r.productService.Get(ctx, marketplaceCode) 66 if err != nil { 67 return nil, err 68 } 69 if variantCode != nil { 70 if configurableProduct, ok := product.(domain.ConfigurableProduct); ok { 71 product, err = configurableProduct.GetConfigurableWithActiveVariant(*variantCode) 72 if err != nil { 73 return nil, err 74 } 75 } 76 } 77 78 cart, err := r.applicationCartReceiverService.ViewCart(ctx, session) 79 if err != nil { 80 return nil, err 81 } 82 result := r.restrictionService.RestrictQty(ctx, session, product, cart, deliveryCode) 83 return result, nil 84 } 85 86 // CartSplit returns graphql specific cart split 87 func (r *CommerceCartQueryResolver) CartSplit(_ context.Context, paymentSelection *cart.DefaultPaymentSelection) ([]*dto.PaymentSelectionSplit, error) { 88 if paymentSelection == nil { 89 return nil, nil 90 } 91 92 paymentSelectionSplit := make([]*dto.PaymentSelectionSplit, 0) 93 for qualifier, charge := range paymentSelection.CartSplit() { 94 paymentSelectionSplit = append(paymentSelectionSplit, &dto.PaymentSelectionSplit{ 95 Qualifier: qualifier, 96 Charge: charge, 97 }) 98 } 99 100 return paymentSelectionSplit, nil 101 } 102 103 // CommerceCartAdditionalDataResolver resolver for custom attributes of cart 104 type CommerceCartAdditionalDataResolver struct{} 105 106 // CustomAttributes of cart 107 func (r *CommerceCartAdditionalDataResolver) CustomAttributes(_ context.Context, obj *cart.AdditionalData) (*dto.CustomAttributes, error) { 108 return &dto.CustomAttributes{Attributes: obj.CustomAttributes}, nil 109 } 110 111 // CommerceCartDeliveryInfoResolver resolver for additional data of delivery info 112 type CommerceCartDeliveryInfoResolver struct{} 113 114 // AdditionalData of delivery info 115 func (r *CommerceCartDeliveryInfoResolver) AdditionalData(_ context.Context, obj *cart.DeliveryInfo) (*dto.CustomAttributes, error) { 116 return &dto.CustomAttributes{Attributes: obj.AdditionalData}, nil 117 }