Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(SPV-736) adding admin contacts methods #223

Merged
merged 7 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions admin_contacts.go
Original file line number Diff line number Diff line change
@@ -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) {
ac4ch marked this conversation as resolved.
Show resolved Hide resolved
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)
}
3 changes: 2 additions & 1 deletion contacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion contacts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
51 changes: 51 additions & 0 deletions transports/http_admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package transports
import (
"context"
"encoding/json"
"fmt"
"net/http"

"github.com/bitcoin-sv/spv-wallet/models"
Expand Down Expand Up @@ -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,
})
dorzepowski marked this conversation as resolved.
Show resolved Hide resolved
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)
}
5 changes: 5 additions & 0 deletions transports/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading