Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
feat: add method to call database once for multiple transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
wregulski committed Nov 23, 2023
1 parent be054eb commit 25b824c
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
33 changes: 33 additions & 0 deletions action_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,25 @@ func (c *Client) GetTransactionByID(ctx context.Context, txID string) (*Transact
return c.GetTransaction(ctx, "", txID)
}

func (c *Client) GetTransactionsByIDs(ctx context.Context, txIDs []string) ([]*Transaction, error) {
// Check for existing NewRelic transaction
ctx = c.GetOrStartTxn(ctx, "get_transactions_by_ids")

// Create the conditions
conditions := generateTxIdFilterConditions(txIDs)

// Get the transactions by it's IDs
transactions, err := getTransactions(
ctx, nil, conditions, nil,
c.DefaultModelOptions()...,
)
if err != nil {
return nil, err
}

return transactions, nil
}

// GetTransactionByHex will get a transaction from the Datastore by its full hex string
// uses GetTransaction
func (c *Client) GetTransactionByHex(ctx context.Context, hex string) (*Transaction, error) {
Expand Down Expand Up @@ -492,3 +511,17 @@ func (c *Client) RevertTransaction(ctx context.Context, id string) error {

return err
}

func generateTxIdFilterConditions(txIDs []string) *map[string]interface{} {
orConditions := make([]map[string]interface{}, len(txIDs))

for i, txID := range txIDs {
orConditions[i] = map[string]interface{}{"id": txID}
}

conditions := &map[string]interface{}{
"$or": orConditions,
}

return conditions
}
10 changes: 10 additions & 0 deletions beef_tx_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,13 @@ func (m *MockTransactionStore) GetTransactionByID(ctx context.Context, txID stri
}
return nil, fmt.Errorf("no records found")
}

func (m *MockTransactionStore) GetTransactionsByIDs(ctx context.Context, txIDs []string) ([]*Transaction, error) {
var txs []*Transaction
for _, txID := range txIDs {
if tx, exists := m.Transactions[txID]; exists {
txs = append(txs, tx)
}
}
return txs, nil
}
1 change: 1 addition & 0 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ type PaymailService interface {
type TransactionService interface {
GetTransaction(ctx context.Context, xPubID, txID string) (*Transaction, error)
GetTransactionByID(ctx context.Context, txID string) (*Transaction, error)
GetTransactionsByIDs(ctx context.Context, txIDs []string) ([]*Transaction, error)
GetTransactionByHex(ctx context.Context, hex string) (*Transaction, error)
GetTransactions(ctx context.Context, metadata *Metadata, conditions *map[string]interface{},
queryParams *datastore.QueryParams, opts ...ModelOps) ([]*Transaction, error)
Expand Down
1 change: 1 addition & 0 deletions model_transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ type Transaction struct {

type TransactionGetter interface {
GetTransactionByID(ctx context.Context, txID string) (*Transaction, error)
GetTransactionsByIDs(ctx context.Context, txIDs []string) ([]*Transaction, error)
}

// newTransactionBase creates the standard transaction model base
Expand Down

0 comments on commit 25b824c

Please sign in to comment.