new payment methods service
This commit is contained in:
@@ -7,11 +7,15 @@ import (
|
||||
"github.com/tech/sendico/payments/storage"
|
||||
"github.com/tech/sendico/payments/storage/model"
|
||||
"github.com/tech/sendico/payments/storage/mongo/store"
|
||||
quotestorage "github.com/tech/sendico/payments/storage/quote"
|
||||
quotemongo "github.com/tech/sendico/payments/storage/quote/mongo"
|
||||
"github.com/tech/sendico/pkg/auth"
|
||||
"github.com/tech/sendico/pkg/db"
|
||||
"github.com/tech/sendico/pkg/db/repository"
|
||||
"github.com/tech/sendico/pkg/merrors"
|
||||
"github.com/tech/sendico/pkg/mlogger"
|
||||
"github.com/tech/sendico/pkg/mservice"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
)
|
||||
|
||||
// Store implements storage.Repository backed by MongoDB.
|
||||
@@ -20,13 +24,20 @@ type Store struct {
|
||||
ping func(context.Context) error
|
||||
|
||||
payments storage.PaymentsStore
|
||||
quotes storage.QuotesStore
|
||||
methods storage.PaymentMethodsStore
|
||||
quotes quotestorage.QuotesStore
|
||||
routes storage.RoutesStore
|
||||
plans storage.PlanTemplatesStore
|
||||
}
|
||||
|
||||
type paymentMethodsConfig struct {
|
||||
enforcer auth.Enforcer
|
||||
permissionRef bson.ObjectID
|
||||
}
|
||||
|
||||
type options struct {
|
||||
quoteRetention time.Duration
|
||||
quoteRetention time.Duration
|
||||
paymentMethodsAuth *paymentMethodsConfig
|
||||
}
|
||||
|
||||
// Option configures the Mongo-backed payments repository.
|
||||
@@ -39,6 +50,16 @@ func WithQuoteRetention(retention time.Duration) Option {
|
||||
}
|
||||
}
|
||||
|
||||
// WithPaymentMethodsAuth enables the payment-methods store and permission checks.
|
||||
func WithPaymentMethodsAuth(enforcer auth.Enforcer, permissionRef bson.ObjectID) Option {
|
||||
return func(opts *options) {
|
||||
opts.paymentMethodsAuth = &paymentMethodsConfig{
|
||||
enforcer: enforcer,
|
||||
permissionRef: permissionRef,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// New constructs a Mongo-backed payments repository from a Mongo connection.
|
||||
func New(logger mlogger.Logger, conn *db.MongoConnection, opts ...Option) (*Store, error) {
|
||||
if conn == nil {
|
||||
@@ -48,11 +69,22 @@ func New(logger mlogger.Logger, conn *db.MongoConnection, opts ...Option) (*Stor
|
||||
quotesRepo := repository.CreateMongoRepository(conn.Database(), (&model.PaymentQuoteRecord{}).Collection())
|
||||
routesRepo := repository.CreateMongoRepository(conn.Database(), (&model.PaymentRoute{}).Collection())
|
||||
plansRepo := repository.CreateMongoRepository(conn.Database(), (&model.PaymentPlanTemplate{}).Collection())
|
||||
return NewWithRepository(logger, conn.Ping, paymentsRepo, quotesRepo, routesRepo, plansRepo, opts...)
|
||||
methodsRepo := repository.CreateMongoRepository(conn.Database(), mservice.PaymentMethods)
|
||||
|
||||
return newWithRepository(logger, conn.Ping, paymentsRepo, methodsRepo, quotesRepo, routesRepo, plansRepo, opts...)
|
||||
}
|
||||
|
||||
// NewWithRepository constructs a payments repository using the provided primitives.
|
||||
func NewWithRepository(logger mlogger.Logger, ping func(context.Context) error, paymentsRepo repository.Repository, quotesRepo repository.Repository, routesRepo repository.Repository, plansRepo repository.Repository, opts ...Option) (*Store, error) {
|
||||
return newWithRepository(logger, ping, paymentsRepo, nil, quotesRepo, routesRepo, plansRepo, opts...)
|
||||
}
|
||||
|
||||
func newWithRepository(
|
||||
logger mlogger.Logger,
|
||||
ping func(context.Context) error,
|
||||
paymentsRepo, methodsRepo, quotesRepo, routesRepo, plansRepo repository.Repository,
|
||||
opts ...Option,
|
||||
) (*Store, error) {
|
||||
if ping == nil {
|
||||
return nil, merrors.InvalidArgument("payments.storage.mongo: ping func is nil")
|
||||
}
|
||||
@@ -93,10 +125,30 @@ func NewWithRepository(logger mlogger.Logger, ping func(context.Context) error,
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var methodsStore storage.PaymentMethodsStore
|
||||
if cfg.paymentMethodsAuth != nil {
|
||||
if methodsRepo == nil {
|
||||
return nil, merrors.InvalidArgument("payments.storage.mongo: payment methods repository is nil")
|
||||
}
|
||||
if cfg.paymentMethodsAuth.enforcer == nil {
|
||||
return nil, merrors.InvalidArgument("payments.storage.mongo: payment methods enforcer is nil")
|
||||
}
|
||||
if cfg.paymentMethodsAuth.permissionRef == bson.NilObjectID {
|
||||
return nil, merrors.InvalidArgument("payments.storage.mongo: payment methods permission reference is required")
|
||||
}
|
||||
|
||||
methodsStore, err = store.NewPaymentMethods(childLogger, methodsRepo, cfg.paymentMethodsAuth.enforcer, cfg.paymentMethodsAuth.permissionRef)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
result := &Store{
|
||||
logger: childLogger,
|
||||
ping: ping,
|
||||
payments: paymentsStore,
|
||||
methods: methodsStore,
|
||||
quotes: quotesRepoStore.Quotes(),
|
||||
routes: routesStore,
|
||||
plans: plansStore,
|
||||
@@ -118,8 +170,13 @@ func (s *Store) Payments() storage.PaymentsStore {
|
||||
return s.payments
|
||||
}
|
||||
|
||||
// PaymentMethods returns the payment-methods store.
|
||||
func (s *Store) PaymentMethods() storage.PaymentMethodsStore {
|
||||
return s.methods
|
||||
}
|
||||
|
||||
// Quotes returns the quotes store.
|
||||
func (s *Store) Quotes() storage.QuotesStore {
|
||||
func (s *Store) Quotes() quotestorage.QuotesStore {
|
||||
return s.quotes
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user