Files
fgueryandGitHub 9b637f957e feat(EN-186): save balances via Tink and Plaid plugins (#559)
* feat: added fetchBalances capability to Tink

* chore: quick refacto of fetch_open_banking_data for clarity

* chore: start on splitting fetch_open_banking_data between components

* chore: revert change on handle_webhook

* feat: added DataToFetch field to the fetchOpenBankingData to support fetching balance (by different ways too)

* feat: fetch balance from accounts api

* fix: try to store psuID and connectionID in account

* chore: improve docker env

* feat: balances work now, but PSPBalance does not have psuID + connectionID

* feat: make sure we store PSUID for balances as well

* feat(EN-185): handle Plaid balances from accounts

* feat: fix up plaid balance fetching

* fix: air rebuilding was sometime failing; this aims to fix it

* fix: linting

* fix: update go version to be the same as the docker image

* chore: remove unused go install flags

* fix: make OpenBankingDataToFetchAccountsAndBalances test a bit more explicit

* fix: added extra null checks as required

* chore: remove duplicated test

* fix: code review comments

* fix: improve translate_plaid_amount to also return asset

* chore: added tests for amount_utils

* fix: make fetch open banking activity fail when child workflow fail
2025-10-07 10:15:07 +00:00

69 lines
1.8 KiB
Go

package tink
import (
"context"
"encoding/json"
"time"
"github.com/formancehq/payments/internal/connectors/plugins/public/tink/client"
"github.com/formancehq/payments/internal/models"
)
func (p *Plugin) fetchNextAccounts(ctx context.Context, req models.FetchNextAccountsRequest) (models.FetchNextAccountsResponse, error) {
var from models.OpenBankingForwardedUserFromPayload
if err := json.Unmarshal(req.FromPayload, &from); err != nil {
return models.FetchNextAccountsResponse{}, err
}
var webhook fetchNextDataRequest
if err := json.Unmarshal(from.FromPayload, &webhook); err != nil {
return models.FetchNextAccountsResponse{}, err
}
account, err := p.client.GetAccount(ctx, webhook.ExternalUserID, webhook.AccountID)
if err != nil {
return models.FetchNextAccountsResponse{}, err
}
accounts := make([]models.PSPAccount, 0, 1)
accounts, err = toPSPAccounts(accounts, []client.Account{account}, from)
if err != nil {
return models.FetchNextAccountsResponse{}, err
}
return models.FetchNextAccountsResponse{
Accounts: accounts,
}, nil
}
func toPSPAccounts(
accounts []models.PSPAccount,
pagedAccounts []client.Account,
from models.OpenBankingForwardedUserFromPayload,
) ([]models.PSPAccount, error) {
for _, account := range pagedAccounts {
raw, err := json.Marshal(account)
if err != nil {
return accounts, err
}
acc := models.PSPAccount{
Reference: account.ID,
CreatedAt: time.Now().UTC(),
Name: &account.Name,
Metadata: make(map[string]string),
PsuID: &from.PSUID,
Raw: raw,
}
// Note -- right now Tink doesn't send us the ConnectionID so we can't save it
if from.OpenBankingConnection != nil {
acc.OpenBankingConnectionID = &from.OpenBankingConnection.ConnectionID
}
accounts = append(accounts, acc)
}
return accounts, nil
}