Files

121 lines
2.9 KiB
Go

package mangopay
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/mangopay/client"
"github.com/hanzo-fi/payments/pkg/domain/models"
"github.com/hanzo-fi/payments/pkg/domain/pagination"
)
type accountsState struct {
LastPage int `json:"lastPage"`
LastCreationDate time.Time `json:"lastCreationDate"`
}
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
}
} else {
oldState = accountsState{
LastPage: 1,
}
}
var from client.User
if req.FromPayload == nil {
return models.FetchNextAccountsResponse{}, models.ErrMissingFromPayloadInRequest
}
if err := json.Unmarshal(req.FromPayload, &from); err != nil {
return models.FetchNextAccountsResponse{}, err
}
newState := accountsState{
LastPage: oldState.LastPage,
LastCreationDate: oldState.LastCreationDate,
}
var accounts []models.PSPAccount
needMore := false
hasMore := false
page := oldState.LastPage
for {
pagedAccounts, err := p.client.GetWallets(ctx, from.ID, page, req.PageSize)
if err != nil {
return models.FetchNextAccountsResponse{}, err
}
accounts, err = fillAccounts(pagedAccounts, accounts, from, oldState)
if err != nil {
return models.FetchNextAccountsResponse{}, err
}
needMore, hasMore = pagination.ShouldFetchMore(accounts, pagedAccounts, req.PageSize)
if !needMore || !hasMore {
break
}
page++
}
if !needMore {
accounts = accounts[:req.PageSize]
}
if len(accounts) > 0 {
newState.LastCreationDate = accounts[len(accounts)-1].CreatedAt
}
newState.LastPage = page
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.Wallet,
accounts []models.PSPAccount,
from client.User,
oldState accountsState,
) ([]models.PSPAccount, error) {
for _, account := range pagedAccounts {
accountCreationDate := time.Unix(account.CreationDate, 0)
if accountCreationDate.Before(oldState.LastCreationDate) {
continue
}
raw, err := json.Marshal(account)
if err != nil {
return nil, err
}
accounts = append(accounts, models.PSPAccount{
Reference: account.ID,
CreatedAt: accountCreationDate,
Name: &account.Description,
DefaultAsset: pointer.For(currency.FormatAsset(supportedCurrenciesWithDecimal, account.Currency)),
Metadata: map[string]string{
userIDMetadataKey: from.ID,
},
Raw: raw,
})
}
return accounts, nil
}