fixed operations idempotency
This commit is contained in:
@@ -19,6 +19,7 @@ const (
|
||||
payoutsCollection = "card_payouts"
|
||||
payoutIdemField = "idempotencyKey"
|
||||
payoutIdField = "paymentRef"
|
||||
payoutOpField = "operationRef"
|
||||
)
|
||||
|
||||
type Payouts struct {
|
||||
@@ -36,6 +37,16 @@ func NewPayouts(logger mlogger.Logger, db *mongo.Database) (*Payouts, error) {
|
||||
logger = logger.Named("payouts").With(zap.String("collection", payoutsCollection))
|
||||
|
||||
repo := repository.CreateMongoRepository(db, payoutsCollection)
|
||||
if err := repo.CreateIndex(&ri.Definition{
|
||||
Keys: []ri.Key{{Field: payoutOpField, Sort: ri.Asc}},
|
||||
Unique: true,
|
||||
Sparse: true,
|
||||
}); err != nil {
|
||||
logger.Error("Failed to create payouts operation index",
|
||||
zap.Error(err),
|
||||
zap.String("index_field", payoutOpField))
|
||||
return nil, err
|
||||
}
|
||||
if err := repo.CreateIndex(&ri.Definition{
|
||||
Keys: []ri.Key{{Field: payoutIdemField, Sort: ri.Asc}},
|
||||
Unique: true,
|
||||
@@ -63,6 +74,10 @@ func (p *Payouts) FindByIdempotencyKey(ctx context.Context, key string) (*model.
|
||||
return p.findOneByField(ctx, payoutIdemField, key)
|
||||
}
|
||||
|
||||
func (p *Payouts) FindByOperationRef(ctx context.Context, operationRef string) (*model.CardPayout, error) {
|
||||
return p.findOneByField(ctx, payoutOpField, operationRef)
|
||||
}
|
||||
|
||||
func (p *Payouts) FindByPaymentID(ctx context.Context, paymentID string) (*model.CardPayout, error) {
|
||||
return p.findOneByField(ctx, payoutIdField, paymentID)
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ type Repository interface {
|
||||
|
||||
type PayoutsStore interface {
|
||||
FindByIdempotencyKey(ctx context.Context, key string) (*model.CardPayout, error)
|
||||
FindByOperationRef(ctx context.Context, key string) (*model.CardPayout, error)
|
||||
FindByPaymentID(ctx context.Context, key string) (*model.CardPayout, error)
|
||||
Upsert(ctx context.Context, record *model.CardPayout) error
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user