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