Skip to content

Commit

Permalink
feat(payments): add metadata in accounts response (#950)
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-nicolas authored Dec 5, 2023
1 parent 6539dd0 commit 84ed29c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 13 deletions.
37 changes: 27 additions & 10 deletions components/payments/cmd/api/internal/api/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@ import (
)

type accountResponse struct {
ID string `json:"id"`
Reference string `json:"reference"`
CreatedAt time.Time `json:"createdAt"`
ConnectorID string `json:"connectorID"`
Provider string `json:"provider"`
DefaultCurrency string `json:"defaultCurrency"` // Deprecated: should be removed soon
DefaultAsset string `json:"defaultAsset"`
AccountName string `json:"accountName"`
Type string `json:"type"`
Raw interface{} `json:"raw"`
ID string `json:"id"`
Reference string `json:"reference"`
CreatedAt time.Time `json:"createdAt"`
ConnectorID string `json:"connectorID"`
Provider string `json:"provider"`
DefaultCurrency string `json:"defaultCurrency"` // Deprecated: should be removed soon
DefaultAsset string `json:"defaultAsset"`
AccountName string `json:"accountName"`
Type string `json:"type"`
Metadata map[string]string `json:"metadata"`
Raw interface{} `json:"raw"`
}

func listAccountsHandler(b backend.Backend) http.HandlerFunc {
Expand Down Expand Up @@ -63,6 +64,14 @@ func listAccountsHandler(b backend.Backend) http.HandlerFunc {
if ret[i].Connector != nil {
data[i].Provider = ret[i].Connector.Provider.String()
}

if ret[i].Metadata != nil {
metadata := make(map[string]string)
for k, v := range ret[i].Metadata {
metadata[k] = v
}
data[i].Metadata = metadata
}
}

err = json.NewEncoder(w).Encode(api.BaseResponse[*accountResponse]{
Expand Down Expand Up @@ -114,6 +123,14 @@ func readAccountHandler(b backend.Backend) http.HandlerFunc {
data.Provider = account.Connector.Provider.String()
}

if account.Metadata != nil {
metadata := make(map[string]string)
for k, v := range account.Metadata {
metadata[k] = v
}
data.Metadata = metadata
}

err = json.NewEncoder(w).Encode(api.BaseResponse[accountResponse]{
Data: data,
})
Expand Down
8 changes: 8 additions & 0 deletions components/payments/cmd/api/internal/api/accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ func TestListAccounts(t *testing.T) {
CreatedAt: time.Date(2023, 11, 22, 8, 0, 0, 0, time.UTC),
Reference: "acc1",
Type: models.AccountTypeInternal,
Metadata: map[string]string{
"foo": "bar",
},
Connector: &models.Connector{
Provider: models.ConnectorProviderDummyPay,
},
Expand Down Expand Up @@ -184,6 +187,7 @@ func TestListAccounts(t *testing.T) {
DefaultAsset: listAccountsResponse[0].DefaultAsset.String(),
AccountName: listAccountsResponse[0].AccountName,
Type: listAccountsResponse[0].Type.String(),
Metadata: listAccountsResponse[0].Metadata,
},
{
ID: listAccountsResponse[1].ID.String(),
Expand Down Expand Up @@ -336,6 +340,9 @@ func TestGetAccount(t *testing.T) {
CreatedAt: time.Date(2023, 11, 22, 8, 0, 0, 0, time.UTC),
Reference: "acc1",
Type: models.AccountTypeInternal,
Metadata: map[string]string{
"foo": "bar",
},
Connector: &models.Connector{
Provider: models.ConnectorProviderDummyPay,
},
Expand All @@ -350,6 +357,7 @@ func TestGetAccount(t *testing.T) {
DefaultCurrency: getAccountResponse.DefaultAsset.String(),
DefaultAsset: getAccountResponse.DefaultAsset.String(),
AccountName: getAccountResponse.AccountName,
Metadata: getAccountResponse.Metadata,
Type: getAccountResponse.Type.String(),
}
} else {
Expand Down
13 changes: 10 additions & 3 deletions components/payments/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1604,9 +1604,13 @@ components:
type: boolean
PaymentMetadata:
type: object
properties:
key:
type: string
additionalProperties:
type: string
nullable: true
AccountMetadata:
type: object
additionalProperties:
type: string
nullable: true
Pool:
type: object
Expand Down Expand Up @@ -1657,6 +1661,7 @@ components:
- defaultAsset
- accountName
- type
- metadata
- raw
properties:
id:
Expand All @@ -1677,6 +1682,8 @@ components:
type: string
type:
type: string
metadata:
$ref: '#/components/schemas/AccountMetadata'
raw:
type: object
nullable: true
Expand Down

1 comment on commit 84ed29c

@vercel
Copy link

@vercel vercel bot commented on 84ed29c Dec 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.