-
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(payments): do not publish payments if no update (#958)
- Loading branch information
1 parent
f516b97
commit 9b02048
Showing
11 changed files
with
372 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
components/payments/cmd/connectors/internal/ingestion/ingester_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package ingestion | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"time" | ||
|
||
"github.com/ThreeDotsLabs/watermill/message" | ||
"github.com/formancehq/payments/internal/models" | ||
"github.com/google/uuid" | ||
) | ||
|
||
type MockStore struct { | ||
paymentIDsNotModified map[string]struct{} | ||
} | ||
|
||
func NewMockStore() *MockStore { | ||
return &MockStore{ | ||
paymentIDsNotModified: make(map[string]struct{}), | ||
} | ||
} | ||
|
||
func (m *MockStore) WithPaymentIDsNotModified(paymentsIDs []models.PaymentID) *MockStore { | ||
for _, id := range paymentsIDs { | ||
m.paymentIDsNotModified[id.String()] = struct{}{} | ||
} | ||
return m | ||
} | ||
|
||
func (m *MockStore) UpsertAccounts(ctx context.Context, accounts []*models.Account) error { | ||
return nil | ||
} | ||
|
||
func (m *MockStore) UpsertPayments(ctx context.Context, payments []*models.Payment) ([]*models.PaymentID, error) { | ||
ids := make([]*models.PaymentID, 0, len(payments)) | ||
for _, payment := range payments { | ||
if _, ok := m.paymentIDsNotModified[payment.ID.String()]; !ok { | ||
ids = append(ids, &payment.ID) | ||
} | ||
} | ||
|
||
return ids, nil | ||
} | ||
|
||
func (m *MockStore) InsertBalances(ctx context.Context, balances []*models.Balance, checkIfAccountExists bool) error { | ||
return nil | ||
} | ||
|
||
func (m *MockStore) UpdateTaskState(ctx context.Context, connectorID models.ConnectorID, descriptor models.TaskDescriptor, state json.RawMessage) error { | ||
return nil | ||
} | ||
|
||
func (m *MockStore) UpdateTransferInitiationPaymentsStatus(ctx context.Context, id models.TransferInitiationID, paymentID *models.PaymentID, status models.TransferInitiationStatus, errorMessage string, attempts int, updatedAt time.Time) error { | ||
return nil | ||
} | ||
|
||
func (m *MockStore) AddTransferInitiationPaymentID(ctx context.Context, id models.TransferInitiationID, paymentID *models.PaymentID, updatedAt time.Time) error { | ||
return nil | ||
} | ||
|
||
func (m *MockStore) LinkBankAccountWithAccount(ctx context.Context, id uuid.UUID, accountID *models.AccountID) error { | ||
return nil | ||
} | ||
|
||
type MockPublisher struct { | ||
messages chan *message.Message | ||
} | ||
|
||
func NewMockPublisher() *MockPublisher { | ||
return &MockPublisher{ | ||
messages: make(chan *message.Message, 100), | ||
} | ||
} | ||
|
||
func (m *MockPublisher) Publish(topic string, messages ...*message.Message) error { | ||
for _, msg := range messages { | ||
m.messages <- msg | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (m *MockPublisher) Close() error { | ||
close(m.messages) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
169 changes: 169 additions & 0 deletions
169
components/payments/cmd/connectors/internal/ingestion/payments_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
package ingestion | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"math/big" | ||
"testing" | ||
"time" | ||
|
||
"github.com/formancehq/payments/internal/models" | ||
"github.com/google/uuid" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
var ( | ||
connectorID = models.ConnectorID{ | ||
Reference: uuid.New(), | ||
Provider: models.ConnectorProviderDummyPay, | ||
} | ||
|
||
p1 = &models.Payment{ | ||
ID: models.PaymentID{ | ||
PaymentReference: models.PaymentReference{ | ||
Reference: "p1", | ||
Type: models.PaymentTypePayIn, | ||
}, | ||
ConnectorID: connectorID, | ||
}, | ||
ConnectorID: connectorID, | ||
CreatedAt: time.Date(2023, 11, 14, 4, 55, 0, 0, time.UTC), | ||
Reference: "p1", | ||
Amount: big.NewInt(100), | ||
Type: models.PaymentTypePayIn, | ||
Status: models.PaymentStatusCancelled, | ||
Scheme: models.PaymentSchemeA2A, | ||
Asset: models.Asset("USD/2"), | ||
} | ||
|
||
p2 = &models.Payment{ | ||
ID: models.PaymentID{ | ||
PaymentReference: models.PaymentReference{ | ||
Reference: "p2", | ||
Type: models.PaymentTypeTransfer, | ||
}, | ||
ConnectorID: connectorID, | ||
}, | ||
ConnectorID: connectorID, | ||
CreatedAt: time.Date(2023, 11, 14, 4, 54, 0, 0, time.UTC), | ||
Reference: "p2", | ||
Amount: big.NewInt(150), | ||
Type: models.PaymentTypeTransfer, | ||
Status: models.PaymentStatusSucceeded, | ||
Scheme: models.PaymentSchemeApplePay, | ||
Asset: models.Asset("EUR/2"), | ||
} | ||
|
||
p3 = &models.Payment{ | ||
ID: models.PaymentID{ | ||
PaymentReference: models.PaymentReference{ | ||
Reference: "p3", | ||
Type: models.PaymentTypePayOut, | ||
}, | ||
ConnectorID: connectorID, | ||
}, | ||
ConnectorID: connectorID, | ||
CreatedAt: time.Date(2023, 11, 14, 4, 53, 0, 0, time.UTC), | ||
Reference: "p3", | ||
Amount: big.NewInt(200), | ||
Type: models.PaymentTypePayOut, | ||
Status: models.PaymentStatusPending, | ||
Scheme: models.PaymentSchemeCardMasterCard, | ||
Asset: models.Asset("USD/2"), | ||
} | ||
) | ||
|
||
type paymentMessagePayload struct { | ||
Paylaod struct { | ||
ID string `json:"id"` | ||
} `json:"payload"` | ||
} | ||
|
||
func TestIngestPayments(t *testing.T) { | ||
t.Parallel() | ||
|
||
type testCase struct { | ||
name string | ||
batch PaymentBatch | ||
paymentIDsNotModified []models.PaymentID | ||
requiredPublishedPaymentIDs []models.PaymentID | ||
} | ||
|
||
testCases := []testCase{ | ||
{ | ||
name: "nominal", | ||
batch: PaymentBatch{ | ||
{ | ||
Payment: p1, | ||
}, | ||
{ | ||
Payment: p2, | ||
}, | ||
{ | ||
Payment: p3, | ||
}, | ||
}, | ||
paymentIDsNotModified: []models.PaymentID{}, | ||
requiredPublishedPaymentIDs: []models.PaymentID{p1.ID, p2.ID, p3.ID}, | ||
}, | ||
{ | ||
name: "only one payment upserted, should publish only one message", | ||
batch: PaymentBatch{ | ||
{ | ||
Payment: p1, | ||
}, | ||
{ | ||
Payment: p2, | ||
}, | ||
{ | ||
Payment: p3, | ||
}, | ||
}, | ||
paymentIDsNotModified: []models.PaymentID{p1.ID, p2.ID}, | ||
requiredPublishedPaymentIDs: []models.PaymentID{p3.ID}, | ||
}, | ||
{ | ||
name: "all payments are not modified, should not publish any message", | ||
batch: PaymentBatch{ | ||
{ | ||
Payment: p1, | ||
}, | ||
{ | ||
Payment: p2, | ||
}, | ||
{ | ||
Payment: p3, | ||
}, | ||
}, | ||
paymentIDsNotModified: []models.PaymentID{p1.ID, p2.ID, p3.ID}, | ||
requiredPublishedPaymentIDs: []models.PaymentID{}, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
tc := tc | ||
t.Run(tc.name, func(t *testing.T) { | ||
publisher := NewMockPublisher() | ||
|
||
ingester := NewDefaultIngester( | ||
models.ConnectorProviderDummyPay, | ||
nil, | ||
NewMockStore().WithPaymentIDsNotModified(tc.paymentIDsNotModified), | ||
publisher, | ||
) | ||
|
||
err := ingester.IngestPayments(context.Background(), connectorID, tc.batch, nil) | ||
publisher.Close() | ||
require.NoError(t, err) | ||
|
||
require.Len(t, publisher.messages, len(tc.requiredPublishedPaymentIDs)) | ||
i := 0 | ||
for msg := range publisher.messages { | ||
var payload paymentMessagePayload | ||
require.NoError(t, json.Unmarshal(msg.Payload, &payload)) | ||
require.Equal(t, tc.requiredPublishedPaymentIDs[i].String(), payload.Paylaod.ID) | ||
i++ | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
9b02048
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
docs – ./
docs-git-main-formance.vercel.app
docs-formance.vercel.app
docs.formance.com
developers.formance.com
docs.numary.com
developers.numary.com