flamingo.me/flamingo-commerce/v3@v3.11.0/sourcing/application/service.go (about) 1 package application 2 3 import ( 4 "context" 5 "errors" 6 7 "flamingo.me/flamingo-commerce/v3/cart/application" 8 "flamingo.me/flamingo-commerce/v3/cart/domain/cart" 9 "flamingo.me/flamingo-commerce/v3/cart/domain/decorator" 10 productDomain "flamingo.me/flamingo-commerce/v3/product/domain" 11 "flamingo.me/flamingo-commerce/v3/sourcing/domain" 12 13 "flamingo.me/flamingo/v3/framework/flamingo" 14 "flamingo.me/flamingo/v3/framework/web" 15 ) 16 17 type ( 18 // SourcingApplication interface 19 SourcingApplication interface { 20 GetAvailableSourcesDeductedByCurrentCart(ctx context.Context, session *web.Session, product productDomain.BasicProduct, deliveryCode string) (domain.AvailableSourcesPerProduct, error) 21 GetAvailableSources(ctx context.Context, session *web.Session, product productDomain.BasicProduct, deliveryCode string) (domain.AvailableSourcesPerProduct, error) 22 } 23 24 // Service to access the sourcing based on current cart 25 Service struct { 26 logger flamingo.Logger 27 sourcingService domain.SourcingService 28 cartReceiverService *application.CartReceiverService 29 deliveryInfoBuilder cart.DeliveryInfoBuilder 30 } 31 ) 32 33 var ( 34 _ SourcingApplication = new(Service) 35 ) 36 37 // Inject dependencies 38 func (s *Service) Inject( 39 l flamingo.Logger, 40 cartReceiverService *application.CartReceiverService, 41 sourcingService domain.SourcingService, 42 deliveryInfoBuilder cart.DeliveryInfoBuilder, 43 ) *Service { 44 s.logger = l.WithField(flamingo.LogKeyModule, "sourcing").WithField(flamingo.LogKeyCategory, "Application.Service") 45 46 s.cartReceiverService = cartReceiverService 47 s.deliveryInfoBuilder = deliveryInfoBuilder 48 s.sourcingService = sourcingService 49 50 return s 51 } 52 53 // GetAvailableSourcesDeductedByCurrentCart fetches available sources minus those already allocated to the cart 54 func (s *Service) GetAvailableSourcesDeductedByCurrentCart(ctx context.Context, session *web.Session, product productDomain.BasicProduct, deliveryCode string) (domain.AvailableSourcesPerProduct, error) { 55 if product == nil { 56 s.logger.WithContext(ctx).Error("No product given for GetAvailableSourcesDeductedByCurrentCart") 57 return nil, errors.New("no product given for GetAvailableSourcesDeductedByCurrentCart") 58 } 59 60 deliveryInfo, decoratedCart, err := s.getDeliveryInfo(ctx, session, deliveryCode) 61 if err != nil { 62 return nil, err 63 } 64 65 return s.sourcingService.GetAvailableSources(ctx, product, deliveryInfo, decoratedCart) 66 } 67 68 // GetAvailableSources without evaluating current cart items 69 func (s *Service) GetAvailableSources(ctx context.Context, session *web.Session, product productDomain.BasicProduct, deliveryCode string) (domain.AvailableSourcesPerProduct, error) { 70 if product == nil { 71 s.logger.WithContext(ctx).Error("No product given for GetAvailableSources") 72 return nil, errors.New("no product given for GetAvailableSources") 73 } 74 75 deliveryInfo, _, err := s.getDeliveryInfo(ctx, session, deliveryCode) 76 if err != nil { 77 return nil, err 78 } 79 80 return s.sourcingService.GetAvailableSources(ctx, product, deliveryInfo, nil) 81 } 82 83 func (s *Service) getDeliveryInfo(ctx context.Context, session *web.Session, deliveryCode string) (*cart.DeliveryInfo, *decorator.DecoratedCart, error) { 84 decoratedCart, err := s.cartReceiverService.ViewDecoratedCart(ctx, session) 85 if err != nil { 86 s.logger.WithContext(ctx).Error(err) 87 return nil, nil, err 88 } 89 var deliveryInfo *cart.DeliveryInfo 90 delivery, found := decoratedCart.Cart.GetDeliveryByCode(deliveryCode) 91 if !found { 92 deliveryInfo, err = s.deliveryInfoBuilder.BuildByDeliveryCode(deliveryCode) 93 if err != nil { 94 s.logger.WithContext(ctx).Error(err) 95 return nil, decoratedCart, err 96 } 97 } else { 98 deliveryInfo = &delivery.DeliveryInfo 99 } 100 return deliveryInfo, decoratedCart, nil 101 }