From 25b824ce30c544614f870ede898d74d4c6405fc9 Mon Sep 17 00:00:00 2001 From: wregulski Date: Thu, 23 Nov 2023 11:34:24 +0100 Subject: [PATCH] feat: add method to call database once for multiple transactions --- action_transaction.go | 33 +++++++++++++++++++++++++++++++++ beef_tx_mock.go | 10 ++++++++++ interface.go | 1 + model_transactions.go | 1 + 4 files changed, 45 insertions(+) diff --git a/action_transaction.go b/action_transaction.go index 218ca51a..67a204a3 100644 --- a/action_transaction.go +++ b/action_transaction.go @@ -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) { @@ -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 +} diff --git a/beef_tx_mock.go b/beef_tx_mock.go index 5aa5fe40..c036bc9b 100644 --- a/beef_tx_mock.go +++ b/beef_tx_mock.go @@ -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 +} diff --git a/interface.go b/interface.go index 2cffb355..d011d44d 100644 --- a/interface.go +++ b/interface.go @@ -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) diff --git a/model_transactions.go b/model_transactions.go index 2a330112..77cd1920 100644 --- a/model_transactions.go +++ b/model_transactions.go @@ -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