- legacy payment template fee lines picking
This commit is contained in:
@@ -1,95 +0,0 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/tech/sendico/pkg/db/storable"
|
||||
"github.com/tech/sendico/pkg/model/account_role"
|
||||
"github.com/tech/sendico/pkg/mservice"
|
||||
)
|
||||
|
||||
// OrchestrationStep defines a template step for execution planning.
|
||||
type OrchestrationStep struct {
|
||||
StepID string `bson:"stepId" json:"stepId"`
|
||||
Rail Rail `bson:"rail" json:"rail"`
|
||||
Operation string `bson:"operation" json:"operation"`
|
||||
ReportVisibility ReportVisibility `bson:"reportVisibility,omitempty" json:"reportVisibility,omitempty"`
|
||||
DependsOn []string `bson:"dependsOn,omitempty" json:"dependsOn,omitempty"`
|
||||
CommitPolicy CommitPolicy `bson:"commitPolicy,omitempty" json:"commitPolicy,omitempty"`
|
||||
CommitAfter []string `bson:"commitAfter,omitempty" json:"commitAfter,omitempty"`
|
||||
FromRole *account_role.AccountRole `bson:"fromRole,omitempty" json:"fromRole,omitempty"`
|
||||
ToRole *account_role.AccountRole `bson:"toRole,omitempty" json:"toRole,omitempty"`
|
||||
}
|
||||
|
||||
// PaymentPlanTemplate stores reusable orchestration templates.
|
||||
type PaymentPlanTemplate struct {
|
||||
storable.Base `bson:",inline" json:",inline"`
|
||||
|
||||
FromRail Rail `bson:"fromRail" json:"fromRail"`
|
||||
ToRail Rail `bson:"toRail" json:"toRail"`
|
||||
Network string `bson:"network,omitempty" json:"network,omitempty"`
|
||||
Steps []OrchestrationStep `bson:"steps,omitempty" json:"steps,omitempty"`
|
||||
IsEnabled bool `bson:"isEnabled" json:"isEnabled"`
|
||||
}
|
||||
|
||||
// Collection implements storable.Storable.
|
||||
func (*PaymentPlanTemplate) Collection() string {
|
||||
return mservice.PaymentPlanTemplates
|
||||
}
|
||||
|
||||
// Normalize standardizes template fields for matching and indexing.
|
||||
func (t *PaymentPlanTemplate) Normalize() {
|
||||
if t == nil {
|
||||
return
|
||||
}
|
||||
t.FromRail = normalizeRail(t.FromRail)
|
||||
t.ToRail = normalizeRail(t.ToRail)
|
||||
t.Network = strings.ToUpper(strings.TrimSpace(t.Network))
|
||||
if len(t.Steps) == 0 {
|
||||
return
|
||||
}
|
||||
for i := range t.Steps {
|
||||
step := &t.Steps[i]
|
||||
step.StepID = strings.TrimSpace(step.StepID)
|
||||
step.Rail = normalizeRail(step.Rail)
|
||||
step.Operation = strings.ToLower(strings.TrimSpace(step.Operation))
|
||||
step.ReportVisibility = NormalizeReportVisibility(step.ReportVisibility)
|
||||
step.CommitPolicy = normalizeCommitPolicy(step.CommitPolicy)
|
||||
step.DependsOn = normalizeStringList(step.DependsOn)
|
||||
step.CommitAfter = normalizeStringList(step.CommitAfter)
|
||||
step.FromRole = normalizeAccountRole(step.FromRole)
|
||||
step.ToRole = normalizeAccountRole(step.ToRole)
|
||||
}
|
||||
}
|
||||
|
||||
func normalizeAccountRole(role *account_role.AccountRole) *account_role.AccountRole {
|
||||
if role == nil {
|
||||
return nil
|
||||
}
|
||||
trimmed := strings.TrimSpace(string(*role))
|
||||
if trimmed == "" {
|
||||
return nil
|
||||
}
|
||||
if parsed, ok := account_role.Parse(trimmed); ok {
|
||||
if parsed == "" {
|
||||
return nil
|
||||
}
|
||||
normalized := parsed
|
||||
return &normalized
|
||||
}
|
||||
normalized := account_role.AccountRole(strings.ToLower(trimmed))
|
||||
return &normalized
|
||||
}
|
||||
|
||||
// PaymentPlanTemplateFilter selects templates for lookup.
|
||||
type PaymentPlanTemplateFilter struct {
|
||||
FromRail Rail
|
||||
ToRail Rail
|
||||
Network string
|
||||
IsEnabled *bool
|
||||
}
|
||||
|
||||
// PaymentPlanTemplateList holds template results.
|
||||
type PaymentPlanTemplateList struct {
|
||||
Items []*PaymentPlanTemplate
|
||||
}
|
||||
@@ -29,7 +29,6 @@ type Store struct {
|
||||
methods storage.PaymentMethodsStore
|
||||
quotes quotestorage.QuotesStore
|
||||
routes storage.RoutesStore
|
||||
plans storage.PlanTemplatesStore
|
||||
}
|
||||
|
||||
type paymentMethodsConfig struct {
|
||||
@@ -70,22 +69,21 @@ func New(logger mlogger.Logger, conn *db.MongoConnection, opts ...Option) (*Stor
|
||||
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())
|
||||
methodsRepo := repository.CreateMongoRepository(conn.Database(), mservice.PaymentMethods)
|
||||
|
||||
return newWithRepository(logger, conn.Ping, conn.Database(), paymentsRepo, methodsRepo, quotesRepo, routesRepo, plansRepo, opts...)
|
||||
return newWithRepository(logger, conn.Ping, conn.Database(), paymentsRepo, methodsRepo, quotesRepo, routesRepo, 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, nil, paymentsRepo, nil, quotesRepo, routesRepo, plansRepo, opts...)
|
||||
func NewWithRepository(logger mlogger.Logger, ping func(context.Context) error, paymentsRepo repository.Repository, quotesRepo repository.Repository, routesRepo repository.Repository, opts ...Option) (*Store, error) {
|
||||
return newWithRepository(logger, ping, nil, paymentsRepo, nil, quotesRepo, routesRepo, opts...)
|
||||
}
|
||||
|
||||
func newWithRepository(
|
||||
logger mlogger.Logger,
|
||||
ping func(context.Context) error,
|
||||
database *mongo.Database,
|
||||
paymentsRepo, methodsRepo, quotesRepo, routesRepo, plansRepo repository.Repository,
|
||||
paymentsRepo, methodsRepo, quotesRepo, routesRepo repository.Repository,
|
||||
opts ...Option,
|
||||
) (*Store, error) {
|
||||
if ping == nil {
|
||||
@@ -100,9 +98,6 @@ func newWithRepository(
|
||||
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 {
|
||||
@@ -124,10 +119,6 @@ func newWithRepository(
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
plansStore, err := store.NewPlanTemplates(childLogger, plansRepo)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var methodsStore storage.PaymentMethodsStore
|
||||
if cfg.paymentMethodsAuth != nil {
|
||||
@@ -155,7 +146,6 @@ func newWithRepository(
|
||||
methods: methodsStore,
|
||||
quotes: quotesRepoStore.Quotes(),
|
||||
routes: routesStore,
|
||||
plans: plansStore,
|
||||
}
|
||||
|
||||
return result, nil
|
||||
@@ -189,11 +179,6 @@ func (s *Store) Routes() storage.RoutesStore {
|
||||
return s.routes
|
||||
}
|
||||
|
||||
// PlanTemplates returns the plan templates store.
|
||||
func (s *Store) PlanTemplates() storage.PlanTemplatesStore {
|
||||
return s.plans
|
||||
}
|
||||
|
||||
// MongoDatabase returns underlying Mongo database when available.
|
||||
func (s *Store) MongoDatabase() *mongo.Database {
|
||||
if s == nil {
|
||||
|
||||
@@ -1,174 +0,0 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"github.com/tech/sendico/payments/storage"
|
||||
"github.com/tech/sendico/payments/storage/model"
|
||||
"github.com/tech/sendico/pkg/db/repository"
|
||||
ri "github.com/tech/sendico/pkg/db/repository/index"
|
||||
"github.com/tech/sendico/pkg/merrors"
|
||||
"github.com/tech/sendico/pkg/mlogger"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type PlanTemplates struct {
|
||||
logger mlogger.Logger
|
||||
repo repository.Repository
|
||||
}
|
||||
|
||||
// NewPlanTemplates constructs a Mongo-backed plan template store.
|
||||
func NewPlanTemplates(logger mlogger.Logger, repo repository.Repository) (*PlanTemplates, error) {
|
||||
if repo == nil {
|
||||
return nil, merrors.InvalidArgument("planTemplatesStore: repository is nil")
|
||||
}
|
||||
|
||||
indexes := []*ri.Definition{
|
||||
{
|
||||
Keys: []ri.Key{
|
||||
{Field: "fromRail", Sort: ri.Asc},
|
||||
{Field: "toRail", Sort: ri.Asc},
|
||||
{Field: "network", Sort: ri.Asc},
|
||||
},
|
||||
Unique: true,
|
||||
},
|
||||
{
|
||||
Keys: []ri.Key{{Field: "fromRail", Sort: ri.Asc}},
|
||||
},
|
||||
{
|
||||
Keys: []ri.Key{{Field: "toRail", Sort: ri.Asc}},
|
||||
},
|
||||
{
|
||||
Keys: []ri.Key{{Field: "isEnabled", Sort: ri.Asc}},
|
||||
},
|
||||
}
|
||||
|
||||
for _, def := range indexes {
|
||||
if err := repo.CreateIndex(def); err != nil {
|
||||
logger.Error("Failed to ensure plan templates index", zap.Error(err), zap.String("collection", repo.Collection()))
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return &PlanTemplates{
|
||||
logger: logger.Named("plan_templates"),
|
||||
repo: repo,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (p *PlanTemplates) Create(ctx context.Context, template *model.PaymentPlanTemplate) error {
|
||||
if template == nil {
|
||||
return merrors.InvalidArgument("planTemplatesStore: nil template")
|
||||
}
|
||||
template.Normalize()
|
||||
if template.FromRail == "" || template.FromRail == model.RailUnspecified {
|
||||
return merrors.InvalidArgument("planTemplatesStore: from_rail is required")
|
||||
}
|
||||
if template.ToRail == "" || template.ToRail == model.RailUnspecified {
|
||||
return merrors.InvalidArgument("planTemplatesStore: to_rail is required")
|
||||
}
|
||||
if len(template.Steps) == 0 {
|
||||
return merrors.InvalidArgument("planTemplatesStore: steps are required")
|
||||
}
|
||||
if template.ID.IsZero() {
|
||||
template.SetID(bson.NewObjectID())
|
||||
} else {
|
||||
template.Update()
|
||||
}
|
||||
|
||||
filter := repository.Filter("fromRail", template.FromRail).And(
|
||||
repository.Filter("toRail", template.ToRail),
|
||||
repository.Filter("network", template.Network),
|
||||
)
|
||||
|
||||
if err := p.repo.Insert(ctx, template, filter); err != nil {
|
||||
if errors.Is(err, merrors.ErrDataConflict) {
|
||||
return storage.ErrDuplicatePlanTemplate
|
||||
}
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *PlanTemplates) Update(ctx context.Context, template *model.PaymentPlanTemplate) error {
|
||||
if template == nil {
|
||||
return merrors.InvalidArgument("planTemplatesStore: nil template")
|
||||
}
|
||||
if template.ID.IsZero() {
|
||||
return merrors.InvalidArgument("planTemplatesStore: missing template id")
|
||||
}
|
||||
template.Normalize()
|
||||
template.Update()
|
||||
if err := p.repo.Update(ctx, template); err != nil {
|
||||
if errors.Is(err, merrors.ErrNoData) {
|
||||
return storage.ErrPlanTemplateNotFound
|
||||
}
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *PlanTemplates) GetByID(ctx context.Context, id bson.ObjectID) (*model.PaymentPlanTemplate, error) {
|
||||
if id == bson.NilObjectID {
|
||||
return nil, merrors.InvalidArgument("planTemplatesStore: template id is required")
|
||||
}
|
||||
entity := &model.PaymentPlanTemplate{}
|
||||
if err := p.repo.Get(ctx, id, entity); err != nil {
|
||||
if errors.Is(err, merrors.ErrNoData) {
|
||||
return nil, storage.ErrPlanTemplateNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
entity.Normalize()
|
||||
return entity, nil
|
||||
}
|
||||
|
||||
func (p *PlanTemplates) List(ctx context.Context, filter *model.PaymentPlanTemplateFilter) (*model.PaymentPlanTemplateList, error) {
|
||||
if filter == nil {
|
||||
filter = &model.PaymentPlanTemplateFilter{}
|
||||
}
|
||||
|
||||
query := repository.Query()
|
||||
|
||||
if from := normalizedRailFilterValues(filter.FromRail); len(from) == 1 {
|
||||
query = query.Filter(repository.Field("fromRail"), from[0])
|
||||
} else if len(from) > 1 {
|
||||
query = query.In(repository.Field("fromRail"), stringSliceToAny(from)...)
|
||||
}
|
||||
if to := normalizedRailFilterValues(filter.ToRail); len(to) == 1 {
|
||||
query = query.Filter(repository.Field("toRail"), to[0])
|
||||
} else if len(to) > 1 {
|
||||
query = query.In(repository.Field("toRail"), stringSliceToAny(to)...)
|
||||
}
|
||||
if network := strings.ToUpper(strings.TrimSpace(filter.Network)); network != "" {
|
||||
query = query.Filter(repository.Field("network"), network)
|
||||
}
|
||||
if filter.IsEnabled != nil {
|
||||
query = query.Filter(repository.Field("isEnabled"), *filter.IsEnabled)
|
||||
}
|
||||
|
||||
templates := make([]*model.PaymentPlanTemplate, 0)
|
||||
decoder := func(cur *mongo.Cursor) error {
|
||||
item := &model.PaymentPlanTemplate{}
|
||||
if err := cur.Decode(item); err != nil {
|
||||
return err
|
||||
}
|
||||
item.Normalize()
|
||||
templates = append(templates, item)
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := p.repo.FindManyByFilter(ctx, query, decoder); err != nil && !errors.Is(err, merrors.ErrNoData) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &model.PaymentPlanTemplateList{
|
||||
Items: templates,
|
||||
}, nil
|
||||
}
|
||||
|
||||
var _ storage.PlanTemplatesStore = (*PlanTemplates)(nil)
|
||||
@@ -19,10 +19,6 @@ var (
|
||||
ErrRouteNotFound = errors.New("payments.storage: route not found")
|
||||
// ErrDuplicateRoute signals that a route already exists for the same transition.
|
||||
ErrDuplicateRoute = errors.New("payments.storage: duplicate route")
|
||||
// ErrPlanTemplateNotFound signals that a plan template record does not exist.
|
||||
ErrPlanTemplateNotFound = errors.New("payments.storage: plan template not found")
|
||||
// ErrDuplicatePlanTemplate signals that a plan template already exists for the same transition.
|
||||
ErrDuplicatePlanTemplate = errors.New("payments.storage: duplicate plan template")
|
||||
)
|
||||
|
||||
// Repository exposes persistence primitives for the payments domain.
|
||||
@@ -32,7 +28,6 @@ type Repository interface {
|
||||
PaymentMethods() PaymentMethodsStore
|
||||
Quotes() quotestorage.QuotesStore
|
||||
Routes() RoutesStore
|
||||
PlanTemplates() PlanTemplatesStore
|
||||
}
|
||||
|
||||
// PaymentsStore manages payment lifecycle state.
|
||||
@@ -68,11 +63,3 @@ type RoutesStore interface {
|
||||
GetByID(ctx context.Context, id bson.ObjectID) (*model.PaymentRoute, error)
|
||||
List(ctx context.Context, filter *model.PaymentRouteFilter) (*model.PaymentRouteList, error)
|
||||
}
|
||||
|
||||
// PlanTemplatesStore manages orchestration plan templates.
|
||||
type PlanTemplatesStore interface {
|
||||
Create(ctx context.Context, template *model.PaymentPlanTemplate) error
|
||||
Update(ctx context.Context, template *model.PaymentPlanTemplate) error
|
||||
GetByID(ctx context.Context, id bson.ObjectID) (*model.PaymentPlanTemplate, error)
|
||||
List(ctx context.Context, filter *model.PaymentPlanTemplateFilter) (*model.PaymentPlanTemplateList, error)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user