Skip to content

Commit

Permalink
feat(BUX-639): add upsert contact
Browse files Browse the repository at this point in the history
  • Loading branch information
arkadiuszos4chain committed Apr 2, 2024
1 parent bbe0b0f commit f43bb1d
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 16 deletions.
10 changes: 10 additions & 0 deletions contacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ import (
"github.com/bitcoin-sv/spv-wallet/models"
)

// 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.
func (b *WalletClient) UpsertContact(ctx context.Context, paymail, fullName string, metadata *models.Metadata) (*models.Contact, transports.ResponseError) {
return b.transport.UpsertContact(ctx, paymail, fullName, metadata, "")
}

// UpsertContactForPaymail 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 specified paymail in their contacts.
func (b *WalletClient) UpsertContactForPaymail(ctx context.Context, paymail, fullName string, metadata *models.Metadata, requesterPaymail string) (*models.Contact, transports.ResponseError) {
return b.transport.UpsertContact(ctx, paymail, fullName, metadata, requesterPaymail)
}

// AcceptContact will accept the contact associated with the paymail
func (b *WalletClient) AcceptContact(ctx context.Context, paymail string) transports.ResponseError {
return b.transport.AcceptContact(ctx, paymail)
Expand Down
58 changes: 42 additions & 16 deletions contacts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,55 @@ import (
// TestRejectContact will test the RejectContact method
func TestContactActionsRouting(t *testing.T) {
tcs := []struct {
name string
route string
f func(c *WalletClient) error
name string
route string
responsePayload string
f func(c *WalletClient) error
}{
{
name: "RejectContact",
route: "/contact/rejected/",
f: func(c *WalletClient) error { return c.RejectContact(context.Background(), fixtures.PaymailAddress) },
name: "RejectContact",
route: "/contact/rejected/",
responsePayload: "{}",
f: func(c *WalletClient) error { return c.RejectContact(context.Background(), fixtures.PaymailAddress) },
},
{
name: "AcceptContact",
route: "/contact/accepted/",
f: func(c *WalletClient) error { return c.AcceptContact(context.Background(), fixtures.PaymailAddress) },
name: "AcceptContact",
route: "/contact/accepted/",
responsePayload: "{}",
f: func(c *WalletClient) error { return c.AcceptContact(context.Background(), fixtures.PaymailAddress) },
},
{
name: "ConfirmContact",
route: "/contact/confirmed/",
f: func(c *WalletClient) error { return c.ConfirmContact(context.Background(), fixtures.PaymailAddress) },
name: "ConfirmContact",
route: "/contact/confirmed/",
responsePayload: "{}",
f: func(c *WalletClient) error { return c.ConfirmContact(context.Background(), fixtures.PaymailAddress) },
},
{
name: "GetContacts",
route: "/contact/search/",
f: func(c *WalletClient) error { _, err := c.GetContacts(context.Background(), nil, nil, nil); return err },
name: "GetContacts",
route: "/contact/search/",
responsePayload: "[]",
f: func(c *WalletClient) error {
_, err := c.GetContacts(context.Background(), nil, nil, nil)
return err
},
},
{
name: "UpsertContact",
route: "/contact/",
responsePayload: "{}",
f: func(c *WalletClient) error {
_, err := c.UpsertContact(context.Background(), "", "", nil)
return err
},
},
{
name: "UpsertContactForPaymail",
route: "/contact/",
responsePayload: "{}",
f: func(c *WalletClient) error {
_, err := c.UpsertContactForPaymail(context.Background(), "", "", nil, "")
return err
},
},
}

Expand All @@ -43,7 +69,7 @@ func TestContactActionsRouting(t *testing.T) {
tmq := testTransportHandler{
Type: fixtures.RequestType,
Path: tc.route,
Result: "[]",
Result: tc.responsePayload,
ClientURL: fixtures.ServerURL,
Client: WithHTTPClient,
}
Expand Down
26 changes: 26 additions & 0 deletions transports/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -693,3 +693,29 @@ func (h *TransportHTTP) GetContacts(ctx context.Context, conditions map[string]i

return result, nil
}

// 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.
func (h *TransportHTTP) UpsertContact(ctx context.Context, paymail, fullName string, metadata *models.Metadata, requesterPaymail string) (*models.Contact, ResponseError) {
payload := map[string]interface{}{
"fullName": fullName,
FieldMetadata: processMetadata(metadata),
}

if requesterPaymail != "" {
payload["requesterPaymail"] = requesterPaymail
}

jsonStr, err := json.Marshal(payload)
if err != nil {
return nil, WrapError(err)
}

var result models.Contact
if err := h.doHTTPRequest(
ctx, http.MethodPut, "/contact/"+paymail, jsonStr, h.xPriv, h.signRequest, &result,
); err != nil {
return nil, err
}

return &result, nil
}
1 change: 1 addition & 0 deletions transports/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type ContactService interface {
RejectContact(ctx context.Context, paymail string) ResponseError
ConfirmContact(ctx context.Context, paymail string) ResponseError
GetContacts(ctx context.Context, conditions map[string]interface{}, metadata *models.Metadata, queryParams *QueryParams) ([]*models.Contact, ResponseError)
UpsertContact(ctx context.Context, paymail, fullName string, metadata *models.Metadata, requesterPaymail string) (*models.Contact, ResponseError)
}

// AdminService is the admin related requests
Expand Down

0 comments on commit f43bb1d

Please sign in to comment.