mirror of
https://github.com/hanzo-fi/payments.git
synced 2026-10-30 20:31:20 +00:00
117 lines
3.2 KiB
Go
117 lines
3.2 KiB
Go
package moneycorp
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"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/moneycorp/client"
|
|
"github.com/hanzo-fi/payments/pkg/domain/models"
|
|
"github.com/hanzo-fi/payments/pkg/domain/pagination"
|
|
)
|
|
|
|
type externalAccountsState struct {
|
|
LastPage int `json:"lastPage"`
|
|
LastCreatedAt time.Time `json:"LastCreatedAt"`
|
|
}
|
|
|
|
func (p *Plugin) fetchNextExternalAccounts(ctx context.Context, req models.FetchNextExternalAccountsRequest) (models.FetchNextExternalAccountsResponse, error) {
|
|
var oldState externalAccountsState
|
|
if req.State != nil {
|
|
if err := json.Unmarshal(req.State, &oldState); err != nil {
|
|
return models.FetchNextExternalAccountsResponse{}, err
|
|
}
|
|
}
|
|
|
|
var from models.PSPAccount
|
|
if req.FromPayload == nil {
|
|
return models.FetchNextExternalAccountsResponse{}, models.ErrMissingFromPayloadInRequest
|
|
}
|
|
if err := json.Unmarshal(req.FromPayload, &from); err != nil {
|
|
return models.FetchNextExternalAccountsResponse{}, err
|
|
}
|
|
|
|
newState := externalAccountsState{
|
|
LastPage: oldState.LastPage,
|
|
LastCreatedAt: oldState.LastCreatedAt,
|
|
}
|
|
|
|
needMore := false
|
|
hasMore := false
|
|
accounts := make([]models.PSPAccount, 0, req.PageSize)
|
|
for page := oldState.LastPage; ; page++ {
|
|
newState.LastPage = page
|
|
|
|
pagedRecipients, err := p.client.GetRecipients(ctx, from.Reference, page, req.PageSize)
|
|
if err != nil {
|
|
return models.FetchNextExternalAccountsResponse{}, err
|
|
}
|
|
|
|
accounts, err = recipientToPSPAccounts(oldState.LastCreatedAt, accounts, pagedRecipients)
|
|
if err != nil {
|
|
return models.FetchNextExternalAccountsResponse{}, err
|
|
}
|
|
|
|
needMore, hasMore = pagination.ShouldFetchMore(accounts, pagedRecipients, 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.FetchNextExternalAccountsResponse{}, err
|
|
}
|
|
|
|
return models.FetchNextExternalAccountsResponse{
|
|
ExternalAccounts: accounts,
|
|
NewState: payload,
|
|
HasMore: hasMore,
|
|
}, nil
|
|
}
|
|
|
|
func recipientToPSPAccounts(
|
|
lastCreatedAt time.Time,
|
|
accounts []models.PSPAccount,
|
|
pagedAccounts []*client.Recipient,
|
|
) ([]models.PSPAccount, error) {
|
|
for _, recipient := range pagedAccounts {
|
|
createdAt, err := time.Parse("2006-01-02T15:04:05.999999999", recipient.Attributes.CreatedAt)
|
|
if err != nil {
|
|
return accounts, fmt.Errorf("failed to parse transaction date: %v", err)
|
|
}
|
|
|
|
switch createdAt.Compare(lastCreatedAt) {
|
|
case -1, 0:
|
|
continue
|
|
default:
|
|
}
|
|
|
|
raw, err := json.Marshal(recipient)
|
|
if err != nil {
|
|
return accounts, err
|
|
}
|
|
|
|
accounts = append(accounts, models.PSPAccount{
|
|
Reference: recipient.ID,
|
|
// Moneycorp does not send the opening date of the account
|
|
CreatedAt: createdAt,
|
|
Name: &recipient.Attributes.BankAccountName,
|
|
DefaultAsset: pointer.For(currency.FormatAsset(supportedCurrenciesWithDecimal, recipient.Attributes.BankAccountCurrency)),
|
|
Raw: raw,
|
|
})
|
|
}
|
|
return accounts, nil
|
|
}
|