Skip to content

Commit

Permalink
payments: add transfer_initiations
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-nicolas committed Sep 26, 2023
1 parent 29571f9 commit 52e5e0e
Show file tree
Hide file tree
Showing 144 changed files with 6,866 additions and 1,161 deletions.
File renamed without changes.
9 changes: 8 additions & 1 deletion components/fctl/cmd/payments/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package payments

import (
"github.com/formancehq/fctl/cmd/payments/connectors"
"github.com/formancehq/fctl/cmd/payments/payments"
"github.com/formancehq/fctl/cmd/payments/transferinitiation"
fctl "github.com/formancehq/fctl/pkg"
"github.com/spf13/cobra"
)
Expand All @@ -11,7 +13,12 @@ func NewCommand() *cobra.Command {
fctl.WithShortDescription("Payments management"),
fctl.WithChildCommands(
connectors.NewConnectorsCommand(),
NewListPaymentsCommand(),
payments.NewListPaymentsCommand(),
transferinitiation.NewShowCommand(),
transferinitiation.NewListTransferInitiationCommand(),
transferinitiation.NewCreateCommand(),
transferinitiation.NewUpdateStatusCommand(),
transferinitiation.NewDeleteCommand(),
),
)
}
86 changes: 86 additions & 0 deletions components/fctl/cmd/payments/transferinitiation/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package transferinitiation

import (
"encoding/json"
"fmt"

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

type TransferInitiationCreateStore struct {
TransferInitiationId string `json:"transferInitiationId"`
}
type TransferInitiationCreateController struct {
store *TransferInitiationCreateStore
}

var _ fctl.Controller[*TransferInitiationCreateStore] = (*TransferInitiationCreateController)(nil)

func NewDefaultTransferInitiationCreateStore() *TransferInitiationCreateStore {
return &TransferInitiationCreateStore{}
}

func NewTransferInitiationCreateController() *TransferInitiationCreateController {
return &TransferInitiationCreateController{
store: NewDefaultTransferInitiationCreateStore(),
}
}

func NewCreateCommand() *cobra.Command {
return fctl.NewCommand("create <file>|-",
fctl.WithShortDescription("Create a transfer initiation"),
fctl.WithAliases("cr", "c"),
fctl.WithArgs(cobra.ExactArgs(1)),
fctl.WithController[*TransferInitiationCreateStore](NewTransferInitiationCreateController()),
)
}

func (c *TransferInitiationCreateController) GetStore() *TransferInitiationCreateStore {
return c.store
}

func (c *TransferInitiationCreateController) Run(cmd *cobra.Command, args []string) (fctl.Renderable, error) {

soc, err := fctl.GetStackOrganizationConfig(cmd)
if err != nil {
return nil, err
}
client, err := fctl.NewStackClient(cmd, soc.Config, soc.Stack)
if err != nil {
return nil, errors.Wrap(err, "creating stack client")
}

script, err := fctl.ReadFile(cmd, soc.Stack, args[0])
if err != nil {
return nil, err
}

request := shared.TransferInitiationRequest{}
if err := json.Unmarshal([]byte(script), &request); err != nil {
return nil, err
}

//nolint:gosimple
response, err := client.Payments.CreateTransferInitiation(cmd.Context(), request)
if err != nil {
return nil, err
}

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

c.store.TransferInitiationId = response.TransferInitiationResponse.Data.ID

return c, nil
}

func (c *TransferInitiationCreateController) Render(cmd *cobra.Command, args []string) error {
pterm.Success.WithWriter(cmd.OutOrStdout()).Printfln("Transfer Initiation created with ID: %s", c.store.TransferInitiationId)

return nil
}
91 changes: 91 additions & 0 deletions components/fctl/cmd/payments/transferinitiation/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package transferinitiation

import (
"fmt"

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

type TransferInitiationDeleteStore struct {
TransferID string `json:"transferId"`
Success bool `json:"success"`
}

type TransferInitiationDeleteController struct {
store *TransferInitiationDeleteStore
}

var _ fctl.Controller[*TransferInitiationDeleteStore] = (*TransferInitiationDeleteController)(nil)

func NewDefaultTransferInitiationDeleteStore() *TransferInitiationDeleteStore {
return &TransferInitiationDeleteStore{}
}

func NewTransferInitiationDeleteController() *TransferInitiationDeleteController {
return &TransferInitiationDeleteController{
store: NewDefaultTransferInitiationDeleteStore(),
}
}
func NewDeleteCommand() *cobra.Command {
return fctl.NewCommand("delete <transferID>",
fctl.WithAliases("del", "d"),
fctl.WithShortDescription("Delete a transfer Initiation"),
fctl.WithArgs(cobra.ExactArgs(1)),
fctl.WithController[*TransferInitiationDeleteStore](NewTransferInitiationDeleteController()),
)
}

func (c *TransferInitiationDeleteController) GetStore() *TransferInitiationDeleteStore {
return c.store
}

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

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, errors.Wrap(err, "creating stack client")
}

response, err := client.Payments.DeleteTransferInitiation(
cmd.Context(),
operations.DeleteTransferInitiationRequest{
TransferID: args[0],
},
)

if err != nil {
return nil, err
}

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

c.store.TransferID = args[0]
c.store.Success = true

return c, nil
}

func (c *TransferInitiationDeleteController) Render(cmd *cobra.Command, args []string) error {
pterm.Success.WithShowLineNumber().Printfln("Transfer Initiation %s Deleted!", c.store.TransferID)
return nil
}
112 changes: 112 additions & 0 deletions components/fctl/cmd/payments/transferinitiation/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package transferinitiation

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 TransferInitiationListStore struct {
Cursor *shared.TransferInitiationsCursorCursor `json:"cursor"`
}

type TransferInitiationListController struct {
store *TransferInitiationListStore
}

var _ fctl.Controller[*TransferInitiationListStore] = (*TransferInitiationListController)(nil)

func NewDefaultTransferInitiationListStore() *TransferInitiationListStore {
return &TransferInitiationListStore{
Cursor: &shared.TransferInitiationsCursorCursor{},
}
}

func NewTransferInitiationListController() *TransferInitiationListController {
return &TransferInitiationListController{
store: NewDefaultTransferInitiationListStore(),
}
}

func (c *TransferInitiationListController) GetStore() *TransferInitiationListStore {
return c.store
}

func (c *TransferInitiationListController) 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.ListTransferInitiations(
cmd.Context(),
operations.ListTransferInitiationsRequest{},
)
if err != nil {
return nil, err
}

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

c.store.Cursor = &response.TransferInitiationsCursor.Cursor

return c, nil
}

func (c *TransferInitiationListController) Render(cmd *cobra.Command, args []string) error {
tableData := fctl.Map(c.store.Cursor.Data, func(tf shared.TransferInitiation) []string {
return []string{
tf.ID,
tf.CreatedAt.Format(time.RFC3339),
tf.UpdatedAt.Format(time.RFC3339),
tf.Description,
tf.SourceAccountID,
tf.DestinationAccountID,
string(tf.Provider),
string(tf.Type),
fmt.Sprint(tf.Amount),
tf.Asset,
string(tf.Status),
tf.Error,
}
})
tableData = fctl.Prepend(tableData, []string{"ID", "CreatedAt", "UpdatedAt", "Description", "Source Account ID",
"Destination Account ID", "Provider", "Type", "Amount", "Asset", "Status", "Error"})
return pterm.DefaultTable.
WithHasHeader().
WithWriter(cmd.OutOrStdout()).
WithData(tableData).
Render()
}

func NewListTransferInitiationCommand() *cobra.Command {
return fctl.NewCommand("list_transfer_initiations",
fctl.WithAliases("ls"),
fctl.WithArgs(cobra.ExactArgs(0)),
fctl.WithShortDescription("List transfer initiation"),
fctl.WithController[*TransferInitiationListStore](NewTransferInitiationListController()),
)
}
Loading

0 comments on commit 52e5e0e

Please sign in to comment.