diff --git a/admin_contacts.go b/admin_contacts.go new file mode 100644 index 0000000..e8c2d53 --- /dev/null +++ b/admin_contacts.go @@ -0,0 +1,34 @@ +package walletclient + +import ( + "context" + + "github.com/bitcoin-sv/spv-wallet/models" + + "github.com/bitcoin-sv/spv-wallet-go-client/transports" +) + +// AdminGetContacts retrieves a list of contacts based on the provided conditions, metadata, and query parameters. +func (wc *WalletClient) AdminGetContacts(ctx context.Context, conditions map[string]interface{}, metadata *models.Metadata, queryParams *transports.QueryParams) ([]*models.Contact, transports.ResponseError) { + return wc.transport.AdminGetContacts(ctx, conditions, metadata, queryParams) +} + +// AdminUpdateContact updates a contact's details such as their full name using the specified contact ID and metadata. +func (wc *WalletClient) AdminUpdateContact(ctx context.Context, id, fullName string, metadata *models.Metadata) (*models.Contact, transports.ResponseError) { + return wc.transport.AdminUpdateContact(ctx, id, fullName, metadata) +} + +// AdminDeleteContact removes a contact from the system using the specified contact ID. +func (wc *WalletClient) AdminDeleteContact(ctx context.Context, id string) transports.ResponseError { + return wc.transport.AdminDeleteContact(ctx, id) +} + +// AdminAcceptContact marks a contact as accepted using the specified contact ID. +func (wc *WalletClient) AdminAcceptContact(ctx context.Context, id string) (*models.Contact, transports.ResponseError) { + return wc.transport.AdminAcceptContact(ctx, id) +} + +// AdminRejectContact marks a contact as rejected using the specified contact ID. +func (wc *WalletClient) AdminRejectContact(ctx context.Context, id string) (*models.Contact, transports.ResponseError) { + return wc.transport.AdminRejectContact(ctx, id) +} diff --git a/contacts.go b/contacts.go index 989270e..e1e47e5 100644 --- a/contacts.go +++ b/contacts.go @@ -5,8 +5,9 @@ import ( "errors" "fmt" - "github.com/bitcoin-sv/spv-wallet-go-client/transports" "github.com/bitcoin-sv/spv-wallet/models" + + "github.com/bitcoin-sv/spv-wallet-go-client/transports" ) // UpsertContact add or update contact. When adding a new contact, the system utilizes Paymail's PIKE capability to dispatch an invitation request, asking the counterparty to include the current user in their contacts. diff --git a/contacts_test.go b/contacts_test.go index 3ed81fc..08d87a6 100644 --- a/contacts_test.go +++ b/contacts_test.go @@ -5,9 +5,10 @@ import ( "strings" "testing" - "github.com/bitcoin-sv/spv-wallet-go-client/fixtures" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + + "github.com/bitcoin-sv/spv-wallet-go-client/fixtures" ) // TestContactActionsRouting will test routing diff --git a/transports/http_admin.go b/transports/http_admin.go index 3c9548f..02ab6b1 100644 --- a/transports/http_admin.go +++ b/transports/http_admin.go @@ -3,6 +3,7 @@ package transports import ( "context" "encoding/json" + "fmt" "net/http" "github.com/bitcoin-sv/spv-wallet/models" @@ -316,3 +317,53 @@ func (h *TransportHTTP) AdminGetSharedConfig(ctx context.Context) (*models.Share return model, nil } + +// AdminGetContacts executes an HTTP POST request to search for contacts based on specified conditions, metadata, and query parameters. +func (h *TransportHTTP) AdminGetContacts(ctx context.Context, conditions map[string]interface{}, metadata *models.Metadata, queryParams *QueryParams) ([]*models.Contact, ResponseError) { + jsonStr, err := json.Marshal(map[string]interface{}{ + FieldConditions: conditions, + FieldMetadata: processMetadata(metadata), + FieldQueryParams: queryParams, + }) + if err != nil { + return nil, WrapError(err) + } + + var contacts []*models.Contact + err = h.doHTTPRequest(ctx, http.MethodPost, "/admin/contact/search", jsonStr, h.adminXPriv, true, &contacts) + return contacts, WrapError(err) +} + +// AdminUpdateContact executes an HTTP PATCH request to update a specific contact's full name using their ID. +func (h *TransportHTTP) AdminUpdateContact(ctx context.Context, id, fullName string, metadata *models.Metadata) (*models.Contact, ResponseError) { + jsonStr, err := json.Marshal(map[string]interface{}{ + "fullName": fullName, + FieldMetadata: processMetadata(metadata), + }) + if err != nil { + return nil, WrapError(err) + } + var contact models.Contact + err = h.doHTTPRequest(ctx, http.MethodPatch, fmt.Sprintf("/admin/contact/%s", id), jsonStr, h.adminXPriv, true, &contact) + return &contact, WrapError(err) +} + +// AdminDeleteContact executes an HTTP DELETE request to remove a contact using their ID. +func (h *TransportHTTP) AdminDeleteContact(ctx context.Context, id string) ResponseError { + err := h.doHTTPRequest(ctx, http.MethodDelete, fmt.Sprintf("/admin/contact/%s", id), nil, h.adminXPriv, true, nil) + return WrapError(err) +} + +// AdminAcceptContact executes an HTTP PATCH request to mark a contact as accepted using their ID. +func (h *TransportHTTP) AdminAcceptContact(ctx context.Context, id string) (*models.Contact, ResponseError) { + var contact models.Contact + err := h.doHTTPRequest(ctx, http.MethodPatch, fmt.Sprintf("/admin/contact/accepted/%s", id), nil, h.adminXPriv, true, &contact) + return &contact, WrapError(err) +} + +// AdminRejectContact executes an HTTP PATCH request to mark a contact as rejected using their ID. +func (h *TransportHTTP) AdminRejectContact(ctx context.Context, id string) (*models.Contact, ResponseError) { + var contact models.Contact + err := h.doHTTPRequest(ctx, http.MethodPatch, fmt.Sprintf("/admin/contact/rejected/%s", id), nil, h.adminXPriv, true, &contact) + return &contact, WrapError(err) +} diff --git a/transports/interface.go b/transports/interface.go index 3ae040b..a7ceeea 100644 --- a/transports/interface.go +++ b/transports/interface.go @@ -82,6 +82,11 @@ type AdminService interface { AdminGetXPubsCount(ctx context.Context, conditions map[string]interface{}, metadata *models.Metadata) (int64, ResponseError) AdminRecordTransaction(ctx context.Context, hex string) (*models.Transaction, ResponseError) AdminGetSharedConfig(ctx context.Context) (*models.SharedConfig, ResponseError) + AdminGetContacts(ctx context.Context, conditions map[string]interface{}, metadata *models.Metadata, queryParams *QueryParams) ([]*models.Contact, ResponseError) + AdminUpdateContact(ctx context.Context, id, fullName string, metadata *models.Metadata) (*models.Contact, ResponseError) + AdminDeleteContact(ctx context.Context, id string) ResponseError + AdminAcceptContact(ctx context.Context, id string) (*models.Contact, ResponseError) + AdminRejectContact(ctx context.Context, id string) (*models.Contact, ResponseError) } // TransportService the transport service interface