-
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(fctl): fix/update payments endpoints (#554)
- Loading branch information
1 parent
751cbb7
commit f3b71ff
Showing
20 changed files
with
1,619 additions
and
201 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.
f3b71ff
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
docs – ./
docs-formance.vercel.app
docs-git-main-formance.vercel.app
docs.numary.com
developers.numary.com
docs.formance.com
developers.formance.com