separated quotation and payments
This commit is contained in:
136
api/payments/storage/mongo/repository.go
Normal file
136
api/payments/storage/mongo/repository.go
Normal file
@@ -0,0 +1,136 @@
|
||||
package mongo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/tech/sendico/payments/storage"
|
||||
"github.com/tech/sendico/payments/storage/model"
|
||||
"github.com/tech/sendico/payments/storage/mongo/store"
|
||||
quotemongo "github.com/tech/sendico/payments/storage/quote/mongo"
|
||||
"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"
|
||||
)
|
||||
|
||||
// Store implements storage.Repository backed by MongoDB.
|
||||
type Store struct {
|
||||
logger mlogger.Logger
|
||||
ping func(context.Context) error
|
||||
|
||||
payments storage.PaymentsStore
|
||||
quotes storage.QuotesStore
|
||||
routes storage.RoutesStore
|
||||
plans storage.PlanTemplatesStore
|
||||
}
|
||||
|
||||
type options struct {
|
||||
quoteRetention time.Duration
|
||||
}
|
||||
|
||||
// Option configures the Mongo-backed payments repository.
|
||||
type Option func(*options)
|
||||
|
||||
// WithQuoteRetention sets how long payment quote records are retained after expiry.
|
||||
func WithQuoteRetention(retention time.Duration) Option {
|
||||
return func(opts *options) {
|
||||
opts.quoteRetention = retention
|
||||
}
|
||||
}
|
||||
|
||||
// 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 {
|
||||
return nil, merrors.InvalidArgument("payments.storage.mongo: connection is nil")
|
||||
}
|
||||
paymentsRepo := repository.CreateMongoRepository(conn.Database(), (&model.Payment{}).Collection())
|
||||
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...)
|
||||
}
|
||||
|
||||
// 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) {
|
||||
if ping == nil {
|
||||
return nil, merrors.InvalidArgument("payments.storage.mongo: ping func is nil")
|
||||
}
|
||||
if paymentsRepo == nil {
|
||||
return nil, merrors.InvalidArgument("payments.storage.mongo: payments repository is nil")
|
||||
}
|
||||
if quotesRepo == nil {
|
||||
return nil, merrors.InvalidArgument("payments.storage.mongo: quotes repository is nil")
|
||||
}
|
||||
if routesRepo == nil {
|
||||
return nil, merrors.InvalidArgument("payments.storage.mongo: routes repository is nil")
|
||||
}
|
||||
if plansRepo == nil {
|
||||
return nil, merrors.InvalidArgument("payments.storage.mongo: plan templates repository is nil")
|
||||
}
|
||||
|
||||
cfg := options{}
|
||||
for _, opt := range opts {
|
||||
if opt != nil {
|
||||
opt(&cfg)
|
||||
}
|
||||
}
|
||||
|
||||
childLogger := logger.Named("storage").Named("mongo")
|
||||
paymentsStore, err := store.NewPayments(childLogger, paymentsRepo)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
quotesRepoStore, err := quotemongo.NewWithRepository(childLogger, ping, quotesRepo, quotemongo.WithQuoteRetention(cfg.quoteRetention))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
routesStore, err := store.NewRoutes(childLogger, routesRepo)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
plansStore, err := store.NewPlanTemplates(childLogger, plansRepo)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := &Store{
|
||||
logger: childLogger,
|
||||
ping: ping,
|
||||
payments: paymentsStore,
|
||||
quotes: quotesRepoStore.Quotes(),
|
||||
routes: routesStore,
|
||||
plans: plansStore,
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// Ping verifies connectivity with the backing database.
|
||||
func (s *Store) Ping(ctx context.Context) error {
|
||||
if s.ping == nil {
|
||||
return merrors.InvalidArgument("payments.storage.mongo: ping func is nil")
|
||||
}
|
||||
return s.ping(ctx)
|
||||
}
|
||||
|
||||
// Payments returns the payments store.
|
||||
func (s *Store) Payments() storage.PaymentsStore {
|
||||
return s.payments
|
||||
}
|
||||
|
||||
// Quotes returns the quotes store.
|
||||
func (s *Store) Quotes() storage.QuotesStore {
|
||||
return s.quotes
|
||||
}
|
||||
|
||||
// Routes returns the routing store.
|
||||
func (s *Store) Routes() storage.RoutesStore {
|
||||
return s.routes
|
||||
}
|
||||
|
||||
// PlanTemplates returns the plan templates store.
|
||||
func (s *Store) PlanTemplates() storage.PlanTemplatesStore {
|
||||
return s.plans
|
||||
}
|
||||
|
||||
var _ storage.Repository = (*Store)(nil)
|
||||
Reference in New Issue
Block a user