Skip to content

Commit

Permalink
feat(payments): list linked pools in accounts (#1013)
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-nicolas authored Dec 14, 2023
1 parent 8d57c67 commit 8d8bdd0
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 2 deletions.
12 changes: 12 additions & 0 deletions components/payments/cmd/api/internal/api/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/formancehq/payments/cmd/api/internal/api/backend"
"github.com/formancehq/payments/internal/models"
"github.com/formancehq/stack/libs/go-libs/api"
"github.com/google/uuid"
"github.com/gorilla/mux"
)

Expand All @@ -22,6 +23,7 @@ type accountResponse struct {
AccountName string `json:"accountName"`
Type string `json:"type"`
Metadata map[string]string `json:"metadata"`
Pools []uuid.UUID `json:"pools"`
Raw interface{} `json:"raw"`
}

Expand Down Expand Up @@ -72,6 +74,11 @@ func listAccountsHandler(b backend.Backend) http.HandlerFunc {
}
data[i].Metadata = metadata
}

data[i].Pools = make([]uuid.UUID, len(ret[i].PoolAccounts))
for j := range ret[i].PoolAccounts {
data[i].Pools[j] = ret[i].PoolAccounts[j].PoolID
}
}

err = json.NewEncoder(w).Encode(api.BaseResponse[*accountResponse]{
Expand Down Expand Up @@ -131,6 +138,11 @@ func readAccountHandler(b backend.Backend) http.HandlerFunc {
data.Metadata = metadata
}

data.Pools = make([]uuid.UUID, len(account.PoolAccounts))
for j := range account.PoolAccounts {
data.Pools[j] = account.PoolAccounts[j].PoolID
}

err = json.NewEncoder(w).Encode(api.BaseResponse[accountResponse]{
Data: data,
})
Expand Down
4 changes: 4 additions & 0 deletions components/payments/cmd/api/internal/api/accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ func TestListAccounts(t *testing.T) {
DefaultAsset: listAccountsResponse[0].DefaultAsset.String(),
AccountName: listAccountsResponse[0].AccountName,
Type: listAccountsResponse[0].Type.String(),
Pools: []uuid.UUID{},
Metadata: listAccountsResponse[0].Metadata,
},
{
Expand All @@ -198,6 +199,7 @@ func TestListAccounts(t *testing.T) {
DefaultCurrency: listAccountsResponse[1].DefaultAsset.String(),
DefaultAsset: listAccountsResponse[1].DefaultAsset.String(),
AccountName: listAccountsResponse[1].AccountName,
Pools: []uuid.UUID{},
// Type is converted to external when it is external formance
Type: string(models.AccountTypeExternal),
},
Expand Down Expand Up @@ -358,6 +360,7 @@ func TestGetAccount(t *testing.T) {
DefaultAsset: getAccountResponse.DefaultAsset.String(),
AccountName: getAccountResponse.AccountName,
Metadata: getAccountResponse.Metadata,
Pools: []uuid.UUID{},
Type: getAccountResponse.Type.String(),
}
} else {
Expand All @@ -383,6 +386,7 @@ func TestGetAccount(t *testing.T) {
DefaultCurrency: getAccountResponse.DefaultAsset.String(),
DefaultAsset: getAccountResponse.DefaultAsset.String(),
AccountName: getAccountResponse.AccountName,
Pools: []uuid.UUID{},
// Type is converted to external when it is external formance
Type: models.AccountTypeExternal.String(),
}
Expand Down
4 changes: 3 additions & 1 deletion components/payments/cmd/api/internal/storage/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ func (s *Storage) ListAccounts(ctx context.Context, pagination PaginatorQuery) (

query := s.db.NewSelect().
Model(&accounts).
Relation("Connector")
Relation("Connector").
Relation("PoolAccounts")

query = pagination.apply(query, "account.created_at")

Expand Down Expand Up @@ -66,6 +67,7 @@ func (s *Storage) GetAccount(ctx context.Context, id string) (*models.Account, e
err := s.db.NewSelect().
Model(&account).
Relation("Connector").
Relation("PoolAccounts").
Where("account.id = ?", id).
Scan(ctx)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion components/payments/internal/models/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ type Account struct {

RawData json.RawMessage

Connector *Connector `bun:"rel:has-one,join:connector_id=id"`
Connector *Connector `bun:"rel:has-one,join:connector_id=id"`
PoolAccounts []*PoolAccounts `bun:"rel:has-many,join:id=account_id"`
}

type AccountType string
Expand Down
4 changes: 4 additions & 0 deletions components/payments/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1722,6 +1722,10 @@ components:
type: string
type:
type: string
pools:
type: array
items:
type: string
metadata:
$ref: '#/components/schemas/AccountMetadata'
raw:
Expand Down
1 change: 1 addition & 0 deletions libs/clients/go/docs/models/shared/paymentsaccount.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
| ~~`DefaultCurrency`~~ | *string* | :heavy_check_mark: | : warning: ** DEPRECATED **: This will be removed in a future release, please migrate away from it as soon as possible. |
| `ID` | *string* | :heavy_check_mark: | N/A |
| `Metadata` | map[string]*string* | :heavy_check_mark: | N/A |
| `Pools` | []*string* | :heavy_minus_sign: | N/A |
| `Raw` | [PaymentsAccountRaw](../../models/shared/paymentsaccountraw.md) | :heavy_check_mark: | N/A |
| `Reference` | *string* | :heavy_check_mark: | N/A |
| `Type` | *string* | :heavy_check_mark: | N/A |
8 changes: 8 additions & 0 deletions libs/clients/go/pkg/models/shared/paymentsaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type PaymentsAccount struct {
DefaultCurrency string `json:"defaultCurrency"`
ID string `json:"id"`
Metadata map[string]string `json:"metadata"`
Pools []string `json:"pools,omitempty"`
Raw *PaymentsAccountRaw `json:"raw"`
Reference string `json:"reference"`
Type string `json:"type"`
Expand Down Expand Up @@ -84,6 +85,13 @@ func (o *PaymentsAccount) GetMetadata() map[string]string {
return o.Metadata
}

func (o *PaymentsAccount) GetPools() []string {
if o == nil {
return nil
}
return o.Pools
}

func (o *PaymentsAccount) GetRaw() *PaymentsAccountRaw {
if o == nil {
return nil
Expand Down

1 comment on commit 8d8bdd0

@vercel
Copy link

@vercel vercel bot commented on 8d8bdd0 Dec 14, 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.