Skip to content

Commit

Permalink
feat(fctl): fix/update payments endpoints (#554)
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-nicolas authored Oct 2, 2023
1 parent 751cbb7 commit f3b71ff
Show file tree
Hide file tree
Showing 20 changed files with 1,619 additions and 201 deletions.
105 changes: 105 additions & 0 deletions components/fctl/cmd/payments/accounts/balances.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package accounts

import (
"fmt"
"time"

fctl "github.com/formancehq/fctl/pkg"
"github.com/formancehq/formance-sdk-go/pkg/models/operations"
"github.com/formancehq/formance-sdk-go/pkg/models/shared"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
)

type ListBalancesStore struct {
Cursor *shared.BalancesCursorCursor `json:"cursor"`
}

type ListBalancesController struct {
store *ListBalancesStore
}

var _ fctl.Controller[*ListBalancesStore] = (*ListBalancesController)(nil)

func NewListBalanceStore() *ListBalancesStore {
return &ListBalancesStore{
Cursor: &shared.BalancesCursorCursor{},
}
}

func NewListBalancesController() *ListBalancesController {
return &ListBalancesController{
store: NewListBalanceStore(),
}
}

func (c *ListBalancesController) GetStore() *ListBalancesStore {
return c.store
}

func (c *ListBalancesController) Run(cmd *cobra.Command, args []string) (fctl.Renderable, error) {
cfg, err := fctl.GetConfig(cmd)
if err != nil {
return nil, err
}

organizationID, err := fctl.ResolveOrganizationID(cmd, cfg)
if err != nil {
return nil, err
}

stack, err := fctl.ResolveStack(cmd, cfg, organizationID)
if err != nil {
return nil, err
}

client, err := fctl.NewStackClient(cmd, cfg, stack)
if err != nil {
return nil, err
}

response, err := client.Payments.GetAccountBalances(
cmd.Context(),
operations.GetAccountBalancesRequest{
AccountID: args[0],
},
)
if err != nil {
return nil, err
}

if response.StatusCode >= 300 {
return nil, fmt.Errorf("unexpected status code: %d", response.StatusCode)
}

c.store.Cursor = &response.BalancesCursor.Cursor

return c, nil
}

func (c *ListBalancesController) Render(cmd *cobra.Command, args []string) error {
tableData := fctl.Map(c.store.Cursor.Data, func(balance shared.AccountBalance) []string {
return []string{
balance.AccountID,
balance.Asset,
balance.Balance.String(),
balance.CreatedAt.Format(time.RFC3339),
balance.LastUpdatedAt.Format(time.RFC3339),
}
})
tableData = fctl.Prepend(tableData, []string{"ID", "Asset", "Balance",
"CreatedAt", "LastUpdatedAt"})
return pterm.DefaultTable.
WithHasHeader().
WithWriter(cmd.OutOrStdout()).
WithData(tableData).
Render()
}

func NewListBalanceCommand() *cobra.Command {
return fctl.NewCommand("balances <accountID>",
fctl.WithArgs(cobra.ExactArgs(1)),
fctl.WithShortDescription("List accounts balances"),
fctl.WithController[*ListBalancesStore](NewListBalancesController()),
)
}
107 changes: 107 additions & 0 deletions components/fctl/cmd/payments/accounts/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package accounts

import (
"fmt"
"time"

fctl "github.com/formancehq/fctl/pkg"
"github.com/formancehq/formance-sdk-go/pkg/models/operations"
"github.com/formancehq/formance-sdk-go/pkg/models/shared"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
)

type ListStore struct {
Cursor *shared.AccountsCursorCursor `json:"cursor"`
}

type ListController struct {
store *ListStore
}

var _ fctl.Controller[*ListStore] = (*ListController)(nil)

func NewListStore() *ListStore {
return &ListStore{
Cursor: &shared.AccountsCursorCursor{},
}
}

func NewListController() *ListController {
return &ListController{
store: NewListStore(),
}
}

func (c *ListController) GetStore() *ListStore {
return c.store
}

func (c *ListController) Run(cmd *cobra.Command, args []string) (fctl.Renderable, error) {
cfg, err := fctl.GetConfig(cmd)
if err != nil {
return nil, err
}

organizationID, err := fctl.ResolveOrganizationID(cmd, cfg)
if err != nil {
return nil, err
}

stack, err := fctl.ResolveStack(cmd, cfg, organizationID)
if err != nil {
return nil, err
}

client, err := fctl.NewStackClient(cmd, cfg, stack)
if err != nil {
return nil, err
}

response, err := client.Payments.PaymentslistAccounts(
cmd.Context(),
operations.PaymentslistAccountsRequest{},
)
if err != nil {
return nil, err
}

if response.StatusCode >= 300 {
return nil, fmt.Errorf("unexpected status code: %d", response.StatusCode)
}

c.store.Cursor = &response.AccountsCursor.Cursor

return c, nil
}

func (c *ListController) Render(cmd *cobra.Command, args []string) error {
tableData := fctl.Map(c.store.Cursor.Data, func(acc shared.PaymentsAccount) []string {
return []string{
acc.ID,
acc.AccountName,
acc.CreatedAt.Format(time.RFC3339),
string(acc.Provider),
acc.DefaultAsset,
acc.DefaultCurrency,
acc.Reference,
acc.Type,
}
})
tableData = fctl.Prepend(tableData, []string{"ID", "AccountName", "CreatedAt",
"Provider", "DefaultAsset", "DefaultCurrency", "Reference", "Type"})
return pterm.DefaultTable.
WithHasHeader().
WithWriter(cmd.OutOrStdout()).
WithData(tableData).
Render()
}

func NewListCommand() *cobra.Command {
return fctl.NewCommand("list",
fctl.WithAliases("ls", "l"),
fctl.WithArgs(cobra.ExactArgs(0)),
fctl.WithShortDescription("List connector accounts"),
fctl.WithController[*ListStore](NewListController()),
)
}
18 changes: 18 additions & 0 deletions components/fctl/cmd/payments/accounts/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package accounts

import (
fctl "github.com/formancehq/fctl/pkg"
"github.com/spf13/cobra"
)

func NewAccountsCommand() *cobra.Command {
return fctl.NewCommand("accounts",
fctl.WithAliases("acc", "a", "ac", "account"),
fctl.WithShortDescription("Accounts management"),
fctl.WithChildCommands(
NewListCommand(),
NewShowCommand(),
NewListBalanceCommand(),
),
)
}
103 changes: 103 additions & 0 deletions components/fctl/cmd/payments/accounts/show.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package accounts

import (
"fmt"
"time"

fctl "github.com/formancehq/fctl/pkg"
"github.com/formancehq/formance-sdk-go/pkg/models/operations"
"github.com/formancehq/formance-sdk-go/pkg/models/shared"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
)

type ShowStore struct {
Account *shared.PaymentsAccount `json:"account"`
}
type ShowController struct {
store *ShowStore
}

var _ fctl.Controller[*ShowStore] = (*ShowController)(nil)

func NewShowStore() *ShowStore {
return &ShowStore{}
}

func NewShowController() *ShowController {
return &ShowController{
store: NewShowStore(),
}
}

func NewShowCommand() *cobra.Command {
return fctl.NewCommand("get <accountID>",
fctl.WithShortDescription("Get account"),
fctl.WithArgs(cobra.ExactArgs(1)),
fctl.WithAliases("sh", "s"),
fctl.WithController[*ShowStore](NewShowController()),
)
}

func (c *ShowController) GetStore() *ShowStore {
return c.store
}

func (c *ShowController) Run(cmd *cobra.Command, args []string) (fctl.Renderable, error) {
cfg, err := fctl.GetConfig(cmd)
if err != nil {
return nil, err
}

organizationID, err := fctl.ResolveOrganizationID(cmd, cfg)
if err != nil {
return nil, err
}

stack, err := fctl.ResolveStack(cmd, cfg, organizationID)
if err != nil {
return nil, err
}

ledgerClient, err := fctl.NewStackClient(cmd, cfg, stack)
if err != nil {
return nil, err
}

response, err := ledgerClient.Payments.PaymentsgetAccount(cmd.Context(), operations.PaymentsgetAccountRequest{
AccountID: args[0],
})
if err != nil {
return nil, err
}

if response.StatusCode >= 300 {
return nil, fmt.Errorf("unexpected status code: %d", response.StatusCode)
}

c.store.Account = &response.PaymentsAccountResponse.Data

return c, nil
}

func (c *ShowController) Render(cmd *cobra.Command, args []string) error {
fctl.Section.WithWriter(cmd.OutOrStdout()).Println("Information")
tableData := pterm.TableData{}
tableData = append(tableData, []string{pterm.LightCyan("ID"), c.store.Account.ID})
tableData = append(tableData, []string{pterm.LightCyan("AccountName"), c.store.Account.AccountName})
tableData = append(tableData, []string{pterm.LightCyan("CreatedAt"), c.store.Account.CreatedAt.Format(time.RFC3339)})
tableData = append(tableData, []string{pterm.LightCyan("Provider"), string(c.store.Account.Provider)})
tableData = append(tableData, []string{pterm.LightCyan("DefaultAsset"), c.store.Account.DefaultAsset})
tableData = append(tableData, []string{pterm.LightCyan("DefaultCurrency"), c.store.Account.DefaultCurrency})
tableData = append(tableData, []string{pterm.LightCyan("Reference"), c.store.Account.Reference})
tableData = append(tableData, []string{pterm.LightCyan("Type"), c.store.Account.Type})

if err := pterm.DefaultTable.
WithWriter(cmd.OutOrStdout()).
WithData(tableData).
Render(); err != nil {
return err
}

return nil
}
Loading

1 comment on commit f3b71ff

@vercel
Copy link

@vercel vercel bot commented on f3b71ff Oct 2, 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.