mirror of
https://github.com/hanzo-fi/payments.git
synced 2026-10-30 20:31:20 +00:00
87 lines
2.2 KiB
Go
87 lines
2.2 KiB
Go
package wise
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"strconv"
|
|
|
|
"github.com/hanzo-fi/go-libs/v5/pkg/types/pointer"
|
|
"github.com/hanzo-fi/go-libs/v5/pkg/types/currency"
|
|
"github.com/hanzo-fi/payments/ce/plugins/wise/client"
|
|
"github.com/hanzo-fi/payments/pkg/domain/models"
|
|
)
|
|
|
|
type accountsState struct {
|
|
// Accounts are ordered by their ID
|
|
LastAccountID uint64 `json:"lastAccountID"`
|
|
}
|
|
|
|
func (p *Plugin) fetchNextAccounts(ctx context.Context, req models.FetchNextAccountsRequest) (models.FetchNextAccountsResponse, error) {
|
|
var oldState accountsState
|
|
if req.State != nil {
|
|
if err := json.Unmarshal(req.State, &oldState); err != nil {
|
|
return models.FetchNextAccountsResponse{}, err
|
|
}
|
|
}
|
|
|
|
var from client.Profile
|
|
if req.FromPayload == nil {
|
|
return models.FetchNextAccountsResponse{}, models.ErrMissingFromPayloadInRequest
|
|
}
|
|
if err := json.Unmarshal(req.FromPayload, &from); err != nil {
|
|
return models.FetchNextAccountsResponse{}, err
|
|
}
|
|
|
|
newState := accountsState{
|
|
LastAccountID: oldState.LastAccountID,
|
|
}
|
|
|
|
var accounts []models.PSPAccount
|
|
hasMore := false
|
|
// Wise balances are considered as accounts on our side.
|
|
balances, err := p.client.GetBalances(ctx, from.ID)
|
|
if err != nil {
|
|
return models.FetchNextAccountsResponse{}, err
|
|
}
|
|
|
|
for _, balance := range balances {
|
|
if oldState.LastAccountID != 0 && balance.ID <= oldState.LastAccountID {
|
|
continue
|
|
}
|
|
|
|
raw, err := json.Marshal(balance)
|
|
if err != nil {
|
|
return models.FetchNextAccountsResponse{}, err
|
|
}
|
|
|
|
accounts = append(accounts, models.PSPAccount{
|
|
Reference: strconv.FormatUint(balance.ID, 10),
|
|
CreatedAt: balance.CreationTime,
|
|
Name: &balance.Name,
|
|
DefaultAsset: pointer.For(currency.FormatAsset(supportedCurrenciesWithDecimal, balance.Amount.Currency)),
|
|
Metadata: map[string]string{
|
|
metadataProfileIDKey: strconv.FormatUint(from.ID, 10),
|
|
},
|
|
Raw: raw,
|
|
})
|
|
|
|
newState.LastAccountID = balance.ID
|
|
|
|
if len(accounts) >= req.PageSize {
|
|
hasMore = true
|
|
break
|
|
}
|
|
}
|
|
|
|
payload, err := json.Marshal(newState)
|
|
if err != nil {
|
|
return models.FetchNextAccountsResponse{}, err
|
|
}
|
|
|
|
return models.FetchNextAccountsResponse{
|
|
Accounts: accounts,
|
|
NewState: payload,
|
|
HasMore: hasMore,
|
|
}, nil
|
|
}
|