Skip to content

Commit

Permalink
feat(*): add transfer and payout creation
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-nicolas committed Oct 10, 2024
1 parent 3acdf8f commit 635952a
Show file tree
Hide file tree
Showing 102 changed files with 6,294 additions and 319 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ coverage.out
dist/
.env
payments
tools/
18 changes: 18 additions & 0 deletions internal/api/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,24 @@ type Backend interface {
PaymentsList(ctx context.Context, query storage.ListPaymentsQuery) (*bunpaginate.Cursor[models.Payment], error)
PaymentsGet(ctx context.Context, id models.PaymentID) (*models.Payment, error)

// Payment Initiations
PaymentInitiationsCreate(ctx context.Context, paymentInitiation models.PaymentInitiation, sendToPSP bool) error
PaymentInitiationsList(ctx context.Context, query storage.ListPaymentInitiationsQuery) (*bunpaginate.Cursor[models.PaymentInitiation], error)
PaymentInitiationsGet(ctx context.Context, id models.PaymentInitiationID) (*models.PaymentInitiation, error)
PaymentInitiationsApprove(ctx context.Context, id models.PaymentInitiationID) error
PaymentInitiationsReject(ctx context.Context, id models.PaymentInitiationID) error
PaymentInitiationsRetry(ctx context.Context, id models.PaymentInitiationID) error
PaymentInitiationsDelete(ctx context.Context, id models.PaymentInitiationID) error

// Payment Initiation Adjustments
PaymentInitiationAdjustmentsList(ctx context.Context, id models.PaymentInitiationID, query storage.ListPaymentInitiationAdjustmentsQuery) (*bunpaginate.Cursor[models.PaymentInitiationAdjustment], error)
PaymentInitiationAdjustmentsListAll(ctx context.Context, id models.PaymentInitiationID) ([]models.PaymentInitiationAdjustment, error)
PaymentInitiationAdjustmentsGetLast(ctx context.Context, id models.PaymentInitiationID) (*models.PaymentInitiationAdjustment, error)

// Payment Initiatiion Related Payments
PaymentInitiationRelatedPaymentsList(ctx context.Context, id models.PaymentInitiationID, query storage.ListPaymentInitiationRelatedPaymentsQuery) (*bunpaginate.Cursor[models.Payment], error)
PaymentInitiationRelatedPaymentListAll(ctx context.Context, id models.PaymentInitiationID) ([]models.Payment, error)

// Pools
PoolsCreate(ctx context.Context, pool models.Pool) error
PoolsGet(ctx context.Context, id uuid.UUID) (*models.Pool, error)
Expand Down
175 changes: 175 additions & 0 deletions internal/api/backend/backend_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion internal/api/services/accounts_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,10 @@ import (
)

func (s *Service) AccountsGet(ctx context.Context, id models.AccountID) (*models.Account, error) {
return s.storage.AccountsGet(ctx, id)
account, err := s.storage.AccountsGet(ctx, id)
if err != nil {
return nil, newStorageError(err, "cannot get account")
}

return account, nil
}
7 changes: 6 additions & 1 deletion internal/api/services/accounts_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,10 @@ import (
)

func (s *Service) AccountsList(ctx context.Context, query storage.ListAccountsQuery) (*bunpaginate.Cursor[models.Account], error) {
return s.storage.AccountsList(ctx, query)
accounts, err := s.storage.AccountsList(ctx, query)
if err != nil {
return nil, newStorageError(err, "cannot list accounts")
}

return accounts, nil
}
7 changes: 6 additions & 1 deletion internal/api/services/balances_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,10 @@ import (
)

func (s *Service) BalancesList(ctx context.Context, query storage.ListBalancesQuery) (*bunpaginate.Cursor[models.Balance], error) {
return s.storage.BalancesList(ctx, query)
balances, err := s.storage.BalancesList(ctx, query)
if err != nil {
return nil, newStorageError(err, "cannot list balances")
}

return balances, nil
}
2 changes: 1 addition & 1 deletion internal/api/services/bank_accounts_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ import (
)

func (s *Service) BankAccountsCreate(ctx context.Context, bankAccount models.BankAccount) error {
return s.storage.BankAccountsUpsert(ctx, bankAccount)
return newStorageError(s.storage.BankAccountsUpsert(ctx, bankAccount), "cannot create bank account")
}
7 changes: 6 additions & 1 deletion internal/api/services/bank_accounts_forward_to_connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,10 @@ import (
)

func (s *Service) BankAccountsForwardToConnector(ctx context.Context, bankAccountID uuid.UUID, connectorID models.ConnectorID) (*models.BankAccount, error) {
return s.engine.ForwardBankAccount(ctx, bankAccountID, connectorID)
ba, err := s.engine.ForwardBankAccount(ctx, bankAccountID, connectorID)
if err != nil {
return nil, handleEngineErrors(err)
}

return ba, nil
}
7 changes: 6 additions & 1 deletion internal/api/services/bank_accounts_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,10 @@ import (
)

func (s *Service) BankAccountsGet(ctx context.Context, id uuid.UUID) (*models.BankAccount, error) {
return s.storage.BankAccountsGet(ctx, id, true)
ba, err := s.storage.BankAccountsGet(ctx, id, true)
if err != nil {
return nil, newStorageError(err, "cannot get bank account")
}

return ba, nil
}
7 changes: 6 additions & 1 deletion internal/api/services/bank_accounts_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,10 @@ import (
)

func (s *Service) BankAccountsList(ctx context.Context, query storage.ListBankAccountsQuery) (*bunpaginate.Cursor[models.BankAccount], error) {
return s.storage.BankAccountsList(ctx, query)
bas, err := s.storage.BankAccountsList(ctx, query)
if err != nil {
return nil, newStorageError(err, "cannot list bank accounts")
}

return bas, nil
}
2 changes: 1 addition & 1 deletion internal/api/services/bank_accounts_update_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ import (
)

func (s *Service) BankAccountsUpdateMetadata(ctx context.Context, id uuid.UUID, metadata map[string]string) error {
return s.storage.BankAccountsUpdateMetadata(ctx, id, metadata)
return newStorageError(s.storage.BankAccountsUpdateMetadata(ctx, id, metadata), "cannot update bank account metadata")
}
2 changes: 1 addition & 1 deletion internal/api/services/connectors_handle_webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ func (s *Service) ConnectorsHandleWebhooks(
urlPath string,
webhook models.Webhook,
) error {
return s.engine.HandleWebhook(ctx, urlPath, webhook)
return handleEngineErrors(s.engine.HandleWebhook(ctx, urlPath, webhook))
}
2 changes: 1 addition & 1 deletion internal/api/services/connectors_uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ func (s *Service) ConnectorsUninstall(ctx context.Context, connectorID models.Co
return newStorageError(err, "get connector")
}

return s.engine.UninstallConnector(ctx, connectorID)
return handleEngineErrors(s.engine.UninstallConnector(ctx, connectorID))
}
28 changes: 28 additions & 0 deletions internal/api/services/payment_initiation_adjustments_get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package services

import (
"context"

"github.com/formancehq/go-libs/bun/bunpaginate"
"github.com/formancehq/payments/internal/models"
"github.com/formancehq/payments/internal/storage"
"github.com/pkg/errors"
)

func (s *Service) PaymentInitiationAdjustmentsGetLast(ctx context.Context, id models.PaymentInitiationID) (*models.PaymentInitiationAdjustment, error) {
q := storage.NewListPaymentInitiationAdjustmentsQuery(
bunpaginate.NewPaginatedQueryOptions(storage.PaymentInitiationAdjustmentsQuery{}).
WithPageSize(1),
)

cursor, err := s.storage.PaymentInitiationAdjustmentsList(ctx, id, q)
if err != nil {
return nil, newStorageError(err, "cannot list payment initiation's adjustments")
}

if len(cursor.Data) == 0 {
return nil, errors.New("payment initiation's adjustments not found")
}

return &cursor.Data[0], nil
}
44 changes: 44 additions & 0 deletions internal/api/services/payment_initiation_adjustments_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package services

import (
"context"

"github.com/formancehq/go-libs/bun/bunpaginate"
"github.com/formancehq/payments/internal/models"
"github.com/formancehq/payments/internal/storage"
)

func (s *Service) PaymentInitiationAdjustmentsList(ctx context.Context, id models.PaymentInitiationID, query storage.ListPaymentInitiationAdjustmentsQuery) (*bunpaginate.Cursor[models.PaymentInitiationAdjustment], error) {
cursor, err := s.storage.PaymentInitiationAdjustmentsList(ctx, id, query)
return cursor, newStorageError(err, "failed to list payment initiation adjustments")
}

func (s *Service) PaymentInitiationAdjustmentsListAll(ctx context.Context, id models.PaymentInitiationID) ([]models.PaymentInitiationAdjustment, error) {
q := storage.NewListPaymentInitiationAdjustmentsQuery(
bunpaginate.NewPaginatedQueryOptions(storage.PaymentInitiationAdjustmentsQuery{}).
WithPageSize(50),
)
var next string
adjustments := []models.PaymentInitiationAdjustment{}
for {
if next != "" {
err := bunpaginate.UnmarshalCursor(next, &q)
if err != nil {
return nil, err
}
}

cursor, err := s.storage.PaymentInitiationAdjustmentsList(ctx, id, q)
if err != nil {
return nil, newStorageError(err, "cannot list payment initiation's adjustments")
}

adjustments = append(adjustments, cursor.Data...)

if cursor.Next == "" {
break
}
}

return adjustments, nil
}
Loading

0 comments on commit 635952a

Please sign in to comment.