Skip to content

Commit

Permalink
feat(SPV-729) filters (#569)
Browse files Browse the repository at this point in the history
* feat(SPV-729): filter for access_keys

* feat(SPV-729): just maps instead of *maps for conditions

* feat(SPV-729): filters for admin endpoints - partial

* feat(SPV-729): filters for UTXOs

* feat(SPV-729): filters for paymails

* feat(SPV-729): filters for xpubs

* feat(SPV-729): filter fileds with camelCase json names

* feat(SPV-729): linter

* feat(SPV-729): unnecessary commented-out code block

* feat(SPV-729): update a comment

* feat(SPV-729): some filter fileds only for admin

* feat(SPV-729): examples for the fields

* fix(SPV-729): paymail filter test

* feat(SPV-729): regenerated swagger

* feat(SPV-729): swagger descriptions and fixes

* feat(SPV-729): swagger after branch update
  • Loading branch information
chris-4chain authored May 10, 2024
1 parent 14f361a commit 1194719
Show file tree
Hide file tree
Showing 66 changed files with 3,952 additions and 1,065 deletions.
19 changes: 9 additions & 10 deletions actions/access_keys/count.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package accesskeys
import (
"net/http"

"github.com/bitcoin-sv/spv-wallet/actions"
"github.com/bitcoin-sv/spv-wallet/server/auth"
"github.com/gin-gonic/gin"
)
Expand All @@ -14,28 +13,28 @@ import (
// @Description Count of access keys
// @Tags Access-key
// @Produce json
// @Param CountRequestParameters body actions.CountRequestParameters false "Enables precise filtering of resource counts using custom conditions or metadata, catering to specific business or analysis needs"
// @Param CountAccessKeys body CountAccessKeys false "Enables filtering of elements to be counted"
// @Success 200 {number} int64 "Count of access keys"
// @Failure 400 "Bad request - Error while parsing CountRequestParameters from request body"
// @Failure 400 "Bad request - Error while parsing CountAccessKeys from request body"
// @Failure 500 "Internal Server Error - Error while fetching count of access keys"
// @Router /v1/access-key/count [post]
// @Security x-auth-xpub
func (a *Action) count(c *gin.Context) {
reqXPubID := c.GetString(auth.ParamXPubHashKey)

metadata, conditions, err := actions.GetCountQueryParameters(c)
if err != nil {
var reqParams CountAccessKeys
if err := c.Bind(&reqParams); err != nil {
c.JSON(http.StatusBadRequest, err.Error())
return
}

var count int64
if count, err = a.Services.SpvWalletEngine.GetAccessKeysByXPubIDCount(
count, err := a.Services.SpvWalletEngine.GetAccessKeysByXPubIDCount(
c.Request.Context(),
reqXPubID,
metadata,
conditions,
); err != nil {
reqParams.Metadata,
reqParams.Conditions.ToDbConditions(),
)
if err != nil {
c.JSON(http.StatusInternalServerError, err.Error())
return
}
Expand Down
12 changes: 11 additions & 1 deletion actions/access_keys/models.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
package accesskeys

import "github.com/bitcoin-sv/spv-wallet/engine"
import (
"github.com/bitcoin-sv/spv-wallet/actions/common"
"github.com/bitcoin-sv/spv-wallet/engine"
"github.com/bitcoin-sv/spv-wallet/models/filter"
)

// CreateAccessKey is the model for creating an access key
type CreateAccessKey struct {
// Accepts a JSON object for embedding custom metadata, enabling arbitrary additional information to be associated with the resource
Metadata engine.Metadata `json:"metadata" swaggertype:"object,string" example:"key:value,key2:value2"`
}

// SearchAccessKeys is a model for handling searching with filters and metadata
type SearchAccessKeys = common.SearchModel[filter.AccessKeyFilter]

// CountAccessKeys is a model for handling counting filtered access keys
type CountAccessKeys = common.ConditionsModel[filter.AccessKeyFilter]
28 changes: 10 additions & 18 deletions actions/access_keys/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package accesskeys
import (
"net/http"

"github.com/bitcoin-sv/spv-wallet/actions"
"github.com/bitcoin-sv/spv-wallet/engine"
"github.com/bitcoin-sv/spv-wallet/mappings"
"github.com/bitcoin-sv/spv-wallet/models"
"github.com/bitcoin-sv/spv-wallet/server/auth"
Expand All @@ -17,35 +15,29 @@ import (
// @Description Search access key
// @Tags Access-key
// @Produce json
// @Param SearchRequestParameters body actions.SearchRequestParameters false "Supports targeted resource searches with filters for metadata and custom conditions, plus options for pagination and sorting to streamline data exploration and analysis"
// @Param SearchAccessKeys body SearchAccessKeys false "Supports targeted resource searches with filters and metadata, plus options for pagination and sorting to streamline data exploration and analysis"
// @Success 200 {object} []models.AccessKey "List of access keys"
// @Failure 400 "Bad request - Error while parsing SearchRequestParameters from request body"
// @Failure 400 "Bad request - Error while SearchAccessKeys from request body"
// @Failure 500 "Internal server error - Error while searching for access keys"
// @Router /v1/access-key/search [post]
// @Security x-auth-xpub
func (a *Action) search(c *gin.Context) {
reqXPubID := c.GetString(auth.ParamXPubHashKey)

queryParams, metadata, conditions, err := actions.GetSearchQueryParameters(c)
if err != nil {
var reqParams SearchAccessKeys
if err := c.Bind(&reqParams); err != nil {
c.JSON(http.StatusBadRequest, err.Error())
return
}

dbConditions := make(map[string]interface{})
if conditions != nil {
dbConditions = *conditions
}
dbConditions["xpub_id"] = reqXPubID

var accessKeys []*engine.AccessKey
if accessKeys, err = a.Services.SpvWalletEngine.GetAccessKeysByXPubID(
accessKeys, err := a.Services.SpvWalletEngine.GetAccessKeysByXPubID(
c.Request.Context(),
reqXPubID,
metadata,
&dbConditions,
queryParams,
); err != nil {
reqParams.Metadata,
reqParams.Conditions.ToDbConditions(),
reqParams.QueryParams,
)
if err != nil {
c.JSON(http.StatusInternalServerError, err.Error())
return
}
Expand Down
40 changes: 19 additions & 21 deletions actions/admin/access_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package admin
import (
"net/http"

"github.com/bitcoin-sv/spv-wallet/actions"
"github.com/bitcoin-sv/spv-wallet/engine"
"github.com/bitcoin-sv/spv-wallet/mappings"
"github.com/bitcoin-sv/spv-wallet/models"
"github.com/gin-gonic/gin"
Expand All @@ -16,26 +14,26 @@ import (
// @Description Access Keys Search
// @Tags Admin
// @Produce json
// @Param SearchRequestParameters body actions.SearchRequestParameters false "Supports targeted resource searches with filters for metadata and custom conditions, plus options for pagination and sorting to streamline data exploration and analysis"
// @Param SearchAccessKeys body SearchAccessKeys false "Supports targeted resource searches with filters and metadata, plus options for pagination and sorting to streamline data exploration and analysis"
// @Success 200 {object} []models.AccessKey "List of access keys"
// @Failure 400 "Bad request - Error while parsing SearchRequestParameters from request body"
// @Failure 400 "Bad request - Error while parsing SearchAccessKeys from request body"
// @Failure 500 "Internal server error - Error while searching for access keys"
// @Router /v1/admin/access-keys/search [post]
// @Security x-auth-xpub
func (a *Action) accessKeysSearch(c *gin.Context) {
queryParams, metadata, conditions, err := actions.GetSearchQueryParameters(c)
if err != nil {
var reqParams SearchAccessKeys
if err := c.Bind(&reqParams); err != nil {
c.JSON(http.StatusBadRequest, err.Error())
return
}

var accessKeys []*engine.AccessKey
if accessKeys, err = a.Services.SpvWalletEngine.GetAccessKeys(
accessKeys, err := a.Services.SpvWalletEngine.GetAccessKeys(
c.Request.Context(),
metadata,
conditions,
queryParams,
); err != nil {
reqParams.Metadata,
reqParams.Conditions.ToDbConditions(),
reqParams.QueryParams,
)
if err != nil {
c.JSON(http.StatusInternalServerError, err.Error())
return
}
Expand All @@ -54,25 +52,25 @@ func (a *Action) accessKeysSearch(c *gin.Context) {
// @Description Access Keys Count
// @Tags Admin
// @Produce json
// @Param CountRequestParameters body actions.CountRequestParameters false "Enables precise filtering of resource counts using custom conditions or metadata, catering to specific business or analysis needs"
// @Param CountAccessKeys body CountAccessKeys false "Enables filtering of elements to be counted"
// @Success 200 {number} int64 "Count of access keys"
// @Failure 400 "Bad request - Error while parsing CountRequestParameters from request body"
// @Failure 400 "Bad request - Error while parsing CountAccessKeys from request body"
// @Failure 500 "Internal Server Error - Error while fetching count of access keys"
// @Router /v1/admin/access-keys/count [post]
// @Security x-auth-xpub
func (a *Action) accessKeysCount(c *gin.Context) {
metadata, conditions, err := actions.GetCountQueryParameters(c)
if err != nil {
var reqParams CountAccessKeys
if err := c.Bind(&reqParams); err != nil {
c.JSON(http.StatusBadRequest, err.Error())
return
}

var count int64
if count, err = a.Services.SpvWalletEngine.GetAccessKeysCount(
count, err := a.Services.SpvWalletEngine.GetAccessKeysCount(
c.Request.Context(),
metadata,
conditions,
); err != nil {
reqParams.Metadata,
reqParams.Conditions.ToDbConditions(),
)
if err != nil {
c.JSON(http.StatusInternalServerError, err.Error())
return
}
Expand Down
15 changes: 10 additions & 5 deletions actions/admin/contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,30 @@ import (
// @Description Search for contacts
// @Tags Admin
// @Produce json
// @Param SearchRequestParameters body actions.SearchRequestParameters false "Supports targeted resource searches with filters for metadata and custom conditions, plus options for pagination and sorting to streamline data exploration and analysis"
// @Param SearchContacts body SearchContacts false "Supports targeted resource searches with filters and metadata, plus options for pagination and sorting to streamline data exploration and analysis"
// @Success 200 {object} []models.Contact "List of contacts"
// @Failure 400 "Bad request - Error while parsing SearchRequestParameters from request body"
// @Failure 400 "Bad request - Error while parsing SearchContacts from request body"
// @Failure 500 "Internal server error - Error while searching for contacts"
// @Router /v1/admin/contact/search [post]
// @Security x-auth-xpub
func (a *Action) contactsSearch(c *gin.Context) {
var reqParams SearchTransactions
var reqParams SearchContacts
if err := c.Bind(&reqParams); err != nil {
c.JSON(http.StatusBadRequest, err.Error())
return
}

conditions, err := reqParams.Conditions.ToDbConditions()
if err != nil {
c.JSON(http.StatusBadRequest, err.Error())
return
}

// Record a new transaction (get the hex from parameters)a
contacts, err := a.Services.SpvWalletEngine.GetContacts(
c.Request.Context(),
reqParams.Metadata,
reqParams.Conditions.ToDbConditions(),
conditions,
reqParams.QueryParams,
)
if err != nil {
Expand Down Expand Up @@ -75,7 +81,6 @@ func (a *Action) contactsUpdate(c *gin.Context) {
reqParams.FullName,
&reqParams.Metadata,
)

if err != nil {
handleErrors(err, c)
return
Expand Down
40 changes: 19 additions & 21 deletions actions/admin/destinations.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package admin
import (
"net/http"

"github.com/bitcoin-sv/spv-wallet/actions"
"github.com/bitcoin-sv/spv-wallet/engine"
"github.com/bitcoin-sv/spv-wallet/mappings"
"github.com/bitcoin-sv/spv-wallet/models"
"github.com/gin-gonic/gin"
Expand All @@ -16,26 +14,26 @@ import (
// @Description Search for destinations
// @Tags Admin
// @Produce json
// @Param SearchRequestParameters body actions.SearchRequestParameters false "Supports targeted resource searches with filters for metadata and custom conditions, plus options for pagination and sorting to streamline data exploration and analysis"
// @Param SearchDestinations body SearchDestinations false "Supports targeted resource searches with filters and metadata, plus options for pagination and sorting to streamline data exploration and analysis"
// @Success 200 {object} []models.Destination "List of destinations"
// @Failure 400 "Bad request - Error while parsing SearchRequestParameters from request body"
// @Failure 400 "Bad request - Error while parsing SearchDestinations from request body"
// @Failure 500 "Internal server error - Error while searching for destinations"
// @Router /v1/admin/destinations/search [post]
// @Security x-auth-xpub
func (a *Action) destinationsSearch(c *gin.Context) {
queryParams, metadata, conditions, err := actions.GetSearchQueryParameters(c)
if err != nil {
var reqParams SearchDestinations
if err := c.Bind(&reqParams); err != nil {
c.JSON(http.StatusBadRequest, err.Error())
return
}

var destinations []*engine.Destination
if destinations, err = a.Services.SpvWalletEngine.GetDestinations(
destinations, err := a.Services.SpvWalletEngine.GetDestinations(
c.Request.Context(),
metadata,
conditions,
queryParams,
); err != nil {
reqParams.Metadata,
reqParams.Conditions.ToDbConditions(),
reqParams.QueryParams,
)
if err != nil {
c.JSON(http.StatusInternalServerError, err.Error())
return
}
Expand All @@ -54,24 +52,24 @@ func (a *Action) destinationsSearch(c *gin.Context) {
// @Description Count destinations
// @Tags Admin
// @Produce json
// @Param CountRequestParameters body actions.CountRequestParameters false "Enables precise filtering of resource counts using custom conditions or metadata, catering to specific business or analysis needs"
// @Param CountDestinations body CountDestinations false "Enables filtering of elements to be counted"
// @Success 200 {number} int64 "Count of destinations"
// @Failure 400 "Bad request - Error while parsing CountRequestParameters from request body"
// @Failure 400 "Bad request - Error while parsing CountDestinations from request body"
// @Failure 500 "Internal Server Error - Error while fetching count of destinations"
// @Security x-auth-xpub
func (a *Action) destinationsCount(c *gin.Context) {
metadata, conditions, err := actions.GetCountQueryParameters(c)
if err != nil {
var reqParams CountDestinations
if err := c.Bind(&reqParams); err != nil {
c.JSON(http.StatusBadRequest, err.Error())
return
}

var count int64
if count, err = a.Services.SpvWalletEngine.GetDestinationsCount(
count, err := a.Services.SpvWalletEngine.GetDestinationsCount(
c.Request.Context(),
metadata,
conditions,
); err != nil {
reqParams.Metadata,
reqParams.Conditions.ToDbConditions(),
)
if err != nil {
c.JSON(http.StatusInternalServerError, err.Error())
return
}
Expand Down
38 changes: 37 additions & 1 deletion actions/admin/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,41 @@ type UpdateContact struct {
FullName string `json:"fullName" example:"John Doe"`
}

// SearchContacts is a model for handling searching with filters and metadata
type SearchContacts = common.SearchModel[filter.ContactFilter]

// SearchAccessKeys is a model for handling searching with filters and metadata
type SearchAccessKeys = common.SearchModel[filter.AdminAccessKeyFilter]

// CountAccessKeys is a model for handling counting filtered transactions
type CountAccessKeys = common.ConditionsModel[filter.AdminAccessKeyFilter]

// SearchDestinations is a model for handling searching with filters and metadata
type SearchDestinations = common.SearchModel[filter.DestinationFilter]

// CountDestinations is a model for handling counting filtered destinations
type CountDestinations = common.ConditionsModel[filter.DestinationFilter]

// SearchTransactions is a model for handling searching with filters and metadata
type SearchTransactions = common.SearchModel[filter.ContactFilter]
type SearchTransactions = common.SearchModel[filter.TransactionFilter]

// CountTransactions is a model for handling counting filtered transactions
type CountTransactions = common.ConditionsModel[filter.TransactionFilter]

// SearchUtxos is a model for handling searching with filters and metadata
type SearchUtxos = common.SearchModel[filter.AdminUtxoFilter]

// CountUtxos is a model for handling counting filtered UTXOs
type CountUtxos = common.ConditionsModel[filter.AdminUtxoFilter]

// SearchPaymails is a model for handling searching with filters and metadata
type SearchPaymails = common.SearchModel[filter.AdminPaymailFilter]

// CountPaymails is a model for handling counting filtered paymails
type CountPaymails = common.ConditionsModel[filter.AdminPaymailFilter]

// SearchXpubs is a model for handling searching with filters and metadata
type SearchXpubs = common.SearchModel[filter.XpubFilter]

// CountXpubs is a model for handling counting filtered xPubs
type CountXpubs = common.ConditionsModel[filter.XpubFilter]
Loading

0 comments on commit 1194719

Please sign in to comment.