mirror of
https://github.com/hanzo-fi/payments.git
synced 2026-10-30 20:31:20 +00:00
110 lines
2.5 KiB
Go
110 lines
2.5 KiB
Go
package modulr
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"time"
|
|
|
|
"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/modulr/client"
|
|
"github.com/hanzo-fi/payments/pkg/domain/models"
|
|
"github.com/hanzo-fi/payments/pkg/domain/pagination"
|
|
)
|
|
|
|
type accountsState struct {
|
|
LastCreatedAt time.Time `json:"lastCreatedAt"`
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|
|
|
|
newState := accountsState{
|
|
LastCreatedAt: oldState.LastCreatedAt,
|
|
}
|
|
|
|
var accounts []models.PSPAccount
|
|
needMore := false
|
|
hasMore := false
|
|
for page := 0; ; page++ {
|
|
pagedAccounts, err := p.client.GetAccounts(ctx, page, req.PageSize, oldState.LastCreatedAt)
|
|
if err != nil {
|
|
return models.FetchNextAccountsResponse{}, err
|
|
}
|
|
|
|
accounts, err = fillAccounts(pagedAccounts, accounts, oldState, req.PageSize)
|
|
if err != nil {
|
|
return models.FetchNextAccountsResponse{}, err
|
|
}
|
|
|
|
needMore, hasMore = pagination.ShouldFetchMore(accounts, pagedAccounts, req.PageSize)
|
|
if !needMore || !hasMore {
|
|
break
|
|
}
|
|
}
|
|
|
|
if !needMore {
|
|
accounts = accounts[:req.PageSize]
|
|
}
|
|
|
|
if len(accounts) > 0 {
|
|
newState.LastCreatedAt = accounts[len(accounts)-1].CreatedAt
|
|
}
|
|
|
|
payload, err := json.Marshal(newState)
|
|
if err != nil {
|
|
return models.FetchNextAccountsResponse{}, err
|
|
}
|
|
|
|
return models.FetchNextAccountsResponse{
|
|
Accounts: accounts,
|
|
NewState: payload,
|
|
HasMore: hasMore,
|
|
}, nil
|
|
}
|
|
|
|
func fillAccounts(
|
|
pagedAccounts []client.Account,
|
|
accounts []models.PSPAccount,
|
|
oldState accountsState,
|
|
pageSize int,
|
|
) ([]models.PSPAccount, error) {
|
|
for _, account := range pagedAccounts {
|
|
if len(accounts) >= pageSize {
|
|
break
|
|
}
|
|
|
|
createdTime, err := time.Parse("2006-01-02T15:04:05.999-0700", account.CreatedDate)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
switch createdTime.Compare(oldState.LastCreatedAt) {
|
|
case -1, 0:
|
|
// Account already ingested, skip
|
|
continue
|
|
default:
|
|
}
|
|
|
|
raw, err := json.Marshal(account)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
accounts = append(accounts, models.PSPAccount{
|
|
Reference: account.ID,
|
|
CreatedAt: createdTime,
|
|
Name: &account.Name,
|
|
DefaultAsset: pointer.For(currency.FormatAsset(supportedCurrenciesWithDecimal, account.Currency)),
|
|
Raw: raw,
|
|
})
|
|
}
|
|
|
|
return accounts, nil
|
|
}
|