Files
zeekay 7d727874d5 chore: rebrand Formance Payments -> Hanzo Payments (hanzo-fi)
- go.mod module path + all self-imports: github.com/formancehq/payments -> github.com/hanzo-fi/payments
  (1590 .go files / 3152 refs; 20 modules: root, pkg/client, pkg/domain, 18 ce/plugins).
  External dep kept: formancehq/go-libs.
- plugins: each ce/plugins/* consumed a *published* pkg/domain; added local
  'replace .../pkg/domain => ../../../pkg/domain' (consistent with root) so every
  standalone plugin module builds in-monorepo. compile-plugin.sh MOD var rebranded too.
- branding: README, LICENSE (dual MIT+EE; upstream copyright retained + Hanzo added),
  goreleaser (release images -> registry.hanzo.ai/hanzo-fi, OCI labels, homepage),
  proto go_package, renovate, speakeasy gen.yaml, connector-template *.gotpl.
- CI: removed Numary/Formance workflows + composite action; added canonical hanzo.yml +
  cicd.yml (hanzoai/ci, gated HANZO_CI_ENABLED) + secretless ci.yml (go build/vet).
- kept as data/compat keys: wire capability enums ALLOW_FORMANCE_*, generated SDK type
  client.Formance, domain field FormanceRedirectURL, OTEL_SERVICE_NAME, upstream base image.
- verified: go build ./... (default + -tags ee) + all 20 nested modules green.
2026-07-05 13:08:55 -07:00

198 lines
5.1 KiB
Go

package workflow
import (
"encoding/json"
"fmt"
"github.com/hanzo-fi/payments/internal/connectors/engine/activities"
"github.com/hanzo-fi/payments/internal/connectors/plugins/registry"
"github.com/hanzo-fi/payments/pkg/domain/models"
"github.com/pkg/errors"
"go.temporal.io/sdk/temporal"
"go.temporal.io/sdk/workflow"
)
type FetchNextBalances struct {
ConnectorID models.ConnectorID `json:"connectorID"`
FromPayload *FromPayload `json:"fromPayload"`
Periodically bool `json:"periodically"`
}
func (w Workflow) runFetchNextBalances(
ctx workflow.Context,
fetchNextBalances FetchNextBalances,
nextTasks []models.ConnectorTaskTree,
) error {
if err := w.createInstance(ctx, fetchNextBalances.ConnectorID); err != nil {
return errors.Wrap(err, "creating instance")
}
err := w.fetchBalances(ctx, fetchNextBalances, nextTasks)
return w.terminateInstance(ctx, fetchNextBalances.ConnectorID, err)
}
func (w Workflow) fetchBalances(
ctx workflow.Context,
fetchNextBalances FetchNextBalances,
nextTasks []models.ConnectorTaskTree,
) error {
stateReference := models.CAPABILITY_FETCH_BALANCES.String()
if fetchNextBalances.FromPayload != nil {
stateReference = fmt.Sprintf("%s-%s", models.CAPABILITY_FETCH_BALANCES.String(), fetchNextBalances.FromPayload.ID)
}
stateID := models.StateID{
Reference: stateReference,
ConnectorID: fetchNextBalances.ConnectorID,
}
state, err := activities.StorageStatesGet(infiniteRetryContext(ctx), stateID)
if err != nil {
return fmt.Errorf("retrieving state %s: %v", stateID.String(), err)
}
// Get pageSize from registry using provider from ConnectorID (no DB call needed)
pageSize, err := registry.GetPageSize(fetchNextBalances.ConnectorID.Provider)
if err != nil {
return fmt.Errorf("getting page size: %w", err)
}
hasMore := true
for hasMore {
balancesResponse, err := activities.PluginFetchNextBalances(
infiniteRetryWithLongTimeoutContext(ctx),
fetchNextBalances.ConnectorID,
fetchNextBalances.FromPayload.GetPayload(),
state.State,
int(pageSize),
fetchNextBalances.Periodically,
)
if err != nil {
return errors.Wrap(err, "fetching next balances")
}
balances, err := models.FromPSPBalances(
balancesResponse.Balances,
fetchNextBalances.ConnectorID,
nil,
nil,
)
if err != nil {
return temporal.NewNonRetryableApplicationError(
"failed to translate psp balances",
ErrValidation,
err,
)
}
if len(balancesResponse.Balances) > 0 {
err = activities.StorageBalancesStore(
infiniteRetryContext(ctx),
balances,
)
if err != nil {
return errors.Wrap(err, "storing next balances")
}
}
wg := workflow.NewWaitGroup(ctx)
errChan := make(chan error, len(balancesResponse.Balances)*2)
if !IsEventOutboxPatternEnabled(ctx) {
for _, balance := range balances {
b := balance
sendEvents := SendEvents{
Balance: &b,
}
w.runSendEventAsChildWorkflow(ctx, wg, sendEvents, errChan)
}
}
// First, we need to get the connector to check if it is scheduled for deletion
// because if it is, we don't need to run the next tasks
plugin, err := w.connectors.Get(fetchNextBalances.ConnectorID)
if err != nil {
return fmt.Errorf("getting connector: %w", err)
}
if !IsRunNextTaskOptimizationsEnabled(ctx) {
for _, balance := range balancesResponse.Balances {
b := balance
payload, err := json.Marshal(b)
if err != nil {
errChan <- errors.Wrap(err, "marshalling balance")
}
fromPayload := &FromPayload{
ID: fmt.Sprintf("%s-balances", b.AccountReference),
Payload: payload,
}
w.runNextTaskAsChildWorkflow(ctx, fetchNextBalances.ConnectorID, nextTasks, wg, fromPayload, errChan)
}
} else if len(nextTasks) > 0 {
if !plugin.IsScheduledForDeletion() {
for _, balance := range balancesResponse.Balances {
b := balance
wg.Add(1)
workflow.Go(ctx, func(ctx workflow.Context) {
defer wg.Done()
payload, err := json.Marshal(b)
if err != nil {
errChan <- errors.Wrap(err, "marshalling balances")
return
}
if err := w.runNextTasksV3_1(
ctx,
fetchNextBalances.ConnectorID,
&FromPayload{
ID: fmt.Sprintf("%s-balances", b.AccountReference),
Payload: payload,
},
nextTasks,
); err != nil {
errChan <- errors.Wrap(err, "running next tasks")
}
})
}
}
}
wg.Wait(ctx)
close(errChan)
for err := range errChan {
if err != nil {
return err
}
}
state.State = balancesResponse.NewState
err = activities.StorageStatesStore(
infiniteRetryContext(ctx),
*state,
)
if err != nil {
return errors.Wrap(err, "storing state")
}
hasMore = balancesResponse.HasMore
if w.shouldContinueAsNew(ctx) {
// If we have lots and lots of accounts, sometimes, we need to
// continue as new to not exeed the maximum history size or length
// of a workflow.
return workflow.NewContinueAsNewError(
ctx,
RunFetchNextBalances,
fetchNextBalances,
nextTasks,
)
}
}
return nil
}
const RunFetchNextBalances = "FetchBalances"