generated from mrz1836/go-template
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat/spv 1119/add a search option by xpubid for admin only (#757)
Co-authored-by: Augustyn Chmiel <augustyn.chmiel@4chain.studio>
- Loading branch information
Showing
5 changed files
with
111 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package admin | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/bitcoin-sv/spv-wallet/actions/common" | ||
"github.com/bitcoin-sv/spv-wallet/engine" | ||
"github.com/bitcoin-sv/spv-wallet/engine/datastore" | ||
"github.com/bitcoin-sv/spv-wallet/mappings" | ||
"github.com/bitcoin-sv/spv-wallet/models/filter" | ||
"github.com/bitcoin-sv/spv-wallet/models/response" | ||
"github.com/bitcoin-sv/spv-wallet/server/reqctx" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
// Helper function to prepare transaction query parameters | ||
func prepareQueryParams(c *gin.Context, searchParams *filter.SearchParams[filter.AdminTransactionFilter]) *transactionQueryParams { | ||
return &transactionQueryParams{ | ||
Context: c.Request.Context(), | ||
XPubID: searchParams.Conditions.XPubID, | ||
Metadata: mappings.MapToMetadata(searchParams.Metadata), | ||
Conditions: searchParams.Conditions.ToDbConditions(), | ||
PageOptions: mappings.MapToDbQueryParams(&searchParams.Page), | ||
} | ||
} | ||
|
||
// Helper function to fetch transactions based on XPubID presence | ||
func fetchTransactions(c *gin.Context, params *transactionQueryParams) ([]*engine.Transaction, error) { | ||
if params.XPubID != nil { | ||
transactions, err := reqctx.Engine(c).GetTransactionsByXpubID(params.Context, *params.XPubID, params.Metadata, params.Conditions, params.PageOptions) | ||
if err != nil { | ||
return nil, fmt.Errorf("fetch transactions by XPubID failed: %w", err) | ||
} | ||
return transactions, nil | ||
} | ||
transactions, err := reqctx.Engine(c).GetTransactions(params.Context, params.Metadata, params.Conditions, params.PageOptions) | ||
if err != nil { | ||
return nil, fmt.Errorf("fetch transactions failed: %w", err) | ||
} | ||
return transactions, nil | ||
} | ||
|
||
// Helper function to count transactions based on XPubID presence | ||
func countTransactions(c *gin.Context, params *transactionQueryParams) (int64, error) { | ||
if params.XPubID != nil { | ||
count, err := reqctx.Engine(c).GetTransactionsByXpubIDCount(params.Context, *params.XPubID, params.Metadata, params.Conditions) | ||
if err != nil { | ||
return 0, fmt.Errorf("count transactions by XPubID failed: %w", err) | ||
} | ||
return count, nil | ||
} | ||
|
||
count, err := reqctx.Engine(c).GetTransactionsCount(params.Context, params.Metadata, params.Conditions) | ||
if err != nil { | ||
return 0, fmt.Errorf("count transactions failed: %w", err) | ||
} | ||
return count, nil | ||
} | ||
|
||
// Helper function to map transactions and send the response | ||
// sendPaginatedResponse sends a paginated response with any content type. | ||
func sendPaginatedResponse[T any, U any](c *gin.Context, content []*T, pageOptions *datastore.QueryParams, count int64, mapToContractFunc func(*T) *U) { | ||
contracts := common.MapToTypeContracts(content, mapToContractFunc) | ||
|
||
result := response.PageModel[U]{ | ||
Content: contracts, | ||
Page: common.GetPageDescriptionFromSearchParams(pageOptions, count), | ||
} | ||
|
||
c.JSON(http.StatusOK, result) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package filter | ||
|
||
// AdminTransactionFilter extends TransactionFilter for admin-specific use, including xpubid filtering | ||
type AdminTransactionFilter struct { | ||
//lint:ignore SA5008 We want to reuse json tags also to mapstructure. | ||
TransactionFilter `json:",inline,squash"` | ||
XPubID *string `json:"xpubid,omitempty" example:"623bc25ce1c0fc510dea72b5ee27b2e70384c099f1f3dce9e73dd987198c3486"` | ||
} | ||
|
||
// ToDbConditions converts filter fields to the datastore conditions for admin-specific queries | ||
func (f *AdminTransactionFilter) ToDbConditions() map[string]interface{} { | ||
conditions := f.TransactionFilter.ToDbConditions() | ||
|
||
return conditions | ||
} |