new payment methods service
This commit is contained in:
@@ -7,7 +7,6 @@ import (
|
||||
mongoimpl "github.com/tech/sendico/pkg/db/internal/mongo"
|
||||
"github.com/tech/sendico/pkg/db/invitation"
|
||||
"github.com/tech/sendico/pkg/db/organization"
|
||||
"github.com/tech/sendico/pkg/db/paymethod"
|
||||
"github.com/tech/sendico/pkg/db/policy"
|
||||
"github.com/tech/sendico/pkg/db/recipient"
|
||||
"github.com/tech/sendico/pkg/db/refreshtokens"
|
||||
@@ -28,7 +27,6 @@ type Factory interface {
|
||||
NewOrganizationDB() (organization.DB, error)
|
||||
NewInvitationsDB() (invitation.DB, error)
|
||||
NewRecipientsDB() (recipient.DB, error)
|
||||
NewPaymentMethodsDB() (paymethod.DB, error)
|
||||
NewVerificationsDB() (verification.DB, error)
|
||||
|
||||
NewRolesDB() (role.DB, error)
|
||||
|
||||
@@ -15,7 +15,6 @@ import (
|
||||
"github.com/tech/sendico/pkg/db/internal/mongo/chainassetsdb"
|
||||
"github.com/tech/sendico/pkg/db/internal/mongo/invitationdb"
|
||||
"github.com/tech/sendico/pkg/db/internal/mongo/organizationdb"
|
||||
"github.com/tech/sendico/pkg/db/internal/mongo/paymethoddb"
|
||||
"github.com/tech/sendico/pkg/db/internal/mongo/policiesdb"
|
||||
"github.com/tech/sendico/pkg/db/internal/mongo/recipientdb"
|
||||
"github.com/tech/sendico/pkg/db/internal/mongo/refreshtokensdb"
|
||||
@@ -24,7 +23,6 @@ import (
|
||||
"github.com/tech/sendico/pkg/db/internal/mongo/verificationimp"
|
||||
"github.com/tech/sendico/pkg/db/invitation"
|
||||
"github.com/tech/sendico/pkg/db/organization"
|
||||
"github.com/tech/sendico/pkg/db/paymethod"
|
||||
"github.com/tech/sendico/pkg/db/policy"
|
||||
"github.com/tech/sendico/pkg/db/recipient"
|
||||
"github.com/tech/sendico/pkg/db/refreshtokens"
|
||||
@@ -204,28 +202,18 @@ func (db *DB) NewOrganizationDB() (organization.DB, error) {
|
||||
}
|
||||
|
||||
func (db *DB) NewRecipientsDB() (recipient.DB, error) {
|
||||
pmdb, err := db.NewPaymentMethodsDB()
|
||||
if err != nil {
|
||||
db.logger.Warn("Failed to create payment methods database", zap.Error(err))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
create := func(ctx context.Context,
|
||||
logger mlogger.Logger,
|
||||
enforcer auth.Enforcer,
|
||||
pdb policy.DB,
|
||||
db *mongo.Database,
|
||||
) (recipient.DB, error) {
|
||||
return recipientdb.Create(ctx, logger, enforcer, pdb, pmdb, db)
|
||||
return recipientdb.Create(ctx, logger, enforcer, pdb, db)
|
||||
}
|
||||
|
||||
return newProtectedDB(db, create)
|
||||
}
|
||||
|
||||
func (db *DB) NewPaymentMethodsDB() (paymethod.DB, error) {
|
||||
return newProtectedDB(db, paymethoddb.Create)
|
||||
}
|
||||
|
||||
func (db *DB) NewRefreshTokensDB() (refreshtokens.DB, error) {
|
||||
return refreshtokensdb.Create(db.logger, db.db())
|
||||
}
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
package paymethoddb
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/tech/sendico/pkg/mutil/mzap"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func (db *PaymentMethodsDB) SetArchived(ctx context.Context, accountRef, organizationRef, objectRef bson.ObjectID, isArchived, cascade bool) error {
|
||||
// Use the ArchivableDB for the main archiving logic
|
||||
if err := db.ArchivableDB.SetArchived(ctx, accountRef, objectRef, isArchived); err != nil {
|
||||
db.DBImp.Logger.Warn("Failed to chnage object archive status", zap.Error(err),
|
||||
mzap.AccRef(accountRef), mzap.ObjRef("organization_ref", organizationRef),
|
||||
mzap.ObjRef("object_ref", objectRef), zap.Bool("archived", isArchived), zap.Bool("cascade", cascade))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1,59 +0,0 @@
|
||||
package paymethoddb
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/tech/sendico/pkg/auth"
|
||||
"github.com/tech/sendico/pkg/db/policy"
|
||||
ri "github.com/tech/sendico/pkg/db/repository/index"
|
||||
"github.com/tech/sendico/pkg/mlogger"
|
||||
"github.com/tech/sendico/pkg/model"
|
||||
"github.com/tech/sendico/pkg/mservice"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type PaymentMethodsDB struct {
|
||||
auth.ProtectedDBImp[*model.PaymentMethod]
|
||||
auth.ArchivableDB[*model.PaymentMethod]
|
||||
}
|
||||
|
||||
func Create(ctx context.Context,
|
||||
logger mlogger.Logger,
|
||||
enforcer auth.Enforcer,
|
||||
pdb policy.DB,
|
||||
db *mongo.Database,
|
||||
) (*PaymentMethodsDB, error) {
|
||||
p, err := auth.CreateDBImp[*model.PaymentMethod](ctx, logger, pdb, enforcer, mservice.PaymentMethods, db)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
createEmpty := func() *model.PaymentMethod {
|
||||
return &model.PaymentMethod{}
|
||||
}
|
||||
|
||||
getArchivable := func(c *model.PaymentMethod) model.Archivable {
|
||||
return &c.ArchivableBase
|
||||
}
|
||||
|
||||
res := &PaymentMethodsDB{
|
||||
ProtectedDBImp: *p,
|
||||
ArchivableDB: auth.NewArchivableDB(
|
||||
p.DBImp,
|
||||
logger,
|
||||
p.Enforcer,
|
||||
createEmpty,
|
||||
getArchivable,
|
||||
),
|
||||
}
|
||||
|
||||
if err := res.DBImp.Repository.CreateIndex(&ri.Definition{
|
||||
Keys: []ri.Key{{Field: "recipientRef", Sort: ri.Asc}},
|
||||
}); err != nil {
|
||||
res.DBImp.Logger.Error("Failed to create recipientRef index for payment methods", zap.Error(err))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return res, nil
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
package paymethoddb
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/tech/sendico/pkg/db/repository"
|
||||
"github.com/tech/sendico/pkg/merrors"
|
||||
"github.com/tech/sendico/pkg/model"
|
||||
mauth "github.com/tech/sendico/pkg/mutil/db/auth"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
)
|
||||
|
||||
func (db *PaymentMethodsDB) List(ctx context.Context, accountRef, organizationRef, recipientRef bson.ObjectID, cursor *model.ViewCursor) ([]model.PaymentMethod, error) {
|
||||
res, err := mauth.GetProtectedObjects[model.PaymentMethod](
|
||||
ctx,
|
||||
db.DBImp.Logger,
|
||||
accountRef, organizationRef, model.ActionRead,
|
||||
repository.OrgFilter(organizationRef).And(repository.Filter("recipientRef", recipientRef)),
|
||||
cursor,
|
||||
db.Enforcer,
|
||||
db.DBImp.Repository,
|
||||
)
|
||||
if errors.Is(err, merrors.ErrNoData) {
|
||||
return []model.PaymentMethod{}, nil
|
||||
}
|
||||
return res, err
|
||||
}
|
||||
@@ -2,16 +2,13 @@ package recipientdb
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/tech/sendico/pkg/merrors"
|
||||
"github.com/tech/sendico/pkg/mutil/mzap"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func (db *RecipientDB) SetArchived(ctx context.Context, accountRef, organizationRef, objectRef bson.ObjectID, isArchived, cascade bool) error {
|
||||
// Use the ArchivableDB for the main archiving logic
|
||||
if err := db.ArchivableDB.SetArchived(ctx, accountRef, objectRef, isArchived); err != nil {
|
||||
db.DBImp.Logger.Warn("Failed to change recipient archive status", zap.Error(err),
|
||||
mzap.AccRef(accountRef), mzap.ObjRef("organization_ref", organizationRef),
|
||||
@@ -19,39 +16,5 @@ func (db *RecipientDB) SetArchived(ctx context.Context, accountRef, organization
|
||||
return err
|
||||
}
|
||||
|
||||
if cascade {
|
||||
if err := db.setArchivedPaymentMethods(ctx, accountRef, organizationRef, objectRef, isArchived); err != nil {
|
||||
db.DBImp.Logger.Warn("Failed to update payment methods archive status", zap.Error(err),
|
||||
mzap.AccRef(accountRef), mzap.ObjRef("organization_ref", organizationRef),
|
||||
mzap.ObjRef("recipient_ref", objectRef), zap.Bool("archived", isArchived), zap.Bool("cascade", cascade))
|
||||
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *RecipientDB) setArchivedPaymentMethods(ctx context.Context, accountRef, organizationRef, recipientRef bson.ObjectID, archived bool) error {
|
||||
db.DBImp.Logger.Debug("Setting archived status for recipient payment methods", mzap.ObjRef("recipient_ref", recipientRef), zap.Bool("archived", archived))
|
||||
|
||||
db.DBImp.Logger.Debug("Applying archived status to payment methods for recipient", mzap.ObjRef("recipient_ref", recipientRef))
|
||||
|
||||
// Get all payMethods for the recipient
|
||||
payMethods, err := db.pmdb.List(ctx, accountRef, organizationRef, recipientRef, nil)
|
||||
if err != nil && !errors.Is(err, merrors.ErrNoData) {
|
||||
db.DBImp.Logger.Warn("Failed to fetch payment methods for recipient", zap.Error(err), mzap.ObjRef("recipient_ref", recipientRef))
|
||||
return err
|
||||
}
|
||||
|
||||
// Archive each payment method
|
||||
for _, pmethod := range payMethods {
|
||||
if err := db.pmdb.SetArchived(ctx, accountRef, organizationRef, pmethod.ID, archived, true); err != nil {
|
||||
db.DBImp.Logger.Warn("Failed to set archived status for payment method", zap.Error(err), mzap.ObjRef("payment_method_ref", pmethod.ID))
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
db.DBImp.Logger.Debug("Successfully updated payment methods archived status", zap.Int("count", len(payMethods)), mzap.ObjRef("recipient_ref", recipientRef))
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
|
||||
"github.com/tech/sendico/pkg/auth"
|
||||
"github.com/tech/sendico/pkg/db/paymethod"
|
||||
"github.com/tech/sendico/pkg/db/policy"
|
||||
"github.com/tech/sendico/pkg/mlogger"
|
||||
"github.com/tech/sendico/pkg/model"
|
||||
@@ -15,14 +14,12 @@ import (
|
||||
type RecipientDB struct {
|
||||
auth.ProtectedDBImp[*model.Recipient]
|
||||
auth.ArchivableDB[*model.Recipient]
|
||||
pmdb paymethod.DB
|
||||
}
|
||||
|
||||
func Create(ctx context.Context,
|
||||
logger mlogger.Logger,
|
||||
enforcer auth.Enforcer,
|
||||
pdb policy.DB,
|
||||
pmdb paymethod.DB,
|
||||
db *mongo.Database,
|
||||
) (*RecipientDB, error) {
|
||||
p, err := auth.CreateDBImp[*model.Recipient](ctx, logger, pdb, enforcer, mservice.Recipients, db)
|
||||
@@ -47,7 +44,6 @@ func Create(ctx context.Context,
|
||||
createEmpty,
|
||||
getArchivable,
|
||||
),
|
||||
pmdb: pmdb,
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
package paymethod
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/tech/sendico/pkg/auth"
|
||||
"github.com/tech/sendico/pkg/model"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
)
|
||||
|
||||
type DB interface {
|
||||
auth.ProtectedDB[*model.PaymentMethod]
|
||||
SetArchived(ctx context.Context, accountRef, organizationRef, methodRef bson.ObjectID, archived, cascade bool) error
|
||||
List(ctx context.Context, accountRef, organizationRef, recipientRef bson.ObjectID, cursor *model.ViewCursor) ([]model.PaymentMethod, error)
|
||||
}
|
||||
Reference in New Issue
Block a user