flamingo.me/flamingo-commerce/v3@v3.11.0/sourcing/module.go (about)

     1  package sourcing
     2  
     3  import (
     4  	"flamingo.me/dingo"
     5  
     6  	"flamingo.me/flamingo-commerce/v3/cart/domain/validation"
     7  	"flamingo.me/flamingo-commerce/v3/sourcing/domain/fake"
     8  	restrictors "flamingo.me/flamingo-commerce/v3/sourcing/domain/restrictor"
     9  
    10  	"flamingo.me/flamingo-commerce/v3/cart"
    11  	"flamingo.me/flamingo-commerce/v3/sourcing/application"
    12  	"flamingo.me/flamingo-commerce/v3/sourcing/domain"
    13  )
    14  
    15  type (
    16  	// Module registers sourcing module
    17  	Module struct {
    18  		useDefaultSourcingService bool
    19  		enableQtyRestrictor       bool
    20  		useFakeService            bool
    21  	}
    22  )
    23  
    24  // Inject dependencies
    25  func (m *Module) Inject(
    26  	config *struct {
    27  		UseDefaultSourcingService bool `inject:"config:commerce.sourcing.useDefaultSourcingService,optional"`
    28  		EnableQtyRestrictor       bool `inject:"config:commerce.sourcing.enableQtyRestrictor,optional"`
    29  		UseFakeService            bool `inject:"config:commerce.sourcing.fake.enable, optional"`
    30  	},
    31  ) {
    32  	if config != nil {
    33  		m.useDefaultSourcingService = config.UseDefaultSourcingService
    34  		m.enableQtyRestrictor = config.EnableQtyRestrictor
    35  		m.useFakeService = config.UseFakeService
    36  	}
    37  
    38  }
    39  
    40  // Configure module
    41  func (m *Module) Configure(injector *dingo.Injector) {
    42  	if m.useDefaultSourcingService {
    43  		injector.Bind(new(domain.SourcingService)).To(domain.DefaultSourcingService{})
    44  	}
    45  
    46  	if m.enableQtyRestrictor {
    47  		injector.Bind(new(validation.MaxQuantityRestrictor)).To(restrictors.Restrictor{})
    48  	}
    49  
    50  	if m.useFakeService {
    51  		injector.Override(new(domain.SourcingService), "").To(fake.SourcingService{})
    52  	}
    53  
    54  	injector.Bind(new(application.SourcingApplication)).To(application.Service{})
    55  }
    56  
    57  // Depends on other modules
    58  func (m *Module) Depends() []dingo.Module {
    59  	return []dingo.Module{
    60  		new(cart.Module),
    61  	}
    62  }
    63  
    64  // CueConfig defines the sourcing module configuration
    65  func (m *Module) CueConfig() string {
    66  	// language=cue
    67  	return `
    68  commerce: {
    69  	sourcing: {
    70  		useDefaultSourcingService: bool | *true
    71  		enableQtyRestrictor: bool | *false
    72  		fake: {
    73  			enable: bool | *false
    74  			jsonPath: string | *""
    75  		}
    76  	}
    77  }
    78  `
    79  }